From c10e07398ef04d754a957430322b17f1acdbcddd Mon Sep 17 00:00:00 2001 From: Randy Dunlap Date: Tue, 24 Aug 2021 10:00:00 +1000 Subject: [PATCH] fs-epoll-use-a-per-cpu-counter-for-users-watches-count-fix fix build errors in kernel/user.c when CONFIG_EPOLL=n Also fix typo: "cpunter" -"counter" in a panic message. [npiggin@gmail.com: move ifdefs into wrapper functions, slightly improve panic message] Link: https://lkml.kernel.org/r/1628051945.fens3r99ox.astroid@bobo.none Fixes: e75b89477811 ("fs/epoll: use a per-cpu counter for user's watches count") Signed-off-by: Randy Dunlap Signed-off-by: Nicholas Piggin Cc: Mark Brown Cc: Guenter Roeck Cc: Stephen Rothwell Signed-off-by: Andrew Morton Signed-off-by: Stephen Rothwell --- kernel/user.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/kernel/user.c b/kernel/user.c index a2673f940506e..57d53568cb33c 100644 --- a/kernel/user.c +++ b/kernel/user.c @@ -129,6 +129,21 @@ static struct user_struct *uid_hash_find(kuid_t uid, struct hlist_head *hashent) return NULL; } +static int user_epoll_alloc(struct user_struct *up) +{ +#ifdef CONFIG_EPOLL + return percpu_counter_init(&up->epoll_watches, 0, GFP_KERNEL); +#endif + return 0; +} + +static void user_epoll_free(struct user_struct *up) +{ +#ifdef CONFIG_EPOLL + percpu_counter_destroy(&up->epoll_watches); +#endif +} + /* IRQs are disabled and uidhash_lock is held upon function entry. * IRQ state (as stored in flags) is restored and uidhash_lock released * upon function exit. @@ -138,7 +153,7 @@ static void free_user(struct user_struct *up, unsigned long flags) { uid_hash_remove(up); spin_unlock_irqrestore(&uidhash_lock, flags); - percpu_counter_destroy(&up->epoll_watches); + user_epoll_free(up); kmem_cache_free(uid_cachep, up); } @@ -186,7 +201,7 @@ struct user_struct *alloc_uid(kuid_t uid) new->uid = uid; refcount_set(&new->__count, 1); - if (percpu_counter_init(&new->epoll_watches, 0, GFP_KERNEL)) { + if (user_epoll_alloc(new)) { kmem_cache_free(uid_cachep, new); return NULL; } @@ -200,7 +215,7 @@ struct user_struct *alloc_uid(kuid_t uid) spin_lock_irq(&uidhash_lock); up = uid_hash_find(uid, hashent); if (up) { - percpu_counter_destroy(&new->epoll_watches); + user_epoll_free(new); kmem_cache_free(uid_cachep, new); } else { uid_hash_insert(new, hashent); @@ -222,8 +237,8 @@ static int __init uid_cache_init(void) for(n = 0; n < UIDHASH_SZ; ++n) INIT_HLIST_HEAD(uidhash_table + n); - if (percpu_counter_init(&root_user.epoll_watches, 0, GFP_KERNEL)) - panic("percpu cpunter alloc failed"); + if (user_epoll_alloc(&root_user)) + panic("root_user epoll percpu counter alloc failed"); /* Insert the root user immediately (init already runs as root) */ spin_lock_irq(&uidhash_lock); -- 2.50.1