From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.49 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796192956; b=D5fVFf0Sc2b/VXTkZhEFXVnMzVoRxRmPckcKAmyC1jKdr+uyTYNsLRf0vH/mQbwsfCrw Hqr7hZ87OaUqURK5G+6Sx2tE4b2lwzMpn1RQlF4kyPHKLsUzfZqiwWnJ+4HxTx/C09uwd iEupm9VAXbkNuUAfU94nPFphVNXAhy8NSv7Pe0MhUz4UhAZm/HqvVOLTDdxRopFotwDce R3DHODxER4/B38U8e5Rqyid6W/3ysXWP70Kqv8iJQOs1suBAaFIWC4y7+biIdGCMGxl0V ybMH2HjrRbmupBg8skwTbtWeHSKvb/QaaJywzO8l3pznSbwOEQcOlpkiE8txn9DYh9A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796192956; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: Message-ID:Date:MIME-Version:User-Agent:Subject:To:Cc:References: Content-Language:From:In-Reply-To:Content-Type: Content-Transfer-Encoding; bh=k+vu/O2tFqzsL3Y6hnXPIICeg1JZ6U4dCjgUfLXHYmU=; b=axLU1WZi5lCVHWH76X9ybXMbBnxY+0iz0zHIqFNhd5cANAvI72wGiXYJQG8toCNtPdAr In3ajAw3oIfjQFZO3yq7gF942WriQWvldtFMGAS7dy1zhO0trhwAA0JFfU2Q47HI7ObR0 JGjl7XMz65ZGAJcLmLhFOnl50E5u4J66t4igyXOagaEkwh9Pf3aUzdxr66oaJi5g1HJMk G0D6Ej0Q4EEf6X3r8aY/gcp+FUXplgHrljuGNxltOFOYcgLVxf4QrSQH9DZQ3ovsyJOQ2 3qXmUZlEZjyBcoUfLrxpziKSrU7alCtt/KTzSAKEMbt+46MkaWsXAthEXYEHY+OURng== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=K6nNiyXg; arc=none smtp.remote-ip=209.85.128.49 Received: from mail-wm1-f49.google.com (mail-wm1-f49.google.com [209.85.128.49]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 339D3606A334 for ; Wed, 2 Dec 2026 06:29:16 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 339D3606A334 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.a=rsa-sha256 header.s=20260707 header.b=K6nNiyXg Received: by mail-wm1-f49.google.com with SMTP id 5b1f17b1804b1-4a2153c8525so101360205e9.3 for ; Tue, 01 Dec 2026 22:29:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796192955; x=1796797755; 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=k+vu/O2tFqzsL3Y6hnXPIICeg1JZ6U4dCjgUfLXHYmU=; b=K6nNiyXgtGmii+PuXBM7HbERUVdthUBoFO91i/ttDT/Zcv605GcwVT51HVwBbhRygV 02lN/oaZxduGVzj872MGVThoj6DcclTxnwQhL8utRYKmOPnwHmUpveEFh7za25IFyOtv H/HwHFg4jO6gTZnPC9FYtio17Cxb8iT0evdR3ehy3Fe2sLF13kpSA52pyQD3dYIAuVQD 6rM+dzLNcAkxaBvx5ARAOSxnpGGmU40VxvIJ677raLOJrjAkHAFWGNkB7YMal2g0IU5O cYsN9Uyhu3DZ+sugexh//SXkiHysdPtdIPJMo25fpA/u1DKs/URO+/UNQRowLWu+msvh KOsw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796192955; x=1796797755; 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=k+vu/O2tFqzsL3Y6hnXPIICeg1JZ6U4dCjgUfLXHYmU=; b=uAqhNgZSO1Th1u5Yo37cZyM/+3rebgBOX3l3iMEDgGuTxLj90putFLCUxdkiDCuZaS EE9PTmbZN4uzZn4DugteG10P+nVrOm9f7Aoq6VjdbU2lp+77eQbizrPjGymLPhnUFLdl aj8zhwU/AGdcOGGpBPxoPomQHfqfJHHH6isHGxg/LRlc5huYPFcKCS+HGBiCANQA128V sblD8yoDWiWKb2wq1u1Cr3Seqp0uG4Uk5sj3W6dkdcC4UPiylD0TKdHzIz1EJcVMoFaC TR0K14w2Vs6kYrUtgue16HWIlnmyhlUP0NAk8cmPitfMuMB8/guRYqqInBTFHqp2gRul 5KyA== X-Forwarded-Encrypted: i=1; AL+hErEfW0fLpJ86DyBHoHrBy90+K/ooLsusvDE2tKjBkAvgkL5Dk1YJf01atSxcm9pTGuzLn8E=@vger.kernel.org X-Gm-Message-State: APuU4Z9iCWXa9511zpZ1dL8Cyli5o72tl41On3QfGKjUz8vVyXYYFgeW ToJhbkVyxqObf6tJFTz/cSzswNndi1B+geFuCtQdfZL/fcdVyXdxjA10 X-Gm-Gg: AZFdTZerpjEORdDSowYuanufuo4CSaP3t+66/k14eii6iWg5gpusNKNwTZr5+medCnf uihZ2XQPEZsmtPqX/jtP7H2bjBPVB/bGdc4altayIaxpEj/WeyncFfH6WGGz3QmoATkFbsu2mEe YriZ7bmdbdpcQ/zvTCaUypU4HkIbxeB+urm2N9beUTNK85BiKshX5s0UXibDZ1jmdoCU5w46A01 hJcz/NA3BMv7m6ddQVpU18xxPnQtBaM+miSZUQctHPqAH0agmDcBoCCshFEzGorUp8HZiInHCwu JJlExsiCylDOIj4ZNa3xso6psqHMcA3Q0mT/IM/ShzTQqZS2QUe0HqZcwT7IYEBhEjqFCkpC11v eSjWSjm0EELzuib3N2nM9rrf7Ct/je5K6/2CzNKOzsowcAxVCK0UvL08uRtkBVOZYSzhfRvcSPN OuQ5YqdtK5p7/ckdRkRgnE3Tzw23x0wpnSxCURcjfXAU5xOPOk/VrQdQPoWYR9EKhgwuf7GM66z 7PZ1BZu1CWkB13oE1XajpGaz6Zdt0EBUXj6ESS6+obitaiMCLTmjnfB8dCXeo9wwh3ArAQj9uMM wL8ag296sNdjHH54NuLgiFInlUz9b5kqR+M8m9TWBDB8xDZPAYWcjBq3NI/wjJuuVzg= X-Received: by 2002:a05:600c:c84:b0:4a8:9a2f:d27 with SMTP id 5b1f17b1804b1-4bb8881f47emr802709295e9.25.1796192954367; Tue, 01 Dec 2026 22:29:14 -0800 (PST) Received: from [172.20.10.4] (92.40.176.68.threembb.co.uk. [92.40.176.68]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bb906640a2sm659013795e9.0.2026.12.01.22.29.11 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 01 Dec 2026 22:29:13 -0800 (PST) Message-ID: Date: Wed, 2 Dec 2026 06:29:07 +0000 X-Mailing-List: bpf@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] bpf: Mix the seed into 8-byte resizable hashtab keys before folding To: Hao Peng , Alexei Starovoitov Cc: ast@kernel.org, andrii@kernel.org, bpf@vger.kernel.org, yatsenko@meta.com References: <20261130140332.80373-1-flyingpeng@tencent.com> Content-Language: en-US From: Mykyta Yatsenko In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On 12/2/26 4:09 AM, Hao Peng wrote: > On Tue, Dec 1, 2026 at 11:17 AM Alexei Starovoitov > wrote: >> >> On Mon Nov 30, 2026 at 2:03 PM UTC, Peng Hao wrote: >>> rhtab_hashfn_long() folds the two halves of the key together and only >>> then XORs in the seed: >>> >>> return (u32)(k ^ (k >> 32)) ^ seed; >>> >>> Two keys that fold to the same value therefore collide under every seed, >>> so rhashtable's defence against long chains, a rehash with a new seed, >>> can't separate them. Keys with equal halves all hash to the seed itself; >>> bpf_get_current_pid_tgid() of a single-threaded task is such a key. >>> >>> Inserting 20000 keys of the form (i << 32 | i) into a >>> BPF_MAP_TYPE_RHASH map with max_entries 40000 on arm64: >>> >>> random n=20000 insert 0.285s lookup 0.154s (7.7 us/lookup) insert failures 0 >>> collide n=20000 insert 3.896s lookup 2.460s (123.0 us/lookup) insert failures 18447 [Device or resource busy x18447] >>> >>> Almost all inserts fail with -EBUSY once the chain is too long and the >>> rehash doesn't help, and lookups of the keys that did go in degrade to a >>> linear scan of one bucket. >>> >>> Use jhash_2words(), as htab_map_hash() does for 8-byte keys, so that the >>> seed affects every bit of the result. >>> >>> Fixes: 9dcbb5045fe5 ("bpf: Optimize word-sized keys for resizable hashtable") >>> Signed-off-by: Peng Hao >>> --- >>> kernel/bpf/hashtab.c | 2 +- >>> 1 file changed, 1 insertion(+), 1 deletion(-) >>> >>> diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c >>> index a2d0bfd4c3ec..6cdd8e1dc2bd 100644 >>> --- a/kernel/bpf/hashtab.c >>> +++ b/kernel/bpf/hashtab.c >>> @@ -3029,7 +3029,7 @@ static __always_inline u32 rhtab_hashfn_long(const void *data, u32 len, u32 seed >>> { >>> u64 k = *(const unsigned long *)data; >>> >>> - return (u32)(k ^ (k >> 32)) ^ seed; >>> + return jhash_2words((u32)k, (u32)(k >> 32), seed); >> >> Looks like real issue, but please benchmark it first and cc Mykyta. >> > Benchmarked on a Kunpeng 920 (arm64) guest, non-debug kernels that only > differ in rhtab_hashfn_long(), bpf-rhashmap-lookup --key_size 8, single > producer, 3 alternating boots x 2 runs per kernel, M lookups/sec: > > keys | max | nr | fold (current) | jhash | delta > ------+-----+------+------------------+------------------+------- > seq | 1K | 750 | 104.12 +- 3.48 | 44.70 +- 0.55 | -57.1% > seq | 1K | 1K | 94.17 +- 2.10 | 48.18 +- 1.67 | -48.8% > seq | 1M | 750K | 81.15 +- 3.34 | 18.42 +- 0.33 | -77.3% > seq | 1M | 1M | 78.18 +- 7.35 | 18.69 +- 0.70 | -76.1% > rand | 1K | 750 | 54.00 +- 1.24 | 42.36 +- 1.13 | -21.6% > rand | 1K | 1K | 60.79 +- 0.66 | 47.17 +- 0.90 | -22.4% > rand | 1M | 750K | 20.12 +- 0.19 | 17.62 +- 0.15 | -12.4% > rand | 1M | 1M | 19.39 +- 0.12 | 17.41 +- 0.10 | -10.2% > My results: jhash_2words() makes lookups with 8-byte keys slower: -35% at 1K and -74% to -77% at 1M. We can use another simple hash function to protect from equal halves hashing to same values, for example: static __always_inline u32 rhtab_hashfn_long(const void *data, u32 len, u32 seed) { u64 x = *(const unsigned long *)data ^ seed; return (x ^ (x >> 33)) * GOLDEN_RATIO_64 >> 32; } Of course it does not protect from malicious uses. > seq = the benchmark's default keys (low word i + 1, high word constant) > rand = random 8-byte keys (getrandom()) > > The "seq" rows mostly measure the key pattern rather than the hash: XOR > folding maps the sequential keys to consecutive buckets, so a 1M lookup > sweep walks the bucket array in order. With random keys the current fold > drops to the same ~19 M/s at 1M that jhash gets with sequential keys. > With random keys jhash_2words() costs about 5ns per lookup: 16.4 vs 21.2 > ns at 1K entries, 51.6 vs 57.4 ns at 1M. > > On the other side, inserting 20000 keys of the form (i << 32 | i), e.g. > bpf_get_current_pid_tgid() of single-threaded tasks, into a map with > max_entries 40000: > > current fold: 19845 of 20000 inserts fail with -EBUSY, 0.83 us/lookup > jhash: 0 failures, 0.33 us/lookup > > >> pw-bot: cr >