bpf: htab fixes
Subsystems (4)
cover bpf: htab fixes
Two bpf hashtable fixes. See individual patches for details.
Alexei Starovoitov (2):
bpf: fix struct htab_elem layout
bpf: convert htab map to hlist_nulls
include/linux/list_nulls.h | 5 ++ include/linux/rculist_nulls.h | 14 +++++ kernel/bpf/hashtab.c | 119 ++++++++++++++++++++++++++++-------------- 3 files changed, 99 insertions(+), 39 deletions(-)
Signature · 1 line
2.8.0
PATCH 1/2 bpf: fix struct htab_elem layout
when htab_elem is removed from the bucket list the htab_elem.hash_node.next field should not be overridden too early otherwise we have a tiny race window between lookup and delete.
The bug was discovered by manual code analysis and reproducible only with explicit udelay() in lookup_elem_raw().
--- kernel/bpf/hashtab.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-)
kernel/bpf/hashtab.c +20 -5
@@ -45,8 +45,13 @@ enum extra_elem_state {struct htab_elem {union {struct hlist_node hash_node; -struct bpf_htab *htab; -struct pcpu_freelist_node fnode; +struct { +void *padding; +union { +struct bpf_htab *htab; +struct pcpu_freelist_node fnode; + }; + }; };union {struct rcu_head rcu;
@@ -162,7 +167,8 @@ static int prealloc_init(struct bpf_htab *htab) offsetof(struct htab_elem , lru_node), htab->elem_size, htab->map.max_entries); else - pcpu_freelist_populate(&htab->freelist, htab->elems, + pcpu_freelist_populate(&htab->freelist, + htab->elems + offsetof(struct htab_elem , fnode), htab->elem_size, htab->map.max_entries);return 0 ;
@@ -217,6 +223,11 @@ static struct bpf_map *htab_map_alloc(union bpf_attr *attr)int err, i;u64 cost; +BUILD_BUG_ON (offsetof(struct htab_elem , htab) != + offsetof(struct htab_elem, hash_node.pprev)); + BUILD_BUG_ON(offsetof(struct htab_elem, fnode.next) != + offsetof(struct htab_elem, hash_node.pprev)); +if (lru && !capable (CAP_SYS_ADMIN ))/* LRU implementation is much complicated than other * maps. Hence, limit to CAP_SYS_ADMIN for now.
@@ -582,9 +593,13 @@ static struct htab_elem *alloc_htab_elem(struct bpf_htab *htab, void *key,int err =0 ;if (prealloc) { - l_new = (struct htab_elem *)pcpu_freelist_pop (&htab->freelist); -if (!l_new) +struct pcpu_freelist_node *l; + + l =pcpu_freelist_pop (&htab->freelist); +if (!l) err = -E2BIG ; +else + l_new = container_of(l, struct htab_elem, fnode); }else {if (atomic_inc_return (&htab->count) > htab->map.max_entries) {atomic_dec (&htab->count);
Signature · 1 line
2.8.0
PATCH 2/2 bpf: convert htab map to hlist_nulls
In reply to Alexei Starovoitovwhen all map elements are pre-allocated one cpu can delete and reuse htab_elem while another cpu is still walking the hlist. In such case the lookup may miss the element. Convert hlist to hlist_nulls to avoid such scenario. When bucket lock is taken there is no need to take such precautions, so only convert map_lookup and map_get_next to nulls.
The race window is extremely small and only reproducible with explicit udelay() inside lookup_nulls_elem_raw()
Similar to hlist add hlist_nulls_for_each_entry_safe() and hlist_nulls_entry_safe() helpers.
--- include/linux/list_nulls.h | 5 +++ include/linux/rculist_nulls.h | 14 +++++++ kernel/bpf/hashtab.c | 94 +++++++++++++++++++++++++++---------------- 3 files changed, 79 insertions(+), 34 deletions(-)
include/linux/list_nulls.h +5 -0
@@ -29,6 +29,11 @@ struct hlist_nulls_node { ((ptr)->first = (struct hlist_nulls_node *) NULLS_MARKER(nulls))#define hlist_nulls_entry (ptr, type, member) container_of(ptr,type,member) + +#define hlist_nulls_entry_safe (ptr, type, member) \ + ({ typeof(ptr) ____ptr = (ptr); \ + !is_a_nulls(____ptr) ? hlist_nulls_entry(____ptr, type, member) : NULL; \ + })/** * ptr_is_a_nulls - Test if a ptr is a nulls * @ptr: ptr to be tested
include/linux/rculist_nulls.h +14 -0
@@ -156,5 +156,19 @@ static inline void hlist_nulls_add_tail_rcu(struct hlist_nulls_node *n, ({ tpos =hlist_nulls_entry (pos,typeof (*tpos), member);1 ; }); \ pos = rcu_dereference_raw(hlist_nulls_next_rcu(pos))) +/** +* hlist_nulls_for_each_entry_safe - +* iterate over list of given type safe against removal of list entry +* @tpos: the type * to use as a loop cursor. +* @pos: the &struct hlist_nulls_node to use as a loop cursor. +* @head: the head for your list. +* @member: the name of the hlist_nulls_node within the struct. +*/ +#define hlist_nulls_for_each_entry_safe (tpos, pos, head, member) \ + for (({barrier();}), \ + pos = rcu_dereference_raw(hlist_nulls_first_rcu(head)); \ + (!is_a_nulls(pos)) && \ + ({ tpos = hlist_nulls_entry(pos, typeof(*tpos), member); \ + pos = rcu_dereference_raw(hlist_nulls_next_rcu(pos)); 1; });)#endif #endif
kernel/bpf/hashtab.c +60 -34
266 lines not on this page · raw message
Signature · 1 line
2.8.0
David Miller
maintainerFrom: Alexei Starovoitov <[email protected]>
Date: Tue, 7 Mar 2017 20:00:11 -0800
Quoted 1 line
Two bpf hashtable fixes. See individual patches for details.
Series applied, thanks Alexei.