Xsphinx.addnodesdocument)}( rawsourcechildren]( translations LanguagesNode)}(hhh](h pending_xref)}(hhh]docutils.nodesTextChinese (Simplified)}parenthsba attributes}(ids]classes]names]dupnames]backrefs] refdomainstdreftypedoc reftarget*/translations/zh_CN/bpf/map_cgroup_storagemodnameN classnameN refexplicitutagnamehhh ubh)}(hhh]hChinese (Traditional)}hh2sbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget*/translations/zh_TW/bpf/map_cgroup_storagemodnameN classnameN refexplicituh1hhh ubh)}(hhh]hItalian}hhFsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget*/translations/it_IT/bpf/map_cgroup_storagemodnameN classnameN refexplicituh1hhh ubh)}(hhh]hJapanese}hhZsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget*/translations/ja_JP/bpf/map_cgroup_storagemodnameN classnameN refexplicituh1hhh ubh)}(hhh]hKorean}hhnsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget*/translations/ko_KR/bpf/map_cgroup_storagemodnameN classnameN refexplicituh1hhh ubh)}(hhh]hSpanish}hhsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget*/translations/sp_SP/bpf/map_cgroup_storagemodnameN classnameN refexplicituh1hhh ubeh}(h]h ]h"]h$]h&]current_languageEnglishuh1h hh _documenthsourceNlineNubhcomment)}(h%SPDX-License-Identifier: GPL-2.0-onlyh]h%SPDX-License-Identifier: GPL-2.0-only}hhsbah}(h]h ]h"]h$]h&] xml:spacepreserveuh1hhhhhhD/var/lib/git/docbuild/linux/Documentation/bpf/map_cgroup_storage.rsthKubh)}(hCopyright (C) 2020 Google LLC.h]hCopyright (C) 2020 Google LLC.}hhsbah}(h]h ]h"]h$]h&]hhuh1hhhhhhhhKubhsection)}(hhh](htitle)}(hBPF_MAP_TYPE_CGROUP_STORAGEh]hBPF_MAP_TYPE_CGROUP_STORAGE}(hhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhhhhhKubh paragraph)}(hX$The ``BPF_MAP_TYPE_CGROUP_STORAGE`` map type represents a local fix-sized storage. It is only available with ``CONFIG_CGROUP_BPF``, and to programs that attach to cgroups; the programs are made available by the same Kconfig. The storage is identified by the cgroup the program is attached to.h](hThe }(hhhhhNhNubhliteral)}(h``BPF_MAP_TYPE_CGROUP_STORAGE``h]hBPF_MAP_TYPE_CGROUP_STORAGE}(hhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhubhJ map type represents a local fix-sized storage. It is only available with }(hhhhhNhNubh)}(h``CONFIG_CGROUP_BPF``h]hCONFIG_CGROUP_BPF}(hhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhubh, and to programs that attach to cgroups; the programs are made available by the same Kconfig. The storage is identified by the cgroup the program is attached to.}(hhhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhhhhubh)}(hThe map provide a local storage at the cgroup that the BPF program is attached to. It provides a faster and simpler access than the general purpose hash table, which performs a hash table lookups, and requires user to track live cgroups on their own.h]hThe map provide a local storage at the cgroup that the BPF program is attached to. It provides a faster and simpler access than the general purpose hash table, which performs a hash table lookups, and requires user to track live cgroups on their own.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK hhhhubh)}(hThis document describes the usage and semantics of the ``BPF_MAP_TYPE_CGROUP_STORAGE`` map type. Some of its behaviors was changed in Linux 5.9 and this document will describe the differences.h](h7This document describes the usage and semantics of the }(hjhhhNhNubh)}(h``BPF_MAP_TYPE_CGROUP_STORAGE``h]hBPF_MAP_TYPE_CGROUP_STORAGE}(hj#hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubhj map type. Some of its behaviors was changed in Linux 5.9 and this document will describe the differences.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhhhhubh)}(hhh](h)}(hUsageh]hUsage}(hj>hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj;hhhhhKubh)}(hThe map uses key of type of either ``__u64 cgroup_inode_id`` or ``struct bpf_cgroup_storage_key``, declared in ``linux/bpf.h``::h](h#The map uses key of type of either }(hjLhhhNhNubh)}(h``__u64 cgroup_inode_id``h]h__u64 cgroup_inode_id}(hjThhhNhNubah}(h]h ]h"]h$]h&]uh1hhjLubh or }(hjLhhhNhNubh)}(h!``struct bpf_cgroup_storage_key``h]hstruct bpf_cgroup_storage_key}(hjfhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjLubh, declared in }(hjLhhhNhNubh)}(h``linux/bpf.h``h]h linux/bpf.h}(hjxhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjLubh:}(hjLhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhj;hhubh literal_block)}(h\struct bpf_cgroup_storage_key { __u64 cgroup_inode_id; __u32 attach_type; };h]h\struct bpf_cgroup_storage_key { __u64 cgroup_inode_id; __u32 attach_type; };}hjsbah}(h]h ]h"]h$]h&]hhuh1jhhhKhj;hhubh)}(hj``cgroup_inode_id`` is the inode id of the cgroup directory. ``attach_type`` is the program's attach type.h](h)}(h``cgroup_inode_id``h]hcgroup_inode_id}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh* is the inode id of the cgroup directory. }(hjhhhNhNubh)}(h``attach_type``h]h attach_type}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh is the program’s attach type.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhK!hj;hhubh)}(hXHLinux 5.9 added support for type ``__u64 cgroup_inode_id`` as the key type. When this key type is used, then all attach types of the particular cgroup and map will share the same storage. Otherwise, if the type is ``struct bpf_cgroup_storage_key``, then programs of different attach types be isolated and see different storages.h](h!Linux 5.9 added support for type }(hjhhhNhNubh)}(h``__u64 cgroup_inode_id``h]h__u64 cgroup_inode_id}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh as the key type. When this key type is used, then all attach types of the particular cgroup and map will share the same storage. Otherwise, if the type is }(hjhhhNhNubh)}(h!``struct bpf_cgroup_storage_key``h]hstruct bpf_cgroup_storage_key}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubhQ, then programs of different attach types be isolated and see different storages.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhK$hj;hhubh)}(hCTo access the storage in a program, use ``bpf_get_local_storage``::h](h(To access the storage in a program, use }(hjhhhNhNubh)}(h``bpf_get_local_storage``h]hbpf_get_local_storage}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh:}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhK*hj;hhubj)}(h1void *bpf_get_local_storage(void *map, u64 flags)h]h1void *bpf_get_local_storage(void *map, u64 flags)}hj sbah}(h]h ]h"]h$]h&]hhuh1jhhhK,hj;hhubh)}(h3``flags`` is reserved for future use and must be 0.h](h)}(h ``flags``h]hflags}(hj2hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj.ubh* is reserved for future use and must be 0.}(hj.hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhK.hj;hhubh)}(hXZThere is no implicit synchronization. Storages of ``BPF_MAP_TYPE_CGROUP_STORAGE`` can be accessed by multiple programs across different CPUs, and user should take care of synchronization by themselves. The bpf infrastructure provides ``struct bpf_spin_lock`` to synchronize the storage. See ``tools/testing/selftests/bpf/progs/test_spin_lock.c``.h](h2There is no implicit synchronization. Storages of }(hjJhhhNhNubh)}(h``BPF_MAP_TYPE_CGROUP_STORAGE``h]hBPF_MAP_TYPE_CGROUP_STORAGE}(hjRhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjJubh can be accessed by multiple programs across different CPUs, and user should take care of synchronization by themselves. The bpf infrastructure provides }(hjJhhhNhNubh)}(h``struct bpf_spin_lock``h]hstruct bpf_spin_lock}(hjdhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjJubh! to synchronize the storage. See }(hjJhhhNhNubh)}(h6``tools/testing/selftests/bpf/progs/test_spin_lock.c``h]h2tools/testing/selftests/bpf/progs/test_spin_lock.c}(hjvhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjJubh.}(hjJhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhK0hj;hhubeh}(h]usageah ]h"]usageah$]h&]uh1hhhhhhhhKubh)}(hhh](h)}(hExamplesh]hExamples}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhK7ubh)}(h:Usage with key type as ``struct bpf_cgroup_storage_key``::h](hUsage with key type as }(hjhhhNhNubh)}(h!``struct bpf_cgroup_storage_key``h]hstruct bpf_cgroup_storage_key}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh:}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhK9hjhhubj)}(hXc#include struct { __uint(type, BPF_MAP_TYPE_CGROUP_STORAGE); __type(key, struct bpf_cgroup_storage_key); __type(value, __u32); } cgroup_storage SEC(".maps"); int program(struct __sk_buff *skb) { __u32 *ptr = bpf_get_local_storage(&cgroup_storage, 0); __sync_fetch_and_add(ptr, 1); return 0; }h]hXc#include struct { __uint(type, BPF_MAP_TYPE_CGROUP_STORAGE); __type(key, struct bpf_cgroup_storage_key); __type(value, __u32); } cgroup_storage SEC(".maps"); int program(struct __sk_buff *skb) { __u32 *ptr = bpf_get_local_storage(&cgroup_storage, 0); __sync_fetch_and_add(ptr, 1); return 0; }}hjsbah}(h]h ]h"]h$]h&]hhuh1jhhhK;hjhhubh)}(h(Userspace accessing map declared above::h]h'Userspace accessing map declared above:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKKhjhhubj)}(hX#include #include __u32 map_lookup(struct bpf_map *map, __u64 cgrp, enum bpf_attach_type type) { struct bpf_cgroup_storage_key = { .cgroup_inode_id = cgrp, .attach_type = type, }; __u32 value; bpf_map_lookup_elem(bpf_map__fd(map), &key, &value); // error checking omitted return value; }h]hX#include #include __u32 map_lookup(struct bpf_map *map, __u64 cgrp, enum bpf_attach_type type) { struct bpf_cgroup_storage_key = { .cgroup_inode_id = cgrp, .attach_type = type, }; __u32 value; bpf_map_lookup_elem(bpf_map__fd(map), &key, &value); // error checking omitted return value; }}hjsbah}(h]h ]h"]h$]h&]hhuh1jhhhKMhjhhubh)}(hAAlternatively, using just ``__u64 cgroup_inode_id`` as key type::h](hAlternatively, using just }(hjhhhNhNubh)}(h``__u64 cgroup_inode_id``h]h__u64 cgroup_inode_id}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh as key type:}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhK\hjhhubj)}(hXK#include struct { __uint(type, BPF_MAP_TYPE_CGROUP_STORAGE); __type(key, __u64); __type(value, __u32); } cgroup_storage SEC(".maps"); int program(struct __sk_buff *skb) { __u32 *ptr = bpf_get_local_storage(&cgroup_storage, 0); __sync_fetch_and_add(ptr, 1); return 0; }h]hXK#include struct { __uint(type, BPF_MAP_TYPE_CGROUP_STORAGE); __type(key, __u64); __type(value, __u32); } cgroup_storage SEC(".maps"); int program(struct __sk_buff *skb) { __u32 *ptr = bpf_get_local_storage(&cgroup_storage, 0); __sync_fetch_and_add(ptr, 1); return 0; }}hjsbah}(h]h ]h"]h$]h&]hhuh1jhhhK^hjhhubh)}(hAnd userspace::h]hAnd userspace:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKnhjhhubj)}(hX #include #include __u32 map_lookup(struct bpf_map *map, __u64 cgrp, enum bpf_attach_type type) { __u32 value; bpf_map_lookup_elem(bpf_map__fd(map), &cgrp, &value); // error checking omitted return value; }h]hX #include #include __u32 map_lookup(struct bpf_map *map, __u64 cgrp, enum bpf_attach_type type) { __u32 value; bpf_map_lookup_elem(bpf_map__fd(map), &cgrp, &value); // error checking omitted return value; }}hj-sbah}(h]h ]h"]h$]h&]hhuh1jhhhKphjhhubeh}(h]examplesah ]h"]examplesah$]h&]uh1hhhhhhhhK7ubh)}(hhh](h)}(h Semanticsh]h Semantics}(hjFhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjChhhhhK|ubh)}(h``BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE`` is a variant of this map type. This per-CPU variant will have different memory regions for each CPU for each storage. The non-per-CPU will have the same memory region for each storage.h](h)}(h&``BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE``h]h"BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE}(hjXhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjTubh is a variant of this map type. This per-CPU variant will have different memory regions for each CPU for each storage. The non-per-CPU will have the same memory region for each storage.}(hjThhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhK~hjChhubh)}(hXMPrior to Linux 5.9, the lifetime of a storage is precisely per-attachment, and for a single ``CGROUP_STORAGE`` map, there can be at most one program loaded that uses the map. A program may be attached to multiple cgroups or have multiple attach types, and each attach creates a fresh zeroed storage. The storage is freed upon detach.h](h\Prior to Linux 5.9, the lifetime of a storage is precisely per-attachment, and for a single }(hjphhhNhNubh)}(h``CGROUP_STORAGE``h]hCGROUP_STORAGE}(hjxhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjpubh map, there can be at most one program loaded that uses the map. A program may be attached to multiple cgroups or have multiple attach types, and each attach creates a fresh zeroed storage. The storage is freed upon detach.}(hjphhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjChhubh)}(hXThere is a one-to-one association between the map of each type (per-CPU and non-per-CPU) and the BPF program during load verification time. As a result, each map can only be used by one BPF program and each BPF program can only use one storage map of each type. Because of map can only be used by one BPF program, sharing of this cgroup's storage with other BPF programs were impossible.h]hXThere is a one-to-one association between the map of each type (per-CPU and non-per-CPU) and the BPF program during load verification time. As a result, each map can only be used by one BPF program and each BPF program can only use one storage map of each type. Because of map can only be used by one BPF program, sharing of this cgroup’s storage with other BPF programs were impossible.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjChhubh)}(hX\Since Linux 5.9, storage can be shared by multiple programs. When a program is attached to a cgroup, the kernel would create a new storage only if the map does not already contain an entry for the cgroup and attach type pair, or else the old storage is reused for the new attachment. If the map is attach type shared, then attach type is simply ignored during comparison. Storage is freed only when either the map or the cgroup attached to is being freed. Detaching will not directly free the storage, but it may cause the reference to the map to reach zero and indirectly freeing all storage in the map.h]hX\Since Linux 5.9, storage can be shared by multiple programs. When a program is attached to a cgroup, the kernel would create a new storage only if the map does not already contain an entry for the cgroup and attach type pair, or else the old storage is reused for the new attachment. If the map is attach type shared, then attach type is simply ignored during comparison. Storage is freed only when either the map or the cgroup attached to is being freed. Detaching will not directly free the storage, but it may cause the reference to the map to reach zero and indirectly freeing all storage in the map.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjChhubh)}(hX4The map is not associated with any BPF program, thus making sharing possible. However, the BPF program can still only associate with one map of each type (per-CPU and non-per-CPU). A BPF program cannot use more than one ``BPF_MAP_TYPE_CGROUP_STORAGE`` or more than one ``BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE``.h](hThe map is not associated with any BPF program, thus making sharing possible. However, the BPF program can still only associate with one map of each type (per-CPU and non-per-CPU). A BPF program cannot use more than one }(hjhhhNhNubh)}(h``BPF_MAP_TYPE_CGROUP_STORAGE``h]hBPF_MAP_TYPE_CGROUP_STORAGE}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh or more than one }(hjhhhNhNubh)}(h&``BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE``h]h"BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjChhubh)}(hXIn all versions, userspace may use the attach parameters of cgroup and attach type pair in ``struct bpf_cgroup_storage_key`` as the key to the BPF map APIs to read or update the storage for a given attachment. For Linux 5.9 attach type shared storages, only the first value in the struct, cgroup inode id, is used during comparison, so userspace may just specify a ``__u64`` directly.h](h[In all versions, userspace may use the attach parameters of cgroup and attach type pair in }(hjhhhNhNubh)}(h!``struct bpf_cgroup_storage_key``h]hstruct bpf_cgroup_storage_key}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh as the key to the BPF map APIs to read or update the storage for a given attachment. For Linux 5.9 attach type shared storages, only the first value in the struct, cgroup inode id, is used during comparison, so userspace may just specify a }(hjhhhNhNubh)}(h ``__u64``h]h__u64}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh directly.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjChhubh)}(hThe storage is bound at attach time. Even if the program is attached to parent and triggers in child, the storage still belongs to the parent.h]hThe storage is bound at attach time. Even if the program is attached to parent and triggers in child, the storage still belongs to the parent.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjChhubh)}(h}Userspace cannot create a new entry in the map or delete an existing entry. Program test runs always use a temporary storage.h]h}Userspace cannot create a new entry in the map or delete an existing entry. Program test runs always use a temporary storage.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjChhubeh}(h] semanticsah ]h"] semanticsah$]h&]uh1hhhhhhhhK|ubeh}(h]bpf-map-type-cgroup-storageah ]h"]bpf_map_type_cgroup_storageah$]h&]uh1hhhhhhhhKubeh}(h]h ]h"]h$]h&]sourcehuh1hcurrent_sourceN current_lineNsettingsdocutils.frontendValues)}(hN generatorN datestampN source_linkN source_urlN toc_backlinksentryfootnote_backlinksK sectnum_xformKstrip_commentsNstrip_elements_with_classesN strip_classesN report_levelK halt_levelKexit_status_levelKdebugNwarning_streamN tracebackinput_encoding utf-8-siginput_encoding_error_handlerstrictoutput_encodingutf-8output_encoding_error_handlerj_error_encodingutf-8error_encoding_error_handlerbackslashreplace language_codeenrecord_dependenciesNconfigN id_prefixhauto_id_prefixid dump_settingsNdump_internalsNdump_transformsNdump_pseudo_xmlNexpose_internalsNstrict_visitorN_disable_configN_sourceh _destinationN _config_files]7/var/lib/git/docbuild/linux/Documentation/docutils.confafile_insertion_enabled raw_enabledKline_length_limitM'pep_referencesN pep_base_urlhttps://peps.python.org/pep_file_url_templatepep-%04drfc_referencesN rfc_base_url&https://datatracker.ietf.org/doc/html/ tab_widthKtrim_footnote_reference_spacesyntax_highlightlong smart_quotessmartquotes_locales]character_level_inline_markupdoctitle_xform docinfo_xformKsectsubtitle_xform image_loadinglinkembed_stylesheetcloak_email_addressessection_self_linkenvNubreporterNindirect_targets]substitution_defs}substitution_names}refnames}refids}nameids}(j9j6jjj@j=j1j.u nametypes}(j9jj@j1uh}(j6hjj;j=jj.jCu footnote_refs} citation_refs} autofootnotes]autofootnote_refs]symbol_footnotes]symbol_footnote_refs] footnotes] citations]autofootnote_startKsymbol_footnote_startK id_counter collectionsCounter}Rparse_messages]transform_messages] transformerN include_log] decorationNhhub.