Add support for kptrs in more BPF maps
Subsystems (4)
Kumar Kartikeya Dwivedi
maintainercover Add support for kptrs in more BPF maps
This set adds support for kptrs in percpu hashmaps, percpu LRU hashmaps, and local storage maps (covering sk, cgrp, task, inode). There are also minor miscellaneous cleanups rolled in, unrelated to the set, that I collected over time. Feel free to drop them, they have been intentionally placed after the kptr support to ease partial application of the series.
Tests are expanded to test more existing maps at runtime and also test the code path for the local storage maps (which is shared by all implementations).
A question for reviewers is what the position of the BPF runtime should be on dealing with reference cycles that can be created by BPF programs at runtime using this additional support. For instance, one can store the kptr of the task in its own task local storage, creating a cycle which prevents destruction of task local storage. Cycles can be formed using arbitrarily long kptr ownership chains. Therefore, just preventing storage of such kptrs in some maps is not a sufficient solution, and is more likely to hurt usability.
There is precedence in existing runtimes which promise memory safety, like Rust, where reference cycles and memory leaks are permitted. However, traditionally the safety guarantees of BPF have been stronger. Thus, more discussion and thought is invited on this topic to ensure we cover all usage aspects.
Kumar Kartikeya Dwivedi (7):
bpf: Support kptrs in percpu hashmap and percpu LRU hashmap
bpf: Support kptrs in local storage maps
bpf: Annotate data races in bpf_local_storage
bpf: Remove unused MEM_ALLOC | PTR_TRUSTED checks
bpf: Fix check_reg_type for PTR_TO_BTF_ID
bpf: Wrap register invalidation with a helper selftests/bpf: Add more tests for kptrs in maps
kernel/bpf/bpf_local_storage.c | 51 ++- kernel/bpf/hashtab.c | 59 ++-- kernel/bpf/syscall.c | 8 +- kernel/bpf/verifier.c | 65 ++-- .../selftests/bpf/prog_tests/map_kptr.c | 96 +++-- tools/testing/selftests/bpf/progs/map_kptr.c | 333 +++++++++++++++--- 6 files changed, 494 insertions(+), 118 deletions(-)
base-commit: 168de0233586fb06c5c5c56304aa9a928a09b0ba
Signature · 1 line
2.39.2
Kumar Kartikeya Dwivedi
maintainerPATCH 1/7 bpf: Support kptrs in percpu hashmap and percpu LRU hashmap
Enable support for kptrs in percpu BPF hashmap and percpu BPF LRU hashmap by wiring up the freeing of these kptrs from percpu map elements.
--- kernel/bpf/hashtab.c | 59 +++++++++++++++++++++++++++----------------- kernel/bpf/syscall.c | 2 ++ 2 files changed, 39 insertions(+), 22 deletions(-)
kernel/bpf/hashtab.c +37 -22
@@ -249,7 +249,18 @@ static void htab_free_prealloced_fields(struct bpf_htab *htab)struct htab_elem *elem; elem =get_htab_elem (htab, i); -bpf_obj_free_fields (htab->map.record, elem->key +round_up (htab->map.key_size,8 )); +if (htab_is_percpu (htab)) { +void __percpu *pptr =htab_elem_get_ptr (elem, htab->map.key_size); +int cpu; + +for_each_possible_cpu (cpu) { +bpf_obj_free_fields (htab->map.record,per_cpu_ptr (pptr, cpu)); +cond_resched (); + } + }else { +bpf_obj_free_fields (htab->map.record, elem->key +round_up (htab->map.key_size,8 )); +cond_resched (); + }cond_resched (); } }
@@ -759,9 +770,17 @@ static int htab_lru_map_gen_lookup(struct bpf_map *map,static void check_and_free_fields (struct bpf_htab *htab,struct htab_elem *elem) { -void *map_value = elem->key +round_up (htab->map.key_size,8 ); +if (htab_is_percpu (htab)) { +void __percpu *pptr =htab_elem_get_ptr (elem, htab->map.key_size); +int cpu; -bpf_obj_free_fields (htab->map.record, map_value); +for_each_possible_cpu (cpu) +bpf_obj_free_fields (htab->map.record,per_cpu_ptr (pptr, cpu)); + }else { +void *map_value = elem->key +round_up (htab->map.key_size,8 ); + +bpf_obj_free_fields (htab->map.record, map_value); + } }/* It is called from the bpf_lru_list when the LRU needs to delete
@@ -858,9 +877,9 @@ static int htab_map_get_next_key(struct bpf_map *map, void *key, void *next_key)static void htab_elem_free (struct bpf_htab *htab,struct htab_elem *l) { +check_and_free_fields (htab, l);if (htab->map.map_type ==BPF_MAP_TYPE_PERCPU_HASH )bpf_mem_cache_free (&htab->pcpu_ma, l->ptr_to_pptr); -check_and_free_fields (htab, l);bpf_mem_cache_free (&htab->ma, l); }
@@ -918,14 +937,13 @@ static void pcpu_copy_value(struct bpf_htab *htab, void __percpu *pptr, {if (!onallcpus) {/* copy true value_size bytes */ -memcpy (this_cpu_ptr (pptr), value, htab->map.value_size); +copy_map_value (&htab->map,this_cpu_ptr (pptr), value); }else {u32 size =round_up (htab->map.value_size,8 );int off =0 , cpu;for_each_possible_cpu (cpu) { -bpf_long_memcpy (per_cpu_ptr (pptr, cpu), - value + off, size); +copy_map_value_long (&htab->map,per_cpu_ptr (pptr, cpu), value + off); off += size; } }
@@ -940,16 +958,14 @@ static void pcpu_init_value(struct bpf_htab *htab, void __percpu *pptr,* (onallcpus=false always when coming from bpf prog). */ if (!onallcpus) { -u32 size =round_up (htab->map.value_size,8 );int current_cpu =raw_smp_processor_id ();int cpu;for_each_possible_cpu (cpu) {if (cpu == current_cpu) -bpf_long_memcpy (per_cpu_ptr (pptr, cpu), value, - size); -else -memset (per_cpu_ptr (pptr, cpu),0 , size); +copy_map_value_long (&htab->map,per_cpu_ptr (pptr, cpu), value); +else /* Since elem is preallocated, we cannot touch special fields */ +zero_map_value (&htab->map,per_cpu_ptr (pptr, cpu)); } }else {pcpu_copy_value (htab, pptr, value, onallcpus);
@@ -1575,9 +1591,8 @@ static int __htab_map_lookup_and_delete_elem(struct bpf_map *map, void *key, pptr =htab_elem_get_ptr (l, key_size);for_each_possible_cpu (cpu) { -bpf_long_memcpy (value + off, -per_cpu_ptr (pptr, cpu), - roundup_value_size); +copy_map_value_long (&htab->map, value + off,per_cpu_ptr (pptr, cpu)); +check_and_init_map_value (&htab->map, value + off); off += roundup_value_size; } }else {
@@ -1772,8 +1787,8 @@ __htab_map_lookup_and_delete_batch(struct bpf_map *map, pptr =htab_elem_get_ptr (l, map->key_size);for_each_possible_cpu (cpu) { -bpf_long_memcpy (dst_val + off, -per_cpu_ptr (pptr, cpu), size); +copy_map_value_long (&htab->map, dst_val + off,per_cpu_ptr (pptr, cpu)); +check_and_init_map_value (&htab->map, dst_val + off); off += size; } }else {
@@ -2046,9 +2061,9 @@ static int __bpf_hash_map_seq_show(struct seq_file *seq, struct htab_elem *elem) roundup_value_size =round_up (map->value_size,8 ); pptr =htab_elem_get_ptr (elem, map->key_size);for_each_possible_cpu (cpu) { -bpf_long_memcpy (info->percpu_value_buf + off, -per_cpu_ptr (pptr, cpu), - roundup_value_size); +copy_map_value_long (map, info->percpu_value_buf + off, +per_cpu_ptr (pptr, cpu)); +check_and_init_map_value (map, info->percpu_value_buf + off); off += roundup_value_size; } ctx.value = info->percpu_value_buf;
@@ -2292,8 +2307,8 @@ int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value)*/ pptr =htab_elem_get_ptr (l, map->key_size);for_each_possible_cpu (cpu) { -bpf_long_memcpy (value + off, -per_cpu_ptr (pptr, cpu), size); +copy_map_value_long (map, value + off,per_cpu_ptr (pptr, cpu)); +check_and_init_map_value (map, value + off); off += size; } ret =0 ;
kernel/bpf/syscall.c +2 -0
@@ -1059,7 +1059,9 @@ static int map_check_btf(struct bpf_map *map, const struct btf *btf,case BPF_KPTR_UNREF :case BPF_KPTR_REF :if (map->map_type !=BPF_MAP_TYPE_HASH && + map->map_type !=BPF_MAP_TYPE_PERCPU_HASH && map->map_type !=BPF_MAP_TYPE_LRU_HASH && + map->map_type !=BPF_MAP_TYPE_LRU_PERCPU_HASH && map->map_type !=BPF_MAP_TYPE_ARRAY && map->map_type !=BPF_MAP_TYPE_PERCPU_ARRAY ) { ret = -EOPNOTSUPP ;
Signature · 1 line
2.39.2
Kumar Kartikeya Dwivedi
maintainerPATCH 2/7 bpf: Support kptrs in local storage maps
In reply to Kumar Kartikeya DwivediEnable support for kptrs in local storage maps by wiring up the freeing of these kptrs from map value.
--- kernel/bpf/bpf_local_storage.c | 35 ++++++++++++++++++++++++++++++---- kernel/bpf/syscall.c | 6 +++++- kernel/bpf/verifier.c | 12 ++++++++---- 3 files changed, 44 insertions(+), 9 deletions(-)
kernel/bpf/bpf_local_storage.c +31 -4
@@ -75,6 +75,7 @@ bpf_selem_alloc(struct bpf_local_storage_map *smap, void *owner,if (selem) {if (value)copy_map_value (&smap->map,SDATA (selem)->data, value); +/* No need to call check_and_init_map_value as memory is zero init */ return selem; }
@@ -103,10 +104,17 @@ static void bpf_selem_free_rcu(struct rcu_head *rcu)struct bpf_local_storage_elem *selem; selem = container_of(rcu, struct bpf_local_storage_elem, rcu); +bpf_obj_free_fields (SDATA (selem)->smap->map.record,SDATA (selem)); +kfree (selem); +} + +static void bpf_selem_free_tasks_trace_rcu (struct rcu_head *rcu) +{ +/* Free directly if Tasks Trace RCU GP also implies RCU GP */ if (rcu_trace_implies_rcu_gp ()) -kfree (selem); +bpf_selem_free_rcu (rcu);else -kfree_rcu (selem, rcu); +call_rcu (rcu, bpf_selem_free_rcu); }/* local_storage->lock must be held and selem->local_storage == local_storage.
@@ -160,9 +168,9 @@ static bool bpf_selem_unlink_storage_nolock(struct bpf_local_storage *local_storRCU_INIT_POINTER (local_storage->cache[smap->cache_idx],NULL );if (use_trace_rcu) -call_rcu_tasks_trace (&selem->rcu, bpf_selem_free_rcu); +call_rcu_tasks_trace (&selem->rcu, bpf_selem_free_tasks_trace_rcu);else -kfree_rcu (selem, rcu); +call_rcu (&selem->rcu, bpf_selem_free_rcu);return free_local_storage; }
@@ -713,6 +721,25 @@ void bpf_local_storage_map_free(struct bpf_map *map,*/ synchronize_rcu (); +/* Only delay freeing of smap, buckets are not needed anymore */ kvfree (smap->buckets); + +/* When local storage has special fields, callbacks for +* bpf_selem_free_rcu and bpf_selem_free_tasks_trace_rcu will keep using +* the map BTF record, we need to execute an RCU barrier to wait for +* them as the record will be freed right after our map_free callback. +*/ +if (!IS_ERR_OR_NULL (smap->map.record)) { +rcu_barrier_tasks_trace (); +/* We cannot skip rcu_barrier() when rcu_trace_implies_rcu_gp() +* is true, because while call_rcu invocation is skipped in that +* case in bpf_selem_free_tasks_trace_rcu (and all local storage +* maps pass use_trace_rcu = true), there can be call_rcu +* callbacks based on use_trace_rcu = false in the earlier while +* ((selem = ...)) loop or from bpf_local_storage_unlink_nolock +* called from owner's free path. +*/ +rcu_barrier (); + }bpf_map_area_free (smap); }
kernel/bpf/syscall.c +5 -1
@@ -1063,7 +1063,11 @@ static int map_check_btf(struct bpf_map *map, const struct btf *btf, map->map_type !=BPF_MAP_TYPE_LRU_HASH && map->map_type !=BPF_MAP_TYPE_LRU_PERCPU_HASH && map->map_type !=BPF_MAP_TYPE_ARRAY && - map->map_type != BPF_MAP_TYPE_PERCPU_ARRAY) { + map->map_type !=BPF_MAP_TYPE_PERCPU_ARRAY && + map->map_type !=BPF_MAP_TYPE_SK_STORAGE && + map->map_type !=BPF_MAP_TYPE_INODE_STORAGE && + map->map_type !=BPF_MAP_TYPE_TASK_STORAGE && + map->map_type != BPF_MAP_TYPE_CGRP_STORAGE) { ret = -EOPNOTSUPP; goto free_map_tab; }
kernel/bpf/verifier.c +8 -4
@@ -7126,22 +7126,26 @@ static int check_map_func_compatibility(struct bpf_verifier_env *env,break ;case BPF_MAP_TYPE_SK_STORAGE :if (func_id != BPF_FUNC_sk_storage_get && - func_id != BPF_FUNC_sk_storage_delete) + func_id != BPF_FUNC_sk_storage_delete && + func_id != BPF_FUNC_kptr_xchg) goto error;break ;case BPF_MAP_TYPE_INODE_STORAGE :if (func_id != BPF_FUNC_inode_storage_get && - func_id != BPF_FUNC_inode_storage_delete) + func_id != BPF_FUNC_inode_storage_delete && + func_id != BPF_FUNC_kptr_xchg) goto error;break ;case BPF_MAP_TYPE_TASK_STORAGE :if (func_id != BPF_FUNC_task_storage_get && - func_id != BPF_FUNC_task_storage_delete) + func_id != BPF_FUNC_task_storage_delete && + func_id != BPF_FUNC_kptr_xchg) goto error;break ;case BPF_MAP_TYPE_CGRP_STORAGE :if (func_id != BPF_FUNC_cgrp_storage_get && - func_id != BPF_FUNC_cgrp_storage_delete) + func_id != BPF_FUNC_cgrp_storage_delete && + func_id != BPF_FUNC_kptr_xchg) goto error;break ;case BPF_MAP_TYPE_BLOOM_FILTER :
Signature · 1 line
2.39.2
kernel test robot
build botRe: [PATCH bpf-next v1 2/7] bpf: Support kptrs in local storage maps
Thank you for the patch! Perhaps something to improve:
Hi Kumar,
Thank you for the patch! Perhaps something to improve:
[auto build test WARNING on 168de0233586fb06c5c5c56304aa9a928a09b0ba]
url: https://github.com/intel-lab-lkp/linux/commits/Kumar-Kartikeya-Dwivedi/bpf-Support-kptrs-in-percpu-hashmap-and-percpu-LRU-hashmap/20230219-235406 base: 168de0233586fb06c5c5c56304aa9a928a09b0ba patch link: https://lore.kernel.org/r/20230219155249.1755998-3-memxor%40gmail.com patch subject: [PATCH bpf-next v1 2/7] bpf: Support kptrs in local storage maps config: nios2-randconfig-s043-20230219 (https://download.01.org/0day-ci/archive/20230220/[email protected]/config) compiler: nios2-linux-gcc (GCC) 12.1.0
reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# apt-get install sparse
# sparse version: v0.6.4-39-gce1a6720-dirty
# https://github.com/intel-lab-lkp/linux/commit/01f54156de471b78c6355e9aba9860afaf61cedb
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Kumar-Kartikeya-Dwivedi/bpf-Support-kptrs-in-percpu-hashmap-and-percpu-LRU-hashmap/20230219-235406 git checkout 01f54156de471b78c6355e9aba9860afaf61cedb
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' O=build_dir ARCH=nios2 olddefconfig COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' O=build_dir ARCH=nios2 SHELL=/bin/bash kernel/bpf/
If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>
| Link: https://lore.kernel.org/oe-kbuild-all/[email protected]/
sparse warnings: (new ones prefixed by >>)
>> kernel/bpf/bpf_local_storage.c:107:41: sparse: sparse: dereference of noderef expression
vim +107 kernel/bpf/bpf_local_storage.c
101
102 static void bpf_selem_free_rcu(struct rcu_head *rcu)
103 {
104 struct bpf_local_storage_elem *selem;
105
106 selem = container_of(rcu, struct bpf_local_storage_elem, rcu);
> 107 bpf_obj_free_fields(SDATA(selem)->smap->map.record, SDATA(selem));
108 kfree(selem);
109 }
110
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests
Kumar Kartikeya Dwivedi
maintainerPATCH 3/7 bpf: Annotate data races in bpf_local_storage
In reply to Kumar Kartikeya DwivediThere are a few cases where hlist_node is checked to be unhashed without holding the lock protecting its modification. In this case, one must use hlist_unhashed_lockless to avoid load tearing and KCSAN reports. Fix this by using lockless variant in places not protected by the lock.
Since this is not prompted by any actual KCSAN reports but only from code review, I have not included a fixes tag.
--- kernel/bpf/bpf_local_storage.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-)
kernel/bpf/bpf_local_storage.c +13 -3
@@ -51,11 +51,21 @@ owner_storage(struct bpf_local_storage_map *smap, void *owner)return map->ops->map_owner_storage_ptr (owner); } +static bool selem_linked_to_storage_lockless (const struct bpf_local_storage_elem *selem) +{ +return !hlist_unhashed_lockless (&selem->snode); +} +static bool selem_linked_to_storage (const struct bpf_local_storage_elem *selem) {return !hlist_unhashed (&selem->snode); } +static bool selem_linked_to_map_lockless (const struct bpf_local_storage_elem *selem) +{ +return !hlist_unhashed_lockless (&selem->map_node); +} +static bool selem_linked_to_map (const struct bpf_local_storage_elem *selem) {return !hlist_unhashed (&selem->map_node);
@@ -182,7 +192,7 @@ static void __bpf_selem_unlink_storage(struct bpf_local_storage_elem *selem,bool free_local_storage = false;unsigned long flags; -if (unlikely (!selem_linked_to_storage (selem))) +if (unlikely (!selem_linked_to_storage_lockless (selem)))/* selem has already been unlinked from sk */ return ;
@@ -216,7 +226,7 @@ void bpf_selem_unlink_map(struct bpf_local_storage_elem *selem)struct bpf_local_storage_map_bucket *b;unsigned long flags; -if (unlikely (!selem_linked_to_map (selem))) +if (unlikely (!selem_linked_to_map_lockless (selem)))/* selem has already be unlinked from smap */ return ;
@@ -428,7 +438,7 @@ bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap, err =check_flags (old_sdata, map_flags);if (err)return ERR_PTR (err); -if (old_sdata &&selem_linked_to_storage (SELEM (old_sdata))) { +if (old_sdata &&selem_linked_to_storage_lockless (SELEM (old_sdata))) {copy_map_value_locked (&smap->map, old_sdata->data, value, false);return old_sdata;
Signature · 1 line
2.39.2
Kumar Kartikeya Dwivedi
maintainerPATCH 4/7 bpf: Remove unused MEM_ALLOC | PTR_TRUSTED checks
In reply to Kumar Kartikeya DwivediThe plan is to supposedly tag everything with PTR_TRUSTED eventually, however those changes should bring in their respective code, instead of leaving it around right now. It is arguable whether PTR_TRUSTED is required for all types, when it's only use case is making PTR_TO_BTF_ID a bit stronger, while all other types are trusted by default.
Hence, just drop the two instances which do not occur in the verifier for now to avoid reader confusion.
--- kernel/bpf/verifier.c | 2 -- 1 file changed, 2 deletions(-)
kernel/bpf/verifier.c +0 -2
@@ -6651,7 +6651,6 @@ int check_func_arg_reg_off(struct bpf_verifier_env *env,case PTR_TO_BTF_ID |MEM_ALLOC :case PTR_TO_BTF_ID |PTR_TRUSTED :case PTR_TO_BTF_ID |MEM_RCU : -case PTR_TO_BTF_ID |MEM_ALLOC |PTR_TRUSTED :case PTR_TO_BTF_ID |MEM_ALLOC |NON_OWN_REF :/* When referenced PTR_TO_BTF_ID is passed to release function, * its fixed offset must be 0. In the other cases, fixed offset
@@ -9210,7 +9209,6 @@ static int check_reg_allocation_locked(struct bpf_verifier_env *env, struct bpf_ ptr = reg->map_ptr;break ;case PTR_TO_BTF_ID |MEM_ALLOC : -case PTR_TO_BTF_ID |MEM_ALLOC |PTR_TRUSTED : ptr = reg->btf;break ;default :
Signature · 1 line
2.39.2
Kumar Kartikeya Dwivedi
maintainerPATCH 5/7 bpf: Fix check_reg_type for PTR_TO_BTF_ID
In reply to Kumar Kartikeya DwivediThe current code does type matching for the case where reg->type is PTR_TO_BTF_ID or has the PTR_TRUSTED flag. However, this only needs to occur for non-MEM_ALLOC and non-MEM_PERCPU cases, but will include both as per the current code.
The MEM_ALLOC case with or without PTR_TRUSTED needs to be handled specially by the code for type_is_alloc case, while MEM_PERCPU case must be ignored. Hence, to restore correct behavior and for clarity, explicitly list out the handled PTR_TO_BTF_ID types which should be handled for each case using a switch statement.
Helpers currently only take:
PTR_TO_BTF_ID
PTR_TO_BTF_ID | PTR_TRUSTED
PTR_TO_BTF_ID | MEM_RCU
PTR_TO_BTF_ID | MEM_ALLOC
PTR_TO_BTF_ID | MEM_PERCPU
PTR_TO_BTF_ID | MEM_PERCPU | PTR_TRUSTED
This fix was also described (for the MEM_ALLOC case) in [0].
[0]: https://lore.kernel.org/bpf/20221121160657.h6z7xuvedybp5y7s@apollo
--- kernel/bpf/verifier.c | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-)
kernel/bpf/verifier.c +20 -3
@@ -6522,7 +6522,14 @@ static int check_reg_type(struct bpf_verifier_env *env, u32 regno,return -EACCES ; found: -if (reg->type ==PTR_TO_BTF_ID || reg->type &PTR_TRUSTED ) { +if (base_type (reg->type) !=PTR_TO_BTF_ID ) +return 0 ; + +switch ((int )reg->type) { +case PTR_TO_BTF_ID : +case PTR_TO_BTF_ID |PTR_TRUSTED : +case PTR_TO_BTF_ID |MEM_RCU : + {/* For bpf_sk_release, it needs to match against first member * 'struct sock_common', hence make an exception for it. This * allows bpf_sk_release to work for multiple socket types.
@@ -6558,13 +6565,23 @@ static int check_reg_type(struct bpf_verifier_env *env, u32 regno,return -EACCES ; } } - }else if (type_is_alloc (reg->type)) { +break ; + } +case PTR_TO_BTF_ID |MEM_ALLOC :if (meta->func_id != BPF_FUNC_spin_lock && meta->func_id != BPF_FUNC_spin_unlock) {verbose (env,"verifier internal error: unimplemented handling of MEM_ALLOC\n" );return -EFAULT ; } +/* Handled by helper specific checks */ +break ; +case PTR_TO_BTF_ID |MEM_PERCPU : +case PTR_TO_BTF_ID |MEM_PERCPU |PTR_TRUSTED : +/* Handled by helper specific checks */ +break ; +default : +verbose (env,"verifier internal error: invalid PTR_TO_BTF_ID register for type match\n" ); +return -EFAULT ; } -return 0 ; }
Signature · 1 line
2.39.2
Kumar Kartikeya Dwivedi
maintainerPATCH 6/7 bpf: Wrap register invalidation with a helper
In reply to Kumar Kartikeya DwivediTypically, verifier should use env->allow_ptr_leaks when invaliding registers for users that don't have CAP_PERFMON or CAP_SYS_ADMIN to avoid leaking the pointer value. This is similar in spirit to c67cae551f0d ("bpf: Tighten ptr_to_btf_id checks."). In a lot of the existing checks, we know the capabilities are present, hence we don't do the check.
Instead of being inconsistent in the application of the check, wrap the action of invalidating a register into a helper named 'mark_invalid_reg' and use it in a uniform fashion to replace open coded invalidation operations, so that the check is always made regardless of the call site and we don't have to remember whether it needs to be done or not for each case.
--- kernel/bpf/verifier.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-)
kernel/bpf/verifier.c +14 -14
@@ -895,6 +895,14 @@ static int unmark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_restatic void __mark_reg_unknown (const struct bpf_verifier_env *env,struct bpf_reg_state *reg); +static void mark_reg_invalid (const struct bpf_verifier_env *env,struct bpf_reg_state *reg) +{ +if (!env->allow_ptr_leaks) +__mark_reg_not_init (env, reg); +else +__mark_reg_unknown (env, reg); +} +static int destroy_if_dynptr_stack_slot (struct bpf_verifier_env *env,struct bpf_func_state *state,int spi) {
@@ -934,12 +942,8 @@ static int destroy_if_dynptr_stack_slot(struct bpf_verifier_env *env,/* Dynptr slices are only PTR_TO_MEM_OR_NULL and PTR_TO_MEM */ if (dreg->type != (PTR_TO_MEM |PTR_MAYBE_NULL ) && dreg->type !=PTR_TO_MEM )continue ; -if (dreg->dynptr_id == dynptr_id) { -if (!env->allow_ptr_leaks) -__mark_reg_not_init (env, dreg); -else -__mark_reg_unknown (env, dreg); - } +if (dreg->dynptr_id == dynptr_id) +mark_reg_invalid (env, dreg); }));/* Do not release reference state, we are destroying dynptr on stack,
@@ -7383,7 +7387,7 @@ static void clear_all_pkt_pointers(struct bpf_verifier_env *env)bpf_for_each_reg_in_vstate (env->cur_state, state, reg, ({if (reg_is_pkt_pointer_any (reg)) -__mark_reg_unknown (env, reg); +mark_reg_invalid (env, reg); })); }
@@ -7428,12 +7432,8 @@ static int release_reference(struct bpf_verifier_env *env,return err;bpf_for_each_reg_in_vstate (env->cur_state, state, reg, ({ -if (reg->ref_obj_id == ref_obj_id) { -if (!env->allow_ptr_leaks) -__mark_reg_not_init (env, reg); -else -__mark_reg_unknown (env, reg); - } +if (reg->ref_obj_id == ref_obj_id) +mark_reg_invalid (env, reg); }));return 0 ;
@@ -7446,7 +7446,7 @@ static void invalidate_non_owning_refs(struct bpf_verifier_env *env)bpf_for_each_reg_in_vstate (env->cur_state, unused, reg, ({if (type_is_non_owning_ref (reg->type)) -__mark_reg_unknown (env, reg); +mark_reg_invalid (env, reg); })); }
Signature · 1 line
2.39.2
Kumar Kartikeya Dwivedi
maintainerPATCH 7/7 selftests/bpf: Add more tests for kptrs in maps
In reply to Kumar Kartikeya DwivediFirstly, ensure programs successfully load when using all of the supported maps. Then, extend existing tests to test more cases at runtime. We are currently testing both the synchronous freeing of items and asynchronous destruction when map is freed, but the code needs to be adjusted a bit to be able to also accomodate support for percpu maps.
We now do a delete on the item (and update for array maps which has a similar effect for kptrs) to perform a synchronous free of the kptr, and test destruction both for the synchronous and asynchronous deletion. Next time the program runs, it should observe the refcount as 1 since all existing references should have been released by then. By running the program after both possible paths freeing kptrs, we establish that they correctly release resources. Next, we augment the existing test to also test the same code path shared by all local storage maps using a task local storage map.
--- .../selftests/bpf/prog_tests/map_kptr.c | 96 +++-- tools/testing/selftests/bpf/progs/map_kptr.c | 333 +++++++++++++++--- 2 files changed, 364 insertions(+), 65 deletions(-)
tools/testing/selftests/bpf/prog_tests/map_kptr.c +73 -23
@@ -7,67 +7,117 @@static void test_map_kptr_success (bool test_run) { +LIBBPF_OPTS (bpf_test_run_opts, lopts);LIBBPF_OPTS (bpf_test_run_opts, opts, .data_in = &pkt_v4, .data_size_in = sizeof(pkt_v4), .repeat = 1, ); +int key =0 , ret, cpu;struct map_kptr *skel; -int key =0 , ret; -char buf[16 ]; +char buf[16 ], *pbuf; skel =map_kptr__open_and_load ();if (!ASSERT_OK_PTR (skel,"map_kptr__open_and_load" ))return ; - ret =bpf_prog_test_run_opts (bpf_program__fd (skel->progs.test_map_kptr_ref), &opts); -ASSERT_OK (ret,"test_map_kptr_ref refcount" ); -ASSERT_OK (opts.retval,"test_map_kptr_ref retval" ); + ret =bpf_prog_test_run_opts (bpf_program__fd (skel->progs.test_map_kptr_ref1), &opts); +ASSERT_OK (ret,"test_map_kptr_ref1 refcount" ); +ASSERT_OK (opts.retval,"test_map_kptr_ref1 retval" ); ret =bpf_prog_test_run_opts (bpf_program__fd (skel->progs.test_map_kptr_ref2), &opts);ASSERT_OK (ret,"test_map_kptr_ref2 refcount" );ASSERT_OK (opts.retval,"test_map_kptr_ref2 retval" ); + ret =bpf_prog_test_run_opts (bpf_program__fd (skel->progs.test_ls_map_kptr_ref1), &lopts); +ASSERT_OK (ret,"test_ls_map_kptr_ref1 refcount" ); +ASSERT_OK (opts.retval,"test_ls_map_kptr_ref1 retval" ); + ret =bpf_prog_test_run_opts (bpf_program__fd (skel->progs.test_ls_map_kptr_ref2), &lopts); +ASSERT_OK (ret,"test_ls_map_kptr_ref2 refcount" ); +ASSERT_OK (opts.retval,"test_ls_map_kptr_ref2 retval" ); +if (test_run) goto exit; + cpu =libbpf_num_possible_cpus (); +if (!ASSERT_GT (cpu,0 ,"libbpf_num_possible_cpus" )) + goto exit; + + pbuf =calloc (cpu,sizeof (buf)); +if (!ASSERT_OK_PTR (pbuf,"calloc(pbuf)" )) + goto exit; + ret =bpf_map__update_elem (skel->maps.array_map, &key,sizeof (key), buf,sizeof (buf),0 );ASSERT_OK (ret,"array_map update" ); - ret =bpf_map__update_elem (skel->maps.array_map, - &key,sizeof (key), buf,sizeof (buf),0 ); -ASSERT_OK (ret,"array_map update2" ); + ret =bpf_prog_test_run_opts (bpf_program__fd (skel->progs.test_map_kptr_ref3), &opts); +ASSERT_OK (ret,"test_map_kptr_ref3 refcount" ); +ASSERT_OK (opts.retval,"test_map_kptr_ref3 retval" ); + + ret =bpf_map__update_elem (skel->maps.pcpu_array_map, + &key,sizeof (key), pbuf, cpu *sizeof (buf),0 ); +ASSERT_OK (ret,"pcpu_array_map update" ); + ret =bpf_prog_test_run_opts (bpf_program__fd (skel->progs.test_map_kptr_ref3), &opts); +ASSERT_OK (ret,"test_map_kptr_ref3 refcount" ); +ASSERT_OK (opts.retval,"test_map_kptr_ref3 retval" ); - ret =bpf_map__update_elem (skel->maps.hash_map, - &key,sizeof (key), buf,sizeof (buf),0 ); -ASSERT_OK (ret,"hash_map update" ); ret =bpf_map__delete_elem (skel->maps.hash_map, &key,sizeof (key),0 );ASSERT_OK (ret,"hash_map delete" ); + ret =bpf_prog_test_run_opts (bpf_program__fd (skel->progs.test_map_kptr_ref3), &opts); +ASSERT_OK (ret,"test_map_kptr_ref3 refcount" ); +ASSERT_OK (opts.retval,"test_map_kptr_ref3 retval" ); + + ret =bpf_map__delete_elem (skel->maps.pcpu_hash_map, &key,sizeof (key),0 ); +ASSERT_OK (ret,"pcpu_hash_map delete" ); + ret =bpf_prog_test_run_opts (bpf_program__fd (skel->progs.test_map_kptr_ref3), &opts); +ASSERT_OK (ret,"test_map_kptr_ref3 refcount" ); +ASSERT_OK (opts.retval,"test_map_kptr_ref3 retval" ); - ret =bpf_map__update_elem (skel->maps.hash_malloc_map, - &key,sizeof (key), buf,sizeof (buf),0 ); -ASSERT_OK (ret,"hash_malloc_map update" ); ret =bpf_map__delete_elem (skel->maps.hash_malloc_map, &key,sizeof (key),0 );ASSERT_OK (ret,"hash_malloc_map delete" ); + ret =bpf_prog_test_run_opts (bpf_program__fd (skel->progs.test_map_kptr_ref3), &opts); +ASSERT_OK (ret,"test_map_kptr_ref3 refcount" ); +ASSERT_OK (opts.retval,"test_map_kptr_ref3 retval" ); + + ret =bpf_map__delete_elem (skel->maps.pcpu_hash_malloc_map, &key,sizeof (key),0 ); +ASSERT_OK (ret,"pcpu_hash_malloc_map delete" ); + ret =bpf_prog_test_run_opts (bpf_program__fd (skel->progs.test_map_kptr_ref3), &opts); +ASSERT_OK (ret,"test_map_kptr_ref3 refcount" ); +ASSERT_OK (opts.retval,"test_map_kptr_ref3 retval" ); - ret =bpf_map__update_elem (skel->maps.lru_hash_map, - &key,sizeof (key), buf,sizeof (buf),0 ); -ASSERT_OK (ret,"lru_hash_map update" ); ret =bpf_map__delete_elem (skel->maps.lru_hash_map, &key,sizeof (key),0 );ASSERT_OK (ret,"lru_hash_map delete" ); + ret =bpf_prog_test_run_opts (bpf_program__fd (skel->progs.test_map_kptr_ref3), &opts); +ASSERT_OK (ret,"test_map_kptr_ref3 refcount" ); +ASSERT_OK (opts.retval,"test_map_kptr_ref3 retval" ); + + ret =bpf_map__delete_elem (skel->maps.lru_pcpu_hash_map, &key,sizeof (key),0 ); +ASSERT_OK (ret,"lru_pcpu_hash_map delete" ); + ret =bpf_prog_test_run_opts (bpf_program__fd (skel->progs.test_map_kptr_ref3), &opts); +ASSERT_OK (ret,"test_map_kptr_ref3 refcount" ); +ASSERT_OK (opts.retval,"test_map_kptr_ref3 retval" ); + ret =bpf_prog_test_run_opts (bpf_program__fd (skel->progs.test_ls_map_kptr_ref_del), &lopts); +ASSERT_OK (ret,"test_ls_map_kptr_ref_del delete" ); +ASSERT_OK (opts.retval,"test_ls_map_kptr_ref_del retval" ); + +free (pbuf); exit:map_kptr__destroy (skel); }void test_map_kptr (void ) { -if (test__start_subtest ("success" )) { +RUN_TESTS (map_kptr_fail); + +if (test__start_subtest ("success-map" )) { +test_map_kptr_success (true); + +ASSERT_OK (kern_sync_rcu (),"sync rcu" ); +/* Observe refcount dropping to 1 on bpf_map_free_deferred */ test_map_kptr_success (false); -/* Do test_run twice, so that we see refcount going back to 1 -* after we leave it in map from first iteration. -*/ + +ASSERT_OK (kern_sync_rcu (),"sync rcu" ); +/* Observe refcount dropping to 1 on synchronous delete elem */ test_map_kptr_success (true); } - -RUN_TESTS (map_kptr_fail); }
tools/testing/selftests/bpf/progs/map_kptr.c +291 -42
425 lines not on this page · raw message
Signature · 1 line
2.39.2
Kumar Kartikeya Dwivedi
maintainerQuoted 9 lines · Kumar Kartikeya Dwivedi
This set adds support for kptrs in percpu hashmaps, percpu LRU hashmaps, and local storage maps (covering sk, cgrp, task, inode). There are also minor miscellaneous cleanups rolled in, unrelated to the set, that I collected over time. Feel free to drop them, they have been intentionally placed after the kptr support to ease partial application of the series.
[...]
I've noticed the error in the CI, I'm trying to reproduce and fix it.