Skip to contentSkip to conversation

af_unix: Remove old GC leftovers.

PATCH Applied Kuniyuki Iwashima 2 participants 4 messages Apr 1, 2024 – Apr 4, 2024

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover af_unix: Remove old GC leftovers.

This is a follow-up series for commit 4090fa373f0e ("af_unix: Replace garbage collection algorithm.") which introduced the new GC for AF_UNIX.

Now we no longer need two ugly tricks for the old GC, let's remove them.

Kuniyuki Iwashima (2):
af_unix: Remove scm_fp_dup() in unix_attach_fds().
af_unix: Remove lock dance in unix_peek_fds().

 include/net/af_unix.h |  1 -
 net/unix/af_unix.c    | 51 ++-----------------------------------------
 net/unix/garbage.c    |  2 +-
 3 files changed, 3 insertions(+), 51 deletions(-)
Signature · 1 line
2.30.2
#1 lore raw

PATCH 1/2 af_unix: Remove scm_fp_dup() in unix_attach_fds().

When we passed fds, we used to bump each file's refcount twice in scm_fp_copy() and scm_fp_dup() before linking the socket to gc_inflight_list.

This is because we incremented the inflight count of the socket and linked it to the list in advance before passing skb to the destination socket.

Otherwise, the inflight socket could have been garbage-collected in a small race window between linking the socket to the list and queuing skb:

CPU 1 : sendmsg(X) w/ A's fd CPU 2 : close(A)
----- -----
/* Here A's refcount is 1, and inflight count is 0 */

bump A's refcount to 2 in scm_fp_copy()
bump A's inflight count to 1
link A to gc_inflight_list
decrement A's refcount to 1

/* A's refcount == inflight count, thus A could be GC candidate */

start GC
mark A as candidate purge A's receive queue

queue skb w/ A's fd to X

/* A is queued, but all data has been lost */

After commit 4090fa373f0e ("af_unix: Replace garbage collection algorithm."), we increment the inflight count and link the socket to the global list only when queuing the skb.

The race no longer exists, so let's not clone the fd nor bump the count in unix_attach_fds().

---
 net/unix/af_unix.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
net/unix/af_unix.c +2 -7
index 533fb682c954..78be8b520cef 100644
@@ -1794,13 +1794,8 @@ static int unix_attach_fds(struct scm_cookie *scm, struct sk_buff *skb)
 	if (too_many_unix_fds(current))
 		return -ETOOMANYREFS;
 
-	/* Need to duplicate file references for the sake of garbage
-	 * collection.  Otherwise a socket in the fps might become a
-	 * candidate for GC while the skb is not yet queued.
-	 */
-	UNIXCB(skb).fp = scm_fp_dup(scm->fp);
-	if (!UNIXCB(skb).fp)
-		return -ENOMEM;
+	UNIXCB(skb).fp = scm->fp;
+	scm->fp = NULL;
 
 	if (unix_prepare_fpl(UNIXCB(skb).fp))
 		return -ENOMEM;
Signature · 1 line
2.30.2
#2 lore raw

PATCH 2/2 af_unix: Remove lock dance in unix_peek_fds().

In reply to Kuniyuki Iwashima

In the previous GC implementation, the shape of the inflight socket graph was not expected to change while GC was in progress.

MSG_PEEK was tricky because it could install inflight fd silently and transform the graph.

Let's say we peeked a fd, which was a listening socket, and accept()ed some embryo sockets from it. The garbage collection algorithm would have been confused because the set of sockets visited in scan_inflight() would change within the same GC invocation.

That's why we placed spin_lock(&unix_gc_lock) and spin_unlock() in unix_peek_fds() with a fat comment.

In the new GC implementation, we no longer garbage-collect the socket if it exists in another queue, that is, if it has a bridge to another SCC. Also, accept() will require the lock if it has edges.

Thus, we need not do the complicated lock dance.

---
 include/net/af_unix.h |  1 -
 net/unix/af_unix.c    | 42 ------------------------------------------
 net/unix/garbage.c    |  2 +-
 3 files changed, 1 insertion(+), 44 deletions(-)
include/net/af_unix.h +0 -1
index 226a8da2cbe3..7311b77edfc7 100644
@@ -17,7 +17,6 @@ static inline struct unix_sock *unix_get_socket(struct file *filp)
 }
 #endif
 
-extern spinlock_t unix_gc_lock;
 extern unsigned int unix_tot_inflight;
 void unix_add_edges(struct scm_fp_list *fpl, struct unix_sock *receiver);
 void unix_del_edges(struct scm_fp_list *fpl);
net/unix/af_unix.c +0 -42

48 lines not on this page · raw message

net/unix/garbage.c +1 -1
index 89ea71d9297b..12a4ec27e0d4 100644
@@ -183,7 +183,7 @@ static void unix_free_vertices(struct scm_fp_list *fpl)
 	}
 }
 
-DEFINE_SPINLOCK(unix_gc_lock);
+static DEFINE_SPINLOCK(unix_gc_lock);
 unsigned int unix_tot_inflight;
 
 void unix_add_edges(struct scm_fp_list *fpl, struct unix_sock *receiver)
Signature · 1 line
2.30.2
#3 lore raw
In reply to Kuniyuki Iwashima
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Quoted 11 lines · On Mon, 1 Apr 2024 10:31:23 -0700 you wrote:

This is a follow-up series for commit 4090fa373f0e ("af_unix: Replace garbage collection algorithm.") which introduced the new GC for AF_UNIX.

Now we no longer need two ugly tricks for the old GC, let's remove them.

Kuniyuki Iwashima (2):
af_unix: Remove scm_fp_dup() in unix_attach_fds().
af_unix: Remove lock dance in unix_peek_fds().

[...]

Here is the summary with links:
- [v1,net-next,1/2] af_unix: Remove scm_fp_dup() in unix_attach_fds().

    https://git.kernel.org/netdev/net-next/c/7c349ed09031
  - [v1,net-next,2/2] af_unix: Remove lock dance in unix_peek_fds().
    https://git.kernel.org/netdev/net-next/c/118f457da9ed

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html