From: Sasha Levin <sashal@kernel.org>
To: joel.granados@kernel.org, akpm@linux-foundation.org,
linux@weissschuh.net
Cc: linux-kernel@vger.kernel.org, Sasha Levin <sashal@kernel.org>
Subject: [PATCH] ucount: Convert ucounts hashtable to use hashtable.h API
Date: Wed, 19 Mar 2025 14:42:45 -0400 [thread overview]
Message-ID: <20250319184245.3052913-1-sashal@kernel.org> (raw)
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@kernel.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
reply other threads:[~2025-03-19 18:42 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250319184245.3052913-1-sashal@kernel.org \
--to=sashal@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=joel.granados@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@weissschuh.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®