]> Git Repo - linux.git/commitdiff
ucounts: Add get_ucounts_or_wrap for clarity
authorEric W. Biederman <[email protected]>
Sat, 16 Oct 2021 19:05:34 +0000 (14:05 -0500)
committerEric W. Biederman <[email protected]>
Wed, 20 Oct 2021 15:45:34 +0000 (10:45 -0500)
Add a helper function get_ucounts_or_wrap that is a trivial
wrapper around atomic_add_negative, that makes it clear
how atomic_add_negative is used in the context of ucounts.

Link: https://lkml.kernel.org/r/87pms2qkr9.fsf_-_@disp2133
Tested-by: Yu Zhao <[email protected]>
Reviewed-by: Alexey Gladkov <[email protected]>
Signed-off-by: "Eric W. Biederman" <[email protected]>
kernel/ucount.c

index 708d05164a7dd2104484a29d241790b18bc702e0..133b6044fda4e212609f32167d0efb5b88cd0359 100644 (file)
@@ -150,9 +150,15 @@ static void hlist_add_ucounts(struct ucounts *ucounts)
        spin_unlock_irq(&ucounts_lock);
 }
 
+static inline bool get_ucounts_or_wrap(struct ucounts *ucounts)
+{
+       /* Returns true on a successful get, false if the count wraps. */
+       return !atomic_add_negative(1, &ucounts->count);
+}
+
 struct ucounts *get_ucounts(struct ucounts *ucounts)
 {
-       if (atomic_add_negative(1, &ucounts->count)) {
+       if (!get_ucounts_or_wrap(ucounts)) {
                put_ucounts(ucounts);
                ucounts = NULL;
        }
@@ -163,7 +169,7 @@ struct ucounts *alloc_ucounts(struct user_namespace *ns, kuid_t uid)
 {
        struct hlist_head *hashent = ucounts_hashentry(ns, uid);
        struct ucounts *ucounts, *new;
-       long overflow;
+       bool wrapped;
 
        spin_lock_irq(&ucounts_lock);
        ucounts = find_ucounts(ns, uid, hashent);
@@ -188,9 +194,9 @@ struct ucounts *alloc_ucounts(struct user_namespace *ns, kuid_t uid)
                        return new;
                }
        }
-       overflow = atomic_add_negative(1, &ucounts->count);
+       wrapped = !get_ucounts_or_wrap(ucounts);
        spin_unlock_irq(&ucounts_lock);
-       if (overflow) {
+       if (wrapped) {
                put_ucounts(ucounts);
                return NULL;
        }
This page took 0.05691 seconds and 4 git commands to generate.