]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
af_unix: Set gc_in_progress to true in unix_gc().
authorKuniyuki Iwashima <kuniyu@google.com>
Fri, 1 May 2026 07:39:41 +0000 (07:39 +0000)
committerJakub Kicinski <kuba@kernel.org>
Tue, 5 May 2026 01:34:45 +0000 (18:34 -0700)
Igor Ushakov reported that unix_gc() could run with gc_in_progress
being false if the work is scheduled while running:

  Thread 1         Thread 2                     Thread 3
  --------         --------                     --------
                   unix_schedule_gc()           unix_schedule_gc()
                   `- if (!gc_in_progress)      `- if (!gc_in_progress)
                      |- gc_in_progress = true     |
                      `- queue_work()              |
  unix_gc() <----------------/                     |
  |                                                |- gc_in_progress = true
  ...                                              `- queue_work()
  |                                                       |
  `- gc_in_progress = false                               |
                                                          |
  unix_gc() <---------------------------------------------'
  |
  ... /* gc_in_progress == false */
  |
  `- gc_in_progress = false

unix_peek_fpl() relies on gc_in_progress not to confuse GC
by MSG_PEEK.

Let's set gc_in_progress to true in unix_gc().

Fixes: 8b90a9f819dc ("af_unix: Run GC on only one CPU.")
Reported-by: Igor Ushakov <sysroot314@gmail.com>
Signed-off-by: Kuniyuki Iwashima <kuniyu@google.com>
Link: https://patch.msgid.link/20260501073945.1884564-1-kuniyu@google.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
net/unix/garbage.c

index a7967a34582737eb1c73d3b0206b9e96f9793479..0783555e2526609770044dc07a5864ac4d69f764 100644 (file)
@@ -607,6 +607,8 @@ static void unix_gc(struct work_struct *work)
        struct sk_buff_head hitlist;
        struct sk_buff *skb;
 
+       WRITE_ONCE(gc_in_progress, true);
+
        spin_lock(&unix_gc_lock);
 
        if (unix_graph_state == UNIX_GRAPH_NOT_CYCLIC) {
@@ -649,10 +651,8 @@ void unix_schedule_gc(struct user_struct *user)
            READ_ONCE(user->unix_inflight) < UNIX_INFLIGHT_SANE_USER)
                return;
 
-       if (!READ_ONCE(gc_in_progress)) {
-               WRITE_ONCE(gc_in_progress, true);
+       if (!READ_ONCE(gc_in_progress))
                queue_work(system_dfl_wq, &unix_gc_work);
-       }
 
        if (user && READ_ONCE(unix_graph_cyclic_sccs))
                flush_work(&unix_gc_work);