From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f47.google.com (mail-wm1-f47.google.com [209.85.128.47]) (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 09BE4337105 for ; Thu, 23 Jul 2026 14:12:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.47 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784815940; cv=none; b=hlPlpZQqaRUc0rwhw6P/Av4Ne6bTf5WL+k/u2f31V6icdD1//HmQ3+Xm/cOkJccB9XGAfaQ0h0RJxNzOGUpsyQqxzDeztVbrsrVvF9Cl7DtT4cD+GKXdkSa2fp5atP8IfF+ZZadejpHRTIohf1dZKpgnt05TDii4IdOhn59JHRI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784815940; c=relaxed/simple; bh=GZTfIQvF0oPDZeyCs+MdWt748jfjlzSCN/KAcJI3Fic=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=XRD8XivhODfO8pvvN+C51gfUuQxIFi1NoIkZmfWM0As073kjHdhq9RLeKygQdSCGmp+GcoqbU9j4Xh/BAZHprakPDyKav/ETKDoSNHCEXtljziLngksnzhfZs6DNjUFqnO5uJhwtU4w3+R5mjhXHWU+gaE97em0gKaet/EyAnbQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=rAldTSC0; arc=none smtp.client-ip=209.85.128.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="rAldTSC0" Received: by mail-wm1-f47.google.com with SMTP id 5b1f17b1804b1-4955aa106b1so6708205e9.0 for ; Thu, 23 Jul 2026 07:12:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1784815923; x=1785420723; darn=vger.kernel.org; h=content-transfer-encoding:content-type:in-reply-to:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:from:to:cc:subject:date:message-id:reply-to :content-type; bh=1XQefTGt3GGOzzOfzTSkYQpSOiy/K/m4PaZ+Y/MEVQw=; b=rAldTSC0foVs88bZHvIP4MTDHfx7sGd70VfbLTtiPoyLJAOb2fH2jNzRmvPTCYdh9F xi0iZpFUfGITlymlhkAzv9xIGqNemuY3zK+iXSQs/fJgaqpOOgwS9ceuuK/rw+MivMlr L2OPQOCCUb+ELJod83aMKdjsKw09wxRM5+TxN+zFaZrrMbBvNVTZ1+LclvuyytS4ocJE Znl2pbXY68q0IAPZluBwy6RA7Pha2WDmwOYBNZ/aZMSO6yTiaV8ZLaP6+etAYZUAu/71 4+YUI+P5C70/FD9CEkHJYTdJY8RCCZufpxIFZ5OurQ/DsZD40R7xmS+lAxZjKpSbTkNI xJbA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784815923; x=1785420723; h=content-transfer-encoding:content-type:in-reply-to:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=1XQefTGt3GGOzzOfzTSkYQpSOiy/K/m4PaZ+Y/MEVQw=; b=sYr1F2yPq/cA9y4NlLDP6LoL2O8Zq+Xn/M8auLJ66tsDaog4QgcP8fTZxY9YA5XVys Odkgpjsnj1D6Xqp8+YhVz/NrM3VB126LYQ4Iy5rTW7OOtkF7smMiC7rsPoZc2v9YXN/b 4REtygWtFkfBQBjYuwO+SUmjPcQbJLv5pzk9eQzPxtrT0wJEb52G/Qd2sp6Sre6y+gf9 HApIKbIMPbkHbhTVZwEiobbTJJdjLIYfYU7l+UNNVEPHuV8ZQGSc7VyMEQfobc8xMDzb zEsuDW697YqGqrXBP0px9+1tm6zjnqiSEW3VXfls9/evr2Fb19aLeyvhZQyG/OazIUvB OPYA== X-Forwarded-Encrypted: i=1; AHgh+Roc2LabSXWSv+QOfKTo1y6Lt95V4dXWfiRwdzCspbhPqbTrgxU3HWX5RF8JdWIJg+lOvb+SeIy2EL2v0+U=@vger.kernel.org X-Gm-Message-State: AOJu0YzGPHmfWD625JJv1pV0l0NmA1jp3BVWSn5IGltiBlInLhJQxMcx ktRCy1L0LHdo5t6xhsO+dCGUoAOTsBYPkT1W1jQVxbuPuxNbvgmdl71s X-Gm-Gg: AR+sD12BPdJhtrXhhsJwk8Jfi9onYz15+0Yr6bqqyEt2hHDkWbkudkXIlqJ+hZxi0F0 2z5DjFM6GyFlAe9xufkxTyoFzSMPkaCey3MFrpepq3WQGfp/t7uyA1p1XAiq3SBDG2yJOIiVHmb u16oO3G00VV8atzKEyfVwWYEnYdIg8nzWTRTGoXe1mCXqVrNvH5+ysGlNjq8ZjgqHlw3BdtstPZ x4YzNegNdsCr8sCkr/32m3gD30TQw7pvzGYADX7p1QvfmZrqATQKyxPc7w/5j4Wb26rtFuk8onU A3yYtA6JZ5aRVVF2fa8xe5T8T93fDz+hugLiXsYbjA5EkdE3Pblv/wVcMCq9KXaHwaGy5Ush8F9 0jqvcjCY72q6jmvn7u6Dtj9NK8uP6IF+GgB++DCMsF7mfFomE01CkswMFThs2VHoaw4bgfp3//6 LJ+uznEpY/Zdco9LbSugeo3e6RmjREysxGVW/8CVUaJM9jVmkHEQ== X-Received: by 2002:a05:600c:4ed2:b0:495:3f26:97fb with SMTP id 5b1f17b1804b1-49573cba103mr36502125e9.3.1784815923172; Thu, 23 Jul 2026 07:12:03 -0700 (PDT) Received: from ?IPV6:2a03:83e0:1126:4:b876:739d:6f1f:1b78? ([2620:10d:c092:500::4:c44e]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4956a4aae16sm154682445e9.0.2026.07.23.07.12.02 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 23 Jul 2026 07:12:02 -0700 (PDT) Message-ID: <0fe51955-39f7-48ca-8ce5-bc842868d53e@gmail.com> Date: Thu, 23 Jul 2026 15:12:01 +0100 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 2/2] bpf: htab: Reduce elem_size by 8 bytes for small key sizes To: "T.J. Mercier" , 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 Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org References: <20260722203801.1854941-1-tjmercier@google.com> <20260722203801.1854941-3-tjmercier@google.com> Content-Language: en-US From: Mykyta Yatsenko In-Reply-To: <20260722203801.1854941-3-tjmercier@google.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 7/22/26 9:38 PM, T.J. Mercier wrote: > For standard and PCPU (non-LRU) hash maps with small key sizes (<= 8 > bytes), comparing keys requires only an 8 byte compare. 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 | 35 +++++++++++++------ > .../selftests/bpf/progs/map_ptr_kern.c | 2 +- > 2 files changed, 25 insertions(+), 12 deletions(-) > > diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c > index 57729c3dff3d..b8c30d402958 100644 > --- a/kernel/bpf/hashtab.c > +++ b/kernel/bpf/hashtab.c ... > > hlist_nulls_for_each_entry_rcu(l, n, head, hash_node) > - if (htab_elem_hash(htab, l) == hash && > + if ((!htab_has_hash(htab) || htab_elem_hash(htab, l) == hash) && > !memcmp(htab_elem_key(htab, l), key, key_size)) > return l; > > @@ -812,7 +825,7 @@ static struct htab_elem *lookup_nulls_elem_raw(struct bpf_htab *htab, > > again: > hlist_nulls_for_each_entry_rcu(l, n, head, hash_node) > - if (htab_elem_hash(htab, l) == hash && > + if ((!htab_has_hash(htab) || htab_elem_hash(htab, l) == hash) && htab_has_hash() is constant for any concrete map, maybe we can store it in the struct bpf_htab (it has few holes) and we already describe layout there, or at least not calculate on each element in the bucket in the loop. > !memcmp(htab_elem_key(htab, l), key, key_size)) > return l; > > @@ -3219,7 +3232,7 @@ static int rhtab_map_check_btf(struct bpf_map *map, const struct btf *btf, > { > struct bpf_rhtab *rhtab = container_of(map, struct bpf_rhtab, map); > > - return bpf_ma_set_dtor(map, &rhtab->ma, rhtab_mem_dtor); > + return bpf_ma_set_dtor(map, &rhtab->ma, rhtab_mem_dtor, offsetof(struct rhtab_elem, data)); > } > > 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/testing/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)); > > VERIFY(hash->n_buckets == MAX_ENTRIES); > - VERIFY(hash->elem_size == 64); > + VERIFY(hash->elem_size == 32); > > VERIFY(hash->count.counter == 0); > VERIFY(bpf_map_sum_elem_count(map) == 0);