From nobody Fri Oct 2 14:03:10 2026 Received: from mail-pf1-f197.google.com (mail-pf1-f197.google.com [209.85.210.197]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CB6942D3EEB for ; Fri, 31 Jul 2026 01:06:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.197 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785460016; cv=none; b=o0MPB5iBNrZf/kDTQzEf03allodcoHlkNXR2VSsgryqba07qvfW2WFD2XAfvit689gOOhFL17VS+Qh7ATtR5EG9He6J5t52zodSqpi6Z6P5czNBWebBNn5PuatSesJnZoetOlLfbDeroDPmespcWCkJJKa6iEP4tC41WEHUgxMY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785460016; c=relaxed/simple; bh=HJDzbKF1xScl5EoUttcXAecGvzdp5/UvxskzNngb7a4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=A6DUSx8PKTDJt/ge7w4YG0FwHeaCWDj6lreC/8nSWq5pLQnPt1V/H1DDNui/3OydE7msBswO1fjLnbeH9FU2TOAvmMWbPLA5h/pIFP9D4ZYboijnDBAs0GE3RgqtJmkyHf+mYln7uNdExsO/+SdHhF6p93zeXUJ0eoWlKosVZAI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--tjmercier.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=IDoY9lEG; arc=none smtp.client-ip=209.85.210.197 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--tjmercier.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="IDoY9lEG" Received: by mail-pf1-f197.google.com with SMTP id d2e1a72fcca58-848662cd2a1so534256b3a.2 for ; Thu, 30 Jul 2026 18:06:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1785460011; x=1786064811; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=/3n8gU6ZQW8nwanNSLCgv01T6pMs3+Rusc6QA/JE/ps=; b=IDoY9lEGdxkh5Nx82wekSci2qMD4LPfyPd9h2XYG56J+wgYTxVMy6A92tx3bCjtAGr gSRqJ6qxj85GLg5WOEhHLAMw2H3yUnPYqnOGjXa21q6mmam5gFVCB/vdc9ICQjONkQnh D/hfsLutQ8pM7Vt/IymtYiN2wfmo9i6g8gFWfa512dWz91eqEWosuwxgw3SIpcL7u7iN +oA12N0lll5PQYaZd7SupogohR6mSzyD7GHaCigDLZIB+pffyKLlJ6jlUqD3neTpuojm 2S7Vz4Q+bNE7Ne9vPsEZOj7cynbY1D1wCfnh7eJe7vwe0uGxSDDVqvpKPF0lYzHC8OvI Kr6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785460011; x=1786064811; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=/3n8gU6ZQW8nwanNSLCgv01T6pMs3+Rusc6QA/JE/ps=; b=ZSnNdFrB5rWs4d7S39pJG5UC01L6tXDJj0xs1YSEW5afJQ7O8S28ijOqlDWSIHkF4K aM/kq8wRcDY88JKQfrr9WDT7966biOB+/DwAAs17HrSMvnVlhNb/Im2rATLEdmTDmCbs t9J2o6hCw0Xhbj6z6mzTgG6uumw31USFO2z6n/vGoCmmuVIp77mODkvRH9O4VHDIrJDa 7bU4tzpZObvbmLDQjBTugfrHecss6IrPx1o6u1sPMTjOVS7ukZ2ndcANfIfBz6VYqWIC 7J15M3hcXoz8REPbY0qRgqEOliVRnGMg7dFTFoIOXqVUJBr6fVwnNyGOwzfqy61n6101 HJhA== X-Forwarded-Encrypted: i=1; AHgh+Ro470p/ZM//FrZtQpb8NYcptGEB5lQLr6lzmmrvK1F7qXgpA6EY/bSGtws5x4yyVCDwcHiJdW6a8ZtYzjE=@vger.kernel.org X-Gm-Message-State: AOJu0YzvPmsM/d1A1DfiQ198cBIXqfRYao2tFzMULpTKWoKBuD/XqR1W 77bvZS2akt/luZpvzt/GXeGLO4zxYaPFFRLF9tCC24XR8irC301fDR6sRzKAeYF2EztrHZ7twe0 g6L1dtuQO2U4W+uirZQ== X-Received: from pfav5.prod.google.com ([2002:a05:6a00:ab05:b0:848:4809:51c6]) (user=tjmercier job=prod-delivery.src-stubby-dispatcher) by 2002:aa7:88c7:0:b0:848:760e:eb76 with SMTP id d2e1a72fcca58-84ebc432c34mr4037710b3a.61.1785460010358; Thu, 30 Jul 2026 18:06:50 -0700 (PDT) Date: Thu, 30 Jul 2026 18:06:43 -0700 In-Reply-To: <20260731010645.3965783-1-tjmercier@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260731010645.3965783-1-tjmercier@google.com> X-Mailer: git-send-email 2.55.0.508.g3f0d502094-goog Message-ID: <20260731010645.3965783-2-tjmercier@google.com> Subject: [PATCH bpf-next v2 1/2] bpf: htab: Split htab_elem_lru and htab_elem_pcpu off of htab_elem From: "T.J. Mercier" To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, eddyz87@gmail.com, memxor@gmail.com, martin.lau@linux.dev, song@kernel.org, yonghong.song@linux.dev, jolsa@kernel.org, emil@etsalapatis.com, mykyta.yatsenko5@gmail.com Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, "T.J. Mercier" Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The htab_elem struct is used as the per-element type for all BPF hash map types and includes bpf_lru_node in a union with a ptr_to_pptr pointer. For standard (non-LRU, non-PCPU) hash maps, the 24 byte union allocated for every element is entirely unused. For non-preallocated PCPU maps, ptr_to_pptr only requires 8 bytes, leaving 16 bytes of unused overhead in the union. For preallocated PCPU maps ptr_to_pptr is unused since elements are freed to the PCPU freelist. Eliminate this per-element memory overhead by splitting htab_elem into dedicated structures for each map type: - struct htab_elem: Minimal structure for standard hash maps and preallocated PCPU maps (saves 24 bytes per element). - struct htab_elem_pcpu: Structure for non-preallocated PCPU maps containing ptr_to_pptr (saves 16 bytes per element). - struct htab_elem_lru: Retains struct bpf_lru_node for LRU maps. Because element sizes now vary by map type, add key_offset to struct bpf_htab to track the dynamic key offset. Update helper accessors and lookups to compute key and value offsets using htab->key_offset. Pointers to struct htab_elem in the existing code (e.g. htab_elem_hash) serve as generic base element pointers. This is possible because htab_elem, htab_elem_pcpu, and htab_elem_lru share a common initial sequence, making pointer casts safe. Signed-off-by: T.J. Mercier --- kernel/bpf/hashtab.c | 362 +++++++++++++++++++++++++--------------- kernel/bpf/map_in_map.c | 13 ++ kernel/bpf/map_in_map.h | 2 + 3 files changed, 242 insertions(+), 135 deletions(-) diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c index 9f394e1aa2e8..f54366da459f 100644 --- a/kernel/bpf/hashtab.c +++ b/kernel/bpf/hashtab.c @@ -102,11 +102,13 @@ struct bpf_htab { bool use_percpu_counter; u32 n_buckets; /* number of hash buckets */ u32 elem_size; /* size of each element in bytes */ + u32 key_offset; /* offset of key in bytes */ u32 hashrnd; }; =20 /* each htab element is struct htab_elem + key + value */ -struct htab_elem { +struct htab_elem; +struct htab_node { union { struct hlist_nulls_node hash_node; struct { @@ -117,11 +119,27 @@ struct htab_elem { }; }; }; - union { - /* pointer to per-cpu pointer */ - void *ptr_to_pptr; - struct bpf_lru_node lru_node; - }; +}; + +struct htab_elem { + struct htab_node node; + u32 hash; + char key[] __aligned(8); +}; + +struct htab_elem_lru { + struct htab_node node; + struct bpf_lru_node lru_node; + u32 hash; + char key[] __aligned(8); +}; + +/* Only for non-preallocated PCPU maps. Preallocated PCPU maps don't need + * ptr_to_pptr, and use htab_elem. + */ +struct htab_elem_pcpu { + struct htab_node node; + void *ptr_to_pptr; u32 hash; char key[] __aligned(8); }; @@ -136,6 +154,21 @@ static inline bool htab_is_prealloc(const struct bpf_h= tab *htab) return !(htab->map.map_flags & BPF_F_NO_PREALLOC); } =20 +static inline struct bpf_lru_node *htab_elem_lru_node(struct htab_elem *l) +{ + return &((struct htab_elem_lru *)l)->lru_node; +} + +static inline void *htab_elem_get_ptr_to_pptr(struct htab_elem *l) +{ + return ((struct htab_elem_pcpu *)l)->ptr_to_pptr; +} + +static inline void htab_elem_set_ptr_to_pptr(struct htab_elem *l, void *pt= r) +{ + ((struct htab_elem_pcpu *)l)->ptr_to_pptr =3D ptr; +} + static void htab_init_buckets(struct bpf_htab *htab) { unsigned int i; @@ -183,25 +216,30 @@ static inline bool is_fd_htab(const struct bpf_htab *= htab) return htab->map.map_type =3D=3D BPF_MAP_TYPE_HASH_OF_MAPS; } =20 -static inline void *htab_elem_value(struct htab_elem *l, u32 key_size) +static inline void *htab_elem_key(struct bpf_htab *htab, struct htab_elem = *l) +{ + return (void *)l + htab->key_offset; +} + +static inline void *htab_elem_value(struct bpf_htab *htab, struct htab_ele= m *l) { - return l->key + round_up(key_size, 8); + return htab_elem_key(htab, l) + round_up(htab->map.key_size, 8); } =20 -static inline void htab_elem_set_ptr(struct htab_elem *l, u32 key_size, +static inline void htab_elem_set_ptr(struct bpf_htab *htab, struct htab_el= em *l, void __percpu *pptr) { - *(void __percpu **)htab_elem_value(l, key_size) =3D pptr; + *(void __percpu **)htab_elem_value(htab, l) =3D pptr; } =20 -static inline void __percpu *htab_elem_get_ptr(struct htab_elem *l, u32 ke= y_size) +static inline void __percpu *htab_elem_get_ptr(struct bpf_htab *htab, stru= ct htab_elem *l) { - return *(void __percpu **)htab_elem_value(l, key_size); + return *(void __percpu **)htab_elem_value(htab, l); } =20 -static void *fd_htab_map_get_ptr(const struct bpf_map *map, struct htab_el= em *l) +static void *fd_htab_map_get_ptr(struct bpf_htab *htab, struct htab_elem *= l) { - return *(void **)htab_elem_value(l, map->key_size); + return *(void **)htab_elem_value(htab, l); } =20 static struct htab_elem *get_htab_elem(struct bpf_htab *htab, int i) @@ -209,6 +247,26 @@ static struct htab_elem *get_htab_elem(struct bpf_htab= *htab, int i) return (struct htab_elem *) (htab->elems + i * (u64)htab->elem_size); } =20 +static inline u32 htab_elem_hash(struct bpf_htab *htab, struct htab_elem *= l) +{ + if (htab_is_lru(htab)) + return ((struct htab_elem_lru *)l)->hash; + else if (htab_is_percpu(htab) && !htab_is_prealloc(htab)) + return ((struct htab_elem_pcpu *)l)->hash; + else + return l->hash; +} + +static inline void htab_elem_set_hash(struct bpf_htab *htab, struct htab_e= lem *l, u32 hash) +{ + if (htab_is_lru(htab)) + ((struct htab_elem_lru *)l)->hash =3D hash; + else if (htab_is_percpu(htab) && !htab_is_prealloc(htab)) + ((struct htab_elem_pcpu *)l)->hash =3D hash; + else + l->hash =3D hash; +} + /* Both percpu and fd htab support in-place update, so no need for * extra elem. LRU itself can remove the least used element, so * there is no need for an extra elem during map_update. @@ -231,7 +289,7 @@ static void htab_free_prealloced_internal_structs(struc= t bpf_htab *htab) =20 elem =3D get_htab_elem(htab, i); bpf_map_free_internal_structs(&htab->map, - htab_elem_value(elem, htab->map.key_size)); + htab_elem_value(htab, elem)); cond_resched(); } } @@ -254,7 +312,7 @@ static void htab_free_prealloced_fields(struct bpf_htab= *htab) =20 elem =3D get_htab_elem(htab, i); if (htab_is_percpu(htab)) { - void __percpu *pptr =3D htab_elem_get_ptr(elem, htab->map.key_size); + void __percpu *pptr =3D htab_elem_get_ptr(htab, elem); int cpu; =20 for_each_possible_cpu(cpu) { @@ -263,7 +321,7 @@ static void htab_free_prealloced_fields(struct bpf_htab= *htab) } } else { bpf_obj_free_fields(htab->map.record, - htab_elem_value(elem, htab->map.key_size)); + htab_elem_value(htab, elem)); cond_resched(); } cond_resched(); @@ -280,8 +338,7 @@ static void htab_free_elems(struct bpf_htab *htab) for (i =3D 0; i < htab->map.max_entries; i++) { void __percpu *pptr; =20 - pptr =3D htab_elem_get_ptr(get_htab_elem(htab, i), - htab->map.key_size); + pptr =3D htab_elem_get_ptr(htab, get_htab_elem(htab, i)); free_percpu(pptr); cond_resched(); } @@ -308,8 +365,8 @@ static struct htab_elem *prealloc_lru_pop(struct bpf_ht= ab *htab, void *key, =20 if (node) { bpf_map_inc_elem_count(&htab->map); - l =3D container_of(node, struct htab_elem, lru_node); - memcpy(l->key, key, htab->map.key_size); + l =3D (struct htab_elem *)container_of(node, struct htab_elem_lru, lru_n= ode); + memcpy(htab_elem_key(htab, l), key, htab->map.key_size); return l; } =20 @@ -340,8 +397,7 @@ static int prealloc_init(struct bpf_htab *htab) GFP_USER | __GFP_NOWARN); if (!pptr) goto free_elems; - htab_elem_set_ptr(get_htab_elem(htab, i), htab->map.key_size, - pptr); + htab_elem_set_ptr(htab, get_htab_elem(htab, i), pptr); cond_resched(); } =20 @@ -349,8 +405,8 @@ static int prealloc_init(struct bpf_htab *htab) if (htab_is_lru(htab)) err =3D bpf_lru_init(&htab->lru, htab->map.map_flags & BPF_F_NO_COMMON_LRU, - offsetof(struct htab_elem, hash) - - offsetof(struct htab_elem, lru_node), + offsetof(struct htab_elem_lru, hash) - + offsetof(struct htab_elem_lru, lru_node), htab_lru_map_delete_node, htab); else @@ -361,11 +417,11 @@ static int prealloc_init(struct bpf_htab *htab) =20 if (htab_is_lru(htab)) bpf_lru_populate(&htab->lru, htab->elems, - offsetof(struct htab_elem, lru_node), + offsetof(struct htab_elem_lru, lru_node), htab->elem_size, num_entries); else pcpu_freelist_populate(&htab->freelist, - htab->elems + offsetof(struct htab_elem, fnode), + htab->elems + offsetof(struct htab_elem, node.fnode), htab->elem_size, num_entries); =20 return 0; @@ -401,7 +457,7 @@ static int alloc_extra_elems(struct bpf_htab *htab) /* pop will succeed, since prealloc_init() * preallocated extra num_possible_cpus elements */ - l_new =3D container_of(l, struct htab_elem, fnode); + l_new =3D container_of(l, struct htab_elem, node.fnode); *per_cpu_ptr(pptr, cpu) =3D l_new; } htab->extra_elems =3D pptr; @@ -425,8 +481,8 @@ static int htab_map_alloc_check(union bpf_attr *attr) bool zero_seed =3D (attr->map_flags & BPF_F_ZERO_SEED); int numa_node =3D bpf_map_attr_numa_node(attr); =20 - BUILD_BUG_ON(offsetof(struct htab_elem, fnode.next) !=3D - offsetof(struct htab_elem, hash_node.pprev)); + BUILD_BUG_ON(offsetof(struct htab_node, fnode.next) !=3D + offsetof(struct htab_node, hash_node.pprev)); =20 if (zero_seed && !capable(CAP_SYS_ADMIN)) /* Guard against local DoS, and discourage production use. */ @@ -476,7 +532,7 @@ static void htab_mem_dtor(void *obj, void *ctx) if (IS_ERR_OR_NULL(hrec->record)) return; =20 - map_value =3D htab_elem_value(elem, hrec->key_size); + map_value =3D (void *)elem + sizeof(struct htab_elem) + round_up(hrec->ke= y_size, 8); bpf_obj_free_fields(hrec->record, map_value); } =20 @@ -583,8 +639,14 @@ static struct bpf_map *htab_map_alloc(union bpf_attr *= attr) =20 htab->n_buckets =3D roundup_pow_of_two(htab->map.max_entries); =20 - htab->elem_size =3D sizeof(struct htab_elem) + - round_up(htab->map.key_size, 8); + if (htab_is_lru(htab)) + htab->key_offset =3D sizeof(struct htab_elem_lru); + else if (percpu && !prealloc) + htab->key_offset =3D sizeof(struct htab_elem_pcpu); + else + htab->key_offset =3D sizeof(struct htab_elem); + + htab->elem_size =3D htab->key_offset + round_up(htab->map.key_size, 8); if (percpu) htab->elem_size +=3D sizeof(void *); else @@ -692,14 +754,16 @@ static inline struct hlist_nulls_head *select_bucket(= struct bpf_htab *htab, u32 } =20 /* this lookup function can only be called with bucket lock taken */ -static struct htab_elem *lookup_elem_raw(struct hlist_nulls_head *head, u3= 2 hash, +static struct htab_elem *lookup_elem_raw(struct bpf_htab *htab, + struct hlist_nulls_head *head, u32 hash, void *key, u32 key_size) { struct hlist_nulls_node *n; struct htab_elem *l; =20 - hlist_nulls_for_each_entry_rcu(l, n, head, hash_node) - if (l->hash =3D=3D hash && !memcmp(&l->key, key, key_size)) + hlist_nulls_for_each_entry_rcu(l, n, head, node.hash_node) + if (htab_elem_hash(htab, l) =3D=3D hash && + !memcmp(htab_elem_key(htab, l), key, key_size)) return l; =20 return NULL; @@ -709,7 +773,8 @@ static struct htab_elem *lookup_elem_raw(struct hlist_n= ulls_head *head, u32 hash * the unlikely event when elements moved from one bucket into another * while link list is being walked */ -static struct htab_elem *lookup_nulls_elem_raw(struct hlist_nulls_head *he= ad, +static struct htab_elem *lookup_nulls_elem_raw(struct bpf_htab *htab, + struct hlist_nulls_head *head, u32 hash, void *key, u32 key_size, u32 n_buckets) { @@ -717,8 +782,9 @@ static struct htab_elem *lookup_nulls_elem_raw(struct h= list_nulls_head *head, struct htab_elem *l; =20 again: - hlist_nulls_for_each_entry_rcu(l, n, head, hash_node) - if (l->hash =3D=3D hash && !memcmp(&l->key, key, key_size)) + hlist_nulls_for_each_entry_rcu(l, n, head, node.hash_node) + if (htab_elem_hash(htab, l) =3D=3D hash && + !memcmp(htab_elem_key(htab, l), key, key_size)) return l; =20 if (unlikely(get_nulls_value(n) !=3D (hash & (n_buckets - 1)))) @@ -747,17 +813,18 @@ static void *__htab_map_lookup_elem(struct bpf_map *m= ap, void *key) =20 head =3D select_bucket(htab, hash); =20 - l =3D lookup_nulls_elem_raw(head, hash, key, key_size, htab->n_buckets); + l =3D lookup_nulls_elem_raw(htab, head, hash, key, key_size, htab->n_buck= ets); =20 return l; } =20 static void *htab_map_lookup_elem(struct bpf_map *map, void *key) { + struct bpf_htab *htab =3D container_of(map, struct bpf_htab, map); struct htab_elem *l =3D __htab_map_lookup_elem(map, key); =20 if (l) - return htab_elem_value(l, map->key_size); + return htab_elem_value(htab, l); =20 return NULL; } @@ -775,6 +842,7 @@ static void *htab_map_lookup_elem(struct bpf_map *map, = void *key) */ static int htab_map_gen_lookup(struct bpf_map *map, struct bpf_insn *insn_= buf) { + struct bpf_htab *htab =3D container_of(map, struct bpf_htab, map); struct bpf_insn *insn =3D insn_buf; const int ret =3D BPF_REG_0; =20 @@ -783,7 +851,7 @@ static int htab_map_gen_lookup(struct bpf_map *map, str= uct bpf_insn *insn_buf) *insn++ =3D BPF_EMIT_CALL(__htab_map_lookup_elem); *insn++ =3D BPF_JMP_IMM(BPF_JEQ, ret, 0, 1); *insn++ =3D BPF_ALU64_IMM(BPF_ADD, ret, - offsetof(struct htab_elem, key) + + htab->key_offset + round_up(map->key_size, 8)); return insn - insn_buf; } @@ -791,12 +859,13 @@ static int htab_map_gen_lookup(struct bpf_map *map, s= truct bpf_insn *insn_buf) static __always_inline void *__htab_lru_map_lookup_elem(struct bpf_map *ma= p, void *key, const bool mark) { + struct bpf_htab *htab =3D container_of(map, struct bpf_htab, map); struct htab_elem *l =3D __htab_map_lookup_elem(map, key); =20 if (l) { if (mark) - bpf_lru_node_set_ref(&l->lru_node); - return htab_elem_value(l, map->key_size); + bpf_lru_node_set_ref(htab_elem_lru_node(l)); + return htab_elem_value(htab, l); } =20 return NULL; @@ -815,6 +884,7 @@ static void *htab_lru_map_lookup_elem_sys(struct bpf_ma= p *map, void *key) static int htab_lru_map_gen_lookup(struct bpf_map *map, struct bpf_insn *insn_buf) { + struct bpf_htab *htab =3D container_of(map, struct bpf_htab, map); struct bpf_insn *insn =3D insn_buf; const int ret =3D BPF_REG_0; const int ref_reg =3D BPF_REG_1; @@ -824,15 +894,15 @@ static int htab_lru_map_gen_lookup(struct bpf_map *ma= p, *insn++ =3D BPF_EMIT_CALL(__htab_map_lookup_elem); *insn++ =3D BPF_JMP_IMM(BPF_JEQ, ret, 0, 4); *insn++ =3D BPF_LDX_MEM(BPF_B, ref_reg, ret, - offsetof(struct htab_elem, lru_node) + + offsetof(struct htab_elem_lru, lru_node) + offsetof(struct bpf_lru_node, ref)); *insn++ =3D BPF_JMP_IMM(BPF_JNE, ref_reg, 0, 1); *insn++ =3D BPF_ST_MEM(BPF_B, ret, - offsetof(struct htab_elem, lru_node) + + offsetof(struct htab_elem_lru, lru_node) + offsetof(struct bpf_lru_node, ref), 1); *insn++ =3D BPF_ALU64_IMM(BPF_ADD, ret, - offsetof(struct htab_elem, key) + + htab->key_offset + round_up(map->key_size, 8)); return insn - insn_buf; } @@ -844,13 +914,13 @@ static void check_and_cancel_fields(struct bpf_htab *= htab, return; =20 if (htab_is_percpu(htab)) { - void __percpu *pptr =3D htab_elem_get_ptr(elem, htab->map.key_size); + void __percpu *pptr =3D htab_elem_get_ptr(htab, elem); int cpu; =20 for_each_possible_cpu(cpu) bpf_obj_cancel_fields(&htab->map, per_cpu_ptr(pptr, cpu)); } else { - void *map_value =3D htab_elem_value(elem, htab->map.key_size); + void *map_value =3D htab_elem_value(htab, elem); =20 bpf_obj_cancel_fields(&htab->map, map_value); } @@ -869,17 +939,17 @@ static bool htab_lru_map_delete_node(void *arg, struc= t bpf_lru_node *node) struct bucket *b; int ret; =20 - tgt_l =3D container_of(node, struct htab_elem, lru_node); - b =3D __select_bucket(htab, tgt_l->hash); + tgt_l =3D (struct htab_elem *)container_of(node, struct htab_elem_lru, lr= u_node); + b =3D __select_bucket(htab, htab_elem_hash(htab, tgt_l)); head =3D &b->head; =20 ret =3D htab_lock_bucket(b, &flags); if (ret) return false; =20 - hlist_nulls_for_each_entry_rcu(l, n, head, hash_node) + hlist_nulls_for_each_entry_rcu(l, n, head, node.hash_node) if (l =3D=3D tgt_l) { - hlist_nulls_del_rcu(&l->hash_node); + hlist_nulls_del_rcu(&l->node.hash_node); bpf_map_dec_elem_count(&htab->map); break; } @@ -912,18 +982,19 @@ static int htab_map_get_next_key(struct bpf_map *map,= void *key, void *next_key) head =3D select_bucket(htab, hash); =20 /* lookup the key */ - l =3D lookup_nulls_elem_raw(head, hash, key, key_size, htab->n_buckets); + l =3D lookup_nulls_elem_raw(htab, head, hash, key, key_size, htab->n_buck= ets); =20 if (!l) goto find_first_elem; =20 /* key was found, get next key in the same bucket */ - next_l =3D hlist_nulls_entry_safe(rcu_dereference_raw(hlist_nulls_next_rc= u(&l->hash_node)), - struct htab_elem, hash_node); + next_l =3D hlist_nulls_entry_safe( + rcu_dereference_raw(hlist_nulls_next_rcu(&l->node.hash_node)), + struct htab_elem, node.hash_node); =20 if (next_l) { /* if next elem in this hash list is non-zero, just return it */ - memcpy(next_key, next_l->key, key_size); + memcpy(next_key, htab_elem_key(htab, next_l), key_size); return 0; } =20 @@ -938,10 +1009,10 @@ static int htab_map_get_next_key(struct bpf_map *map= , void *key, void *next_key) =20 /* pick first element in the bucket */ next_l =3D hlist_nulls_entry_safe(rcu_dereference_raw(hlist_nulls_first_= rcu(head)), - struct htab_elem, hash_node); + struct htab_elem, node.hash_node); if (next_l) { /* if it's not empty, just return it */ - memcpy(next_key, next_l->key, key_size); + memcpy(next_key, htab_elem_key(htab, next_l), key_size); return 0; } } @@ -955,7 +1026,7 @@ static void htab_elem_free(struct bpf_htab *htab, stru= ct htab_elem *l) check_and_cancel_fields(htab, l); =20 if (htab->map.map_type =3D=3D BPF_MAP_TYPE_PERCPU_HASH) - bpf_mem_cache_free(&htab->pcpu_ma, l->ptr_to_pptr); + bpf_mem_cache_free(&htab->pcpu_ma, htab_elem_get_ptr_to_pptr(l)); bpf_mem_cache_free(&htab->ma, l); } =20 @@ -965,7 +1036,7 @@ static void htab_put_fd_value(struct bpf_htab *htab, s= truct htab_elem *l) void *ptr; =20 if (map->ops->map_fd_put_ptr) { - ptr =3D fd_htab_map_get_ptr(map, l); + ptr =3D fd_htab_map_get_ptr(htab, l); map->ops->map_fd_put_ptr(map, ptr, true); } } @@ -1006,7 +1077,7 @@ static void free_htab_elem(struct bpf_htab *htab, str= uct htab_elem *l) if (htab_is_prealloc(htab)) { bpf_map_dec_elem_count(&htab->map); check_and_cancel_fields(htab, l); - pcpu_freelist_push(&htab->freelist, &l->fnode); + pcpu_freelist_push(&htab->freelist, &l->node.fnode); } else { dec_elem_count(htab); htab_elem_free(htab, l); @@ -1097,7 +1168,7 @@ static struct htab_elem *alloc_htab_elem(struct bpf_h= tab *htab, void *key, l =3D __pcpu_freelist_pop(&htab->freelist); if (!l) return ERR_PTR(-E2BIG); - l_new =3D container_of(l, struct htab_elem, fnode); + l_new =3D container_of(l, struct htab_elem, node.fnode); bpf_map_inc_elem_count(&htab->map); } } else { @@ -1117,10 +1188,10 @@ static struct htab_elem *alloc_htab_elem(struct bpf= _htab *htab, void *key, } } =20 - memcpy(l_new->key, key, key_size); + memcpy(htab_elem_key(htab, l_new), key, key_size); if (percpu) { if (prealloc) { - pptr =3D htab_elem_get_ptr(l_new, key_size); + pptr =3D htab_elem_get_ptr(htab, l_new); } else { /* alloc_percpu zero-fills */ void *ptr =3D bpf_mem_cache_alloc(&htab->pcpu_ma); @@ -1130,26 +1201,26 @@ static struct htab_elem *alloc_htab_elem(struct bpf= _htab *htab, void *key, l_new =3D ERR_PTR(-ENOMEM); goto dec_count; } - l_new->ptr_to_pptr =3D ptr; + htab_elem_set_ptr_to_pptr(l_new, ptr); pptr =3D *(void __percpu **)ptr; } =20 pcpu_init_value(htab, pptr, value, onallcpus, map_flags); =20 if (!prealloc) - htab_elem_set_ptr(l_new, key_size, pptr); + htab_elem_set_ptr(htab, l_new, pptr); } else if (fd_htab_map_needs_adjust(htab)) { size =3D round_up(size, 8); - memcpy(htab_elem_value(l_new, key_size), value, size); + memcpy(htab_elem_value(htab, l_new), value, size); } else if (map_flags & BPF_F_LOCK) { copy_map_value_locked(&htab->map, - htab_elem_value(l_new, key_size), + htab_elem_value(htab, l_new), value, false); } else { - copy_map_value(&htab->map, htab_elem_value(l_new, key_size), value); + copy_map_value(&htab->map, htab_elem_value(htab, l_new), value); } =20 - l_new->hash =3D hash; + htab_elem_set_hash(htab, l_new, hash); return l_new; dec_count: dec_elem_count(htab); @@ -1199,7 +1270,7 @@ static long htab_map_update_elem(struct bpf_map *map,= void *key, void *value, if (unlikely(!btf_record_has_field(map->record, BPF_SPIN_LOCK))) return -EINVAL; /* find an element without taking the bucket lock */ - l_old =3D lookup_nulls_elem_raw(head, hash, key, key_size, + l_old =3D lookup_nulls_elem_raw(htab, head, hash, key, key_size, htab->n_buckets); ret =3D check_flags(htab, l_old, map_flags); if (ret) @@ -1207,7 +1278,7 @@ static long htab_map_update_elem(struct bpf_map *map,= void *key, void *value, if (l_old) { /* grab the element lock and update value in place */ copy_map_value_locked(map, - htab_elem_value(l_old, key_size), + htab_elem_value(htab, l_old), value, false); return 0; } @@ -1221,7 +1292,7 @@ static long htab_map_update_elem(struct bpf_map *map,= void *key, void *value, if (ret) return ret; =20 - l_old =3D lookup_elem_raw(head, hash, key, key_size); + l_old =3D lookup_elem_raw(htab, head, hash, key, key_size); =20 ret =3D check_flags(htab, l_old, map_flags); if (ret) @@ -1235,7 +1306,7 @@ static long htab_map_update_elem(struct bpf_map *map,= void *key, void *value, * and update element in place */ copy_map_value_locked(map, - htab_elem_value(l_old, key_size), + htab_elem_value(htab, l_old), value, false); ret =3D 0; goto err; @@ -1252,9 +1323,9 @@ static long htab_map_update_elem(struct bpf_map *map,= void *key, void *value, /* add new element to the head of the list, so that * concurrent search will find it before old elem */ - hlist_nulls_add_head_rcu(&l_new->hash_node, head); + hlist_nulls_add_head_rcu(&l_new->node.hash_node, head); if (l_old) { - hlist_nulls_del_rcu(&l_old->hash_node); + hlist_nulls_del_rcu(&l_old->node.hash_node); =20 /* l_old has already been stashed in htab->extra_elems, cancel * its reusable special fields before it is available for reuse. @@ -1275,7 +1346,7 @@ static void htab_lru_push_free(struct bpf_htab *htab,= struct htab_elem *elem) { check_and_cancel_fields(htab, elem); bpf_map_dec_elem_count(&htab->map); - bpf_lru_push_free(&htab->lru, &elem->lru_node); + bpf_lru_push_free(&htab->lru, htab_elem_lru_node(elem)); } =20 static long htab_lru_map_update_elem(struct bpf_map *map, void *key, void = *value, @@ -1310,13 +1381,13 @@ static long htab_lru_map_update_elem(struct bpf_map= *map, void *key, void *value l_new =3D prealloc_lru_pop(htab, key, hash); if (!l_new) return -ENOMEM; - copy_map_value(&htab->map, htab_elem_value(l_new, map->key_size), value); + copy_map_value(&htab->map, htab_elem_value(htab, l_new), value); =20 ret =3D htab_lock_bucket(b, &flags); if (ret) goto err_lock_bucket; =20 - l_old =3D lookup_elem_raw(head, hash, key, key_size); + l_old =3D lookup_elem_raw(htab, head, hash, key, key_size); =20 ret =3D check_flags(htab, l_old, map_flags); if (ret) @@ -1325,10 +1396,10 @@ static long htab_lru_map_update_elem(struct bpf_map= *map, void *key, void *value /* add new element to the head of the list, so that * concurrent search will find it before old elem */ - hlist_nulls_add_head_rcu(&l_new->hash_node, head); + hlist_nulls_add_head_rcu(&l_new->node.hash_node, head); if (l_old) { - bpf_lru_node_set_ref(&l_new->lru_node); - hlist_nulls_del_rcu(&l_old->hash_node); + bpf_lru_node_set_ref(htab_elem_lru_node(l_new)); + hlist_nulls_del_rcu(&l_old->node.hash_node); } ret =3D 0; =20 @@ -1383,7 +1454,7 @@ static long htab_map_update_elem_in_place(struct bpf_= map *map, void *key, if (ret) return ret; =20 - l_old =3D lookup_elem_raw(head, hash, key, key_size); + l_old =3D lookup_elem_raw(htab, head, hash, key, key_size); =20 ret =3D check_flags(htab, l_old, map_flags); if (ret) @@ -1392,10 +1463,10 @@ static long htab_map_update_elem_in_place(struct bp= f_map *map, void *key, if (l_old) { /* Update value in-place */ if (percpu) { - pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size), + pcpu_copy_value(htab, htab_elem_get_ptr(htab, l_old), value, onallcpus, map_flags); } else { - void **inner_map_pptr =3D htab_elem_value(l_old, key_size); + void **inner_map_pptr =3D htab_elem_value(htab, l_old); =20 old_map_ptr =3D *inner_map_pptr; WRITE_ONCE(*inner_map_pptr, *(void **)value); @@ -1407,7 +1478,7 @@ static long htab_map_update_elem_in_place(struct bpf_= map *map, void *key, ret =3D PTR_ERR(l_new); goto err; } - hlist_nulls_add_head_rcu(&l_new->hash_node, head); + hlist_nulls_add_head_rcu(&l_new->node.hash_node, head); } err: htab_unlock_bucket(b, flags); @@ -1456,22 +1527,22 @@ static long __htab_lru_percpu_map_update_elem(struc= t bpf_map *map, void *key, if (ret) goto err_lock_bucket; =20 - l_old =3D lookup_elem_raw(head, hash, key, key_size); + l_old =3D lookup_elem_raw(htab, head, hash, key, key_size); =20 ret =3D check_flags(htab, l_old, map_flags); if (ret) goto err; =20 if (l_old) { - bpf_lru_node_set_ref(&l_old->lru_node); + bpf_lru_node_set_ref(htab_elem_lru_node(l_old)); =20 /* per-cpu hash map can update value in-place */ - pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size), + pcpu_copy_value(htab, htab_elem_get_ptr(htab, l_old), value, onallcpus, map_flags); } else { - pcpu_init_value(htab, htab_elem_get_ptr(l_new, key_size), + pcpu_init_value(htab, htab_elem_get_ptr(htab, l_new), value, onallcpus, map_flags); - hlist_nulls_add_head_rcu(&l_new->hash_node, head); + hlist_nulls_add_head_rcu(&l_new->node.hash_node, head); l_new =3D NULL; } ret =3D 0; @@ -1480,7 +1551,7 @@ static long __htab_lru_percpu_map_update_elem(struct = bpf_map *map, void *key, err_lock_bucket: if (l_new) { bpf_map_dec_elem_count(&htab->map); - bpf_lru_push_free(&htab->lru, &l_new->lru_node); + bpf_lru_push_free(&htab->lru, htab_elem_lru_node(l_new)); } return ret; } @@ -1521,9 +1592,9 @@ static long htab_map_delete_elem(struct bpf_map *map,= void *key) if (ret) return ret; =20 - l =3D lookup_elem_raw(head, hash, key, key_size); + l =3D lookup_elem_raw(htab, head, hash, key, key_size); if (l) - hlist_nulls_del_rcu(&l->hash_node); + hlist_nulls_del_rcu(&l->node.hash_node); else ret =3D -ENOENT; =20 @@ -1556,10 +1627,10 @@ static long htab_lru_map_delete_elem(struct bpf_map= *map, void *key) if (ret) return ret; =20 - l =3D lookup_elem_raw(head, hash, key, key_size); + l =3D lookup_elem_raw(htab, head, hash, key, key_size); =20 if (l) - hlist_nulls_del_rcu(&l->hash_node); + hlist_nulls_del_rcu(&l->node.hash_node); else ret =3D -ENOENT; =20 @@ -1581,8 +1652,8 @@ static void delete_all_elements(struct bpf_htab *htab) struct hlist_nulls_node *n; struct htab_elem *l; =20 - hlist_nulls_for_each_entry_safe(l, n, head, hash_node) { - hlist_nulls_del_rcu(&l->hash_node); + hlist_nulls_for_each_entry_safe(l, n, head, node.hash_node) { + hlist_nulls_del_rcu(&l->node.hash_node); htab_elem_free(htab, l); } cond_resched(); @@ -1599,10 +1670,10 @@ static void htab_free_malloced_internal_structs(str= uct bpf_htab *htab) struct hlist_nulls_node *n; struct htab_elem *l; =20 - hlist_nulls_for_each_entry(l, n, head, hash_node) { + hlist_nulls_for_each_entry(l, n, head, node.hash_node) { /* We only free internal structs on uref dropping to zero */ bpf_map_free_internal_structs(&htab->map, - htab_elem_value(l, htab->map.key_size)); + htab_elem_value(htab, l)); } cond_resched_rcu(); } @@ -1697,7 +1768,7 @@ static int __htab_map_lookup_and_delete_elem(struct b= pf_map *map, void *key, if (ret) return ret; =20 - l =3D lookup_elem_raw(head, hash, key, key_size); + l =3D lookup_elem_raw(htab, head, hash, key, key_size); if (!l) { ret =3D -ENOENT; goto out_unlock; @@ -1708,14 +1779,14 @@ static int __htab_map_lookup_and_delete_elem(struct= bpf_map *map, void *key, void __percpu *pptr; int off =3D 0, cpu; =20 - pptr =3D htab_elem_get_ptr(l, key_size); + pptr =3D htab_elem_get_ptr(htab, l); for_each_possible_cpu(cpu) { copy_map_value_long(&htab->map, value + off, per_cpu_ptr(pptr, cpu)); check_and_init_map_value(&htab->map, value + off); off +=3D roundup_value_size; } } else { - void *src =3D htab_elem_value(l, map->key_size); + void *src =3D htab_elem_value(htab, l); =20 if (flags & BPF_F_LOCK) copy_map_value_locked(map, value, src, true); @@ -1724,7 +1795,7 @@ static int __htab_map_lookup_and_delete_elem(struct b= pf_map *map, void *key, /* Zeroing special fields in the temp buffer */ check_and_init_map_value(map, value); } - hlist_nulls_del_rcu(&l->hash_node); + hlist_nulls_del_rcu(&l->node.hash_node); =20 out_unlock: htab_unlock_bucket(b, bflags); @@ -1860,7 +1931,7 @@ __htab_map_lookup_and_delete_batch(struct bpf_map *ma= p, } =20 bucket_cnt =3D 0; - hlist_nulls_for_each_entry_rcu(l, n, head, hash_node) + hlist_nulls_for_each_entry_rcu(l, n, head, node.hash_node) bucket_cnt++; =20 if (bucket_cnt && !locked) { @@ -1897,14 +1968,14 @@ __htab_map_lookup_and_delete_batch(struct bpf_map *= map, if (!locked) goto next_batch; =20 - hlist_nulls_for_each_entry_safe(l, n, head, hash_node) { - memcpy(dst_key, l->key, key_size); + hlist_nulls_for_each_entry_safe(l, n, head, node.hash_node) { + memcpy(dst_key, htab_elem_key(htab, l), key_size); =20 if (is_percpu) { int off =3D 0, cpu; void __percpu *pptr; =20 - pptr =3D htab_elem_get_ptr(l, map->key_size); + pptr =3D htab_elem_get_ptr(htab, l); if (elem_map_flags & BPF_F_CPU) { cpu =3D elem_map_flags >> 32; copy_map_value(&htab->map, dst_val, per_cpu_ptr(pptr, cpu)); @@ -1918,7 +1989,7 @@ __htab_map_lookup_and_delete_batch(struct bpf_map *ma= p, } } } else { - value =3D htab_elem_value(l, key_size); + value =3D htab_elem_value(htab, l); if (is_fd_htab(htab)) { struct bpf_map **inner_map =3D value; =20 @@ -1936,7 +2007,7 @@ __htab_map_lookup_and_delete_batch(struct bpf_map *ma= p, check_and_init_map_value(map, dst_val); } if (do_delete) { - hlist_nulls_del_rcu(&l->hash_node); + hlist_nulls_del_rcu(&l->node.hash_node); =20 /* bpf_lru_push_free() will acquire lru_lock, which * may cause deadlock. See comments in function @@ -1948,7 +2019,7 @@ __htab_map_lookup_and_delete_batch(struct bpf_map *ma= p, * lock being held and it violates the lock rule, so * invoke free_htab_elem() after unlock as well. */ - l->batch_flink =3D node_to_free; + l->node.batch_flink =3D node_to_free; node_to_free =3D l; } dst_key +=3D key_size; @@ -1960,7 +2031,7 @@ __htab_map_lookup_and_delete_batch(struct bpf_map *ma= p, =20 while (node_to_free) { l =3D node_to_free; - node_to_free =3D node_to_free->batch_flink; + node_to_free =3D node_to_free->node.batch_flink; if (is_lru_map) htab_lru_push_free(htab, l); else @@ -2108,8 +2179,8 @@ bpf_hash_map_seq_find_next(struct bpf_iter_seq_hash_m= ap_info *info, /* no update/deletion on this bucket, prev_elem should be still valid * and we won't skip elements. */ - n =3D rcu_dereference_raw(hlist_nulls_next_rcu(&prev_elem->hash_node)); - elem =3D hlist_nulls_entry_safe(n, struct htab_elem, hash_node); + n =3D rcu_dereference_raw(hlist_nulls_next_rcu(&prev_elem->node.hash_nod= e)); + elem =3D hlist_nulls_entry_safe(n, struct htab_elem, node.hash_node); if (elem) return elem; =20 @@ -2125,7 +2196,7 @@ bpf_hash_map_seq_find_next(struct bpf_iter_seq_hash_m= ap_info *info, =20 count =3D 0; head =3D &b->head; - hlist_nulls_for_each_entry_rcu(elem, n, head, hash_node) { + hlist_nulls_for_each_entry_rcu(elem, n, head, node.hash_node) { if (count >=3D skip_elems) { info->bucket_id =3D i; info->skip_elems =3D count; @@ -2183,12 +2254,12 @@ static int __bpf_hash_map_seq_show(struct seq_file = *seq, struct htab_elem *elem) ctx.meta =3D &meta; ctx.map =3D info->map; if (elem) { - ctx.key =3D elem->key; + ctx.key =3D htab_elem_key(info->htab, elem); if (!info->percpu_value_buf) { - ctx.value =3D htab_elem_value(elem, map->key_size); + ctx.value =3D htab_elem_value(info->htab, elem); } else { roundup_value_size =3D round_up(map->value_size, 8); - pptr =3D htab_elem_get_ptr(elem, map->key_size); + pptr =3D htab_elem_get_ptr(info->htab, elem); for_each_possible_cpu(cpu) { copy_map_value_long(map, info->percpu_value_buf + off, per_cpu_ptr(pptr, cpu)); @@ -2292,14 +2363,14 @@ static long bpf_for_each_hash_elem(struct bpf_map *= map, bpf_callback_t callback_ b =3D &htab->buckets[i]; rcu_read_lock(); head =3D &b->head; - hlist_nulls_for_each_entry_safe(elem, n, head, hash_node) { - key =3D elem->key; + hlist_nulls_for_each_entry_safe(elem, n, head, node.hash_node) { + key =3D htab_elem_key(htab, elem); if (is_percpu) { /* current cpu value for percpu map */ - pptr =3D htab_elem_get_ptr(elem, map->key_size); + pptr =3D htab_elem_get_ptr(htab, elem); val =3D this_cpu_ptr(pptr); } else { - val =3D htab_elem_value(elem, map->key_size); + val =3D htab_elem_value(htab, elem); } num_elems++; ret =3D callback_fn((u64)(long)map, (u64)(long)key, @@ -2403,10 +2474,11 @@ const struct bpf_map_ops htab_lru_map_ops =3D { /* Called from eBPF program */ static void *htab_percpu_map_lookup_elem(struct bpf_map *map, void *key) { + struct bpf_htab *htab =3D container_of(map, struct bpf_htab, map); struct htab_elem *l =3D __htab_map_lookup_elem(map, key); =20 if (l) - return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size)); + return this_cpu_ptr(htab_elem_get_ptr(htab, l)); else return NULL; } @@ -2414,6 +2486,7 @@ static void *htab_percpu_map_lookup_elem(struct bpf_m= ap *map, void *key) /* inline bpf_map_lookup_elem() call for per-CPU hashmap */ static int htab_percpu_map_gen_lookup(struct bpf_map *map, struct bpf_insn= *insn_buf) { + struct bpf_htab *htab =3D container_of(map, struct bpf_htab, map); struct bpf_insn *insn =3D insn_buf; =20 if (!bpf_jit_supports_percpu_insn()) @@ -2424,7 +2497,7 @@ static int htab_percpu_map_gen_lookup(struct bpf_map = *map, struct bpf_insn *insn *insn++ =3D BPF_EMIT_CALL(__htab_map_lookup_elem); *insn++ =3D BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 3); *insn++ =3D BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, - offsetof(struct htab_elem, key) + roundup(map->key_size, 8)); + htab->key_offset + roundup(map->key_size, 8)); *insn++ =3D BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_0, 0); *insn++ =3D BPF_MOV64_PERCPU_REG(BPF_REG_0, BPF_REG_0); =20 @@ -2433,6 +2506,7 @@ static int htab_percpu_map_gen_lookup(struct bpf_map = *map, struct bpf_insn *insn =20 static void *htab_percpu_map_lookup_percpu_elem(struct bpf_map *map, void = *key, u32 cpu) { + struct bpf_htab *htab =3D container_of(map, struct bpf_htab, map); struct htab_elem *l; =20 if (cpu >=3D nr_cpu_ids) @@ -2440,18 +2514,19 @@ static void *htab_percpu_map_lookup_percpu_elem(str= uct bpf_map *map, void *key, =20 l =3D __htab_map_lookup_elem(map, key); if (l) - return per_cpu_ptr(htab_elem_get_ptr(l, map->key_size), cpu); + return per_cpu_ptr(htab_elem_get_ptr(htab, l), cpu); else return NULL; } =20 static void *htab_lru_percpu_map_lookup_elem(struct bpf_map *map, void *ke= y) { + struct bpf_htab *htab =3D container_of(map, struct bpf_htab, map); struct htab_elem *l =3D __htab_map_lookup_elem(map, key); =20 if (l) { - bpf_lru_node_set_ref(&l->lru_node); - return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size)); + bpf_lru_node_set_ref(htab_elem_lru_node(l)); + return this_cpu_ptr(htab_elem_get_ptr(htab, l)); } =20 return NULL; @@ -2459,6 +2534,7 @@ static void *htab_lru_percpu_map_lookup_elem(struct b= pf_map *map, void *key) =20 static void *htab_lru_percpu_map_lookup_percpu_elem(struct bpf_map *map, v= oid *key, u32 cpu) { + struct bpf_htab *htab =3D container_of(map, struct bpf_htab, map); struct htab_elem *l; =20 if (cpu >=3D nr_cpu_ids) @@ -2466,8 +2542,8 @@ static void *htab_lru_percpu_map_lookup_percpu_elem(s= truct bpf_map *map, void *k =20 l =3D __htab_map_lookup_elem(map, key); if (l) { - bpf_lru_node_set_ref(&l->lru_node); - return per_cpu_ptr(htab_elem_get_ptr(l, map->key_size), cpu); + bpf_lru_node_set_ref(htab_elem_lru_node(l)); + return per_cpu_ptr(htab_elem_get_ptr(htab, l), cpu); } =20 return NULL; @@ -2475,6 +2551,7 @@ static void *htab_lru_percpu_map_lookup_percpu_elem(s= truct bpf_map *map, void *k =20 int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value, u64 = map_flags) { + struct bpf_htab *htab =3D container_of(map, struct bpf_htab, map); struct htab_elem *l; void __percpu *pptr; int ret =3D -ENOENT; @@ -2494,7 +2571,7 @@ int bpf_percpu_hash_copy(struct bpf_map *map, void *k= ey, void *value, u64 map_fl /* We do not mark LRU map element here in order to not mess up * eviction heuristics when user space does a map walk. */ - pptr =3D htab_elem_get_ptr(l, map->key_size); + pptr =3D htab_elem_get_ptr(htab, l); if (map_flags & BPF_F_CPU) { cpu =3D map_flags >> 32; copy_map_value(map, value, per_cpu_ptr(pptr, cpu)); @@ -2532,6 +2609,7 @@ int bpf_percpu_hash_update(struct bpf_map *map, void = *key, void *value, static void htab_percpu_map_seq_show_elem(struct bpf_map *map, void *key, struct seq_file *m) { + struct bpf_htab *htab =3D container_of(map, struct bpf_htab, map); struct htab_elem *l; void __percpu *pptr; int cpu; @@ -2546,7 +2624,7 @@ static void htab_percpu_map_seq_show_elem(struct bpf_= map *map, void *key, =20 btf_type_seq_show(map->btf, map->btf_key_type_id, key, m); seq_puts(m, ": {\n"); - pptr =3D htab_elem_get_ptr(l, map->key_size); + pptr =3D htab_elem_get_ptr(htab, l); for_each_possible_cpu(cpu) { seq_printf(m, "\tcpu%d: ", cpu); btf_type_seq_show(map->btf, map->btf_value_type_id, @@ -2619,8 +2697,8 @@ static void fd_htab_map_free(struct bpf_map *map) for (i =3D 0; i < htab->n_buckets; i++) { head =3D select_bucket(htab, i); =20 - hlist_nulls_for_each_entry_safe(l, n, head, hash_node) { - void *ptr =3D fd_htab_map_get_ptr(map, l); + hlist_nulls_for_each_entry_safe(l, n, head, node.hash_node) { + void *ptr =3D fd_htab_map_get_ptr(htab, l); =20 map->ops->map_fd_put_ptr(map, ptr, false); } @@ -2705,6 +2783,7 @@ static void *htab_of_map_lookup_elem(struct bpf_map *= map, void *key) static int htab_of_map_gen_lookup(struct bpf_map *map, struct bpf_insn *insn_buf) { + struct bpf_htab *htab =3D container_of(map, struct bpf_htab, map); struct bpf_insn *insn =3D insn_buf; const int ret =3D BPF_REG_0; =20 @@ -2713,7 +2792,7 @@ static int htab_of_map_gen_lookup(struct bpf_map *map, *insn++ =3D BPF_EMIT_CALL(__htab_map_lookup_elem); *insn++ =3D BPF_JMP_IMM(BPF_JEQ, ret, 0, 2); *insn++ =3D BPF_ALU64_IMM(BPF_ADD, ret, - offsetof(struct htab_elem, key) + + htab->key_offset + round_up(map->key_size, 8)); *insn++ =3D BPF_LDX_MEM(BPF_DW, ret, ret, 0); =20 @@ -3533,3 +3612,16 @@ const struct bpf_map_ops rhtab_map_ops =3D { .map_btf_id =3D &rhtab_map_btf_ids[0], .iter_seq_info =3D &rhash_iter_seq_info, }; + +size_t bpf_htab_map_meta_size(void) +{ + return sizeof(struct bpf_htab); +} + +void bpf_htab_map_meta_init(struct bpf_map *inner_map_meta, struct bpf_map= *inner_map) +{ + struct bpf_htab *inner_htab_meta =3D container_of(inner_map_meta, struct = bpf_htab, map); + struct bpf_htab *inner_htab =3D container_of(inner_map, struct bpf_htab, = map); + + inner_htab_meta->key_offset =3D inner_htab->key_offset; +} diff --git a/kernel/bpf/map_in_map.c b/kernel/bpf/map_in_map.c index d2cbab4bdf64..b2db5d0c98be 100644 --- a/kernel/bpf/map_in_map.c +++ b/kernel/bpf/map_in_map.c @@ -7,6 +7,15 @@ =20 #include "map_in_map.h" =20 +static bool bpf_map_type_is_htab(enum bpf_map_type type) +{ + return type =3D=3D BPF_MAP_TYPE_HASH || + type =3D=3D BPF_MAP_TYPE_PERCPU_HASH || + type =3D=3D BPF_MAP_TYPE_LRU_HASH || + type =3D=3D BPF_MAP_TYPE_LRU_PERCPU_HASH || + type =3D=3D BPF_MAP_TYPE_HASH_OF_MAPS; +} + struct bpf_map *bpf_map_meta_alloc(int inner_map_ufd) { struct bpf_map *inner_map, *inner_map_meta; @@ -29,6 +38,8 @@ struct bpf_map *bpf_map_meta_alloc(int inner_map_ufd) /* In some cases verifier needs to access beyond just base map. */ if (inner_map->ops =3D=3D &array_map_ops || inner_map->ops =3D=3D &percpu= _array_map_ops) inner_map_meta_size =3D sizeof(struct bpf_array); + else if (bpf_map_type_is_htab(inner_map->map_type)) + inner_map_meta_size =3D bpf_htab_map_meta_size(); =20 inner_map_meta =3D kzalloc(inner_map_meta_size, GFP_USER); if (!inner_map_meta) @@ -70,6 +81,8 @@ struct bpf_map *bpf_map_meta_alloc(int inner_map_ufd) inner_array_meta->index_mask =3D inner_array->index_mask; inner_array_meta->elem_size =3D inner_array->elem_size; inner_map_meta->bypass_spec_v1 =3D inner_map->bypass_spec_v1; + } else if (bpf_map_type_is_htab(inner_map->map_type)) { + bpf_htab_map_meta_init(inner_map_meta, inner_map); } return inner_map_meta; } diff --git a/kernel/bpf/map_in_map.h b/kernel/bpf/map_in_map.h index 7d61602354de..1edc205772d8 100644 --- a/kernel/bpf/map_in_map.h +++ b/kernel/bpf/map_in_map.h @@ -15,5 +15,7 @@ void *bpf_map_fd_get_ptr(struct bpf_map *map, struct file= *map_file, int ufd); void bpf_map_fd_put_ptr(struct bpf_map *map, void *ptr, bool need_defer); u32 bpf_map_fd_sys_lookup_elem(void *ptr); +size_t bpf_htab_map_meta_size(void); +void bpf_htab_map_meta_init(struct bpf_map *inner_map_meta, struct bpf_map= *inner_map); =20 #endif --=20 2.55.0.508.g3f0d502094-goog From nobody Fri Oct 2 14:03:10 2026 Received: from mail-pj1-f69.google.com (mail-pj1-f69.google.com [209.85.216.69]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 11B1C1A262A for ; Fri, 31 Jul 2026 01:06:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.69 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785460016; cv=none; b=FMMS0yt3gYuDP2E/GutifT8CNG05CEHr+1SBhFSccSWgzz07xn6P0YhFhx2qEUCMdKb4mwoRs+bBPfBsFGbFJF1ZqUyTR6rUIOgKIHiMTykxKWHALjZxzss6WqsiYp0pa4EaEXmUFDfOB22pE6BG7w377C6mQwXWn+3+WiSlSKM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785460016; c=relaxed/simple; bh=aMN9K+ucJ9PqN9jP2AlMpp65oeD4EosnfNW8wUo+m90=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Jnjcv9A1OpFeGHSZkMSwUqUlL0We+SpX8Mpt91nmj5Kj9JcyjZIO6UbfLX8h+FhqyNZT5ok4peUWF2d/C0xBg8wBePxh/Bp6HdTvyQOHgBPgALlOluAOlLVmNROxFW/uCzhdo3FowJGaJl0zTq9cDblX20bUkxfPwVeE/wduiXU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--tjmercier.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=TZprlr2g; arc=none smtp.client-ip=209.85.216.69 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--tjmercier.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="TZprlr2g" Received: by mail-pj1-f69.google.com with SMTP id 98e67ed59e1d1-38dbf293831so996037a91.3 for ; Thu, 30 Jul 2026 18:06:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1785460012; x=1786064812; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=huo7f/j7IwCgJXmU/Er7HJwA0atRtiKEqfp2YGx3T7s=; b=TZprlr2gtpfN/+VuMWh8yhmZoM5adahTle0ASOXxhLT1WPiVRGCzMXXe5N++VFbwFR FHoNkcg7GpyK3WcMb9DO70kaPkDvdXSTr5VRKncwgFEa7bcqm283zx4yD+HUyb+tx18g gGUZScE3GbhLtY5swq++Pux7iQtOgPX8IoNB14CTdPMZvqEuvTkscQYpgNdICF2rvUFg MxcHGsHpYGMnNcxDONhxXSLEGoLsylCSAC3y+PCXVwkDVhpR15B0ker3DQznlWiyVC9/ kST6RwnZ5iLIf132jMqNsL1W8nPTbCVWkBKmuIQIujmbRFxELjmjfO5ABqoZh3XjYER2 P32Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785460012; x=1786064812; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=huo7f/j7IwCgJXmU/Er7HJwA0atRtiKEqfp2YGx3T7s=; b=XdzYny3VuB6C26WXJcPtGDIxvkzR8KmgXImb3iuxi4+TrTKWBsYD6kNofVZRg7cz0U 5s1QLE5ujQifxEnUZIoxWbD5AS9mspmc3zf/xLvtixIqQo34ytMDvPA+4y5P0brvDyIu 0yGAG1trKYJYwcq+O3Aowmfm38pNLQoT63PM/2TMLROcqUCN7gBXcfU7JeiDfqSjr5ZR l1l/jC2bZTPFTPXLA2+KdeDeVlSrY/ZH/y5qBd0uzR/pA8p5TDl8ScpYOtEbTwqXZqY0 UWU/OYerZoScicRRtZfF7YTwoD2hQQstP772WV+SknOIf8rHpx21EpifA8oavSZsns+g EzZw== X-Forwarded-Encrypted: i=1; AHgh+Ro97vZ4zlg3ZNnWlNWPOgqenrp8uiaNtnpD0Mk7Ty0b2vsRx6c1SPJjpuuRC1kUITn9lB6XZxBIPhLvQ1Y=@vger.kernel.org X-Gm-Message-State: AOJu0Yyh0yH26j/te9V8iUTkbVSCBdjJkA9HcHPNH38vKrvisClK9JnH VQzB3+6mT9WTCy4lPK1WZLvwACNwVtYEAOxPoN4m97qBzb9tglvZFeAcEtzeccvxAIAbmgVyh9E Ks2lt0JbtO/KuEkpY3A== X-Received: from pjbnb4.prod.google.com ([2002:a17:90b:35c4:b0:38e:c91c:3667]) (user=tjmercier job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:5847:b0:38e:6c37:d95a with SMTP id 98e67ed59e1d1-38fb1197f1fmr80060a91.19.1785460012140; Thu, 30 Jul 2026 18:06:52 -0700 (PDT) Date: Thu, 30 Jul 2026 18:06:44 -0700 In-Reply-To: <20260731010645.3965783-1-tjmercier@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260731010645.3965783-1-tjmercier@google.com> X-Mailer: git-send-email 2.55.0.508.g3f0d502094-goog Message-ID: <20260731010645.3965783-3-tjmercier@google.com> Subject: [PATCH bpf-next v2 2/2] bpf: htab: Reduce elem_size by 8 bytes for small key sizes From: "T.J. Mercier" To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, eddyz87@gmail.com, memxor@gmail.com, martin.lau@linux.dev, song@kernel.org, yonghong.song@linux.dev, jolsa@kernel.org, emil@etsalapatis.com, mykyta.yatsenko5@gmail.com Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, "T.J. Mercier" Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" For standard and PCPU (non-LRU) hash maps with small key sizes (less than or equal to the word size), comparing keys requires only a single instruction. Storing a cached 32-bit hash value to shortcut full key comparisons provides no performance advantage for small keys, and consumes memory for every element. This memory can be saved by eliminating hash along with its associated 4 byte padding before the key, reducing the elem_size (and key_offset) by 8 bytes for standard and PCPU maps. Introduce htab_has_hash() to check whether a map requires a cached hash field. Update htab_elem_set_hash(), lookup_elem_raw(), and lookup_nulls_elem_raw() to conditionally bypass hash checking and storage when htab_has_hash() is false. Together with the previous patch, this reduces the minimum standard and preallocated hash map element size from 64 bytes down to 32 bytes, and non-preallocated per-CPU element size from 64 bytes down to 40 bytes. Signed-off-by: T.J. Mercier --- kernel/bpf/hashtab.c | 85 ++++++++++++------- .../selftests/bpf/progs/map_ptr_kern.c | 2 +- 2 files changed, 57 insertions(+), 30 deletions(-) diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c index f54366da459f..82b19bd54310 100644 --- a/kernel/bpf/hashtab.c +++ b/kernel/bpf/hashtab.c @@ -100,6 +100,7 @@ struct bpf_htab { struct percpu_counter pcount; atomic_t count; bool use_percpu_counter; + bool has_hash; u32 n_buckets; /* number of hash buckets */ u32 elem_size; /* size of each element in bytes */ u32 key_offset; /* offset of key in bytes */ @@ -123,15 +124,13 @@ struct htab_node { =20 struct htab_elem { struct htab_node node; - u32 hash; - char key[] __aligned(8); + u8 data[] __aligned(8); }; =20 struct htab_elem_lru { struct htab_node node; struct bpf_lru_node lru_node; - u32 hash; - char key[] __aligned(8); + u8 data[] __aligned(8); }; =20 /* Only for non-preallocated PCPU maps. Preallocated PCPU maps don't need @@ -140,13 +139,13 @@ struct htab_elem_lru { struct htab_elem_pcpu { struct htab_node node; void *ptr_to_pptr; - u32 hash; - char key[] __aligned(8); + u8 data[] __aligned(8); }; =20 struct htab_btf_record { struct btf_record *record; u32 key_size; + u32 key_offset; }; =20 static inline bool htab_is_prealloc(const struct bpf_htab *htab) @@ -247,24 +246,31 @@ static struct htab_elem *get_htab_elem(struct bpf_hta= b *htab, int i) return (struct htab_elem *) (htab->elems + i * (u64)htab->elem_size); } =20 +static inline bool htab_has_hash(const struct bpf_htab *htab) +{ + return htab->has_hash; +} + static inline u32 htab_elem_hash(struct bpf_htab *htab, struct htab_elem *= l) { if (htab_is_lru(htab)) - return ((struct htab_elem_lru *)l)->hash; + return *(u32 *)((struct htab_elem_lru *)l)->data; else if (htab_is_percpu(htab) && !htab_is_prealloc(htab)) - return ((struct htab_elem_pcpu *)l)->hash; + return *(u32 *)((struct htab_elem_pcpu *)l)->data; else - return l->hash; + return *(u32 *)l->data; } =20 static inline void htab_elem_set_hash(struct bpf_htab *htab, struct htab_e= lem *l, u32 hash) { + if (!htab_has_hash(htab)) + return; if (htab_is_lru(htab)) - ((struct htab_elem_lru *)l)->hash =3D hash; + *(u32 *)((struct htab_elem_lru *)l)->data =3D hash; else if (htab_is_percpu(htab) && !htab_is_prealloc(htab)) - ((struct htab_elem_pcpu *)l)->hash =3D hash; + *(u32 *)((struct htab_elem_pcpu *)l)->data =3D hash; else - l->hash =3D hash; + *(u32 *)l->data =3D hash; } =20 /* Both percpu and fd htab support in-place update, so no need for @@ -405,7 +411,7 @@ static int prealloc_init(struct bpf_htab *htab) if (htab_is_lru(htab)) err =3D bpf_lru_init(&htab->lru, htab->map.map_flags & BPF_F_NO_COMMON_LRU, - offsetof(struct htab_elem_lru, hash) - + offsetof(struct htab_elem_lru, data) - offsetof(struct htab_elem_lru, lru_node), htab_lru_map_delete_node, htab); @@ -532,7 +538,7 @@ static void htab_mem_dtor(void *obj, void *ctx) if (IS_ERR_OR_NULL(hrec->record)) return; =20 - map_value =3D (void *)elem + sizeof(struct htab_elem) + round_up(hrec->ke= y_size, 8); + map_value =3D (void *)elem + hrec->key_offset + round_up(hrec->key_size, = 8); bpf_obj_free_fields(hrec->record, map_value); } =20 @@ -558,7 +564,7 @@ static void htab_dtor_ctx_free(void *ctx) } =20 static int bpf_ma_set_dtor(struct bpf_map *map, struct bpf_mem_alloc *ma, - void (*dtor)(void *, void *)) + void (*dtor)(void *, void *), u32 key_offset) { struct htab_btf_record *hrec; int err; @@ -571,6 +577,7 @@ static int bpf_ma_set_dtor(struct bpf_map *map, struct = bpf_mem_alloc *ma, if (!hrec) return -ENOMEM; hrec->key_size =3D map->key_size; + hrec->key_offset =3D key_offset; hrec->record =3D btf_record_dup(map->record); if (IS_ERR(hrec->record)) { err =3D PTR_ERR(hrec->record); @@ -593,9 +600,9 @@ static int htab_map_check_btf(struct bpf_map *map, cons= t struct btf *btf, * populated in htab_map_alloc(), so it will always appear as NULL. */ if (htab_is_percpu(htab)) - return bpf_ma_set_dtor(map, &htab->pcpu_ma, htab_pcpu_mem_dtor); + return bpf_ma_set_dtor(map, &htab->pcpu_ma, htab_pcpu_mem_dtor, htab->ke= y_offset); else - return bpf_ma_set_dtor(map, &htab->ma, htab_mem_dtor); + return bpf_ma_set_dtor(map, &htab->ma, htab_mem_dtor, htab->key_offset); } =20 static struct bpf_map *htab_map_alloc(union bpf_attr *attr) @@ -618,6 +625,12 @@ static struct bpf_map *htab_map_alloc(union bpf_attr *= attr) =20 bpf_map_init_from_attr(&htab->map, attr); =20 + /* Avoid hash memory use and comparisons where unnecessary. + * u32 hash reads are always atomic. If we elide them, key comparisons mu= st also be atomic + * to avoid false positive key matches due to torn key reads / writes. So= check key_size. + */ + htab->has_hash =3D htab_is_lru(htab) || htab->map.key_size > sizeof(unsig= ned long); + if (percpu_lru) { /* ensure each CPU's lru list has >=3D1 elements. * since we are at it, make each lru list has the same @@ -640,11 +653,13 @@ static struct bpf_map *htab_map_alloc(union bpf_attr = *attr) htab->n_buckets =3D roundup_pow_of_two(htab->map.max_entries); =20 if (htab_is_lru(htab)) - htab->key_offset =3D sizeof(struct htab_elem_lru); + htab->key_offset =3D offsetof(struct htab_elem_lru, data) + 8; else if (percpu && !prealloc) - htab->key_offset =3D sizeof(struct htab_elem_pcpu); + htab->key_offset =3D offsetof(struct htab_elem_pcpu, data) + + (htab_has_hash(htab) ? 8 : 0); else - htab->key_offset =3D sizeof(struct htab_elem); + htab->key_offset =3D offsetof(struct htab_elem, data) + + (htab_has_hash(htab) ? 8 : 0); =20 htab->elem_size =3D htab->key_offset + round_up(htab->map.key_size, 8); if (percpu) @@ -761,10 +776,16 @@ static struct htab_elem *lookup_elem_raw(struct bpf_h= tab *htab, struct hlist_nulls_node *n; struct htab_elem *l; =20 - hlist_nulls_for_each_entry_rcu(l, n, head, node.hash_node) - if (htab_elem_hash(htab, l) =3D=3D hash && - !memcmp(htab_elem_key(htab, l), key, key_size)) - return l; + if (htab_has_hash(htab)) { + hlist_nulls_for_each_entry_rcu(l, n, head, node.hash_node) + if (htab_elem_hash(htab, l) =3D=3D hash && + !memcmp(htab_elem_key(htab, l), key, key_size)) + return l; + } else { + hlist_nulls_for_each_entry_rcu(l, n, head, node.hash_node) + if (!memcmp(htab_elem_key(htab, l), key, key_size)) + return l; + } =20 return NULL; } @@ -782,10 +803,16 @@ static struct htab_elem *lookup_nulls_elem_raw(struct= bpf_htab *htab, struct htab_elem *l; =20 again: - hlist_nulls_for_each_entry_rcu(l, n, head, node.hash_node) - if (htab_elem_hash(htab, l) =3D=3D hash && - !memcmp(htab_elem_key(htab, l), key, key_size)) - return l; + if (htab_has_hash(htab)) { + hlist_nulls_for_each_entry_rcu(l, n, head, node.hash_node) + if (htab_elem_hash(htab, l) =3D=3D hash && + !memcmp(htab_elem_key(htab, l), key, key_size)) + return l; + } else { + hlist_nulls_for_each_entry_rcu(l, n, head, node.hash_node) + if (!memcmp(htab_elem_key(htab, l), key, key_size)) + return l; + } =20 if (unlikely(get_nulls_value(n) !=3D (hash & (n_buckets - 1)))) goto again; @@ -3191,7 +3218,7 @@ static int rhtab_map_check_btf(struct bpf_map *map, c= onst struct btf *btf, { struct bpf_rhtab *rhtab =3D container_of(map, struct bpf_rhtab, map); =20 - return bpf_ma_set_dtor(map, &rhtab->ma, rhtab_mem_dtor); + return bpf_ma_set_dtor(map, &rhtab->ma, rhtab_mem_dtor, offsetof(struct r= htab_elem, data)); } =20 static void rhtab_map_free_internal_structs(struct bpf_map *map) diff --git a/tools/testing/selftests/bpf/progs/map_ptr_kern.c b/tools/testi= ng/selftests/bpf/progs/map_ptr_kern.c index 373c8d17ea55..6bd4cb68c20c 100644 --- a/tools/testing/selftests/bpf/progs/map_ptr_kern.c +++ b/tools/testing/selftests/bpf/progs/map_ptr_kern.c @@ -114,7 +114,7 @@ static inline int check_hash(void) VERIFY(check_default_noinline(&hash->map, map)); =20 VERIFY(hash->n_buckets =3D=3D MAX_ENTRIES); - VERIFY(hash->elem_size =3D=3D 64); + VERIFY(hash->elem_size =3D=3D 32); =20 VERIFY(hash->count.counter =3D=3D 0); VERIFY(bpf_map_sum_elem_count(map) =3D=3D 0); --=20 2.55.0.508.g3f0d502094-goog