Skip to content

Commit 1aedcb7

Browse files
q2venmikeNG
authored andcommitted
UPSTREAM: af_unix: Do not use atomic ops for unix_sk(sk)->inflight.
[ Upstream commit 97af84a6bba2ab2b9c704c08e67de3b5ea551bb2 ] When touching unix_sk(sk)->inflight, we are always under spin_lock(&unix_gc_lock). Let's convert unix_sk(sk)->inflight to the normal unsigned long. Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com> Reviewed-by: Simon Horman <horms@kernel.org> Link: https://lore.kernel.org/r/20240123170856.41348-3-kuniyu@amazon.com Signed-off-by: Jakub Kicinski <kuba@kernel.org> Signed-off-by: Sasha Levin <sashal@kernel.org> (cherry picked from commit c8a2b1f7208b0ea0a4ad4355e0510d84f508a9ff) Signed-off-by: Vegard Nossum <vegard.nossum@oracle.com> Change-Id: I0d965d5f2a863d798c06de9f21d0467f256b538e
1 parent d7aa06c commit 1aedcb7

4 files changed

Lines changed: 16 additions & 15 deletions

File tree

include/net/af_unix.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ struct unix_sock {
5656
struct mutex iolock, bindlock;
5757
struct sock *peer;
5858
struct list_head link;
59-
atomic_long_t inflight;
59+
unsigned long inflight;
6060
spinlock_t lock;
6161
unsigned char recursion_level;
6262
unsigned long gc_flags;

net/unix/af_unix.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -806,11 +806,11 @@ static struct sock *unix_create1(struct net *net, struct socket *sock, int kern)
806806
sk->sk_write_space = unix_write_space;
807807
sk->sk_max_ack_backlog = net->unx.sysctl_max_dgram_qlen;
808808
sk->sk_destruct = unix_sock_destructor;
809-
u = unix_sk(sk);
809+
u = unix_sk(sk);
810+
u->inflight = 0;
810811
u->path.dentry = NULL;
811812
u->path.mnt = NULL;
812813
spin_lock_init(&u->lock);
813-
atomic_long_set(&u->inflight, 0);
814814
INIT_LIST_HEAD(&u->link);
815815
mutex_init(&u->iolock); /* single task reading lock */
816816
mutex_init(&u->bindlock); /* single task binding lock */

net/unix/garbage.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -171,17 +171,18 @@ static void scan_children(struct sock *x, void (*func)(struct unix_sock *),
171171

172172
static void dec_inflight(struct unix_sock *usk)
173173
{
174-
atomic_long_dec(&usk->inflight);
174+
usk->inflight--;
175175
}
176176

177177
static void inc_inflight(struct unix_sock *usk)
178178
{
179-
atomic_long_inc(&usk->inflight);
179+
usk->inflight++;
180180
}
181181

182182
static void inc_inflight_move_tail(struct unix_sock *u)
183183
{
184-
atomic_long_inc(&u->inflight);
184+
u->inflight++;
185+
185186
/* If this still might be part of a cycle, move it to the end
186187
* of the list, so that it's checked even if it was already
187188
* passed over
@@ -241,14 +242,12 @@ void unix_gc(void)
241242
*/
242243
list_for_each_entry_safe(u, next, &gc_inflight_list, link) {
243244
long total_refs;
244-
long inflight_refs;
245245

246246
total_refs = file_count(u->sk.sk_socket->file);
247-
inflight_refs = atomic_long_read(&u->inflight);
248247

249-
BUG_ON(inflight_refs < 1);
250-
BUG_ON(total_refs < inflight_refs);
251-
if (total_refs == inflight_refs) {
248+
BUG_ON(!u->inflight);
249+
BUG_ON(total_refs < u->inflight);
250+
if (total_refs == u->inflight) {
252251
list_move_tail(&u->link, &gc_candidates);
253252
__set_bit(UNIX_GC_CANDIDATE, &u->gc_flags);
254253
__set_bit(UNIX_GC_MAYBE_CYCLE, &u->gc_flags);
@@ -275,7 +274,7 @@ void unix_gc(void)
275274
/* Move cursor to after the current position. */
276275
list_move(&cursor, &u->link);
277276

278-
if (atomic_long_read(&u->inflight) > 0) {
277+
if (u->inflight) {
279278
list_move_tail(&u->link, &not_cycle_list);
280279
__clear_bit(UNIX_GC_MAYBE_CYCLE, &u->gc_flags);
281280
scan_children(&u->sk, inc_inflight_move_tail, NULL);

net/unix/scm.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,13 @@ void unix_inflight(struct user_struct *user, struct file *fp)
5050
if (s) {
5151
struct unix_sock *u = unix_sk(s);
5252

53-
if (atomic_long_inc_return(&u->inflight) == 1) {
53+
if (!u->inflight) {
5454
BUG_ON(!list_empty(&u->link));
5555
list_add_tail(&u->link, &gc_inflight_list);
5656
} else {
5757
BUG_ON(list_empty(&u->link));
5858
}
59+
u->inflight++;
5960
/* Paired with READ_ONCE() in wait_for_unix_gc() */
6061
WRITE_ONCE(unix_tot_inflight, unix_tot_inflight + 1);
6162
}
@@ -72,10 +73,11 @@ void unix_notinflight(struct user_struct *user, struct file *fp)
7273
if (s) {
7374
struct unix_sock *u = unix_sk(s);
7475

75-
BUG_ON(!atomic_long_read(&u->inflight));
76+
BUG_ON(!u->inflight);
7677
BUG_ON(list_empty(&u->link));
7778

78-
if (atomic_long_dec_and_test(&u->inflight))
79+
u->inflight--;
80+
if (!u->inflight)
7981
list_del_init(&u->link);
8082
/* Paired with READ_ONCE() in wait_for_unix_gc() */
8183
WRITE_ONCE(unix_tot_inflight, unix_tot_inflight - 1);

0 commit comments

Comments
 (0)