lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [day] [month] [year] [list]
Message-Id: <20250319184245.3052913-1-sashal@kernel.org>
Date: Wed, 19 Mar 2025 14:42:45 -0400
From: Sasha Levin <sashal@...nel.org>
To: joel.granados@...nel.org,
	akpm@...ux-foundation.org,
	linux@...ssschuh.net
Cc: linux-kernel@...r.kernel.org,
	Sasha Levin <sashal@...nel.org>
Subject: [PATCH] ucount: Convert ucounts hashtable to use hashtable.h API

Convert the manual hashtable implementation in kernel/ucount.c to use
the standard hashtable.h interface. This simplifies the code by:

- Replacing custom hlist_head array with DEFINE_HASHTABLE
- Removing ucounts_hashfn and ucounts_hashentry macros
- Using hash_add, hash_del, and hash_for_each_possible instead of manual
  hlist operations

This conversion makes the code more maintainable by using the standard
hashtable implementation while preserving the same functionality.

Signed-off-by: Sasha Levin <sashal@...nel.org>
---
 kernel/ucount.c | 74 +++++++++++++++++++++----------------------------
 1 file changed, 31 insertions(+), 43 deletions(-)

diff --git a/kernel/ucount.c b/kernel/ucount.c
index 86c5f1c0bad90..db4c1a8534b33 100644
--- a/kernel/ucount.c
+++ b/kernel/ucount.c
@@ -7,6 +7,7 @@
 #include <linux/hash.h>
 #include <linux/kmemleak.h>
 #include <linux/user_namespace.h>
+#include <linux/hashtable.h>
 
 struct ucounts init_ucounts = {
 	.ns    = &init_user_ns,
@@ -15,16 +16,9 @@ struct ucounts init_ucounts = {
 };
 
 #define UCOUNTS_HASHTABLE_BITS 10
-static struct hlist_head ucounts_hashtable[(1 << UCOUNTS_HASHTABLE_BITS)];
+static DEFINE_HASHTABLE(ucounts_hashtable, UCOUNTS_HASHTABLE_BITS);
 static DEFINE_SPINLOCK(ucounts_lock);
 
-#define ucounts_hashfn(ns, uid)						\
-	hash_long((unsigned long)__kuid_val(uid) + (unsigned long)(ns), \
-		  UCOUNTS_HASHTABLE_BITS)
-#define ucounts_hashentry(ns, uid)	\
-	(ucounts_hashtable + ucounts_hashfn(ns, uid))
-
-
 #ifdef CONFIG_SYSCTL
 static struct ctl_table_set *
 set_lookup(struct ctl_table_root *root)
@@ -127,22 +121,13 @@ void retire_userns_sysctls(struct user_namespace *ns)
 #endif
 }
 
-static struct ucounts *find_ucounts(struct user_namespace *ns, kuid_t uid, struct hlist_head *hashent)
-{
-	struct ucounts *ucounts;
-
-	hlist_for_each_entry(ucounts, hashent, node) {
-		if (uid_eq(ucounts->uid, uid) && (ucounts->ns == ns))
-			return ucounts;
-	}
-	return NULL;
-}
-
 static void hlist_add_ucounts(struct ucounts *ucounts)
 {
-	struct hlist_head *hashent = ucounts_hashentry(ucounts->ns, ucounts->uid);
+	unsigned long hash_key = (unsigned long)__kuid_val(ucounts->uid) +
+				(unsigned long)(ucounts->ns);
+
 	spin_lock_irq(&ucounts_lock);
-	hlist_add_head(&ucounts->node, hashent);
+	hash_add(ucounts_hashtable, &ucounts->node, hash_key);
 	spin_unlock_irq(&ucounts_lock);
 }
 
@@ -163,33 +148,36 @@ struct ucounts *get_ucounts(struct ucounts *ucounts)
 
 struct ucounts *alloc_ucounts(struct user_namespace *ns, kuid_t uid)
 {
-	struct hlist_head *hashent = ucounts_hashentry(ns, uid);
+	unsigned long hash_key = (unsigned long)__kuid_val(uid) + (unsigned long)(ns);
 	bool wrapped;
 	struct ucounts *ucounts, *new = NULL;
 
 	spin_lock_irq(&ucounts_lock);
-	ucounts = find_ucounts(ns, uid, hashent);
-	if (!ucounts) {
-		spin_unlock_irq(&ucounts_lock);
-
-		new = kzalloc(sizeof(*new), GFP_KERNEL);
-		if (!new)
-			return NULL;
-
-		new->ns = ns;
-		new->uid = uid;
-		atomic_set(&new->count, 1);
-
-		spin_lock_irq(&ucounts_lock);
-		ucounts = find_ucounts(ns, uid, hashent);
-		if (!ucounts) {
-			hlist_add_head(&new->node, hashent);
-			get_user_ns(new->ns);
-			spin_unlock_irq(&ucounts_lock);
-			return new;
-		}
+	hash_for_each_possible(ucounts_hashtable, ucounts, node, hash_key) {
+		if (uid_eq(ucounts->uid, uid) && (ucounts->ns == ns))
+			goto found;
+	}
+	spin_unlock_irq(&ucounts_lock);
+
+	new = kzalloc(sizeof(*new), GFP_KERNEL);
+	if (!new)
+		return NULL;
+
+	new->ns = ns;
+	new->uid = uid;
+	atomic_set(&new->count, 1);
+
+	spin_lock_irq(&ucounts_lock);
+	hash_for_each_possible(ucounts_hashtable, ucounts, node, hash_key) {
+		if (uid_eq(ucounts->uid, uid) && (ucounts->ns == ns))
+			goto found;
 	}
+	hash_add(ucounts_hashtable, &new->node, hash_key);
+	get_user_ns(new->ns);
+	spin_unlock_irq(&ucounts_lock);
+	return new;
 
+found:
 	wrapped = !get_ucounts_or_wrap(ucounts);
 	spin_unlock_irq(&ucounts_lock);
 	kfree(new);
@@ -205,7 +193,7 @@ void put_ucounts(struct ucounts *ucounts)
 	unsigned long flags;
 
 	if (atomic_dec_and_lock_irqsave(&ucounts->count, &ucounts_lock, flags)) {
-		hlist_del_init(&ucounts->node);
+		hash_del(&ucounts->node);
 		spin_unlock_irqrestore(&ucounts_lock, flags);
 		put_user_ns(ucounts->ns);
 		kfree(ucounts);
-- 
2.39.5


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