Skip to content

Commit 33fb426

Browse files
committed
Merge branch 'ucount-fixes-for-v5.16' of git://git.kernel.org/pub/scm/linux/kernel/git/ebiederm/user-namespace
Pull ucount cleanups from Eric Biederman: "While working on the ucount fixes a for v5.15 a number of cleanups suggested themselves. Little things like not testing for NULL when a pointer can not be NULL and wrapping atomic_add_negative with a more descriptive name, so that people reading the code can more quickly understand what is going on" * 'ucount-fixes-for-v5.16' of git://git.kernel.org/pub/scm/linux/kernel/git/ebiederm/user-namespace: ucounts: Use atomic_long_sub_return for clarity ucounts: Add get_ucounts_or_wrap for clarity ucounts: Remove unnecessary test for NULL ucount in get_ucounts ucounts: In set_cred_ucounts assume new->ucounts is non-NULL
2 parents a85373f + 3234270 commit 33fb426

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

kernel/cred.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -676,15 +676,14 @@ int set_cred_ucounts(struct cred *new)
676676
* This optimization is needed because alloc_ucounts() uses locks
677677
* for table lookups.
678678
*/
679-
if (old_ucounts && old_ucounts->ns == new->user_ns && uid_eq(old_ucounts->uid, new->euid))
679+
if (old_ucounts->ns == new->user_ns && uid_eq(old_ucounts->uid, new->euid))
680680
return 0;
681681

682682
if (!(new_ucounts = alloc_ucounts(new->user_ns, new->euid)))
683683
return -EAGAIN;
684684

685685
new->ucounts = new_ucounts;
686-
if (old_ucounts)
687-
put_ucounts(old_ucounts);
686+
put_ucounts(old_ucounts);
688687

689688
return 0;
690689
}

kernel/ucount.c

Lines changed: 13 additions & 7 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 (ucounts && 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
}
@@ -276,7 +282,7 @@ bool dec_rlimit_ucounts(struct ucounts *ucounts, enum ucount_type type, long v)
276282
struct ucounts *iter;
277283
long new = -1; /* Silence compiler warning */
278284
for (iter = ucounts; iter; iter = iter->ns->ucounts) {
279-
long dec = atomic_long_add_return(-v, &iter->ucount[type]);
285+
long dec = atomic_long_sub_return(v, &iter->ucount[type]);
280286
WARN_ON_ONCE(dec < 0);
281287
if (iter == ucounts)
282288
new = dec;
@@ -289,7 +295,7 @@ static void do_dec_rlimit_put_ucounts(struct ucounts *ucounts,
289295
{
290296
struct ucounts *iter, *next;
291297
for (iter = ucounts; iter != last; iter = next) {
292-
long dec = atomic_long_add_return(-1, &iter->ucount[type]);
298+
long dec = atomic_long_sub_return(1, &iter->ucount[type]);
293299
WARN_ON_ONCE(dec < 0);
294300
next = iter->ns->ucounts;
295301
if (dec == 0)
@@ -326,7 +332,7 @@ long inc_rlimit_get_ucounts(struct ucounts *ucounts, enum ucount_type type)
326332
}
327333
return ret;
328334
dec_unwind:
329-
dec = atomic_long_add_return(-1, &iter->ucount[type]);
335+
dec = atomic_long_sub_return(1, &iter->ucount[type]);
330336
WARN_ON_ONCE(dec < 0);
331337
unwind:
332338
do_dec_rlimit_put_ucounts(ucounts, iter, type);

0 commit comments

Comments
 (0)