• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/procd/jail/cgroups.c

  1 /*
  2  * Copyright (C) 2020 Daniel Golle <daniel@makrotopia.org>
  3  *
  4  * This program is free software; you can redistribute it and/or modify
  5  * it under the terms of the GNU Lesser General Public License version 2.1
  6  * as published by the Free Software Foundation
  7  *
  8  * This program is distributed in the hope that it will be useful,
  9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 11  * GNU General Public License for more details.
 12  *
 13  * reads unified cgroup config as proposed in
 14  * https://github.com/opencontainers/runtime-spec/pull/1040
 15  * attempt conversion from cgroup1 -> cgroup2
 16  * https://github.com/containers/crun/blob/0.14.1/crun.1.md#cgroup-v2
 17  *
 18  * ToDo:
 19  *  - convert cgroup1 net_prio and net_cls to eBPF program
 20  *  - rdma (anyone?) intelrdt (anyone?)
 21  */
 22 
 23 #define _GNU_SOURCE
 24 
 25 #include <errno.h>
 26 #include <fcntl.h>
 27 #include <limits.h>
 28 #include <poll.h>
 29 #include <stdlib.h>
 30 #include <stdio.h>
 31 #include <string.h>
 32 #include <sys/stat.h>
 33 #include <sys/mman.h>
 34 #include <time.h>
 35 #include <unistd.h>
 36 #include <libgen.h>
 37 #include <inttypes.h>
 38 
 39 #include <libubox/avl.h>
 40 #include <libubox/avl-cmp.h>
 41 #include <libubox/blobmsg.h>
 42 #include <libubox/list.h>
 43 #include <libubox/utils.h>
 44 
 45 #include "log.h"
 46 #include "cgroups.h"
 47 #include "cgroups-bpf.h"
 48 
 49 #define CGROUP_ROOT "/sys/fs/cgroup/"
 50 #define CGROUP_IO_WEIGHT_MAX 10000
 51 #define CGROUP_DRAIN_TIMEOUT_MSEC 1000
 52 #define CGROUP_CPU_PERIOD_DEFAULT 100000
 53 
 54 struct cgval {
 55         struct avl_node avl;
 56         char *val;
 57 };
 58 
 59 struct cpu_max {
 60         uint64_t quota;
 61         uint64_t period;
 62         bool unlimited;
 63 };
 64 
 65 struct avl_tree cgvals;
 66 static struct avl_tree cgceil, cgprev;
 67 static char cg_refused[NAME_MAX + 1];
 68 static char *cgroup_path;
 69 static bool initialized;
 70 static bool destroyed;
 71 static bool cg_sealed;
 72 
 73 void cgroups_prepare(void) {
 74         initialized = false;
 75         destroyed = false;
 76 }
 77 
 78 void cgroups_init(const char *p) {
 79         avl_init(&cgvals, avl_strcmp, false, NULL);
 80         avl_init(&cgceil, avl_strcmp, false, NULL);
 81         avl_init(&cgprev, avl_strcmp, false, NULL);
 82         cgroup_path = strdup(p);
 83         initialized = true;
 84         cg_sealed = false;
 85 }
 86 
 87 bool cgroups_initialized(void)
 88 {
 89         return initialized;
 90 }
 91 
 92 static void cgroups_tree_free(struct avl_tree *tree)
 93 {
 94         struct cgval *valp, *tmp;
 95 
 96         avl_remove_all_elements(tree, valp, avl, tmp) {
 97                 free((void *)(valp->avl.key));
 98                 free(valp->val);
 99                 free(valp);
100         }
101 }
102 
103 static void cgroups_tree_copy(struct avl_tree *dst, struct avl_tree *src)
104 {
105         struct cgval *valp, *copy;
106 
107         cgroups_tree_free(dst);
108         avl_for_each_element(src, valp, avl) {
109                 copy = malloc(sizeof(*copy));
110                 if (!copy)
111                         exit(ENOMEM);
112 
113                 copy->avl.key = strdup((const char *)valp->avl.key);
114                 copy->val = strdup(valp->val);
115                 avl_insert(dst, &copy->avl);
116         }
117 }
118 
119 static const char *cgroups_tree_get(struct avl_tree *tree, const char *key)
120 {
121         struct cgval *valp;
122 
123         valp = avl_find_element(tree, key, valp, avl);
124         return valp ? valp->val : NULL;
125 }
126 
127 void cgroups_seal(void)
128 {
129         if (!initialized)
130                 return;
131 
132         cgroups_tree_copy(&cgceil, &cgvals);
133         cgroups_devices_seal();
134         cg_sealed = true;
135 }
136 
137 void cgroups_update_begin(void)
138 {
139         cgroups_tree_copy(&cgprev, &cgvals);
140         cgroups_devices_update_begin();
141 }
142 
143 void cgroups_update_rollback(void)
144 {
145         cgroups_tree_copy(&cgvals, &cgprev);
146         cgroups_devices_update_rollback();
147 }
148 
149 static void cgroups_val_restore(struct cgval *valp, const char *ceil)
150 {
151         free(valp->val);
152         valp->val = strdup(ceil);
153 }
154 
155 static bool cgroups_u64_parse(const char *s, uint64_t *v)
156 {
157         char *end;
158 
159         if (*s < '' || *s > '9')
160                 return false;
161 
162         errno = 0;
163         *v = strtoull(s, &end, 10);
164         return !errno && *end == '\0';
165 }
166 
167 static bool cgroups_cpu_max_parse(const char *s, struct cpu_max *cm)
168 {
169         char *end;
170 
171         cm->period = 0;
172         cm->unlimited = !strncmp(s, "max", 3);
173         if (cm->unlimited) {
174                 cm->quota = 0;
175                 end = (char *)s + 3;
176         } else {
177                 if (*s < '' || *s > '9')
178                         return false;
179 
180                 errno = 0;
181                 cm->quota = strtoull(s, &end, 10);
182                 if (errno)
183                         return false;
184         }
185 
186         if (*end == '\0')
187                 return true;
188 
189         if (*end != ' ')
190                 return false;
191 
192         return cgroups_u64_parse(end + 1, &cm->period);
193 }
194 
195 static bool cgroups_key_bounded(const char *key)
196 {
197         return !strcmp(key, "memory.max") || !strcmp(key, "memory.swap.max") ||
198                !strcmp(key, "pids.max") || !strcmp(key, "cpu.max.burst");
199 }
200 
201 static int cgroups_check_bounded(struct cgval *valp, const char *ceil)
202 {
203         uint64_t req, lim;
204 
205         if (!strcmp(valp->val, "max")) {
206                 if (ceil)
207                         cgroups_val_restore(valp, ceil);
208                 return 0;
209         }
210 
211         if (!ceil || !strcmp(ceil, "max") || !strcmp(ceil, valp->val))
212                 return 0;
213 
214         if (!cgroups_u64_parse(valp->val, &req) ||
215             !cgroups_u64_parse(ceil, &lim))
216                 return EPERM;
217 
218         return req <= lim ? 0 : EPERM;
219 }
220 
221 static void cgroups_cpu_max_store(struct cgval *valp, const struct cpu_max *cm)
222 {
223         char buf[48];
224 
225         snprintf(buf, sizeof(buf), "%" PRIu64 " %" PRIu64, cm->quota, cm->period);
226         cgroups_val_restore(valp, buf);
227 }
228 
229 static uint64_t cgroups_cpu_period_current(uint64_t ceil_period)
230 {
231         struct cpu_max prev;
232         const char *cur;
233 
234         cur = cgroups_tree_get(&cgprev, "cpu.max");
235         if (cur && cgroups_cpu_max_parse(cur, &prev) && prev.period)
236                 return prev.period;
237 
238         return ceil_period;
239 }
240 
241 static int cgroups_check_cpu_max_raw(struct cgval *valp, const char *ceil)
242 {
243         struct cpu_max req;
244 
245         if (!strcmp(valp->val, ceil))
246                 return 0;
247 
248         if (!cgroups_cpu_max_parse(valp->val, &req) || !req.unlimited)
249                 return EPERM;
250 
251         cgroups_val_restore(valp, ceil);
252         return 0;
253 }
254 
255 static int cgroups_check_cpu_max(struct cgval *valp, const char *ceil)
256 {
257         struct cpu_max req, lim;
258         bool pin = false;
259 
260         if (!ceil)
261                 return 0;
262 
263         if (!cgroups_cpu_max_parse(ceil, &lim))
264                 return cgroups_check_cpu_max_raw(valp, ceil);
265 
266         if (lim.unlimited)
267                 return 0;
268 
269         if (!cgroups_cpu_max_parse(valp->val, &req))
270                 return EPERM;
271 
272         if (!lim.period)
273                 lim.period = CGROUP_CPU_PERIOD_DEFAULT;
274 
275         if (req.unlimited) {
276                 cgroups_cpu_max_store(valp, &lim);
277                 return 0;
278         }
279 
280         if (!req.period) {
281                 req.period = cgroups_cpu_period_current(lim.period);
282                 pin = true;
283         }
284 
285         if (req.quota > UINT32_MAX || req.period > UINT32_MAX ||
286             lim.quota > UINT32_MAX || lim.period > UINT32_MAX)
287                 return ERANGE;
288 
289         if (req.quota * lim.period > lim.quota * req.period)
290                 return EPERM;
291 
292         if (pin)
293                 cgroups_cpu_max_store(valp, &req);
294 
295         return 0;
296 }
297 
298 static const char *cgroups_oci_name(const char *key)
299 {
300         if (!strcmp(key, "memory.max"))
301                 return "linux.resources.memory.limit";
302 
303         if (!strcmp(key, "memory.swap.max"))
304                 return "linux.resources.memory.swap";
305 
306         if (!strcmp(key, "pids.max"))
307                 return "linux.resources.pids.limit";
308 
309         if (!strcmp(key, "cpu.max"))
310                 return "linux.resources.cpu.quota";
311 
312         if (!strcmp(key, "cpu.max.burst"))
313                 return "linux.resources.cpu.burst";
314 
315         if (!strncmp(key, "hugetlb.", 8))
316                 return "linux.resources.hugepageLimits";
317 
318         return key;
319 }
320 
321 static int cgroups_check_attrs(const char **key)
322 {
323         struct cgval *valp;
324         const char *ceil;
325         int ret = 0;
326 
327         avl_for_each_element(&cgvals, valp, avl) {
328                 *key = (const char *)valp->avl.key;
329                 ceil = cgroups_tree_get(&cgceil, *key);
330                 if (!strcmp(*key, "cpu.max"))
331                         ret = cgroups_check_cpu_max(valp, ceil);
332                 else if (cgroups_key_bounded(*key))
333                         ret = cgroups_check_bounded(valp, ceil);
334                 else if (ceil && strcmp(ceil, valp->val))
335                         ret = ENOTSUP;
336 
337                 if (ret)
338                         break;
339         }
340 
341         return ret;
342 }
343 
344 int cgroups_update_check(const char **refused)
345 {
346         const char *key = NULL;
347         int ret;
348 
349         if (!cg_sealed)
350                 return 0;
351 
352         ret = cgroups_check_attrs(&key);
353         if (ret) {
354                 snprintf(cg_refused, sizeof(cg_refused), "%s",
355                          cgroups_oci_name(key));
356                 *refused = cg_refused;
357         } else if (cgroups_devices_differ()) {
358                 *refused = "linux.resources.devices";
359                 ret = ENOTSUP;
360         }
361 
362         if (!ret)
363                 return 0;
364 
365         cgroups_update_rollback();
366 
367         return ret;
368 }
369 
370 static void cgroups_set(const char *key, const char *val)
371 {
372         struct cgval *valp;
373 
374         valp = avl_find_element(&cgvals, key, valp, avl);
375         if (!valp) {
376                 valp = malloc(sizeof(struct cgval));
377                 if (!valp)
378                         exit(ENOMEM);
379 
380                 valp->avl.key = strdup(key);
381                 avl_insert(&cgvals, &valp->avl);
382         } else {
383                 DEBUG("overwriting previous cgroup2 assignment %s=\"%s\"!\n", key, valp->val);
384                 free(valp->val);
385         }
386 
387         valp->val = strdup(val);
388 }
389 
390 static int64_t cgroups_memory_max_staged(void)
391 {
392         struct cgval *valp;
393 
394         valp = avl_find_element(&cgvals, "memory.max", valp, avl);
395         if (!valp || !strcmp(valp->val, "max"))
396                 return -1;
397 
398         return strtoll(valp->val, NULL, 10);
399 }
400 
401 void cgroups_free(void)
402 {
403         if (initialized) {
404                 cgroups_tree_free(&cgvals);
405                 cgroups_tree_free(&cgceil);
406                 cgroups_tree_free(&cgprev);
407                 cgroups_devices_free();
408                 cg_sealed = false;
409                 free(cgroup_path);
410                 cgroup_path = NULL;
411                 initialized = false;
412         }
413 }
414 
415 static int cgroups_write_attr(const char *attr, const char *val, size_t vlen)
416 {
417         char *ent;
418         int fd, ret = 0;
419         size_t len;
420 
421         if (!cgroup_path)
422                 return -ENODEV;
423 
424         len = strlen(cgroup_path) + 1 + strlen(attr) + 1;
425         ent = malloc(len);
426         if (!ent)
427                 return -ENOMEM;
428 
429         snprintf(ent, len, "%s/%s", cgroup_path, attr);
430         fd = open(ent, O_WRONLY);
431         if (fd < 0) {
432                 ret = -errno;
433                 free(ent);
434                 return ret;
435         }
436 
437         if (write(fd, val, vlen) < 0)
438                 ret = -errno;
439 
440         close(fd);
441         free(ent);
442         return ret;
443 }
444 
445 int cgroups_kill_all(void)
446 {
447         return cgroups_write_attr("cgroup.kill", "1", 1);
448 }
449 
450 int cgroups_set_frozen(bool frozen)
451 {
452         return cgroups_write_attr("cgroup.freeze", frozen ? "1" : "", 1);
453 }
454 
455 int cgroups_reclaim(int64_t bytes, int32_t swappiness)
456 {
457         char val[64];
458         int len, ret;
459         int attempt;
460 
461         if (bytes < 0)
462                 return -EINVAL;
463 
464         if (swappiness >= 0)
465                 len = snprintf(val, sizeof(val), "%" PRId64 " swappiness=%" PRId32,
466                                bytes, swappiness);
467         else
468                 len = snprintf(val, sizeof(val), "%" PRId64, bytes);
469         if (len < 0 || len >= (int)sizeof(val))
470                 return -EINVAL;
471 
472         for (attempt = 0; attempt < 2; ++attempt) {
473                 ret = cgroups_write_attr("memory.reclaim", val, len);
474                 if (ret != -EINTR)
475                         break;
476         }
477         if (ret == -ENOENT)
478                 ret = -ENODEV;
479         return ret;
480 }
481 
482 int cgroups_attach_pid(pid_t pid)
483 {
484         char *ent;
485         int fd, ret = 0;
486         size_t len;
487 
488         if (!cgroup_path)
489                 return -ENODEV;
490 
491         len = strlen(cgroup_path) + strlen("/cgroup.procs") + 1;
492         ent = malloc(len);
493         if (!ent)
494                 return -ENOMEM;
495 
496         snprintf(ent, len, "%s/cgroup.procs", cgroup_path);
497         fd = open(ent, O_WRONLY);
498         if (fd < 0) {
499                 ret = -errno;
500                 free(ent);
501                 return ret;
502         }
503 
504         if (dprintf(fd, "%d", pid) < 0)
505                 ret = -errno;
506 
507         close(fd);
508         free(ent);
509         return ret;
510 }
511 
512 static void cgroups_compute_subtree_control(char *out, size_t outlen)
513 {
514         struct cgval *valp;
515         bool cpuset = false,
516              cpu = false,
517              hugetlb = false,
518              io = false,
519              memory = false,
520              pids = false,
521              rdma = false;
522         char *p;
523 
524         out[0] = '\0';
525 
526         avl_for_each_element(&cgvals, valp, avl) {
527                 const char *ent = (const char *)valp->avl.key;
528 
529                 if (!strncmp("cpuset.", ent, 7))
530                         cpuset = true;
531                 else if (!strncmp("cpu.", ent, 4))
532                         cpu = true;
533                 else if (!strncmp("hugetlb.", ent, 8))
534                         hugetlb = true;
535                 else if (!strncmp("io.", ent, 3))
536                         io = true;
537                 else if (!strncmp("memory.", ent, 7))
538                         memory = true;
539                 else if (!strncmp("pids.", ent, 5))
540                         pids = true;
541                 else if (!strncmp("rdma.", ent, 5))
542                         rdma = true;
543         }
544 
545         if (cpuset)
546                 strncat(out, "+cpuset ", outlen - strlen(out) - 1);
547 
548         if (cpu)
549                 strncat(out, "+cpu ", outlen - strlen(out) - 1);
550 
551         if (hugetlb)
552                 strncat(out, "+hugetlb ", outlen - strlen(out) - 1);
553 
554         if (io)
555                 strncat(out, "+io ", outlen - strlen(out) - 1);
556 
557         if (memory)
558                 strncat(out, "+memory ", outlen - strlen(out) - 1);
559 
560         if (pids)
561                 strncat(out, "+pids ", outlen - strlen(out) - 1);
562 
563         if (rdma)
564                 strncat(out, "+rdma ", outlen - strlen(out) - 1);
565 
566         p = strchr(out, '\0');
567         if (p > out && p[-1] == ' ')
568                 p[-1] = '\0';
569 }
570 
571 static bool cgroups_populated(int fd)
572 {
573         char buf[128];
574         ssize_t len;
575         char *val;
576 
577         if (lseek(fd, 0, SEEK_SET) < 0)
578                 return false;
579 
580         len = read(fd, buf, sizeof(buf) - 1);
581         if (len <= 0)
582                 return false;
583 
584         buf[len] = '\0';
585         val = strstr(buf, "populated ");
586         if (!val)
587                 return false;
588 
589         return val[strlen("populated ")] == '1';
590 }
591 
592 static void cgroups_wait_empty(void)
593 {
594         struct timespec deadline, now;
595         struct pollfd pfd;
596         int remaining;
597         size_t len;
598         char *ent;
599         int fd;
600 
601         len = strlen(cgroup_path) + strlen("/cgroup.events") + 1;
602         ent = malloc(len);
603         if (!ent)
604                 return;
605 
606         snprintf(ent, len, "%s/cgroup.events", cgroup_path);
607         fd = open(ent, O_RDONLY);
608         free(ent);
609         if (fd < 0)
610                 return;
611 
612         clock_gettime(CLOCK_MONOTONIC, &deadline);
613         deadline.tv_sec += CGROUP_DRAIN_TIMEOUT_MSEC / 1000;
614 
615         pfd.fd = fd;
616         pfd.events = POLLPRI;
617 
618         while (cgroups_populated(fd)) {
619                 clock_gettime(CLOCK_MONOTONIC, &now);
620                 remaining = (deadline.tv_sec - now.tv_sec) * 1000 +
621                             (deadline.tv_nsec - now.tv_nsec) / 1000000;
622                 if (remaining <= 0)
623                         break;
624 
625                 if (poll(&pfd, 1, remaining) < 0 && errno != EINTR)
626                         break;
627         }
628 
629         close(fd);
630 }
631 
632 void cgroups_destroy(void)
633 {
634         char *sep;
635 
636         destroyed = true;
637 
638         if (!cgroup_path)
639                 return;
640 
641         cgroups_kill_all();
642         cgroups_wait_empty();
643 
644         if (rmdir(cgroup_path) && errno == EBUSY)
645                 ERROR("failed to remove busy cgroup %s\n", cgroup_path);
646 
647         sep = strrchr(cgroup_path, '/');
648         if (sep && sep != cgroup_path) {
649                 *sep = '\0';
650                 (void)rmdir(cgroup_path);
651                 *sep = '/';
652         }
653 }
654 
655 bool cgroups_destroyed(void)
656 {
657         return destroyed;
658 }
659 
660 void cgroups_create(void)
661 {
662         char subtree_control[64] = { 0 };
663         char *cdir, *ent;
664         size_t maxlen;
665         int fd;
666 
667         if (!cgroup_path)
668                 return;
669 
670         DEBUG("creating cgroup %s\n", cgroup_path);
671         mkdir_p(cgroup_path, 0700);
672 
673         cgroups_compute_subtree_control(subtree_control, sizeof(subtree_control));
674         if (!subtree_control[0]) {
675                 DEBUG("no cgroup controllers requested, skipping subtree_control walk\n");
676                 return;
677         }
678 
679         maxlen = strlen(cgroup_path) + strlen("/cgroup.subtree_control") + 1;
680         ent = malloc(maxlen);
681         if (!ent)
682                 exit(ENOMEM);
683 
684         DEBUG("recursively applying cgroup.subtree_control = \"%s\"\n", subtree_control);
685         cdir = &cgroup_path[strlen(CGROUP_ROOT) - 2];
686         while ((cdir = strchr(cdir + 1, '/'))) {
687                 *cdir = '\0';
688                 snprintf(ent, maxlen, "%s/cgroup.subtree_control", cgroup_path);
689                 DEBUG(" * %s\n", ent);
690                 if ((fd = open(ent, O_WRONLY)) < 0) {
691                         ERROR("can't open %s: %m\n", ent);
692                         *cdir = '/';
693                         continue;
694                 }
695                 if (write(fd, subtree_control, strlen(subtree_control)) == -1)
696                         ERROR("can't write to %s: %m\n", ent);
697                 close(fd);
698                 *cdir = '/';
699         }
700         free(ent);
701 }
702 
703 /* a swap limit narrows nothing on a kernel built without swap */
704 static bool cgroups_attr_vacuous(const char *key)
705 {
706         if (strcmp(key, "memory.swap.max"))
707                 return false;
708 
709         return access("/proc/swaps", F_OK) != 0;
710 }
711 
712 int cgroups_configure(void)
713 {
714         struct cgval *valp;
715         size_t maxlen = 0;
716         int fd, dirfd;
717         int ret = 0;
718         char *ent;
719 
720         if (!cgroup_path)
721                 return 0;
722 
723         avl_for_each_element(&cgvals, valp, avl) {
724                 size_t klen = strlen((char *)valp->avl.key);
725 
726                 if (klen > maxlen)
727                         maxlen = klen;
728         }
729         maxlen += strlen(cgroup_path) + 2;
730 
731         ent = malloc(maxlen);
732         if (!ent)
733                 exit(ENOMEM);
734 
735         avl_for_each_element(&cgvals, valp, avl) {
736                 DEBUG("applying cgroup2 %s=\"%s\"\n", (char *)valp->avl.key, valp->val);
737                 snprintf(ent, maxlen, "%s/%s", cgroup_path, (char *)valp->avl.key);
738                 fd = open(ent, O_WRONLY);
739                 if (fd < 0) {
740                         if (cgroups_attr_vacuous((char *)valp->avl.key)) {
741                                 INFO("%s is unavailable here, \"%s\" narrows nothing\n",
742                                      ent, valp->val);
743                                 continue;
744                         }
745                         ERROR("can't open %s: %m\n", ent);
746                         jail_cgroup_refused((char *)valp->avl.key);
747                         ret = ENOTSUP;
748                         break;
749                 }
750                 if (dprintf(fd, "%s", valp->val) < 0) {
751                         ERROR("can't write to %s: %m\n", ent);
752                         close(fd);
753                         if (cgroups_attr_vacuous((char *)valp->avl.key))
754                                 continue;
755 
756                         jail_cgroup_refused((char *)valp->avl.key);
757                         ret = ENOTSUP;
758                         break;
759                 }
760                 close(fd);
761         }
762         free(ent);
763 
764         if (ret)
765                 return ret;
766 
767         dirfd = open(cgroup_path, O_DIRECTORY);
768         if (dirfd < 0) {
769                 ERROR("can't open %s: %m\n", cgroup_path);
770         } else {
771                 attach_cgroups_ebpf(dirfd);
772                 close(dirfd);
773         }
774 
775         return 0;
776 }
777 
778 int cgroups_apply(pid_t pid)
779 {
780         int ret;
781 
782         cgroups_create();
783 
784         ret = cgroups_configure();
785         if (ret)
786                 return ret;
787 
788         cgroups_attach_pid(pid);
789 
790         return 0;
791 }
792 
793 enum {
794         OCI_LINUX_CGROUPS_BLOCKIO_WEIGHTDEVICE_MAJOR,
795         OCI_LINUX_CGROUPS_BLOCKIO_WEIGHTDEVICE_MINOR,
796         OCI_LINUX_CGROUPS_BLOCKIO_WEIGHTDEVICE_WEIGHT,
797         OCI_LINUX_CGROUPS_BLOCKIO_WEIGHTDEVICE_LEAFWEIGHT,
798         __OCI_LINUX_CGROUPS_BLOCKIO_WEIGHTDEVICE_MAX,
799 };
800 
801 static const struct blobmsg_policy oci_linux_cgroups_blockio_weightdevice_policy[] = {
802         [OCI_LINUX_CGROUPS_BLOCKIO_WEIGHTDEVICE_MAJOR] = { "major", BLOBMSG_CAST_INT64 },
803         [OCI_LINUX_CGROUPS_BLOCKIO_WEIGHTDEVICE_MINOR] = { "minor", BLOBMSG_CAST_INT64 },
804         [OCI_LINUX_CGROUPS_BLOCKIO_WEIGHTDEVICE_WEIGHT] = { "weight", BLOBMSG_TYPE_INT32 },
805         [OCI_LINUX_CGROUPS_BLOCKIO_WEIGHTDEVICE_LEAFWEIGHT] = { "leafWeight", BLOBMSG_TYPE_INT32 },
806 };
807 
808 enum {
809         OCI_LINUX_CGROUPS_BLOCKIO_THROTTLEDEVICE_MAJOR,
810         OCI_LINUX_CGROUPS_BLOCKIO_THROTTLEDEVICE_MINOR,
811         OCI_LINUX_CGROUPS_BLOCKIO_THROTTLEDEVICE_RATE,
812         __OCI_LINUX_CGROUPS_BLOCKIO_THROTTLEDEVICE_MAX,
813 };
814 
815 static const struct blobmsg_policy oci_linux_cgroups_blockio_throttledevice_policy[] = {
816         [OCI_LINUX_CGROUPS_BLOCKIO_THROTTLEDEVICE_MAJOR] = { "major", BLOBMSG_CAST_INT64 },
817         [OCI_LINUX_CGROUPS_BLOCKIO_THROTTLEDEVICE_MINOR] = { "minor", BLOBMSG_CAST_INT64 },
818         [OCI_LINUX_CGROUPS_BLOCKIO_THROTTLEDEVICE_RATE] = { "rate", BLOBMSG_CAST_INT64 },
819 };
820 
821 enum {
822         OCI_LINUX_CGROUPS_BLOCKIO_WEIGHT,
823         OCI_LINUX_CGROUPS_BLOCKIO_LEAFWEIGHT,
824         OCI_LINUX_CGROUPS_BLOCKIO_WEIGHTDEVICE,
825         OCI_LINUX_CGROUPS_BLOCKIO_THROTTLEREADBPSDEVICE,
826         OCI_LINUX_CGROUPS_BLOCKIO_THROTTLEWRITEBPSDEVICE,
827         OCI_LINUX_CGROUPS_BLOCKIO_THROTTLEREADIOPSDEVICE,
828         OCI_LINUX_CGROUPS_BLOCKIO_THROTTLEWRITEIOPSDEVICE,
829         __OCI_LINUX_CGROUPS_BLOCKIO_MAX,
830 };
831 
832 static const struct blobmsg_policy oci_linux_cgroups_blockio_policy[] = {
833         [OCI_LINUX_CGROUPS_BLOCKIO_WEIGHT] = { "weight", BLOBMSG_TYPE_INT32 },
834         [OCI_LINUX_CGROUPS_BLOCKIO_LEAFWEIGHT] = { "leafWeight", BLOBMSG_TYPE_INT32 },
835         [OCI_LINUX_CGROUPS_BLOCKIO_WEIGHTDEVICE] = { "weightDevice", BLOBMSG_TYPE_ARRAY },
836         [OCI_LINUX_CGROUPS_BLOCKIO_THROTTLEREADBPSDEVICE] = { "throttleReadBpsDevice", BLOBMSG_TYPE_ARRAY },
837         [OCI_LINUX_CGROUPS_BLOCKIO_THROTTLEWRITEBPSDEVICE] = { "throttleWriteBpsDevice", BLOBMSG_TYPE_ARRAY },
838         [OCI_LINUX_CGROUPS_BLOCKIO_THROTTLEREADIOPSDEVICE] = { "throttleReadIOPSDevice", BLOBMSG_TYPE_ARRAY },
839         [OCI_LINUX_CGROUPS_BLOCKIO_THROTTLEWRITEIOPSDEVICE] = { "throttleWriteIOPSDevice", BLOBMSG_TYPE_ARRAY },
840 };
841 
842 struct posix_dev {
843         uint64_t major;
844         uint64_t minor;
845 };
846 
847 struct iomax_line {
848         struct avl_node avl;
849         struct posix_dev dev;
850         uint64_t rbps;
851         uint64_t wbps;
852         uint64_t riops;
853         uint64_t wiops;
854 };
855 
856 static int avl_devcmp(const void *k1, const void *k2, void *ptr)
857 {
858         struct posix_dev *d1 = (struct posix_dev *)k1, *d2 = (struct posix_dev *)k2;
859 
860         if (d1->major < d2->major)
861                 return -1;
862 
863         if (d1->major > d2->major)
864                 return 1;
865 
866         if (d1->minor < d2->minor)
867                 return -1;
868 
869         if (d1->minor > d2->minor)
870                 return 1;
871 
872         return 0;
873 }
874 
875 static struct iomax_line *get_iomax_line(struct avl_tree *iomax, uint64_t major, uint64_t minor)
876 {
877         struct iomax_line *l;
878         struct posix_dev d;
879         d.major = major;
880         d.minor = minor;
881         l = avl_find_element(iomax, &d, l, avl);
882         if (!l) {
883                 l = malloc(sizeof(struct iomax_line));
884                 if (!l)
885                         exit(ENOMEM);
886 
887                 l->dev.major = d.major;
888                 l->dev.minor = d.minor;
889                 l->avl.key = &l->dev;
890                 l->rbps = -1;
891                 l->wbps = -1;
892                 l->riops = -1;
893                 l->wiops = -1;
894                 avl_insert(iomax, &l->avl);
895         }
896 
897         return l;
898 }
899 
900 static int parseOCIlinuxcgroups_legacy_blockio(struct blob_attr *msg)
901 {
902         struct blob_attr *tb[__OCI_LINUX_CGROUPS_BLOCKIO_MAX],
903                          *tbwd[__OCI_LINUX_CGROUPS_BLOCKIO_WEIGHTDEVICE_MAX],
904                          *tbtd[__OCI_LINUX_CGROUPS_BLOCKIO_THROTTLEDEVICE_MAX],
905                          *cur;
906         int rem;
907         int weight = -1;
908         size_t numweightstrs = 0, numiomaxstrs = 0, strtotlen = 1;
909         char **weightstrs = NULL, **iomaxstrs = NULL, **curstr;
910         char *weightstr, *iomaxstr;
911         struct avl_tree iomax;
912         struct iomax_line *curiomax, *tmp;
913 
914         blobmsg_parse(oci_linux_cgroups_blockio_policy, __OCI_LINUX_CGROUPS_BLOCKIO_MAX, tb, blobmsg_data(msg), blobmsg_len(msg));
915 
916         if (tb[OCI_LINUX_CGROUPS_BLOCKIO_WEIGHT]) {
917                 weight = blobmsg_get_u32(tb[OCI_LINUX_CGROUPS_BLOCKIO_WEIGHT]);
918                 ++numweightstrs;
919         }
920 
921         if (weight > CGROUP_IO_WEIGHT_MAX)
922                 return ERANGE;
923 
924         if (tb[OCI_LINUX_CGROUPS_BLOCKIO_LEAFWEIGHT])
925                 return jail_unsupported(tb[OCI_LINUX_CGROUPS_BLOCKIO_LEAFWEIGHT]);
926 
927         blobmsg_for_each_attr(cur, tb[OCI_LINUX_CGROUPS_BLOCKIO_WEIGHTDEVICE], rem)
928                 ++numweightstrs;
929 
930         weightstrs = calloc(numweightstrs + 1, sizeof(char *));
931         if (!weightstrs)
932                 exit(ENOMEM);
933 
934         numweightstrs = 0;
935 
936         if (weight > -1)
937                 if (asprintf(&weightstrs[numweightstrs++], "default %d", weight) < 0)
938                         return ENOMEM;
939 
940         blobmsg_for_each_attr(cur, tb[OCI_LINUX_CGROUPS_BLOCKIO_WEIGHTDEVICE], rem) {
941                 uint64_t major, minor;
942                 int devweight = weight;
943 
944                 blobmsg_parse(oci_linux_cgroups_blockio_weightdevice_policy, __OCI_LINUX_CGROUPS_BLOCKIO_WEIGHTDEVICE_MAX, tbwd, blobmsg_data(cur), blobmsg_len(cur));
945                 if (!tbwd[OCI_LINUX_CGROUPS_BLOCKIO_WEIGHTDEVICE_MAJOR] ||
946                     !tbwd[OCI_LINUX_CGROUPS_BLOCKIO_WEIGHTDEVICE_MINOR])
947                         return ENODATA;
948 
949                 if (!tbwd[OCI_LINUX_CGROUPS_BLOCKIO_WEIGHTDEVICE_WEIGHT] &&
950                     !tbwd[OCI_LINUX_CGROUPS_BLOCKIO_WEIGHTDEVICE_LEAFWEIGHT])
951                         return ENODATA;
952 
953                 if (tbwd[OCI_LINUX_CGROUPS_BLOCKIO_WEIGHTDEVICE_WEIGHT])
954                         devweight = blobmsg_get_u32(tbwd[OCI_LINUX_CGROUPS_BLOCKIO_WEIGHTDEVICE_WEIGHT]);
955 
956                 if (devweight > CGROUP_IO_WEIGHT_MAX)
957                         return ERANGE;
958 
959                 if (tbwd[OCI_LINUX_CGROUPS_BLOCKIO_WEIGHTDEVICE_LEAFWEIGHT])
960                         return jail_unsupported(
961                                 tbwd[OCI_LINUX_CGROUPS_BLOCKIO_WEIGHTDEVICE_LEAFWEIGHT]);
962 
963                 major = blobmsg_cast_u64(tbwd[OCI_LINUX_CGROUPS_BLOCKIO_WEIGHTDEVICE_MAJOR]);
964                 minor = blobmsg_cast_u64(tbwd[OCI_LINUX_CGROUPS_BLOCKIO_WEIGHTDEVICE_MINOR]);
965 
966                 if (asprintf(&weightstrs[numweightstrs++], "%" PRIu64 ":%" PRIu64 " %u", major, minor, devweight) < 0)
967                         return ENOMEM;
968         }
969 
970         if (numweightstrs) {
971                 curstr = weightstrs;
972                 while (*curstr)
973                         strtotlen += strlen(*(curstr++)) + 1;
974 
975                 weightstr = calloc(strtotlen, sizeof(char));
976                 if (!weightstr)
977                         exit(ENOMEM);
978 
979                 curstr = weightstrs;
980                 while (*curstr) {
981                         strcat(weightstr, *curstr);
982                         strcat(weightstr, "\n");
983                         free(*(curstr++));
984                 }
985 
986                 cgroups_set("io.bfq.weight", weightstr);
987                 free(weightstr);
988         };
989 
990         free(weightstrs);
991 
992         avl_init(&iomax, avl_devcmp, false, NULL);
993 
994         blobmsg_for_each_attr(cur, tb[OCI_LINUX_CGROUPS_BLOCKIO_THROTTLEREADBPSDEVICE], rem) {
995                 struct iomax_line *l;
996 
997                 blobmsg_parse(oci_linux_cgroups_blockio_throttledevice_policy, __OCI_LINUX_CGROUPS_BLOCKIO_THROTTLEDEVICE_MAX, tbtd, blobmsg_data(cur), blobmsg_len(cur));
998 
999                 if (!tbtd[OCI_LINUX_CGROUPS_BLOCKIO_THROTTLEDEVICE_MAJOR] ||
1000                     !tbtd[OCI_LINUX_CGROUPS_BLOCKIO_THROTTLEDEVICE_MINOR] ||
1001                     !tbtd[OCI_LINUX_CGROUPS_BLOCKIO_THROTTLEDEVICE_RATE])
1002                         return ENODATA;
1003 
1004                 l = get_iomax_line(&iomax,
1005                                    blobmsg_cast_u64(tbtd[OCI_LINUX_CGROUPS_BLOCKIO_THROTTLEDEVICE_MAJOR]),
1006                                    blobmsg_cast_u64(tbtd[OCI_LINUX_CGROUPS_BLOCKIO_THROTTLEDEVICE_MINOR]));
1007 
1008                 l->rbps = blobmsg_cast_u64(tbtd[OCI_LINUX_CGROUPS_BLOCKIO_THROTTLEDEVICE_RATE]);
1009         }
1010 
1011         blobmsg_for_each_attr(cur, tb[OCI_LINUX_CGROUPS_BLOCKIO_THROTTLEWRITEBPSDEVICE], rem) {
1012                 struct iomax_line *l;
1013 
1014                 blobmsg_parse(oci_linux_cgroups_blockio_throttledevice_policy, __OCI_LINUX_CGROUPS_BLOCKIO_THROTTLEDEVICE_MAX, tbtd, blobmsg_data(cur), blobmsg_len(cur));
1015 
1016                 if (!tbtd[OCI_LINUX_CGROUPS_BLOCKIO_THROTTLEDEVICE_MAJOR] ||
1017                     !tbtd[OCI_LINUX_CGROUPS_BLOCKIO_THROTTLEDEVICE_MINOR] ||
1018                     !tbtd[OCI_LINUX_CGROUPS_BLOCKIO_THROTTLEDEVICE_RATE])
1019                         return ENODATA;
1020 
1021                 l = get_iomax_line(&iomax,
1022                                    blobmsg_cast_u64(tbtd[OCI_LINUX_CGROUPS_BLOCKIO_THROTTLEDEVICE_MAJOR]),
1023                                    blobmsg_cast_u64(tbtd[OCI_LINUX_CGROUPS_BLOCKIO_THROTTLEDEVICE_MINOR]));
1024 
1025                 l->wbps = blobmsg_cast_u64(tbtd[OCI_LINUX_CGROUPS_BLOCKIO_THROTTLEDEVICE_RATE]);
1026         }
1027 
1028         blobmsg_for_each_attr(cur, tb[OCI_LINUX_CGROUPS_BLOCKIO_THROTTLEREADIOPSDEVICE], rem) {
1029                 struct iomax_line *l;
1030 
1031                 blobmsg_parse(oci_linux_cgroups_blockio_throttledevice_policy, __OCI_LINUX_CGROUPS_BLOCKIO_THROTTLEDEVICE_MAX, tbtd, blobmsg_data(cur), blobmsg_len(cur));
1032 
1033                 if (!tbtd[OCI_LINUX_CGROUPS_BLOCKIO_THROTTLEDEVICE_MAJOR] ||
1034                     !tbtd[OCI_LINUX_CGROUPS_BLOCKIO_THROTTLEDEVICE_MINOR] ||
1035                     !tbtd[OCI_LINUX_CGROUPS_BLOCKIO_THROTTLEDEVICE_RATE])
1036                         return ENODATA;
1037 
1038                 l = get_iomax_line(&iomax,
1039                                    blobmsg_cast_u64(tbtd[OCI_LINUX_CGROUPS_BLOCKIO_THROTTLEDEVICE_MAJOR]),
1040                                    blobmsg_cast_u64(tbtd[OCI_LINUX_CGROUPS_BLOCKIO_THROTTLEDEVICE_MINOR]));
1041 
1042                 l->riops = blobmsg_cast_u64(tbtd[OCI_LINUX_CGROUPS_BLOCKIO_THROTTLEDEVICE_RATE]);
1043         }
1044 
1045         blobmsg_for_each_attr(cur, tb[OCI_LINUX_CGROUPS_BLOCKIO_THROTTLEWRITEIOPSDEVICE], rem) {
1046                 struct iomax_line *l;
1047 
1048                 blobmsg_parse(oci_linux_cgroups_blockio_throttledevice_policy, __OCI_LINUX_CGROUPS_BLOCKIO_THROTTLEDEVICE_MAX, tbtd, blobmsg_data(cur), blobmsg_len(cur));
1049 
1050                 if (!tbtd[OCI_LINUX_CGROUPS_BLOCKIO_THROTTLEDEVICE_MAJOR] ||
1051                     !tbtd[OCI_LINUX_CGROUPS_BLOCKIO_THROTTLEDEVICE_MINOR] ||
1052                     !tbtd[OCI_LINUX_CGROUPS_BLOCKIO_THROTTLEDEVICE_RATE])
1053                         return ENODATA;
1054 
1055                 l = get_iomax_line(&iomax,
1056                                    blobmsg_cast_u64(tbtd[OCI_LINUX_CGROUPS_BLOCKIO_THROTTLEDEVICE_MAJOR]),
1057                                    blobmsg_cast_u64(tbtd[OCI_LINUX_CGROUPS_BLOCKIO_THROTTLEDEVICE_MINOR]));
1058 
1059                 l->wiops = blobmsg_cast_u64(tbtd[OCI_LINUX_CGROUPS_BLOCKIO_THROTTLEDEVICE_RATE]);
1060         }
1061 
1062         avl_for_each_element(&iomax, curiomax, avl)
1063                 ++numiomaxstrs;
1064 
1065         if (!numiomaxstrs)
1066                 return 0;
1067 
1068         iomaxstrs = calloc(numiomaxstrs + 1, sizeof(char *));
1069         if (!iomaxstrs)
1070                 exit(ENOMEM);
1071 
1072         numiomaxstrs = 0;
1073 
1074         avl_for_each_element(&iomax, curiomax, avl) {
1075                 char iomaxlstr[160];
1076                 char lstr[32];
1077 
1078                 sprintf(iomaxlstr, "%" PRIu64 ":%" PRIu64 " ", curiomax->dev.major, curiomax->dev.minor);
1079 
1080                 if (curiomax->rbps != -1) {
1081                         sprintf(lstr, "rbps=%" PRIu64 " ", curiomax->rbps);
1082                         strcat(iomaxlstr, lstr);
1083                 }
1084                 if (curiomax->wbps != -1) {
1085                         sprintf(lstr, "wbps=%" PRIu64 " ", curiomax->wbps);
1086                         strcat(iomaxlstr, lstr);
1087                 }
1088                 if (curiomax->riops != -1) {
1089                         sprintf(lstr, "riops=%" PRIu64 " ", curiomax->riops);
1090                         strcat(iomaxlstr, lstr);
1091                 }
1092                 if (curiomax->wiops != -1) {
1093                         sprintf(lstr, "wiops=%" PRIu64 " ", curiomax->wiops);
1094                         strcat(iomaxlstr, lstr);
1095                 }
1096 
1097                 iomaxstrs[numiomaxstrs++] = strdup(iomaxlstr);
1098         }
1099 
1100         avl_for_each_element_safe(&iomax, curiomax, avl, tmp) {
1101                 avl_delete(&iomax, &curiomax->avl);
1102                 free(curiomax);
1103         }
1104 
1105         strtotlen = 1; /* 1 accounts for \0 at end of string */
1106         if (numiomaxstrs) {
1107                 curstr = iomaxstrs;
1108                 while (*curstr)
1109                         strtotlen += strlen(*(curstr++)) + 1; /* +1 accounts for \n at end of line */
1110 
1111                 iomaxstr = calloc(strtotlen, sizeof(char));
1112                 if (!iomaxstr)
1113                         exit(ENOMEM);
1114 
1115                 curstr = iomaxstrs;
1116 
1117                 while (*curstr) {
1118                         strcat(iomaxstr, *curstr);
1119                         strcat(iomaxstr, "\n");
1120                         free(*(curstr++));
1121                 }
1122 
1123                 cgroups_set("io.max", iomaxstr);
1124                 free(iomaxstr);
1125         };
1126 
1127         free(iomaxstrs);
1128 
1129         return 0;
1130 }
1131 
1132 
1133 enum {
1134         OCI_LINUX_CGROUPS_CPU_SHARES,
1135         OCI_LINUX_CGROUPS_CPU_PERIOD,
1136         OCI_LINUX_CGROUPS_CPU_QUOTA,
1137         OCI_LINUX_CGROUPS_CPU_BURST,
1138         OCI_LINUX_CGROUPS_CPU_IDLE,
1139         OCI_LINUX_CGROUPS_CPU_REALTIMERUNTIME,
1140         OCI_LINUX_CGROUPS_CPU_REALTIMEPERIOD,
1141         OCI_LINUX_CGROUPS_CPU_CPUS,
1142         OCI_LINUX_CGROUPS_CPU_MEMS,
1143         __OCI_LINUX_CGROUPS_CPU_MAX,
1144 };
1145 
1146 static const struct blobmsg_policy oci_linux_cgroups_cpu_policy[] = {
1147         [OCI_LINUX_CGROUPS_CPU_SHARES] = { "shares", BLOBMSG_CAST_INT64 },
1148         [OCI_LINUX_CGROUPS_CPU_PERIOD] = { "period", BLOBMSG_CAST_INT64 },
1149         [OCI_LINUX_CGROUPS_CPU_QUOTA] = { "quota", BLOBMSG_CAST_INT64 }, /* signed int64! */
1150         [OCI_LINUX_CGROUPS_CPU_BURST] = { "burst", BLOBMSG_CAST_INT64 },
1151         [OCI_LINUX_CGROUPS_CPU_IDLE] = { "idle", BLOBMSG_CAST_INT64 },
1152         [OCI_LINUX_CGROUPS_CPU_REALTIMEPERIOD] = { "realtimePeriod", BLOBMSG_CAST_INT64 },
1153         [OCI_LINUX_CGROUPS_CPU_REALTIMERUNTIME] = { "realtimeRuntime", BLOBMSG_CAST_INT64 },
1154         [OCI_LINUX_CGROUPS_CPU_CPUS] = { "cpus", BLOBMSG_TYPE_STRING },
1155         [OCI_LINUX_CGROUPS_CPU_MEMS] = { "mems", BLOBMSG_TYPE_STRING },
1156 };
1157 
1158 static int parseOCIlinuxcgroups_legacy_cpu(struct blob_attr *msg)
1159 {
1160         struct blob_attr *tb[__OCI_LINUX_CGROUPS_CPU_MAX];
1161         uint64_t shares, period = 0;
1162         int64_t quota = -2; /* unset */
1163         char tmp[64] = { 0 };
1164 
1165         blobmsg_parse(oci_linux_cgroups_cpu_policy, __OCI_LINUX_CGROUPS_CPU_MAX, tb, blobmsg_data(msg), blobmsg_len(msg));
1166 
1167         /* no equivalent in cgroup2 */
1168         if (tb[OCI_LINUX_CGROUPS_CPU_REALTIMEPERIOD])
1169                 return jail_unsupported(tb[OCI_LINUX_CGROUPS_CPU_REALTIMEPERIOD]);
1170 
1171         if (tb[OCI_LINUX_CGROUPS_CPU_REALTIMERUNTIME])
1172                 return jail_unsupported(tb[OCI_LINUX_CGROUPS_CPU_REALTIMERUNTIME]);
1173 
1174         if (tb[OCI_LINUX_CGROUPS_CPU_SHARES]) {
1175                 shares = blobmsg_cast_u64(tb[OCI_LINUX_CGROUPS_CPU_SHARES]);
1176                 if ((shares < 2) || (shares > 262144))
1177                         return ERANGE;
1178 
1179                 snprintf(tmp, sizeof(tmp), "%" PRIu64, (((uint64_t)1) + ((shares - 2) * 9999) / 262142));
1180                 cgroups_set("cpu.weight", tmp);
1181                 tmp[0] = '\0';
1182         }
1183 
1184         if (tb[OCI_LINUX_CGROUPS_CPU_QUOTA])
1185                 quota = blobmsg_cast_s64(tb[OCI_LINUX_CGROUPS_CPU_QUOTA]);
1186 
1187         if (tb[OCI_LINUX_CGROUPS_CPU_PERIOD])
1188                 period = blobmsg_cast_u64(tb[OCI_LINUX_CGROUPS_CPU_PERIOD]);
1189 
1190         if (period) {
1191                 if (quota >= 0)
1192                         snprintf(tmp, sizeof(tmp), "%" PRId64 " %" PRIu64 , quota, period);
1193                 else
1194                         snprintf(tmp, sizeof(tmp), "max %" PRIu64, period); /* assume default */
1195         } else if (quota >= 0) {
1196                 snprintf(tmp, sizeof(tmp), "%" PRId64, quota);
1197         } else if (quota == -1) {
1198                 strcpy(tmp, "max");
1199         }
1200 
1201         if (tmp[0])
1202                 cgroups_set("cpu.max", tmp);
1203 
1204         if (tb[OCI_LINUX_CGROUPS_CPU_BURST]) {
1205                 snprintf(tmp, sizeof(tmp), "%" PRIu64,
1206                          blobmsg_cast_u64(tb[OCI_LINUX_CGROUPS_CPU_BURST]));
1207                 cgroups_set("cpu.max.burst", tmp);
1208         }
1209 
1210         if (tb[OCI_LINUX_CGROUPS_CPU_IDLE]) {
1211                 snprintf(tmp, sizeof(tmp), "%" PRId64,
1212                          blobmsg_cast_s64(tb[OCI_LINUX_CGROUPS_CPU_IDLE]));
1213                 cgroups_set("cpu.idle", tmp);
1214         }
1215 
1216         if (tb[OCI_LINUX_CGROUPS_CPU_CPUS])
1217                 cgroups_set("cpuset.cpus", blobmsg_get_string(tb[OCI_LINUX_CGROUPS_CPU_CPUS]));
1218 
1219         if (tb[OCI_LINUX_CGROUPS_CPU_MEMS])
1220                 cgroups_set("cpuset.mems", blobmsg_get_string(tb[OCI_LINUX_CGROUPS_CPU_MEMS]));
1221 
1222         return 0;
1223 }
1224 
1225 
1226 enum {
1227         OCI_LINUX_CGROUPS_MEMORY_LIMIT,
1228         OCI_LINUX_CGROUPS_MEMORY_RESERVATION,
1229         OCI_LINUX_CGROUPS_MEMORY_SWAP,
1230         OCI_LINUX_CGROUPS_MEMORY_KERNEL,
1231         OCI_LINUX_CGROUPS_MEMORY_KERNELTCP,
1232         OCI_LINUX_CGROUPS_MEMORY_SWAPPINESS,
1233         OCI_LINUX_CGROUPS_MEMORY_DISABLEOOMKILLER,
1234         OCI_LINUX_CGROUPS_MEMORY_USEHIERARCHY,
1235         OCI_LINUX_CGROUPS_MEMORY_CHECKBEFOREUPDATE,
1236         __OCI_LINUX_CGROUPS_MEMORY_MAX,
1237 };
1238 
1239 static const struct blobmsg_policy oci_linux_cgroups_memory_policy[] = {
1240         [OCI_LINUX_CGROUPS_MEMORY_LIMIT] = { "limit", BLOBMSG_CAST_INT64 }, /* signed int64! */
1241         [OCI_LINUX_CGROUPS_MEMORY_RESERVATION] = { "reservation", BLOBMSG_CAST_INT64 }, /* signed int64! */
1242         [OCI_LINUX_CGROUPS_MEMORY_SWAP] = { "swap", BLOBMSG_CAST_INT64 }, /* signed int64! */
1243         [OCI_LINUX_CGROUPS_MEMORY_KERNEL] = { "kernel", BLOBMSG_CAST_INT64 }, /* signed int64! ignored */
1244         [OCI_LINUX_CGROUPS_MEMORY_KERNELTCP] = { "kernelTCP", BLOBMSG_CAST_INT64 }, /* signed int64! ignored */
1245         [OCI_LINUX_CGROUPS_MEMORY_SWAPPINESS] = { "swappiness", BLOBMSG_CAST_INT64 },
1246         [OCI_LINUX_CGROUPS_MEMORY_DISABLEOOMKILLER] = { "disableOOMKiller", BLOBMSG_TYPE_BOOL },
1247         [OCI_LINUX_CGROUPS_MEMORY_USEHIERARCHY] = { "useHierarchy", BLOBMSG_TYPE_BOOL },
1248         [OCI_LINUX_CGROUPS_MEMORY_CHECKBEFOREUPDATE] = { "checkBeforeUpdate", BLOBMSG_TYPE_BOOL },
1249 };
1250 
1251 static int64_t read_int64_file(const char *path)
1252 {
1253         char buf[32];
1254         char *end;
1255         int64_t v;
1256         int fd;
1257         ssize_t n;
1258 
1259         fd = open(path, O_RDONLY | O_CLOEXEC);
1260         if (fd < 0)
1261                 return -1;
1262         do {
1263                 n = read(fd, buf, sizeof(buf) - 1);
1264         } while (n < 0 && errno == EINTR);
1265         close(fd);
1266         if (n <= 0)
1267                 return -1;
1268         buf[n] = '\0';
1269         v = strtoll(buf, &end, 10);
1270         if (end == buf || (*end != '\0' && *end != '\n'))
1271                 return -1;
1272         return v;
1273 }
1274 
1275 int64_t cgroups_read_int64(const char *attr)
1276 {
1277         char path[PATH_MAX];
1278 
1279         if (!cgroup_path)
1280                 return -1;
1281 
1282         snprintf(path, sizeof(path), "%s/%s", cgroup_path, attr);
1283         return read_int64_file(path);
1284 }
1285 
1286 int cgroups_open_attr(const char *attr)
1287 {
1288         char path[PATH_MAX];
1289 
1290         if (!cgroup_path)
1291                 return -1;
1292 
1293         snprintf(path, sizeof(path), "%s/%s", cgroup_path, attr);
1294         return open(path, O_RDONLY | O_CLOEXEC);
1295 }
1296 
1297 int cgroups_open_dir(void)
1298 {
1299         if (!cgroup_path)
1300                 return -1;
1301         return open(cgroup_path, O_PATH | O_DIRECTORY | O_CLOEXEC);
1302 }
1303 
1304 void cgroups_set_memory_limit(int64_t bytes)
1305 {
1306         char tmp[32];
1307 
1308         snprintf(tmp, sizeof(tmp), "%" PRId64, bytes);
1309         cgroups_set("memory.max", tmp);
1310 }
1311 
1312 static int parseOCIlinuxcgroups_legacy_memory(struct blob_attr *msg, bool is_update)
1313 {
1314         struct blob_attr *tb[__OCI_LINUX_CGROUPS_MEMORY_MAX];
1315         char tmp[32] = { 0 };
1316         int64_t limit, swap = -1, reservation;
1317 
1318         blobmsg_parse(oci_linux_cgroups_memory_policy, __OCI_LINUX_CGROUPS_MEMORY_MAX, tb, blobmsg_data(msg), blobmsg_len(msg));
1319 
1320         /*
1321          * not all properties of the OCI memory section can be mapped to cgroup2
1322          * kernel memory accounting is always enabled and included in the set
1323          *   memory limit, hence these options can be ignored
1324          * disableOOMKiller could be emulated using oom_score_adj + seccomp eBPF
1325          *   preventing self-upgrade (but allow downgrade)
1326          *
1327          * see also https://github.com/opencontainers/runtime-spec/issues/1005
1328          */
1329         if (tb[OCI_LINUX_CGROUPS_MEMORY_SWAPPINESS])
1330                 return jail_unsupported(tb[OCI_LINUX_CGROUPS_MEMORY_SWAPPINESS]);
1331 
1332         if (tb[OCI_LINUX_CGROUPS_MEMORY_DISABLEOOMKILLER])
1333                 return jail_unsupported(tb[OCI_LINUX_CGROUPS_MEMORY_DISABLEOOMKILLER]);
1334 
1335         if (tb[OCI_LINUX_CGROUPS_MEMORY_USEHIERARCHY])
1336                 return jail_unsupported(tb[OCI_LINUX_CGROUPS_MEMORY_USEHIERARCHY]);
1337 
1338         if (tb[OCI_LINUX_CGROUPS_MEMORY_LIMIT])
1339                 limit = blobmsg_cast_s64(tb[OCI_LINUX_CGROUPS_MEMORY_LIMIT]);
1340         else
1341                 limit = cgroups_memory_max_staged();
1342 
1343         if (tb[OCI_LINUX_CGROUPS_MEMORY_SWAP]) {
1344                 swap = blobmsg_cast_s64(tb[OCI_LINUX_CGROUPS_MEMORY_SWAP]);
1345                 if (swap != -1 && limit != -1 && swap < limit)
1346                         return EINVAL;
1347         }
1348 
1349         if (is_update && tb[OCI_LINUX_CGROUPS_MEMORY_CHECKBEFOREUPDATE] &&
1350             blobmsg_get_bool(tb[OCI_LINUX_CGROUPS_MEMORY_CHECKBEFOREUPDATE])) {
1351                 char path[PATH_MAX];
1352                 int64_t current;
1353 
1354                 snprintf(path, sizeof(path), "%s/memory.current", cgroup_path);
1355                 current = read_int64_file(path);
1356                 if (current < 0) {
1357                         ERROR("memory.checkBeforeUpdate: cannot read %s: %m\n", path);
1358                         return EIO;
1359                 }
1360 
1361                 if (tb[OCI_LINUX_CGROUPS_MEMORY_LIMIT]) {
1362                         int64_t new_limit = blobmsg_cast_s64(tb[OCI_LINUX_CGROUPS_MEMORY_LIMIT]);
1363                         if (new_limit != -1 && new_limit < current) {
1364                                 ERROR("memory.checkBeforeUpdate: new limit %" PRId64
1365                                       " < current usage %" PRId64 "\n", new_limit, current);
1366                                 return EBUSY;
1367                         }
1368                 }
1369                 if (tb[OCI_LINUX_CGROUPS_MEMORY_RESERVATION]) {
1370                         int64_t new_res = blobmsg_cast_s64(tb[OCI_LINUX_CGROUPS_MEMORY_RESERVATION]);
1371                         if (new_res != -1 && new_res < current) {
1372                                 ERROR("memory.checkBeforeUpdate: new reservation %" PRId64
1373                                       " < current usage %" PRId64 "\n", new_res, current);
1374                                 return EBUSY;
1375                         }
1376                 }
1377         }
1378 
1379         if (tb[OCI_LINUX_CGROUPS_MEMORY_LIMIT]) {
1380                 if (limit == -1)
1381                         strcpy(tmp, "max");
1382                 else
1383                         snprintf(tmp, sizeof(tmp), "%" PRId64, limit);
1384 
1385                 cgroups_set("memory.max", tmp);
1386         }
1387 
1388         if (tb[OCI_LINUX_CGROUPS_MEMORY_RESERVATION]) {
1389                 reservation = blobmsg_cast_s64(tb[OCI_LINUX_CGROUPS_MEMORY_RESERVATION]);
1390 
1391                 if (reservation == -1)
1392                         strcpy(tmp, "max");
1393                 else
1394                         snprintf(tmp, sizeof(tmp), "%" PRId64, reservation);
1395 
1396                 cgroups_set("memory.low", tmp);
1397         }
1398 
1399         /* OCI 'swap' acounts for memory+swap */
1400         if (tb[OCI_LINUX_CGROUPS_MEMORY_SWAP]) {
1401                 if (swap == -1)
1402                         strcpy(tmp, "max");
1403                 else if (limit == -1)
1404                         snprintf(tmp, sizeof(tmp), "%" PRId64, swap);
1405                 else
1406                         snprintf(tmp, sizeof(tmp), "%" PRId64, swap - limit);
1407 
1408                 cgroups_set("memory.swap.max", tmp);
1409         }
1410 
1411         return 0;
1412 }
1413 
1414 
1415 enum {
1416         OCI_LINUX_CGROUPS_PIDS_LIMIT,
1417         __OCI_LINUX_CGROUPS_PIDS_MAX,
1418 };
1419 
1420 static const struct blobmsg_policy oci_linux_cgroups_pids_policy[] = {
1421         [OCI_LINUX_CGROUPS_PIDS_LIMIT] = { "limit", BLOBMSG_CAST_INT64 },
1422 };
1423 
1424 static int parseOCIlinuxcgroups_legacy_pids(struct blob_attr *msg)
1425 {
1426         struct blob_attr *tb[__OCI_LINUX_CGROUPS_MEMORY_MAX];
1427         char tmp[32] = { 0 };
1428         int64_t limit;
1429 
1430         blobmsg_parse(oci_linux_cgroups_pids_policy, __OCI_LINUX_CGROUPS_PIDS_MAX, tb, blobmsg_data(msg), blobmsg_len(msg));
1431 
1432         if (!tb[OCI_LINUX_CGROUPS_PIDS_LIMIT])
1433                 return 0;
1434 
1435         limit = blobmsg_cast_s64(tb[OCI_LINUX_CGROUPS_PIDS_LIMIT]);
1436         if (limit < 0)
1437                 strcpy(tmp, "max");
1438         else
1439                 snprintf(tmp, sizeof(tmp), "%" PRId64, limit);
1440 
1441         cgroups_set("pids.max", tmp);
1442 
1443         return 0;
1444 }
1445 
1446 enum {
1447         OCI_LINUX_CGROUPS_HUGEPAGELIMIT_PAGESIZE,
1448         OCI_LINUX_CGROUPS_HUGEPAGELIMIT_LIMIT,
1449         __OCI_LINUX_CGROUPS_HUGEPAGELIMIT_MAX,
1450 };
1451 
1452 static const struct blobmsg_policy oci_linux_cgroups_hugepagelimit_policy[] = {
1453         [OCI_LINUX_CGROUPS_HUGEPAGELIMIT_PAGESIZE] = { "pageSize", BLOBMSG_TYPE_STRING },
1454         [OCI_LINUX_CGROUPS_HUGEPAGELIMIT_LIMIT] = { "limit", BLOBMSG_CAST_INT64 },
1455 };
1456 
1457 static bool hugepage_size_valid(const char *size)
1458 {
1459         size_t len = strspn(size, "0123456789");
1460 
1461         if (!len)
1462                 return false;
1463 
1464         if (size[len] && strchr("KMG", size[len]))
1465                 len++;
1466 
1467         return !strcmp(&size[len], "B");
1468 }
1469 
1470 static int parseOCIlinuxcgroups_hugepagelimits(struct blob_attr *msg)
1471 {
1472         struct blob_attr *tb[__OCI_LINUX_CGROUPS_HUGEPAGELIMIT_MAX];
1473         char key[NAME_MAX + 1], val[32];
1474         struct blob_attr *cur;
1475         const char *size;
1476         int rem;
1477 
1478         blobmsg_for_each_attr(cur, msg, rem) {
1479                 if (blobmsg_type(cur) != BLOBMSG_TYPE_TABLE)
1480                         return EINVAL;
1481 
1482                 blobmsg_parse(oci_linux_cgroups_hugepagelimit_policy,
1483                               __OCI_LINUX_CGROUPS_HUGEPAGELIMIT_MAX, tb,
1484                               blobmsg_data(cur), blobmsg_len(cur));
1485 
1486                 if (!tb[OCI_LINUX_CGROUPS_HUGEPAGELIMIT_PAGESIZE] ||
1487                     !tb[OCI_LINUX_CGROUPS_HUGEPAGELIMIT_LIMIT])
1488                         return EINVAL;
1489 
1490                 size = blobmsg_get_string(tb[OCI_LINUX_CGROUPS_HUGEPAGELIMIT_PAGESIZE]);
1491                 if (!hugepage_size_valid(size))
1492                         return EINVAL;
1493 
1494                 snprintf(key, sizeof(key), "hugetlb.%s.max", size);
1495                 snprintf(val, sizeof(val), "%" PRIu64,
1496                          blobmsg_cast_u64(tb[OCI_LINUX_CGROUPS_HUGEPAGELIMIT_LIMIT]));
1497                 cgroups_set(key, val);
1498         }
1499 
1500         return 0;
1501 }
1502 
1503 static int parseOCIlinuxcgroups_unified(struct blob_attr *msg)
1504 {
1505         struct blob_attr *cur;
1506         int rem;
1507 
1508         blobmsg_for_each_attr(cur, msg, rem) {
1509                 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
1510                         return EINVAL;
1511 
1512                 /* restrict keys */
1513                 if (strchr(blobmsg_name(cur), '/') ||
1514                     !strcmp(blobmsg_name(cur), "cgroup.subtree_control") ||
1515                     !strcmp(blobmsg_name(cur), "cgroup.procs") ||
1516                     !strcmp(blobmsg_name(cur), "cgroup.threads") ||
1517                     !strcmp(blobmsg_name(cur), "cgroup.freeze"))
1518                         return EINVAL;
1519 
1520                 cgroups_set(blobmsg_name(cur), blobmsg_get_string(cur));
1521         }
1522 
1523         return 0;
1524 }
1525 
1526 enum {
1527         OCI_LINUX_CGROUPS_BLOCKIO,
1528         OCI_LINUX_CGROUPS_CPU,
1529         OCI_LINUX_CGROUPS_DEVICES,
1530         OCI_LINUX_CGROUPS_HUGEPAGELIMITS,
1531         OCI_LINUX_CGROUPS_INTELRDT,
1532         OCI_LINUX_CGROUPS_MEMORY,
1533         OCI_LINUX_CGROUPS_NETWORK,
1534         OCI_LINUX_CGROUPS_PIDS,
1535         OCI_LINUX_CGROUPS_RDMA,
1536         OCI_LINUX_CGROUPS_UNIFIED,
1537         __OCI_LINUX_CGROUPS_MAX,
1538 };
1539 
1540 static const struct blobmsg_policy oci_linux_cgroups_policy[] = {
1541         [OCI_LINUX_CGROUPS_BLOCKIO] = { "blockIO", BLOBMSG_TYPE_TABLE },
1542         [OCI_LINUX_CGROUPS_CPU] = { "cpu", BLOBMSG_TYPE_TABLE },
1543         [OCI_LINUX_CGROUPS_DEVICES] = { "devices", BLOBMSG_TYPE_ARRAY },
1544         [OCI_LINUX_CGROUPS_HUGEPAGELIMITS] = { "hugepageLimits", BLOBMSG_TYPE_ARRAY },
1545         [OCI_LINUX_CGROUPS_INTELRDT] = { "intelRdt", BLOBMSG_TYPE_TABLE },
1546         [OCI_LINUX_CGROUPS_MEMORY] = { "memory", BLOBMSG_TYPE_TABLE },
1547         [OCI_LINUX_CGROUPS_NETWORK] = { "network", BLOBMSG_TYPE_TABLE },
1548         [OCI_LINUX_CGROUPS_PIDS] = { "pids", BLOBMSG_TYPE_TABLE },
1549         [OCI_LINUX_CGROUPS_RDMA] = { "rdma", BLOBMSG_TYPE_TABLE },
1550         [OCI_LINUX_CGROUPS_UNIFIED] = { "unified", BLOBMSG_TYPE_TABLE },
1551 };
1552 
1553 int parseOCIlinuxcgroups(struct blob_attr *msg, bool is_update)
1554 {
1555         struct blob_attr *tb[__OCI_LINUX_CGROUPS_MAX];
1556         int ret;
1557 
1558         blobmsg_parse(oci_linux_cgroups_policy, __OCI_LINUX_CGROUPS_MAX, tb, blobmsg_data(msg), blobmsg_len(msg));
1559 
1560         if (tb[OCI_LINUX_CGROUPS_INTELRDT])
1561                 return jail_unsupported(tb[OCI_LINUX_CGROUPS_INTELRDT]);
1562 
1563         if (tb[OCI_LINUX_CGROUPS_NETWORK])
1564                 return jail_unsupported(tb[OCI_LINUX_CGROUPS_NETWORK]);
1565 
1566         if (tb[OCI_LINUX_CGROUPS_RDMA])
1567                 return jail_unsupported(tb[OCI_LINUX_CGROUPS_RDMA]);
1568 
1569         if (tb[OCI_LINUX_CGROUPS_BLOCKIO]) {
1570                 ret = parseOCIlinuxcgroups_legacy_blockio(tb[OCI_LINUX_CGROUPS_BLOCKIO]);
1571                 if (ret)
1572                         return ret;
1573         }
1574 
1575         if (tb[OCI_LINUX_CGROUPS_CPU]) {
1576                 ret = parseOCIlinuxcgroups_legacy_cpu(tb[OCI_LINUX_CGROUPS_CPU]);
1577                 if (ret)
1578                         return ret;
1579         }
1580 
1581         if (tb[OCI_LINUX_CGROUPS_DEVICES]) {
1582                 ret = parseOCIlinuxcgroups_devices(tb[OCI_LINUX_CGROUPS_DEVICES]);
1583                 if (ret)
1584                         return ret;
1585         }
1586 
1587         if (tb[OCI_LINUX_CGROUPS_HUGEPAGELIMITS]) {
1588                 ret = parseOCIlinuxcgroups_hugepagelimits(tb[OCI_LINUX_CGROUPS_HUGEPAGELIMITS]);
1589                 if (ret)
1590                         return ret;
1591         }
1592 
1593         if (tb[OCI_LINUX_CGROUPS_MEMORY]) {
1594                 ret = parseOCIlinuxcgroups_legacy_memory(tb[OCI_LINUX_CGROUPS_MEMORY], is_update);
1595                 if (ret)
1596                         return ret;
1597         }
1598 
1599         if (tb[OCI_LINUX_CGROUPS_PIDS]) {
1600                 ret = parseOCIlinuxcgroups_legacy_pids(tb[OCI_LINUX_CGROUPS_PIDS]);
1601                 if (ret)
1602                         return ret;
1603         }
1604 
1605         if (tb[OCI_LINUX_CGROUPS_UNIFIED]) {
1606                 ret = parseOCIlinuxcgroups_unified(tb[OCI_LINUX_CGROUPS_UNIFIED]);
1607                 if (ret)
1608                         return ret;
1609         }
1610 
1611         return 0;
1612 }
1613 

This page was automatically generated by LXR 0.3.1.  •  OpenWrt