From nobody Thu Dec 18 20:19:30 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 75FB71A2396 for ; Wed, 18 Dec 2024 11:16:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734520593; cv=none; b=PJq0Z1oYOANuBHB9oc7d012wU0Ws7u3jr54ETXg1O/kcIrneP3VjWRCy50bi+GeHY1ax0w6IrBA7n4OraK+TvedN8LwqxDj3iLvtnu1k6C8z+B1VOp40rIU0Jtd2zmvfbuVydQeiZGqKJPKyX2Zhos/gRtgaKoSP7WeW85OjruQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734520593; c=relaxed/simple; bh=KUtNoG6W1ZSSkFBnPeDDIlHEmrhXr7E6zS1pVpjwhkQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Ju/mpSlrPYDU9/0qNHiy7NyWQCeZD9K1LhMiqjNIu65SgkpDrzk7L8WFg9TVj13JYCrZIeeaShLM1G3CVGzmYqdq0Mdzp7JVUYJv4daP5TFAFtiEbU/625xxE1HP27aAKOBOBlfzbZvSxOqhgAPMQVQXGUowVMot6Ks3tMXwihY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=3/DfEiuV; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=JGTGfDcN; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="3/DfEiuV"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="JGTGfDcN" From: Sebastian Andrzej Siewior DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1734520589; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=2NFPMwUTOUxStVZVcnmN4MuCLIQZ4Qw1x9ywgplHsew=; b=3/DfEiuVfvO04rbcbDZDReFNuqFzn704FYTab+I7D/TVDIOkE5ttYEvEfCAt8Dmte8gVy+ sbCb+hEunVqhzZY094IKw4xOE8tiFw5MOSnJGs+uijt5JmO2puh7MQ6kTqfnX/1s70tcIo 6fvBcpYz/RQfuonLnfN7XRRnTjGwwdZIECmYlFvBOZdRsXxvA9NMSeoCoaqimBcxFYes8/ Xv1a3pZSdVL9Hpze6r3zJZPgZjroHUz9uyo2ldp7dsjILAEQuQDvT/H4dqI6ChYceuuaGm 3m5AYVp31r0wtAbf8dT9ESO36h4TnLBGTaBpiCiIkGcOXWDHbU8GHNSx/wpUng== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1734520589; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=2NFPMwUTOUxStVZVcnmN4MuCLIQZ4Qw1x9ywgplHsew=; b=JGTGfDcNopj5bXPc6F//W1sd9f+xse3foUMZCKkHfBLJx+gmOY4EuvP43Ull1bMnpGNj/D PFOvrsaeOccoTcAQ== To: linux-kernel@vger.kernel.org Cc: =?UTF-8?q?Andr=C3=A9=20Almeida?= , Darren Hart , Davidlohr Bueso , Ingo Molnar , Juri Lelli , Peter Zijlstra , Thomas Gleixner , Valentin Schneider , Waiman Long , Sebastian Andrzej Siewior Subject: [PATCH v6 12/15] futex: Use a hashmask instead of hashsize. Date: Wed, 18 Dec 2024 12:09:50 +0100 Message-ID: <20241218111618.268028-13-bigeasy@linutronix.de> In-Reply-To: <20241218111618.268028-1-bigeasy@linutronix.de> References: <20241218111618.268028-1-bigeasy@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The global hash uses futex_hashsize to save the amount of the hash buckets that have been allocated during system boot. On each futex_hash() invocation this number is substracted by one to get the mask. This can be optimized by saving directly the mask avoiding the substraction on each futex_hash() invocation. Rename futex_hashsize to futex_hashmask and save the mask of the allocated hash map. Signed-off-by: Sebastian Andrzej Siewior --- kernel/futex/core.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/kernel/futex/core.c b/kernel/futex/core.c index 95a177f0c5d68..0fdbf691ec95b 100644 --- a/kernel/futex/core.c +++ b/kernel/futex/core.c @@ -52,10 +52,10 @@ */ static struct { struct futex_hash_bucket *queues; - unsigned long hashsize; + unsigned long hashmask; } __futex_data __read_mostly __aligned(2*sizeof(long)); #define futex_queues (__futex_data.queues) -#define futex_hashsize (__futex_data.hashsize) +#define futex_hashmask (__futex_data.hashmask) =20 struct futex_hash_bucket_private { rcuref_t users; @@ -273,7 +273,7 @@ struct futex_hash_bucket *futex_hash(union futex_key *k= ey) hash =3D jhash2((u32 *)key, offsetof(typeof(*key), both.offset) / 4, key->both.offset); - return &futex_queues[hash & (futex_hashsize - 1)]; + return &futex_queues[hash & futex_hashmask]; } =20 void futex_hash_put(struct futex_hash_bucket *hb) @@ -1370,8 +1370,8 @@ static int futex_hash_allocate(unsigned int hash_slot= s) hash_slots =3D 16; if (hash_slots < 2) hash_slots =3D 2; - if (hash_slots > futex_hashsize) - hash_slots =3D futex_hashsize; + if (hash_slots > futex_hashmask + 1) + hash_slots =3D futex_hashmask + 1; if (!is_power_of_2(hash_slots)) hash_slots =3D rounddown_pow_of_two(hash_slots); =20 @@ -1427,7 +1427,7 @@ int futex_hash_allocate_default(void) =20 buckets =3D roundup_pow_of_two(4 * threads); buckets =3D max(buckets, 16); - buckets =3D min(buckets, futex_hashsize); + buckets =3D min(buckets, futex_hashmask + 1); if (current_buckets >=3D buckets) return 0; =20 @@ -1467,24 +1467,25 @@ int futex_hash_prctl(unsigned long arg2, unsigned l= ong arg3) =20 static int __init futex_init(void) { + unsigned long i, hashsize; unsigned int futex_shift; - unsigned long i; =20 #ifdef CONFIG_BASE_SMALL - futex_hashsize =3D 16; + hashsize =3D 16; #else - futex_hashsize =3D roundup_pow_of_two(256 * num_possible_cpus()); + hashsize =3D roundup_pow_of_two(256 * num_possible_cpus()); #endif =20 futex_queues =3D alloc_large_system_hash("futex", sizeof(*futex_queues), - futex_hashsize, 0, 0, + hashsize, 0, 0, &futex_shift, NULL, - futex_hashsize, futex_hashsize); - futex_hashsize =3D 1UL << futex_shift; + hashsize, hashsize); + hashsize =3D 1UL << futex_shift; =20 - for (i =3D 0; i < futex_hashsize; i++) + for (i =3D 0; i < hashsize; i++) futex_hash_bucket_init(&futex_queues[i], 0); =20 + futex_hashmask =3D hashsize - 1; return 0; } core_initcall(futex_init); --=20 2.45.2