netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Kuniyuki Iwashima <kuniyu@amazon.com>
To: "David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@google.com>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>
Cc: Kuniyuki Iwashima <kuniyu@amazon.com>,
	Kuniyuki Iwashima <kuni1840@gmail.com>, <netdev@vger.kernel.org>
Subject: [PATCH v1 net-next 05/16] af_unix: Fix up unix_edge.successor for embryo socket.
Date: Fri, 2 Feb 2024 19:00:47 -0800	[thread overview]
Message-ID: <20240203030058.60750-6-kuniyu@amazon.com> (raw)
In-Reply-To: <20240203030058.60750-1-kuniyu@amazon.com>

To garbage collect inflight AF_UNIX sockets, we must define the
cyclic reference appropriately.  This is a bit tricky if the loop
consists of embryo sockets.

Suppose that the fd of AF_UNIX socket A is passed to D and the fd B
to C and that C and D are embryo sockets of A and B, respectively.
It may appear that there are two separate graphs, A (-> D) and
B (-> C), but this is not correct.

     A --. .-- B
          X
     C <-' `-> D

Now, D holds A's refcount, and C has B's refcount, so unix_release()
will never be called for A and B when we close() them.  However, no
one can call close() for D and C to free skbs holding refcounts of A
and B because C/D is in A/B's receive queue, which should have been
purged by unix_release() for A and B.

So, here's a new type of cyclic reference.  When a fd of an AF_UNIX
socket is passed to an embryo socket, the reference is indirectly
held by its parent listening socket.

  .-> A                             .-> B
  |   `- sk_receive_queue           |   `- sk_receive_queue
  |      `- skb                     |      `- skb
  |         `- sk == C              |         `- sk == D
  |            `- sk_receive_queue  |           `- sk_receive_queue
  |               `- skb +----------'               `- skb +--.
  |                                                           |
  `-----------------------------------------------------------'

Technically, the graph must be denoted as A <-> B instead of A (-> D)
and B (-> C) to find such a cyclic reference without touching each
socket's receive queue.

  .-> A --. .-- B <-.
  |        X        |  ==  A <-> B
  `-- C <-' `-> D --'

We apply this fixup in unix_add_edges() if the receiver is an embryo
socket.

We also link such edges to the embryo socket because we need
to restore the original separte graphs A (-> D) and B (-> C) in
unix_update_edges() once accept() is called.

Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
---
 include/net/af_unix.h |  2 ++
 net/unix/af_unix.c    |  2 +-
 net/unix/garbage.c    | 29 ++++++++++++++++++++++++++++-
 3 files changed, 31 insertions(+), 2 deletions(-)

diff --git a/include/net/af_unix.h b/include/net/af_unix.h
index 438d2a18ba2e..2d8e93775e61 100644
--- a/include/net/af_unix.h
+++ b/include/net/af_unix.h
@@ -25,6 +25,7 @@ void unix_notinflight(struct user_struct *user, struct file *fp);
 void unix_init_vertex(struct unix_sock *u);
 void unix_add_edges(struct scm_fp_list *fpl, struct unix_sock *receiver);
 void unix_del_edges(struct scm_fp_list *fpl);
+void unix_update_edges(struct unix_sock *receiver);
 int unix_alloc_edges(struct scm_fp_list *fpl);
 void unix_free_edges(struct scm_fp_list *fpl);
 void unix_gc(void);
@@ -40,6 +41,7 @@ struct unix_edge {
 	struct unix_vertex *predecessor;
 	struct unix_vertex *successor;
 	struct list_head entry;
+	struct list_head embryo_entry;
 };
 
 struct sock *unix_peer_get(struct sock *sk);
diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c
index 1ebc3c15f972..dab5d8d96e87 100644
--- a/net/unix/af_unix.c
+++ b/net/unix/af_unix.c
@@ -1734,7 +1734,7 @@ static int unix_accept(struct socket *sock, struct socket *newsock, int flags,
 	}
 
 	tsk = skb->sk;
-	unix_sk(tsk)->listener = NULL;
+	unix_update_edges(unix_sk(tsk));
 	skb_free_datagram(sk, skb);
 	wake_up_interruptible(&unix_sk(sk)->peer_wait);
 
