map_cgroup_storage.rst 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. .. SPDX-License-Identifier: GPL-2.0-only
  2. .. Copyright (C) 2020 Google LLC.
  3. ===========================
  4. BPF_MAP_TYPE_CGROUP_STORAGE
  5. ===========================
  6. The ``BPF_MAP_TYPE_CGROUP_STORAGE`` map type represents a local fix-sized
  7. storage. It is only available with ``CONFIG_CGROUP_BPF``, and to programs that
  8. attach to cgroups; the programs are made available by the same Kconfig. The
  9. storage is identified by the cgroup the program is attached to.
  10. The map provide a local storage at the cgroup that the BPF program is attached
  11. to. It provides a faster and simpler access than the general purpose hash
  12. table, which performs a hash table lookups, and requires user to track live
  13. cgroups on their own.
  14. This document describes the usage and semantics of the
  15. ``BPF_MAP_TYPE_CGROUP_STORAGE`` map type. Some of its behaviors was changed in
  16. Linux 5.9 and this document will describe the differences.
  17. Usage
  18. =====
  19. The map uses key of type of either ``__u64 cgroup_inode_id`` or
  20. ``struct bpf_cgroup_storage_key``, declared in ``linux/bpf.h``::
  21. struct bpf_cgroup_storage_key {
  22. __u64 cgroup_inode_id;
  23. __u32 attach_type;
  24. };
  25. ``cgroup_inode_id`` is the inode id of the cgroup directory.
  26. ``attach_type`` is the program's attach type.
  27. Linux 5.9 added support for type ``__u64 cgroup_inode_id`` as the key type.
  28. When this key type is used, then all attach types of the particular cgroup and
  29. map will share the same storage. Otherwise, if the type is
  30. ``struct bpf_cgroup_storage_key``, then programs of different attach types
  31. be isolated and see different storages.
  32. To access the storage in a program, use ``bpf_get_local_storage``::
  33. void *bpf_get_local_storage(void *map, u64 flags)
  34. ``flags`` is reserved for future use and must be 0.
  35. There is no implicit synchronization. Storages of ``BPF_MAP_TYPE_CGROUP_STORAGE``
  36. can be accessed by multiple programs across different CPUs, and user should
  37. take care of synchronization by themselves. The bpf infrastructure provides
  38. ``struct bpf_spin_lock`` to synchronize the storage. See
  39. ``tools/testing/selftests/bpf/progs/test_spin_lock.c``.
  40. Examples
  41. ========
  42. Usage with key type as ``struct bpf_cgroup_storage_key``::
  43. #include <bpf/bpf.h>
  44. struct {
  45. __uint(type, BPF_MAP_TYPE_CGROUP_STORAGE);
  46. __type(key, struct bpf_cgroup_storage_key);
  47. __type(value, __u32);
  48. } cgroup_storage SEC(".maps");
  49. int program(struct __sk_buff *skb)
  50. {
  51. __u32 *ptr = bpf_get_local_storage(&cgroup_storage, 0);
  52. __sync_fetch_and_add(ptr, 1);
  53. return 0;
  54. }
  55. Userspace accessing map declared above::
  56. #include <linux/bpf.h>
  57. #include <linux/libbpf.h>
  58. __u32 map_lookup(struct bpf_map *map, __u64 cgrp, enum bpf_attach_type type)
  59. {
  60. struct bpf_cgroup_storage_key = {
  61. .cgroup_inode_id = cgrp,
  62. .attach_type = type,
  63. };
  64. __u32 value;
  65. bpf_map_lookup_elem(bpf_map__fd(map), &key, &value);
  66. // error checking omitted
  67. return value;
  68. }
  69. Alternatively, using just ``__u64 cgroup_inode_id`` as key type::
  70. #include <bpf/bpf.h>
  71. struct {
  72. __uint(type, BPF_MAP_TYPE_CGROUP_STORAGE);
  73. __type(key, __u64);
  74. __type(value, __u32);
  75. } cgroup_storage SEC(".maps");
  76. int program(struct __sk_buff *skb)
  77. {
  78. __u32 *ptr = bpf_get_local_storage(&cgroup_storage, 0);
  79. __sync_fetch_and_add(ptr, 1);
  80. return 0;
  81. }
  82. And userspace::
  83. #include <linux/bpf.h>
  84. #include <linux/libbpf.h>
  85. __u32 map_lookup(struct bpf_map *map, __u64 cgrp, enum bpf_attach_type type)
  86. {
  87. __u32 value;
  88. bpf_map_lookup_elem(bpf_map__fd(map), &cgrp, &value);
  89. // error checking omitted
  90. return value;
  91. }
  92. Semantics
  93. =========
  94. ``BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE`` is a variant of this map type. This
  95. per-CPU variant will have different memory regions for each CPU for each
  96. storage. The non-per-CPU will have the same memory region for each storage.
  97. Prior to Linux 5.9, the lifetime of a storage is precisely per-attachment, and
  98. for a single ``CGROUP_STORAGE`` map, there can be at most one program loaded
  99. that uses the map. A program may be attached to multiple cgroups or have
  100. multiple attach types, and each attach creates a fresh zeroed storage. The
  101. storage is freed upon detach.
  102. There is a one-to-one association between the map of each type (per-CPU and
  103. non-per-CPU) and the BPF program during load verification time. As a result,
  104. each map can only be used by one BPF program and each BPF program can only use
  105. one storage map of each type. Because of map can only be used by one BPF
  106. program, sharing of this cgroup's storage with other BPF programs were
  107. impossible.
  108. Since Linux 5.9, storage can be shared by multiple programs. When a program is
  109. attached to a cgroup, the kernel would create a new storage only if the map
  110. does not already contain an entry for the cgroup and attach type pair, or else
  111. the old storage is reused for the new attachment. If the map is attach type
  112. shared, then attach type is simply ignored during comparison. Storage is freed
  113. only when either the map or the cgroup attached to is being freed. Detaching
  114. will not directly free the storage, but it may cause the reference to the map
  115. to reach zero and indirectly freeing all storage in the map.
  116. The map is not associated with any BPF program, thus making sharing possible.
  117. However, the BPF program can still only associate with one map of each type
  118. (per-CPU and non-per-CPU). A BPF program cannot use more than one
  119. ``BPF_MAP_TYPE_CGROUP_STORAGE`` or more than one
  120. ``BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE``.
  121. In all versions, userspace may use the attach parameters of cgroup and
  122. attach type pair in ``struct bpf_cgroup_storage_key`` as the key to the BPF map
  123. APIs to read or update the storage for a given attachment. For Linux 5.9
  124. attach type shared storages, only the first value in the struct, cgroup inode
  125. id, is used during comparison, so userspace may just specify a ``__u64``
  126. directly.
  127. The storage is bound at attach time. Even if the program is attached to parent
  128. and triggers in child, the storage still belongs to the parent.
  129. Userspace cannot create a new entry in the map or delete an existing entry.
  130. Program test runs always use a temporary storage.