Skip to contentSkip to conversation

net: skmsg: pin the delayed-work psock in sk_psock_backlog

PATCH In review Zhang Cen 2 participants 6 messages May 15

Subsystems (6)
lore
Share & tools
lore

Zhang Cen

author
#0 lore raw

PATCH 1/1 net: skmsg: pin the delayed-work psock in sk_psock_backlog

sk_psock_backlog() recovers the psock it operates on from the delayed work item, but it takes its lifetime reference with sk_psock_get(psock->sk).
That reloads sk->sk_user_data and can therefore return a replacement psock after the old psock was detached and a new one was attached to the same socket.

In that case the worker locks and drains the old psock, but the reference it acquired belongs to the replacement psock. The exit path then puts the detached old psock, which can underflow its refcount after the last unlink while the replacement psock keeps the leaked reference.

Take the reference on the delayed-work psock directly with refcount_inc_not_zero(). If that fails, the old psock is already being dropped, so skip the detached backlog instead of processing or putting it. This keeps the worker's get/put pair on the same psock whose work_state, ingress queue and state bits it manipulates.

The buggy scenario involves two paths, with each column showing the order within that path:

path A label: detach and reattach path path B label: old backlog worker
1. The last unlink drops the old 1. Delayed work resumes from the
psock into sk_psock_drop(). old psock embedded in work.
2. sk_psock_drop() clears 2. The worker still sees
sk->sk_user_data before the old SK_PSOCK_TX_ENABLED on that
TX state is cleared. old psock.
3. A new attach publishes a 3. sk_psock_get(psock->sk)
replacement psock on the same reloads sk->sk_user_data and
socket. refs the replacement psock.
4. The old psock is still queued for 4. The worker locks, drains and
delayed backlog work. finally puts the detached old
psock.

Sanitizer validation reported:
Non-fatal target warning: refcount_t underflow/use-after-free warning from refcount_warn_saturate triggered by sk_psock_backlog putting the detached old psock after last_old_ref_before_put reached 0. use-after-free

---

net/core/skmsg.c +5 -5
@@ -684,12 +684,12 @@ static void sk_psock_backlog(struct work_struct *work)
 	if (!sk_psock_test_state(psock, SK_PSOCK_TX_ENABLED))
 		return;
 
-	/* Increment the psock refcnt to synchronize with close(fd) path in
-	 * sock_map_close(), ensuring we wait for backlog thread completion
-	 * before sk_socket freed. If refcnt increment fails, it indicates
-	 * sock_map_close() completed with sk_socket potentially already freed.
+	/* Hold the delayed-work psock itself so teardown synchronizes with
+	 * the same object whose work_state, queues and state bits we touch.
+	 * If the refcnt is already zero, this psock is being dropped and its
+	 * detached backlog must no longer run.
 	 */