diff --git a/net/unix/garbage.c b/net/unix/garbage.c
index 572ac0994c69..d731438d3c2b 100644
--- a/net/unix/garbage.c
+++ b/net/unix/garbage.c
@@ -115,10 +115,16 @@ static LIST_HEAD(unix_unvisited_vertices);
 
 void unix_add_edges(struct scm_fp_list *fpl, struct unix_sock *receiver)
 {
+	struct unix_vertex *successor;
 	int i = 0, j = 0;
 
 	spin_lock(&unix_gc_lock);
 
+	if (receiver->listener)
+		successor = &unix_sk(receiver->listener)->vertex;
+	else
+		successor = &receiver->vertex;
+
 	while (i < fpl->count_unix) {
 		struct unix_sock *inflight = unix_get_socket(fpl->fp[j++]);
 		struct unix_edge *edge;
@@ -128,13 +134,18 @@ void unix_add_edges(struct scm_fp_list *fpl, struct unix_sock *receiver)
 
 		edge = fpl->edges + i++;
 		edge->predecessor = &inflight->vertex;
-		edge->successor = &receiver->vertex;
+		edge->successor = successor;
 
 		if (!edge->predecessor->out_degree++)
 			list_add_tail(&edge->predecessor->entry, &unix_unvisited_vertices);
 
 		INIT_LIST_HEAD(&edge->entry);
 		list_add_tail(&edge->entry, &edge->predecessor->edges);
+
+		if (receiver->listener) {
+			INIT_LIST_HEAD(&edge->embryo_entry);
+			list_add_tail(&edge->embryo_entry, &receiver->vertex.edges);
+		}
 	}
 
 	spin_unlock(&unix_gc_lock);
@@ -162,6 +173,22 @@ void unix_del_edges(struct scm_fp_list *fpl)
 	fpl->inflight = false;
 }
 
+void unix_update_edges(struct unix_sock *receiver)
+{
+	struct unix_edge *edge;
+
+	spin_lock(&unix_gc_lock);
+
+	list_for_each_entry(edge, &receiver->vertex.edges, embryo_entry)
+		edge->successor = &receiver->vertex;
+
+	list_del_init(&receiver->vertex.edges);
+
+	receiver->listener = NULL;
+
+	spin_unlock(&unix_gc_lock);
+}
+
 int unix_alloc_edges(struct scm_fp_list *fpl)
 {
 	if (!fpl->count_unix)
-- 
2.30.2


  parent reply	other threads:[~2024-02-03  3:03 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-03  3:00 [PATCH v1 net-next 00/16] af_unix: Reimplment GC Kuniyuki Iwashima
2024-02-03  3:00 ` [PATCH v1 net-next 01/16] af_unix: Add struct unix_vertex in struct unix_sock Kuniyuki Iwashima
2024-02-03  3:00 ` [PATCH v1 net-next 02/16] af_unix: Allocate struct unix_edge for each inflight AF_UNIX fd Kuniyuki Iwashima
2024-02-03 20:20   ` kernel test robot
2024-02-03  3:00 ` [PATCH v1 net-next 03/16] af_unix: Link struct unix_edge when queuing skb Kuniyuki Iwashima
2024-02-20 12:06   ` Paolo Abeni
2024-02-03  3:00 ` [PATCH v1 net-next 04/16] af_unix: Save listener for embryo socket Kuniyuki Iwashima
2024-02-03  3:00 ` Kuniyuki Iwashima [this message]
2024-02-03  3:00 ` [PATCH v1 net-next 06/16] af_unix: Bulk update unix_tot_inflight/unix_inflight when queuing skb Kuniyuki Iwashima
2024-02-03  3:00 ` [PATCH v1 net-next 07/16] af_unix: Detect Strongly Connected Components Kuniyuki Iwashima
2024-02-03 19:59   ` kernel test robot
2024-02-03 21:36   ` kernel test robot
2024-02-03  3:00 ` [PATCH v1 net-next 08/16] af_unix: Save O(n) setup of Tarjan's algo Kuniyuki Iwashima
2024-02-03  3:00 ` [PATCH v1 net-next 09/16] af_unix: Avoid Tarjan's algorithm if unnecessary Kuniyuki Iwashima
2024-02-03  3:00 ` [PATCH v1 net-next 10/16] af_unix: Skip GC if no cycle exists Kuniyuki Iwashima
2024-02-03  3:00 ` [PATCH v1 net-next 11/16] af_unix: Assign a unique index to SCC Kuniyuki Iwashima
2024-02-03  3:00 ` [PATCH v1 net-next 12/16] af_unix: Detect dead SCC Kuniyuki Iwashima
2024-02-03  3:00 ` [PATCH v1 net-next 13/16] af_unix: Replace garbage collection algorithm Kuniyuki Iwashima
2024-02-03  3:00 ` [PATCH v1 net-next 14/16] af_unix: Remove scm_fp_dup() in unix_attach_fds() Kuniyuki Iwashima
2024-02-03  3:00 ` [PATCH v1 net-next 15/16] af_unix: Remove lock dance in unix_peek_fds() Kuniyuki Iwashima
2024-02-03  3:00 ` [PATCH v1 net-next 16/16] selftest: af_unix: Test GC for SCM_RIGHTS Kuniyuki Iwashima

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240203030058.60750-6-kuniyu@amazon.com \
    --to=kuniyu@amazon.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=kuba@kernel.org \
    --cc=kuni1840@gmail.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).