Skip to content

Commit da70d31

Browse files
committed
ucounts: Add get_ucounts_or_wrap for clarity
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]>
1 parent 5fc9e37 commit da70d31

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

kernel/ucount.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,15 @@ static void hlist_add_ucounts(struct ucounts *ucounts)
150150
spin_unlock_irq(&ucounts_lock);
151151
}
152152

153+
static inline bool get_ucounts_or_wrap(struct ucounts *ucounts)
154+
{
155+
/* Returns true on a successful get, false if the count wraps. */
156+
return !atomic_add_negative(1, &ucounts->count);
157+
}
158+
153159
struct ucounts *get_ucounts(struct ucounts *ucounts)
154160
{
155-
if (atomic_add_negative(1, &ucounts->count)) {
161+
if (!get_ucounts_or_wrap(ucounts)) {
156162
put_ucounts(ucounts);
157163
ucounts = NULL;
158164
}
@@ -163,7 +169,7 @@ struct ucounts *alloc_ucounts(struct user_namespace *ns, kuid_t uid)
163169
{
164170
struct hlist_head *hashent = ucounts_hashentry(ns, uid);
165171
struct ucounts *ucounts, *new;
166-
long overflow;
172+
bool wrapped;
167173

168174
spin_lock_irq(&ucounts_lock);
169175
ucounts = find_ucounts(ns, uid, hashent);
@@ -188,9 +194,9 @@ struct ucounts *alloc_ucounts(struct user_namespace *ns, kuid_t uid)
188194
return new;
189195
}
190196
}
191-
overflow = atomic_add_negative(1, &ucounts->count);
197+
wrapped = !get_ucounts_or_wrap(ucounts);
192198
spin_unlock_irq(&ucounts_lock);
193-
if (overflow) {
199+
if (wrapped) {
194200
put_ucounts(ucounts);
195201
return NULL;
196202
}

0 commit comments

Comments
 (0)