-	if (!sk_psock_get(psock->sk))
+	if (!refcount_inc_not_zero(&psock->refcnt))
 		return;
 	mutex_lock(&psock->work_mutex);
 	while ((skb = skb_peek(&psock->ingress_skb))) {
maintainer
#1 lore raw
Quoted 35 lines · Zhang Cen

sk_psock_backlog() recovers the psock it operates on from the delayed work item, but it takes its lifetime reference with sk_psock_get(psock->sk).
That reloads sk->sk_user_data and can therefore return a replacement psock after the old psock was detached and a new one was attached to the same socket.

In that case the worker locks and drains the old psock, but the reference it acquired belongs to the replacement psock. The exit path then puts the detached old psock, which can underflow its refcount after the last unlink while the replacement psock keeps the leaked reference.

Take the reference on the delayed-work psock directly with refcount_inc_not_zero(). If that fails, the old psock is already being dropped, so skip the detached backlog instead of processing or putting it. This keeps the worker's get/put pair on the same psock whose work_state, ingress queue and state bits it manipulates.

The buggy scenario involves two paths, with each column showing the order within that path:

path A label: detach and reattach path path B label: old backlog worker
1. The last unlink drops the old 1. Delayed work resumes from the
psock into sk_psock_drop(). old psock embedded in work.
2. sk_psock_drop() clears 2. The worker still sees
sk->sk_user_data before the old SK_PSOCK_TX_ENABLED on that
TX state is cleared. old psock.
3. A new attach publishes a 3. sk_psock_get(psock->sk)
replacement psock on the same reloads sk->sk_user_data and
socket. refs the replacement psock.
4. The old psock is still queued for 4. The worker locks, drains and
delayed backlog work. finally puts the detached old
psock.

Quoted 2 lines

Sanitizer validation reported:
Non-fatal target warning: refcount_t underflow/use-after-free warning from refcount_warn_saturate triggered by sk_psock_backlog putting the detached old psock after last_old_ref_before_put reached 0.

Where is the 'last_old_ref_before_put' symbol from? I can't find it anywhere in the tree.

If you are using LLMs to dig into races like this, please also have them produce a reproducer, e.g. patch mdelay() into

the relevant windows to widen them, then trigger it from userspace.

author
#2 lore raw

Dear Jiayuan Chen

Quoted 9 lines · Jiayuan Chen <[email protected]> 于2026年5月15日周五 14:10写道:

Where is the 'last_old_ref_before_put' symbol from? I can't find it anywhere in the tree.

If you are using LLMs to dig into races like this, please also have them produce a reproducer, e.g. patch mdelay() into

the relevant windows to widen them, then trigger it from userspace.

Hi Jiayuan,

Thanks for checking this. You are right: last_old_ref_before_put is not an in-tree kernel symbol. It was a temporary validation probe label which recorded the old psock refcount immediately before the backlog worker's final put, and it should not have appeared in the commit message as if it were kernel output.

The in-tree path I was trying to describe is:

sk_psock_backlog() starts at net/core/skmsg.c:670. get path: sk_psock_get(psock->sk), net/core/skmsg.c:692. put path: sk_psock_put(psock->sk, psock), net/core/skmsg.c:746. detach clears sk_user_data at net/core/skmsg.c:892. reattach publishes a replacement psock at net/core/skmsg.c:793. warning path: REFCOUNT_SUB_UAF at lib/refcount.c:28.

The trigger was based on the in-tree sockmap_redir BPF selftest under tools/testing/selftests/bpf/prog_tests/.
The one-shot test used AF_UNIX SOCK_STREAM socket pairs, attached the sk_skb verdict program to the input map, inserted one socket into the input map and one destination socket into the sockmap at key 0, then sent one byte through the input peer so the destination psock backlog worker was queued.
For validation I used a temporary local instrumentation patch in net/core/skmsg.c. It added a debugfs-controlled gate in sk_psock_backlog() after the TX-enabled check and before the existing sk_psock_get(psock->sk) call, plus counters and pr_info() snapshots in sk_psock_backlog(), sk_psock_init() and sk_psock_drop(). It also stored the pointer returned by sk_psock_get(psock->sk) for logging. The worker still used the existing get path and the existing sk_psock_put(psock->sk, psock) exit path.
With the worker parked before sk_psock_get(psock->sk), the test
forked: the child deleted the destination sockmap entry, and the parent retried BPF_NOEXIST update of the same key with the same destination socket fd until reattach succeeded.
After the delete completed, the test released the old worker. At that point sk->sk_user_data referred to the replacement psock, while the delayed work still belonged to the old psock. The recorded state before the warning had the sk_user_data psock and the psock returned by sk_psock_get(psock->sk) equal to each other, but different from the delayed-work container. The instrumentation was only used to make that interleaving deterministic and observable. The warning below is the kernel's normal refcount warning path.

The native kernel report from that run was:

refcount_t: underflow; use-after-free.

  WARNING: lib/refcount.c:28 at refcount_warn_saturate+0xbf/0xf0
  Workqueue: events sk_psock_backlog
  RIP: 0010:refcount_warn_saturate+0xbf/0xf0
  Call trace:
    sk_psock_backlog() (net/core/skmsg.c:670)
    process_one_work() (kernel/workqueue.c:3200)

So the reproducer is instrumentation-assisted, not an unmodified upstream selftest. The instrumentation can widen the race window and record the participating psock pointers, but it does not publish a replacement psock, clear sk->sk_user_data, or add an extra put on the old psock. The final warning is reached through the existing sk_psock_put(psock->sk, psock) path after the test has forced delete-plus-reattach to happen before the parked worker resumes.

I will send v2 as a new thread after the netdev 24-hour interval, with the lab probe label removed from the commit text. If useful, I can also share the small instrumentation/selftest diff separately to show the exact widened window.

Thanks,
Zhang Cen

maintainer
#3 lore raw
Quoted 43 lines · Cen Zhang

Dear Jiayuan Chen

Quoted 9 lines · Jiayuan Chen <[email protected]> 于2026年5月15日周五 14:10写道:

Where is the 'last_old_ref_before_put' symbol from? I can't find it anywhere in the tree.

If you are using LLMs to dig into races like this, please also have them produce a reproducer, e.g. patch mdelay() into

the relevant windows to widen them, then trigger it from userspace.

Hi Jiayuan,

Thanks for checking this. You are right: last_old_ref_before_put is not an in-tree kernel symbol. It was a temporary validation probe label which recorded the old psock refcount immediately before the backlog worker's final put, and it should not have appeared in the commit message as if it were kernel output.

The in-tree path I was trying to describe is:

sk_psock_backlog() starts at net/core/skmsg.c:670. get path: sk_psock_get(psock->sk), net/core/skmsg.c:692. put path: sk_psock_put(psock->sk, psock), net/core/skmsg.c:746. detach clears sk_user_data at net/core/skmsg.c:892. reattach publishes a replacement psock at net/core/skmsg.c:793. warning path: REFCOUNT_SUB_UAF at lib/refcount.c:28.

The trigger was based on the in-tree sockmap_redir BPF selftest under tools/testing/selftests/bpf/prog_tests/.
The one-shot test used AF_UNIX SOCK_STREAM socket pairs, attached the sk_skb verdict program to the input map, inserted one socket into the input map and one destination socket into the sockmap at key 0, then sent one byte through the input peer so the destination psock backlog worker was queued.
For validation I used a temporary local instrumentation patch in net/core/skmsg.c. It added a debugfs-controlled gate in sk_psock_backlog() after the TX-enabled check and before the existing sk_psock_get(psock->sk) call, plus counters and pr_info() snapshots in sk_psock_backlog(), sk_psock_init() and sk_psock_drop(). It also stored the pointer returned by sk_psock_get(psock->sk) for logging. The worker still used the

Quoted 8 lines

existing get path and the existing sk_psock_put(psock->sk, psock) exit path.
With the worker parked before sk_psock_get(psock->sk), the test
forked: the child deleted the destination sockmap entry, and the parent retried BPF_NOEXIST update of the same key with the same destination socket fd until reattach succeeded.
After the delete completed, the test released the old worker. At that point sk->sk_user_data referred to the replacement psock, while

So, should the fix swap the order of sk->sk_user_data = null and sk_psock_clear_state(psock, SK_PSOCK_TX_ENABLED)?

Quoted 26 lines

the delayed work still belonged to the old psock. The recorded state before the warning had the sk_user_data psock and the psock returned by sk_psock_get(psock->sk) equal to each other, but different from the delayed-work container. The instrumentation was only used to make that interleaving deterministic and observable. The warning below is the kernel's normal refcount warning path.

The native kernel report from that run was:

   refcount_t: underflow; use-after-free.
   WARNING: lib/refcount.c:28 at refcount_warn_saturate+0xbf/0xf0
   Workqueue: events sk_psock_backlog
   RIP: 0010:refcount_warn_saturate+0xbf/0xf0
   Call trace:
     sk_psock_backlog() (net/core/skmsg.c:670)
     process_one_work() (kernel/workqueue.c:3200)

So the reproducer is instrumentation-assisted, not an unmodified upstream selftest. The instrumentation can widen the race window and record the participating psock pointers, but it does not publish a replacement psock, clear sk->sk_user_data, or add an extra put on the old psock. The final warning is reached through the existing sk_psock_put(psock->sk, psock) path after the test has forced delete-plus-reattach to happen before the parked worker resumes.

Quoted 4 lines

I will send v2 as a new thread after the netdev 24-hour interval, with the lab probe label removed from the commit text. If useful, I can also share the small instrumentation/selftest diff separately to show the exact widened window.

You can just put the kernel patch and userspace program patch in this thread (no need to send a new patch).

Also this patch should be targeted to bpf not net.

--
pw-bot: cr

maintainer
#4 lore raw
Quoted 54 lines · Jiayuan Chen
Quoted 43 lines · Cen Zhang

Dear Jiayuan Chen

Quoted 9 lines · Jiayuan Chen <[email protected]> 于2026年5月15日周五 14:10写道:

Where is the 'last_old_ref_before_put' symbol from? I can't find it anywhere in the tree.

If you are using LLMs to dig into races like this, please also have them produce a reproducer, e.g. patch mdelay() into

the relevant windows to widen them, then trigger it from userspace.

Hi Jiayuan,

Thanks for checking this. You are right: last_old_ref_before_put is not an in-tree kernel symbol. It was a temporary validation probe label which recorded the old psock refcount immediately before the backlog worker's final put, and it should not have appeared in the commit message as if it were kernel output.

The in-tree path I was trying to describe is:

   sk_psock_backlog() starts at net/core/skmsg.c:670. get path: sk_psock_get(psock->sk), net/core/skmsg.c:692. put path: sk_psock_put(psock->sk, psock), net/core/skmsg.c:746. detach clears sk_user_data at net/core/skmsg.c:892. reattach publishes a replacement psock at net/core/skmsg.c:793. warning path: REFCOUNT_SUB_UAF at lib/refcount.c:28.

The trigger was based on the in-tree sockmap_redir BPF selftest under tools/testing/selftests/bpf/prog_tests/.
The one-shot test used AF_UNIX SOCK_STREAM socket pairs, attached the sk_skb verdict program to the input map, inserted one socket into the input map and one destination socket into the sockmap at key 0, then sent one byte through the input peer so the destination psock backlog worker was queued.
For validation I used a temporary local instrumentation patch in net/core/skmsg.c. It added a debugfs-controlled gate in sk_psock_backlog() after the TX-enabled check and before the existing sk_psock_get(psock->sk) call, plus counters and pr_info() snapshots in sk_psock_backlog(), sk_psock_init() and sk_psock_drop(). It also stored the pointer returned by sk_psock_get(psock->sk) for logging. The worker still used the

Quoted 8 lines

existing get path and the existing sk_psock_put(psock->sk, psock) exit path.
With the worker parked before sk_psock_get(psock->sk), the test
forked: the child deleted the destination sockmap entry, and the parent retried BPF_NOEXIST update of the same key with the same destination socket fd until reattach succeeded.
After the delete completed, the test released the old worker. At that point sk->sk_user_data referred to the replacement psock, while

Quoted 3 lines

So, should the fix swap the order of sk->sk_user_data = null and sk_psock_clear_state(psock, SK_PSOCK_TX_ENABLED)?

Please also carry a Fixes tag.

[...]

Quoted 15 lines
Quoted 5 lines

I will send v2 as a new thread after the netdev 24-hour interval, with the lab probe label removed from the commit text. If useful, I can also share the small instrumentation/selftest diff separately to show the exact widened window.

You can just put the kernel patch and userspace program patch in this thread (no need to send a new patch).

Also this patch should be targeted to bpf not net.

Signature · 1 line
pw-bot: cr
author
#5 lore raw

Hi Jiayuan,

Thanks, understood. I will treat any follow-up submission as bpf, not net, and I will not send a new patch revision just for this reproducer information. I will also carry this Fixes tag in the next formal patch:

Fixes 8259eb0e06d8 ("bpf, sockmap: Avoid using sk_socket after free

when sending")

On the ordering question: moving the TX_ENABLED clear before clearing sk_user_data would close the window for workers which enter sk_psock_backlog() after teardown has started, but I do not think it is sufficient for the interleaving reproduced below.

The backlog worker tests TX_ENABLED before it calls sk_psock_get():

if (!sk_psock_test_state(psock, SK_PSOCK_TX_ENABLED))

          return;

if (!sk_psock_get(psock->sk))

          return;

That test is not serialized with sk_psock_stop()'s ingress_lock. A worker can pass the TX_ENABLED test and then be preempted before sk_psock_get(psock->sk). If teardown clears TX_ENABLED after that point, then clears sk_user_data, and a new attach publishes a replacement psock, the old worker still calls sk_psock_get(psock->sk) and can pin the replacement while continuing to process the old delayed-work psock. The instrumentation below parks the worker exactly in that window: after the TX_ENABLED test and before sk_psock_get(psock->sk). So a pure ordering change in sk_psock_drop() would not affect the worker once it has already passed the initial TX_ENABLED test.

Below are the two local patches I used for the reproducer, against 917719c41. The first patch only adds the debugfs gate/counters and logging around the existing skmsg paths. It does not publish a replacement psock, does not clear sk_user_data, and does not add an extra put. The second patch adds a one-shot sockmap_redir selftest path which creates AF_UNIX SOCK_STREAM socket pairs, redirects one byte to the destination sockmap entry, waits until the old backlog worker is parked, deletes that sockmap entry, reattaches the same destination fd with BPF_NOEXIST, and then releases the parked worker. The PMB prefix in the diff is only the local probe namespace.

I ran it as:

  make -C tools/testing/selftests/bpf LLVM=1 \
          OUTPUT=/tmp/bpf-out test_progs_sockmap_redir test_sockmap

  PMB_SOCKMAP_ONE_SHOT=1 \
  PMB_SOCKMAP_REDIR=sk_skb-to-ingress \
  PMB_SOCKMAP_FAMILY=u_str \
  PMB_SOCKMAP_MAP=sockmap \
  PMB_PSOCK_PHASE=pointer \
          /tmp/bpf-out/test_progs-sockmap_redir -vv

The warning came from the existing final put path in the old worker:

refcount_t: underflow; use-after-free.

  WARNING: lib/refcount.c:28 at refcount_warn_saturate+0xbf/0xf0
  Workqueue: events sk_psock_backlog
  RIP: 0010:refcount_warn_saturate+0xbf/0xf0
  Call Trace:
    sk_psock_backlog+0xc8c/0x1ad0
    process_one_work+0x8b7/0x1af0
    worker_thread+0x574/0xf10
    kthread+0x2fc/0x3f0
    ret_from_fork+0x58b/0x830
    ret_from_fork_asm+0x1a/0x30

Thanks,
Zhang

--- 8< --- kernel instrumentation patch --- 8< ---

net/core/skmsg.c +51 -0
index 6187a83bd..79d8769bb 100644
@@ -4,12 +4,101 @@
 #include <linux/skmsg.h>
 #include <linux/skbuff.h>
 #include <linux/scatterlist.h>
+#include <linux/debugfs.h>
+#include <linux/delay.h>
+#include <linux/init.h>
+#include <linux/jiffies.h>
+#include <linux/sock_diag.h>
 
 #include <net/sock.h>
 #include <net/tcp.h>
 #include <net/tls.h>
 #include <trace/events/sock.h>
 
+/* PMB_PSOCK_BACKLOG_GATE */
+static u32 pmb_psock_gate_enable;
+static u32 pmb_psock_gate_release;
+static u32 pmb_psock_gate_parked;
+static u32 pmb_psock_mismatch_seen;
+static u32 pmb_psock_get_seen;
+static u32 pmb_psock_last_old_ref;
+static u32 pmb_psock_last_old_ref_before_put;
+static u32 pmb_psock_last_old_tx;
+static u64 pmb_psock_last_cookie;
+static u64 pmb_psock_last_sk;
+static u64 pmb_psock_last_container;
+static u64 pmb_psock_last_current;
+static u64 pmb_psock_last_get;
+
+static void pmb_psock_probe_snapshot(struct sk_psock *container,
+      struct sk_psock *current_psock,
+      struct sk_psock *held)
+{
+ WRITE_ONCE(pmb_psock_last_cookie, sock_gen_cookie(container->sk));
+ WRITE_ONCE(pmb_psock_last_sk, (u64)(unsigned long)container->sk);
+ WRITE_ONCE(pmb_psock_last_container, (u64)(unsigned long)container);
+ WRITE_ONCE(pmb_psock_last_current, (u64)(unsigned long)current_psock);
+ WRITE_ONCE(pmb_psock_last_get, (u64)(unsigned long)held);
+ if (current_psock && current_psock != container)
+ WRITE_ONCE(pmb_psock_mismatch_seen, 1);
+ if (held && held != container)
+ WRITE_ONCE(pmb_psock_mismatch_seen, 1);
+}
+
+static void pmb_psock_maybe_gate(struct sk_psock *psock)
+{
+ unsigned long timeout = jiffies + msecs_to_jiffies(10000);
+
+ if (!READ_ONCE(pmb_psock_gate_enable) || psock->sk->sk_family != AF_UNIX)
+ return;
+
+ WRITE_ONCE(pmb_psock_gate_enable, 0);
+ WRITE_ONCE(pmb_psock_gate_parked, 1);
+ WRITE_ONCE(pmb_psock_last_cookie, sock_gen_cookie(psock->sk));
+ WRITE_ONCE(pmb_psock_last_sk, (u64)(unsigned long)psock->sk);
+ WRITE_ONCE(pmb_psock_last_container, (u64)(unsigned long)psock);
+ WRITE_ONCE(pmb_psock_last_old_ref, refcount_read(&psock->refcnt));
+ WRITE_ONCE(pmb_psock_last_old_tx,
+    sk_psock_test_state(psock, SK_PSOCK_TX_ENABLED));
+ pr_info("PMB_PSOCK_BACKLOG_GATE park cookie=%llu sk=%px

container=%px old_tx=%u old_ref=%u\n",
+ (unsigned long long)READ_ONCE(pmb_psock_last_cookie),
+ psock->sk, psock, READ_ONCE(pmb_psock_last_old_tx),
+ READ_ONCE(pmb_psock_last_old_ref));
+
+ while (!READ_ONCE(pmb_psock_gate_release) && time_before(jiffies, timeout))
+ msleep(1);
+ pr_info("PMB_PSOCK_BACKLOG_GATE resume cookie=%llu sk=%px container=%px released=%u timeout=%ld\n",
+ (unsigned long long)READ_ONCE(pmb_psock_last_cookie),
+ psock->sk, psock, READ_ONCE(pmb_psock_gate_release),
+ time_before(jiffies, timeout) ? 0L : 1L);
+}
+
+static int __init pmb_psock_probe_debugfs_init(void)
+{
+ struct dentry *dir;
+
+ dir = debugfs_create_dir("pmb_psock", NULL);
+ if (IS_ERR_OR_NULL(dir))
+ return 0;
+
+ debugfs_create_u32("gate_enable", 0600, dir, &pmb_psock_gate_enable);
+ debugfs_create_u32("gate_release", 0600, dir, &pmb_psock_gate_release);
+ debugfs_create_u32("gate_parked", 0600, dir, &pmb_psock_gate_parked);
+ debugfs_create_u32("mismatch_seen", 0600, dir, &pmb_psock_mismatch_seen);
+ debugfs_create_u32("get_seen", 0600, dir, &pmb_psock_get_seen);
+ debugfs_create_u32("last_old_ref", 0600, dir, &pmb_psock_last_old_ref);
+ debugfs_create_u32("last_old_ref_before_put", 0600, dir,
+ &pmb_psock_last_old_ref_before_put);
+ debugfs_create_u32("last_old_tx", 0600, dir, &pmb_psock_last_old_tx);
+ debugfs_create_x64("last_cookie", 0600, dir, &pmb_psock_last_cookie);
+ debugfs_create_x64("last_sk", 0600, dir, &pmb_psock_last_sk);
+ debugfs_create_x64("last_container", 0600, dir, &pmb_psock_last_container);
+ debugfs_create_x64("last_current", 0600, dir, &pmb_psock_last_current);
+ debugfs_create_x64("last_get", 0600, dir, &pmb_psock_last_get);
+ return 0;
+}
+late_initcall(pmb_psock_probe_debugfs_init);
+
static bool sk_msg_try_coalesce_ok(struct sk_msg *msg, int elem_first_coalesce) {
if (msg->sg.end > msg->sg.start &&
@@ -671,6 +760,8 @@ static void sk_psock_backlog(struct work_struct *work)
{
struct delayed_work *dwork = to_delayed_work(work);
struct sk_psock *psock = container_of(dwork, struct sk_psock, work);
+ struct sk_psock *current_psock;
+ struct sk_psock *held_psock;
struct sk_psock_work_state *state = &psock->work_state;
struct sk_buff *skb = NULL;
u32 len = 0, off = 0;
@@ -684,13 +775,35 @@ static void sk_psock_backlog(struct work_struct *work)
if (!sk_psock_test_state(psock, SK_PSOCK_TX_ENABLED))
return;

+ pmb_psock_maybe_gate(psock);
+ rcu_read_lock();
+ current_psock = sk_psock(psock->sk);
+ rcu_read_unlock();
+ WRITE_ONCE(pmb_psock_last_old_ref, refcount_read(&psock->refcnt));
+ WRITE_ONCE(pmb_psock_last_old_tx,
+ sk_psock_test_state(psock, SK_PSOCK_TX_ENABLED));
+ pmb_psock_probe_snapshot(psock, current_psock, NULL);
+ pr_info("PMB_PSOCK_BACKLOG_RESUME cookie=%llu sk=%px container=%px current=%px old_tx=%u old_ref=%u mismatch=%u\n",
+ (unsigned long long)READ_ONCE(pmb_psock_last_cookie),
+ psock->sk, psock, current_psock,
+ READ_ONCE(pmb_psock_last_old_tx),
+ READ_ONCE(pmb_psock_last_old_ref),
+ READ_ONCE(pmb_psock_mismatch_seen));
+
/* Increment the psock refcnt to synchronize with close(fd) path in
* sock_map_close(), ensuring we wait for backlog thread completion
* before sk_socket freed. If refcnt increment fails, it indicates
* sock_map_close() completed with sk_socket potentially already freed. */
- if (!sk_psock_get(psock->sk))
+ held_psock = sk_psock_get(psock->sk);
+ if (!held_psock)
return;
+ WRITE_ONCE(pmb_psock_get_seen, 1);
+ pmb_psock_probe_snapshot(psock, current_psock, held_psock);
+ pr_info("PMB_PSOCK_BACKLOG_GET cookie=%llu sk=%px container=%px held=%px mismatch=%u\n",
+ (unsigned long long)READ_ONCE(pmb_psock_last_cookie),
+ psock->sk, psock, held_psock, READ_ONCE(pmb_psock_mismatch_seen)); +
mutex_lock(&psock->work_mutex);
while ((skb = skb_peek(&psock->ingress_skb))) {
len = skb->len;
@@ -743,6 +856,11 @@ static void sk_psock_backlog(struct work_struct *work)
}
end:
mutex_unlock(&psock->work_mutex);
+ WRITE_ONCE(pmb_psock_last_old_ref_before_put, refcount_read(&psock->refcnt));
+ pr_info("PMB_PSOCK_BACKLOG_PUT cookie=%llu sk=%px container=%px held=%px ref_before_put=%u\n",
+ (unsigned long long)READ_ONCE(pmb_psock_last_cookie),
+ psock->sk, psock, held_psock,
+ READ_ONCE(pmb_psock_last_old_ref_before_put)); sk_psock_put(psock->sk, psock);
}

@@ -789,10 +907,15 @@ struct sk_psock *sk_psock_init(struct sock *sk, int node)

sk_psock_set_state(psock, SK_PSOCK_TX_ENABLED); refcount_set(&psock->refcnt, 1);
+ pr_info("PMB_PSOCK_INIT_TRACE alloc cookie=%llu sk=%px psock=%px ref=%d\n",
+ (unsigned long long)sock_gen_cookie(sk), sk, psock,
+ refcount_read(&psock->refcnt));

__rcu_assign_sk_user_data_with_flags(sk, psock,

       SK_USER_DATA_NOCOPY |
       SK_USER_DATA_PSOCK);

+ pr_info("PMB_PSOCK_INIT_TRACE publish cookie=%llu sk=%px psock=%px\n",
+ (unsigned long long)sock_gen_cookie(sk), sk, psock); sock_hold(sk);

out:
@@ -890,12 +1013,20 @@ void sk_psock_drop(struct sock *sk, struct sk_psock *psock)
write_lock_bh(&sk->sk_callback_lock); sk_psock_restore_proto(sk, psock); rcu_assign_sk_user_data(sk, NULL);
+ pr_info("PMB_PSOCK_DROP_TRACE clear cookie=%llu sk=%px psock=%px ref=%d tx=%d\n",
+ (unsigned long long)sock_gen_cookie(sk), sk, psock,
+ refcount_read(&psock->refcnt),
+ sk_psock_test_state(psock, SK_PSOCK_TX_ENABLED));
if (psock->progs.stream_parser)
sk_psock_stop_strp(sk, psock);
else if (psock->progs.stream_verdict || psock->progs.skb_verdict) sk_psock_stop_verdict(sk, psock); write_unlock_bh(&sk->sk_callback_lock);

+ pr_info("PMB_PSOCK_DROP_TRACE stop cookie=%llu sk=%px psock=%px ref=%d tx=%d\n",
+ (unsigned long long)sock_gen_cookie(sk), sk, psock,
+ refcount_read(&psock->refcnt),
+ sk_psock_test_state(psock, SK_PSOCK_TX_ENABLED)); sk_psock_stop(psock);

  INIT_RCU_WORK(&psock->rwork, sk_psock_destroy);

--- 8< --- userspace selftest patch --- 8< ---

(quoted diff) +0 -0

b/tools/testing/selftests/bpf/prog_tests/sockmap_redir.c index 9c461d931..ebaf5cf21 100644

tools/testing/selftests/bpf/prog_tests/sockmap_redir.c +330 -0

353 lines not on this page · raw message

held=0x%llx ref_before_put=%llu\n",
+ phase ? phase : "pointer", container, current, held,
+ ref_before_put);
+
+ if (!ASSERT_EQ(mismatch, 1, "mismatch_seen"))
+ goto cleanup_maps;
+ if (!ASSERT_NEQ(container, 0, "container_nonzero"))
+ goto cleanup_maps;
+ if (!ASSERT_NEQ(current, 0, "current_nonzero"))
+ goto cleanup_maps;
+ if (!ASSERT_NEQ(held, 0, "held_nonzero"))
+ goto cleanup_maps;
+ if (!ASSERT_NEQ(container, current, "container_vs_current"))
+ goto cleanup_maps;
+ if (!ASSERT_NEQ(container, held, "container_vs_held"))
+ goto cleanup_maps;
+ if (phase && !strcmp(phase, "refcount") &&
+ !ASSERT_EQ(ref_before_put, 0, "old_ref_before_put"))
+ goto cleanup_maps;
+
+wait_child:
+ if (delete_pid > 0) {
+ err = pmb_wait_child_ok(delete_pid);
+ delete_pid = -1;
+ ASSERT_OK(err, "delete_out_wait");
+ }
+
+cleanup_maps:
+ bpf_map_delete_elem(maps.out, &key);
+ bpf_map_delete_elem(maps.in, &key);
+cleanup_gate:
+ pmb_write_u64("gate_enable", 0);
+ pmb_write_u64("gate_release", 1);
+cleanup_sockets:
+ if (sockets_ready)
+ socket_spec_close(&s);
+detach:
+ if (attached)
+ xbpf_prog_detach2(prog_fd, maps.in, attach_type); +done:
+ if (skel)
+ test_sockmap_redir__destroy(skel);
+}
+
static void get_redir_params(struct redir_spec *redir,
struct test_sockmap_redir *skel, int *prog_fd, enum bpf_attach_type *attach_type,
@@ -460,6 +833,11 @@ static void test_map(enum bpf_map_type type)

void serial_test_sockmap_redir(void)
{
+ if (pmb_sockmap_one_shot_enabled()) {
+ pmb_test_sockmap_one_shot();
+ return;
+ }
+
test_map(BPF_MAP_TYPE_SOCKMAP);
test_map(BPF_MAP_TYPE_SOCKHASH);
}