-
Notifications
You must be signed in to change notification settings - Fork 318
/
cgroup-resources.c
1367 lines (1165 loc) · 41.5 KB
/
cgroup-resources.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* crun - OCI runtime written in C
*
* Copyright (C) 2017, 2018, 2019 Giuseppe Scrivano <[email protected]>
* crun is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* crun is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with crun. If not, see <http://www.gnu.org/licenses/>.
*/
#define _GNU_SOURCE
#include <config.h>
#include "cgroup.h"
#include "cgroup-internal.h"
#include "cgroup-systemd.h"
#include "cgroup-utils.h"
#include "cgroup-resources.h"
#include "ebpf.h"
#include "utils.h"
#include "status.h"
#include <string.h>
#include <sys/types.h>
#include <sys/vfs.h>
#include <inttypes.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <libgen.h>
static inline int
write_cgroup_file (int dirfd, const char *name, const void *data, size_t len, libcrun_error_t *err)
{
return write_file_at_with_flags (dirfd, O_WRONLY | O_CLOEXEC, 0, name, data, len, err);
}
static int
write_cgroup_file_or_alias (int dirfd, const char *name, const char *alias, const void *data, size_t len, libcrun_error_t *err)
{
int ret;
ret = write_file_at_with_flags (dirfd, O_WRONLY | O_CLOEXEC, 0, name, data, len, err);
if (UNLIKELY (alias != NULL && ret < 0 && crun_error_get_errno (err) == ENOENT))
{
crun_error_release (err);
ret = write_file_at_with_flags (dirfd, O_WRONLY | O_CLOEXEC, 0, alias, data, len, err);
}
return ret;
}
static inline int
openat_with_alias (int dirfd, const char *name, const char *alias, const char **used_name, int flags, libcrun_error_t *err)
{
int ret;
*used_name = name;
ret = openat (dirfd, name, flags);
if (UNLIKELY (ret < 0 && alias != NULL && errno == ENOENT))
{
*used_name = alias;
ret = openat (dirfd, alias, flags);
}
if (UNLIKELY (ret < 0))
return crun_make_error (err, errno, "open `%s`", name);
return ret;
}
static int
is_rwm (const char *str, libcrun_error_t *err)
{
const char *it;
bool r = false;
bool w = false;
bool m = false;
for (it = str; *it; it++)
switch (*it)
{
case 'r':
r = true;
break;
case 'w':
w = true;
break;
case 'm':
m = true;
break;
default:
return crun_make_error (err, 0, "invalid mode specified `%s`", str);
}
return r && w && m ? 1 : 0;
}
static int
check_cgroup_v2_controller_available_wrapper (int ret, int cgroup_dirfd, const char *name, libcrun_error_t *err)
{
if (ret == 0 || err == NULL)
return 0;
errno = crun_error_get_errno (err);
/* If the file is not found, try to give a more meaningful error message. */
if (errno == ENOENT || errno == EPERM || errno == EACCES)
{
cleanup_free char *controllers = NULL;
libcrun_error_t tmp_err = NULL;
cleanup_free char *key = NULL;
char *saveptr = NULL;
bool found = false;
const char *token;
char *it;
/* Check if the specified controller is enabled. */
key = xstrdup (name);
it = strchr (key, '.');
if (it == NULL)
{
crun_error_release (err);
return crun_make_error (err, 0, "the specified key has not the form CONTROLLER.VALUE `%s`", name);
}
*it = '\0';
/* cgroup. files are not part of a controller. Return the original error. */
if (strcmp (key, "cgroup") == 0)
return ret;
/* If the cgroup.controllers file cannot be read, return the original error. */
if (read_all_file_at (cgroup_dirfd, "cgroup.controllers", &controllers, NULL, &tmp_err) < 0)
{
crun_error_release (&tmp_err);
return ret;
}
for (token = strtok_r (controllers, " \n", &saveptr); token; token = strtok_r (NULL, " \n", &saveptr))
{
if (strcmp (token, key) == 0)
{
found = true;
break;
}
}
if (! found)
{
crun_error_release (err);
return crun_make_error (err, 0, "the requested cgroup controller `%s` is not available", key);
}
}
return ret;
}
static int
write_file_and_check_controllers_at (bool cgroup2, int dirfd, const char *name, const char *name_alias,
const void *data, size_t len, libcrun_error_t *err)
{
int ret;
ret = write_cgroup_file_or_alias (dirfd, name, name_alias, data, len, err);
if (cgroup2)
return check_cgroup_v2_controller_available_wrapper (ret, dirfd, name, err);
return ret;
}
/* The parser generates different structs but they are really all the same. */
typedef runtime_spec_schema_defs_linux_block_io_device_throttle throttling_s;
static int
write_blkio_v1_resources_throttling (int dirfd, const char *name, throttling_s **throttling, size_t throttling_len,
libcrun_error_t *err)
{
char fmt_buf[128];
size_t i;
cleanup_close int fd = -1;
if (throttling == NULL)
return 0;
fd = openat (dirfd, name, O_WRONLY | O_CLOEXEC);
if (UNLIKELY (fd < 0))
return crun_make_error (err, errno, "open `%s`", name);
for (i = 0; i < throttling_len; i++)
{
int ret;
size_t len;
len = sprintf (fmt_buf, "%" PRIu64 ":%" PRIu64 " %" PRIu64 "\n", throttling[i]->major, throttling[i]->minor,
throttling[i]->rate);
ret = TEMP_FAILURE_RETRY (write (fd, fmt_buf, len));
if (UNLIKELY (ret < 0))
return crun_make_error (err, errno, "write `%s`", name);
}
return 0;
}
static int
write_blkio_v2_resources_throttling (int fd, const char *name, throttling_s **throttling, size_t throttling_len,
libcrun_error_t *err)
{
char fmt_buf[128];
size_t i;
if (throttling == NULL)
return 0;
for (i = 0; i < throttling_len; i++)
{
int ret;
size_t len;
len = sprintf (fmt_buf, "%" PRIu64 ":%" PRIu64 " %s=%" PRIu64 "\n", throttling[i]->major, throttling[i]->minor,
name, throttling[i]->rate);
ret = TEMP_FAILURE_RETRY (write (fd, fmt_buf, len));
if (UNLIKELY (ret < 0))
return crun_make_error (err, errno, "write `%s`", name);
}
return 0;
}
static int
write_blkio_resources (int dirfd, bool cgroup2, runtime_spec_schema_config_linux_resources_block_io *blkio,
libcrun_error_t *err)
{
char fmt_buf[128];
size_t len;
int ret;
if (blkio->weight)
{
uint32_t val = blkio->weight;
len = sprintf (fmt_buf, "%" PRIu32, val);
if (! cgroup2)
{
ret = write_cgroup_file_or_alias (dirfd, "blkio.weight", "blkio.bfq.weight", fmt_buf, len, err);
if (UNLIKELY (ret < 0))
return ret;
}
else
{
ret = write_cgroup_file (dirfd, "io.bfq.weight", fmt_buf, len, err);
if (UNLIKELY (ret < 0))
{
if (crun_error_get_errno (err) == ENOENT)
{
crun_error_release (err);
/* convert linearly from [10-1000] to [1-10000] */
val = 1 + (val - 10) * 9999 / 990;
len = sprintf (fmt_buf, "%" PRIu32, val);
ret = write_cgroup_file (dirfd, "io.weight", fmt_buf, len, err);
}
if (UNLIKELY (ret < 0))
return ret;
}
}
}
if (blkio->leaf_weight)
{
if (cgroup2)
return crun_make_error (err, 0, "cannot set leaf_weight with cgroupv2");
len = sprintf (fmt_buf, "%d", blkio->leaf_weight);
ret = write_cgroup_file (dirfd, "blkio.leaf_weight", fmt_buf, len, err);
if (UNLIKELY (ret < 0))
return ret;
}
if (blkio->weight_device_len)
{
if (cgroup2)
{
cleanup_close int wfd = -1;
size_t i;
wfd = openat (dirfd, "io.bfq.weight", O_WRONLY | O_CLOEXEC);
if (UNLIKELY (wfd < 0))
return crun_make_error (err, errno, "open io.weight");
for (i = 0; i < blkio->weight_device_len; i++)
{
uint32_t w = blkio->weight_device[i]->weight;
len = sprintf (fmt_buf, "%" PRIu64 ":%" PRIu64 " %i\n", blkio->weight_device[i]->major,
blkio->weight_device[i]->minor, w);
ret = TEMP_FAILURE_RETRY (write (wfd, fmt_buf, len));
if (UNLIKELY (ret < 0))
return crun_make_error (err, errno, "write io.weight");
/* Ignore blkio->weight_device[i]->leaf_weight. */
}
}
else
{
const char *leaf_weight_device_file_name = NULL;
const char *weight_device_file_name = NULL;
cleanup_close int w_leafdevice_fd = -1;
cleanup_close int w_device_fd = -1;
size_t i;
w_device_fd = openat_with_alias (dirfd, "blkio.weight_device", "blkio.bfq.weight_device",
&weight_device_file_name, O_WRONLY | O_CLOEXEC, err);
if (UNLIKELY (w_device_fd < 0))
return w_device_fd;
w_leafdevice_fd = openat_with_alias (dirfd, "blkio.leaf_weight_device", "blkio.bfq.leaf_weight_device",
&leaf_weight_device_file_name, O_WRONLY | O_CLOEXEC, err);
if (UNLIKELY (w_leafdevice_fd < 0))
{
/* If the .leaf_weight_device file is missing, just ignore it. */
crun_error_release (err);
}
for (i = 0; i < blkio->weight_device_len; i++)
{
len = sprintf (fmt_buf, "%" PRIu64 ":%" PRIu64 " %" PRIu16 "\n", blkio->weight_device[i]->major,
blkio->weight_device[i]->minor, blkio->weight_device[i]->weight);
ret = TEMP_FAILURE_RETRY (write (w_device_fd, fmt_buf, len));
if (UNLIKELY (ret < 0))
return crun_make_error (err, errno, "write `%s`", weight_device_file_name);
if (w_leafdevice_fd >= 0)
{
len = sprintf (fmt_buf, "%" PRIu64 ":%" PRIu64 " %" PRIu16 "\n", blkio->weight_device[i]->major,
blkio->weight_device[i]->minor, blkio->weight_device[i]->leaf_weight);
ret = TEMP_FAILURE_RETRY (write (w_leafdevice_fd, fmt_buf, len));
if (UNLIKELY (ret < 0))
return crun_make_error (err, errno, "write `%s`", leaf_weight_device_file_name);
}
}
}
}
if (cgroup2)
{
cleanup_close int wfd = -1;
const char *name = "io.max";
wfd = openat (dirfd, name, O_WRONLY | O_CLOEXEC);
if (UNLIKELY (wfd < 0))
{
ret = crun_make_error (err, errno, "open `%s`", name);
return check_cgroup_v2_controller_available_wrapper (ret, dirfd, name, err);
}
ret = write_blkio_v2_resources_throttling (wfd, "rbps", (throttling_s **) blkio->throttle_read_bps_device,
blkio->throttle_read_bps_device_len, err);
if (UNLIKELY (ret < 0))
return ret;
ret = write_blkio_v2_resources_throttling (wfd, "wbps", (throttling_s **) blkio->throttle_write_bps_device,
blkio->throttle_write_bps_device_len, err);
if (UNLIKELY (ret < 0))
return ret;
ret = write_blkio_v2_resources_throttling (wfd, "riops", (throttling_s **) blkio->throttle_read_iops_device,
blkio->throttle_read_iops_device_len, err);
if (UNLIKELY (ret < 0))
return ret;
ret = write_blkio_v2_resources_throttling (wfd, "wiops", (throttling_s **) blkio->throttle_write_iops_device,
blkio->throttle_write_iops_device_len, err);
if (UNLIKELY (ret < 0))
return ret;
}
else
{
ret = write_blkio_v1_resources_throttling (dirfd, "blkio.throttle.read_bps_device",
(throttling_s **) blkio->throttle_read_bps_device,
blkio->throttle_read_bps_device_len, err);
if (UNLIKELY (ret < 0))
return ret;
ret = write_blkio_v1_resources_throttling (dirfd, "blkio.throttle.write_bps_device",
(throttling_s **) blkio->throttle_write_bps_device,
blkio->throttle_write_bps_device_len, err);
if (UNLIKELY (ret < 0))
return ret;
ret = write_blkio_v1_resources_throttling (dirfd, "blkio.throttle.read_iops_device",
(throttling_s **) blkio->throttle_read_iops_device,
blkio->throttle_read_iops_device_len, err);
if (UNLIKELY (ret < 0))
return ret;
ret = write_blkio_v1_resources_throttling (dirfd, "blkio.throttle.write_iops_device",
(throttling_s **) blkio->throttle_write_iops_device,
blkio->throttle_write_iops_device_len, err);
if (UNLIKELY (ret < 0))
return ret;
}
return 0;
}
static int
write_network_resources (int dirfd_netclass, int dirfd_netprio, runtime_spec_schema_config_linux_resources_network *net,
libcrun_error_t *err)
{
char fmt_buf[128];
size_t len;
int ret;
if (net->class_id)
{
len = sprintf (fmt_buf, "%d", net->class_id);
ret = write_cgroup_file (dirfd_netclass, "net_cls.classid", fmt_buf, len, err);
if (UNLIKELY (ret < 0))
return ret;
}
if (net->priorities_len)
{
size_t i;
cleanup_close int fd = -1;
fd = openat (dirfd_netprio, "net_prio.ifpriomap", O_WRONLY | O_CLOEXEC);
if (UNLIKELY (fd < 0))
return crun_make_error (err, errno, "open `net_prio.ifpriomap`");
for (i = 0; i < net->priorities_len; i++)
{
len = sprintf (fmt_buf, "%s %d\n", net->priorities[i]->name, net->priorities[i]->priority);
ret = TEMP_FAILURE_RETRY (write (fd, fmt_buf, len));
if (UNLIKELY (ret < 0))
return crun_make_error (err, errno, "write net_prio.ifpriomap");
}
}
return 0;
}
static int
write_hugetlb_resources (int dirfd, bool cgroup2,
runtime_spec_schema_config_linux_resources_hugepage_limits_element **htlb, size_t htlb_len,
libcrun_error_t *err)
{
char fmt_buf[128];
size_t i;
for (i = 0; i < htlb_len; i++)
{
cleanup_free char *filename = NULL;
const char *suffix;
size_t len;
int ret;
suffix = cgroup2 ? "max" : "limit_in_bytes";
xasprintf (&filename, "hugetlb.%s.%s", htlb[i]->page_size, suffix);
len = sprintf (fmt_buf, "%" PRIu64, htlb[i]->limit);
ret = write_file_and_check_controllers_at (cgroup2, dirfd, filename, NULL, fmt_buf, len, err);
if (UNLIKELY (ret < 0))
return ret;
}
return 0;
}
static int
write_devices_resources_v1 (int dirfd, runtime_spec_schema_defs_linux_device_cgroup **devs, size_t devs_len,
libcrun_error_t *err)
{
size_t i, len;
int ret;
char *default_devices[] = {
"c *:* m",
"b *:* m",
"c 1:3 rwm",
"c 1:8 rwm",
"c 1:7 rwm",
"c 5:0 rwm",
"c 1:5 rwm",
"c 1:9 rwm",
"c 5:1 rwm",
"c 136:* rwm",
"c 5:2 rwm",
NULL
};
for (i = 0; i < devs_len; i++)
{
/* It is plenty of room for "TYPE MAJOR:MINOR ACCESS", where type is one char, and ACCESS is at most 3. */
#define FMT_BUF_LEN 64
char fmt_buf[FMT_BUF_LEN];
const char *file = devs[i]->allow ? "devices.allow" : "devices.deny";
if (devs[i]->type == NULL || devs[i]->type[0] == 'a')
{
strcpy (fmt_buf, "a");
len = 1;
}
else
{
char fmt_buf_major[16];
char fmt_buf_minor[16];
#define FMT_DEV(x, b) \
do \
{ \
if (x##_present) \
sprintf (b, "%" PRIi64, x); \
else \
strcpy (b, "*"); \
} while (0)
FMT_DEV (devs[i]->major, fmt_buf_major);
FMT_DEV (devs[i]->minor, fmt_buf_minor);
len = snprintf (fmt_buf, FMT_BUF_LEN - 1, "%s %s:%s %s", devs[i]->type, fmt_buf_major, fmt_buf_minor,
devs[i]->access);
/* Make sure it is still a NUL terminated string. */
fmt_buf[len] = '\0';
}
ret = write_cgroup_file (dirfd, file, fmt_buf, len, err);
if (UNLIKELY (ret < 0))
return ret;
}
for (i = 0; default_devices[i]; i++)
{
ret = write_cgroup_file (dirfd, "devices.allow", default_devices[i], strlen (default_devices[i]), err);
if (UNLIKELY (ret < 0))
return ret;
}
return 0;
}
static int
write_devices_resources_v2_internal (int dirfd, runtime_spec_schema_defs_linux_device_cgroup **devs, size_t devs_len,
libcrun_error_t *err)
{
int i, ret;
cleanup_free struct bpf_program *program = NULL;
struct default_dev_s
{
char type;
int major;
int minor;
const char *access;
};
struct default_dev_s default_devices[] = {
{ 'c', -1, -1, "m" },
{ 'b', -1, -1, "m" },
{ 'c', 1, 3, "rwm" },
{ 'c', 1, 8, "rwm" },
{ 'c', 1, 7, "rwm" },
{ 'c', 5, 0, "rwm" },
{ 'c', 1, 5, "rwm" },
{ 'c', 1, 9, "rwm" },
{ 'c', 5, 1, "rwm" },
{ 'c', 136, -1, "rwm" },
{ 'c', 5, 2, "rwm" },
};
program = bpf_program_new (2048);
program = bpf_program_init_dev (program, err);
if (UNLIKELY (program == NULL))
return -1;
for (i = (sizeof (default_devices) / sizeof (default_devices[0])) - 1; i >= 0; i--)
{
program = bpf_program_append_dev (program, default_devices[i].access, default_devices[i].type,
default_devices[i].major, default_devices[i].minor, true, err);
if (UNLIKELY (program == NULL))
return -1;
}
for (i = devs_len - 1; i >= 0; i--)
{
char type = 'a';
int minor = -1, major = -1;
if (devs[i]->type != NULL)
type = devs[i]->type[0];
if (devs[i]->major_present)
major = devs[i]->major;
if (devs[i]->minor_present)
minor = devs[i]->minor;
program = bpf_program_append_dev (program, devs[i]->access, type, major, minor, devs[i]->allow, err);
if (UNLIKELY (program == NULL))
return -1;
}
program = bpf_program_complete_dev (program, err);
if (UNLIKELY (program == NULL))
return -1;
ret = libcrun_ebpf_load (program, dirfd, NULL, err);
if (ret < 0)
return ret;
return 0;
}
static int
write_devices_resources_v2 (int dirfd, runtime_spec_schema_defs_linux_device_cgroup **devs, size_t devs_len,
libcrun_error_t *err)
{
int ret;
size_t i;
bool can_skip = true;
ret = write_devices_resources_v2_internal (dirfd, devs, devs_len, err);
if (LIKELY (ret == 0))
return 0;
/* If writing the resources ebpf failed, check if it is fine to ignore the error. */
for (i = 0; i < devs_len; i++)
{
if (devs[i]->allow_present && ! devs[i]->allow)
{
can_skip = false;
break;
}
}
if (! can_skip)
{
libcrun_error_t tmp_err = NULL;
int rootless;
rootless = is_rootless (&tmp_err);
if (UNLIKELY (rootless < 0))
{
crun_error_release (err);
*err = tmp_err;
return ret;
}
if (rootless)
can_skip = true;
}
if (can_skip)
{
crun_error_release (err);
ret = 0;
}
return ret;
}
static int
write_devices_resources (int dirfd, bool cgroup2, runtime_spec_schema_defs_linux_device_cgroup **devs, size_t devs_len,
libcrun_error_t *err)
{
int ret;
if (cgroup2)
ret = write_devices_resources_v2 (dirfd, devs, devs_len, err);
else
ret = write_devices_resources_v1 (dirfd, devs, devs_len, err);
if (UNLIKELY (ret < 0))
{
libcrun_error_t tmp_err = NULL;
int rootless;
rootless = is_rootless (&tmp_err);
if (UNLIKELY (rootless < 0))
{
crun_error_release (&tmp_err);
return ret;
}
if (rootless)
{
crun_error_release (err);
ret = 0;
}
}
return ret;
}
/* use for cgroupv2 files with .min, .max, .low, or .high suffix */
static int
cg_itoa (char *buf, int64_t value, bool use_max)
{
if (use_max && value < 0)
{
memcpy (buf, "max", 4);
return 3;
}
return sprintf (buf, "%" PRIi64, value);
}
static int
write_memory (int dirfd, bool cgroup2, runtime_spec_schema_config_linux_resources_memory *memory, libcrun_error_t *err)
{
char limit_buf[32];
size_t limit_buf_len;
if (! memory->limit_present)
return 0;
limit_buf_len = cg_itoa (limit_buf, memory->limit, cgroup2);
return write_cgroup_file (dirfd, cgroup2 ? "memory.max" : "memory.limit_in_bytes", limit_buf, limit_buf_len, err);
}
static int
write_memory_swap (int dirfd, bool cgroup2, runtime_spec_schema_config_linux_resources_memory *memory,
libcrun_error_t *err)
{
int ret;
int64_t swap;
char swap_buf[32];
size_t swap_buf_len;
const char *fname = cgroup2 ? "memory.swap.max" : "memory.memsw.limit_in_bytes";
if (! memory->swap_present)
return 0;
swap = memory->swap;
// Cgroupv2 apply limit must check if swap > 0, since `0` and `-1` are special case
// 0: This means process will not be able to use any swap space.
// -1: This means that the process can use as much swap as it needs.
if (cgroup2 && memory->swap > 0)
{
if (! memory->limit_present)
return crun_make_error (err, 0, "cannot set swap limit without the memory limit");
if (memory->swap < memory->limit)
return crun_make_error (err, 0, "cannot set memory+swap limit less than the memory limit");
swap -= memory->limit;
}
swap_buf_len = cg_itoa (swap_buf, swap, cgroup2);
ret = write_cgroup_file (dirfd, fname, swap_buf, swap_buf_len, err);
if (ret >= 0)
return ret;
/* If swap is not enabled, ignore the error. */
if (crun_error_get_errno (err) == ENOENT)
{
crun_error_release (err);
return 0;
}
return ret;
}
static int
write_memory_resources (int dirfd, bool cgroup2, runtime_spec_schema_config_linux_resources_memory *memory,
libcrun_error_t *err)
{
size_t len;
int ret;
char fmt_buf[32];
bool memory_limits_written = false;
if (cgroup2 && memory->check_before_update_present && memory->check_before_update)
{
cleanup_free char *swap_current = NULL;
cleanup_free char *current = NULL;
uint64_t limit = 0;
uint64_t val, val_swap;
int ret;
ret = read_all_file_at (dirfd, "memory.current", ¤t, NULL, err);
if (UNLIKELY (ret < 0))
return ret;
ret = read_all_file_at (dirfd, "memory.swap.current", &swap_current, NULL, err);
if (UNLIKELY (ret < 0))
return ret;
errno = 0;
val = strtoll (current, NULL, 10);
if (UNLIKELY (errno))
return crun_make_error (err, errno, "parse memory.current");
val_swap = strtoll (swap_current, NULL, 10);
if (UNLIKELY (errno))
return crun_make_error (err, errno, "parse memory.swap.current");
if (memory->limit_present && memory->limit >= 0)
limit = memory->limit;
if (memory->swap_present && memory->swap >= 0)
limit += memory->swap;
if (limit <= val + val_swap)
return crun_make_error (err, 0, "cannot set the memory limit lower than its current usage");
}
if (memory->limit_present)
{
ret = write_memory (dirfd, cgroup2, memory, err);
if (ret >= 0)
memory_limits_written = true;
else
{
if (cgroup2 || crun_error_get_errno (err) != EINVAL)
return ret;
/*
If we get an EINVAL error on cgroup v1 we reverse
the order we write the memory limit and the swap.
Attempt to write again the memory limit once the memory
swap is written.
*/
crun_error_release (err);
}
}
ret = write_memory_swap (dirfd, cgroup2, memory, err);
if (UNLIKELY (ret < 0))
return ret;
if (memory->limit_present && ! memory_limits_written)
{
ret = write_memory (dirfd, cgroup2, memory, err);
if (UNLIKELY (ret < 0))
return ret;
}
if (memory->kernel_present)
{
if (cgroup2)
return crun_make_error (err, 0, "cannot set kernel memory with cgroupv2");
len = sprintf (fmt_buf, "%" PRIu64, memory->kernel);
ret = write_cgroup_file (dirfd, "memory.kmem.limit_in_bytes", fmt_buf, len, err);
if (UNLIKELY (ret < 0))
return ret;
}
// allows users set use_hierarchy as 0 when defined as false in spec.
// if use_hierarchy is not defined in spec value defaults to 1 (True).
// Note: users can only toggle use_hierarchy if the parent cgroup has use_hierarchy configured as 0.
if (memory->use_hierarchy_present)
{
if (cgroup2)
return crun_make_error (err, 0, "cannot set useHierarchy memory with cgroupv2");
ret = write_cgroup_file (dirfd, "memory.use_hierarchy", (memory->use_hierarchy) ? "1" : "0", 1, err);
if (UNLIKELY (ret < 0))
return ret;
}
if (memory->reservation_present)
{
len = sprintf (fmt_buf, "%" PRIu64, memory->reservation);
ret = write_file_and_check_controllers_at (cgroup2, dirfd, cgroup2 ? "memory.low" : "memory.soft_limit_in_bytes",
NULL, fmt_buf, len, err);
if (UNLIKELY (ret < 0))
return ret;
}
if (memory->disable_oom_killer)
{
if (cgroup2)
return crun_make_error (err, 0, "cannot disable OOM killer with cgroupv2");
ret = write_cgroup_file (dirfd, "memory.oom_control", "1", 1, err);
if (UNLIKELY (ret < 0))
return ret;
}
if (memory->kernel_tcp_present)
{
if (cgroup2)
return crun_make_error (err, 0, "cannot set kernel TCP with cgroupv2");
len = sprintf (fmt_buf, "%" PRIu64, memory->kernel_tcp);
ret = write_cgroup_file (dirfd, "memory.kmem.tcp.limit_in_bytes", fmt_buf, len, err);
if (UNLIKELY (ret < 0))
return ret;
}
if (memory->swappiness_present)
{
if (cgroup2)
return crun_make_error (err, 0, "cannot set memory swappiness with cgroupv2");
len = sprintf (fmt_buf, "%" PRIu64, memory->swappiness);
ret = write_cgroup_file (dirfd, "memory.swappiness", fmt_buf, len, err);
if (UNLIKELY (ret < 0))
return ret;
}
return 0;
}
int
write_cpu_burst (int cpu_dirfd, bool cgroup2, runtime_spec_schema_config_linux_resources_cpu *cpu,
libcrun_error_t *err)
{
char fmt_buf[32];
size_t len;
if (! cpu->burst_present)
return 0;
len = sprintf (fmt_buf, "%" PRIi64, cpu->burst);
return write_cgroup_file (cpu_dirfd, cgroup2 ? "cpu.max.burst" : "cpu.cfs_burst_us", fmt_buf, len, err);
}
static int
write_pids_resources (int dirfd, bool cgroup2, runtime_spec_schema_config_linux_resources_pids *pids,
libcrun_error_t *err)
{
if (pids->limit)
{
char fmt_buf[32];
size_t len;
int ret;
len = cg_itoa (fmt_buf, pids->limit, true);
ret = write_file_and_check_controllers_at (cgroup2, dirfd, "pids.max", NULL, fmt_buf, len, err);
if (UNLIKELY (ret < 0))
return ret;
}
return 0;
}
static int
write_cpu_resources (int dirfd_cpu, bool cgroup2, runtime_spec_schema_config_linux_resources_cpu *cpu,
libcrun_error_t *err)
{
size_t len, period_len;
int ret;
char fmt_buf[64];
int64_t period = -1;
int64_t quota = -1;
cleanup_free char *period_str = NULL;
if (cpu->shares)
{
uint32_t val = cpu->shares;
if (cgroup2)
val = convert_shares_to_weight (val);
len = sprintf (fmt_buf, "%u", val);
ret = write_file_and_check_controllers_at (cgroup2, dirfd_cpu, cgroup2 ? "cpu.weight" : "cpu.shares",
NULL, fmt_buf, len, err);
if (UNLIKELY (ret < 0))
return ret;
}
if (cpu->period)
{
if (cgroup2)
period = cpu->period;
else
{
len = sprintf (fmt_buf, "%" PRIu64, cpu->period);
ret = write_cgroup_file (dirfd_cpu, "cpu.cfs_period_us", fmt_buf, len, err);
if (UNLIKELY (ret < 0))
{
/*
Sometimes when the period to be set is smaller than the current one,
it is rejected by the kernel (EINVAL) as old_quota/new_period exceeds
the parent cgroup quota limit. If this happens and the quota is going
to be set, ignore the error for now and retry after setting the quota.
*/
if (! cpu->quota || crun_error_get_errno (err) != EINVAL)
return ret;
crun_error_release (err);
period_str = xstrdup (fmt_buf);
period_len = len;
}
}
}
if (cpu->quota)
{
if (cgroup2)
quota = cpu->quota;
else
{
len = sprintf (fmt_buf, "%" PRIi64, cpu->quota);
ret = write_cgroup_file (dirfd_cpu, "cpu.cfs_quota_us", fmt_buf, len, err);
if (UNLIKELY (ret < 0))
return ret;
if (period_str != NULL)
{
ret = write_cgroup_file (dirfd_cpu, "cpu.cfs_period_us", period_str, period_len, err);
if (UNLIKELY (ret < 0))
return ret;
}
}
}
if (cpu->realtime_period)
{
if (cgroup2)
return crun_make_error (err, 0, "realtime period not supported on cgroupv2");
len = sprintf (fmt_buf, "%" PRIu64, cpu->realtime_period);
ret = write_cgroup_file (dirfd_cpu, "cpu.rt_period_us", fmt_buf, len, err);
if (UNLIKELY (ret < 0))
return ret;