netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Paolo Abeni <pabeni@redhat.com>
To: Kuniyuki Iwashima <kuniyu@amazon.com>,
	"David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@google.com>,
	Jakub Kicinski <kuba@kernel.org>
Cc: Kuniyuki Iwashima <kuni1840@gmail.com>, netdev@vger.kernel.org
Subject: Re: [PATCH v1 net-next 03/16] af_unix: Link struct unix_edge when queuing skb.
Date: Tue, 20 Feb 2024 13:06:18 +0100	[thread overview]
Message-ID: <82046b6cf70823e8c17e102c8233f1cb219bb9f5.camel@redhat.com> (raw)
In-Reply-To: <20240203030058.60750-4-kuniyu@amazon.com>

On Fri, 2024-02-02 at 19:00 -0800, Kuniyuki Iwashima wrote:
> Just before queuing skb with inflight fds, we call scm_stat_add(),
> which is a good place to set up the preallocated struct unix_edge
> in UNIXCB(skb).fp->edges.
> 
> Then, we call unix_add_edges() and construct the directed graph
> as follows:
> 
>   1. Set the inflight socket's unix_vertex to unix_edge.predecessor
>   2. Set the receiver's unix_vertex to unix_edge.successor
>   3. Link unix_edge.entry to the inflight socket's unix_vertex.edges
>   4. Link inflight socket's unix_vertex.entry to unix_unvisited_vertices.
> 
> Let's say we pass the fd of AF_UNIX socket A to B and the fd of B
> to C.  The graph looks like this:
> 
>   +-------------------------+
>   | unix_unvisited_vertices | <------------------------.
>   +-------------------------+                          |
>   +                                                    |
>   |   +-------------+                +-------------+   |            +-------------+
>   |   | unix_sock A |                | unix_sock B |   |            | unix_sock C |
>   |   +-------------+                +-------------+   |            +-------------+
>   |   | unix_vertex | <----.  .----> | unix_vertex | <-|--.  .----> | unix_vertex |
>   |   | +-----------+      |  |      | +-----------+   |  |  |      | +-----------+
>   `-> | |   entry   | +------------> | |   entry   | +-'  |  |      | |   entry   |
>       | |-----------|      |  |      | |-----------|      |  |      | |-----------|
>       | |   edges   | <-.  |  |      | |   edges   | <-.  |  |      | |   edges   |
>       +-+-----------+   |  |  |      +-+-----------+   |  |  |      +-+-----------+
>                         |  |  |                        |  |  |
>   .---------------------'  |  |  .---------------------'  |  |
>   |                        |  |  |                        |  |
>   |   +-------------+      |  |  |   +-------------+      |  |
>   |   |  unix_edge  |      |  |  |   |  unix_edge  |      |  |
>   |   +-------------+      |  |  |   +-------------+      |  |
>   `-> |    entry    |      |  |  `-> |    entry    |      |  |
>       |-------------|      |  |      |-------------|      |  |
>       | predecessor | +----'  |      | predecessor | +----'  |
>       |-------------|         |      |-------------|         |
>       |  successor  | +-------'      |  successor  | +-------'
>       +-------------+                +-------------+
> 
> Henceforth, we denote such a graph as A -> B (-> C).
> 
> Now, we can express all inflight fd graphs that do not contain
> embryo sockets.  The following two patches will support the
> particular case.
> 
> Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
> ---
>  include/net/af_unix.h |  2 ++
>  include/net/scm.h     |  1 +
>  net/core/scm.c        |  2 ++
>  net/unix/af_unix.c    |  8 +++++--
>  net/unix/garbage.c    | 56 ++++++++++++++++++++++++++++++++++++++++++-
>  5 files changed, 66 insertions(+), 3 deletions(-)
> 
> diff --git a/include/net/af_unix.h b/include/net/af_unix.h
> index cab9dfb666f3..54d62467a70b 100644
> --- a/include/net/af_unix.h
> +++ b/include/net/af_unix.h
> @@ -23,6 +23,8 @@ extern unsigned int unix_tot_inflight;
>  void unix_inflight(struct user_struct *user, struct file *fp);
>  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);
>  int unix_alloc_edges(struct scm_fp_list *fpl);
>  void unix_free_edges(struct scm_fp_list *fpl);
>  void unix_gc(void);
> diff --git a/include/net/scm.h b/include/net/scm.h
> index a1142dee086c..7d807fe466a3 100644
> --- a/include/net/scm.h
> +++ b/include/net/scm.h
> @@ -32,6 +32,7 @@ struct scm_fp_list {
>  	short			count_unix;
>  	short			max;
>  #ifdef CONFIG_UNIX
> +	bool			inflight;
>  	struct unix_edge	*edges;
>  #endif
>  	struct user_struct	*user;
> diff --git a/net/core/scm.c b/net/core/scm.c
> index 8661524ed6e5..d141c00eb116 100644
> --- a/net/core/scm.c
> +++ b/net/core/scm.c
> @@ -87,6 +87,7 @@ static int scm_fp_copy(struct cmsghdr *cmsg, struct scm_fp_list **fplp)
>  		*fplp = fpl;
>  		fpl->count = 0;
>  		fpl->count_unix = 0;
> +		fpl->inflight = false;
>  		fpl->edges = NULL;
>  		fpl->max = SCM_MAX_FD;
>  		fpl->user = NULL;
> @@ -378,6 +379,7 @@ struct scm_fp_list *scm_fp_dup(struct scm_fp_list *fpl)
>  		for (i = 0; i < fpl->count; i++)
>  			get_file(fpl->fp[i]);
>  
> +		new_fpl->inflight = false;
>  		new_fpl->edges = NULL;
>  		new_fpl->max = new_fpl->count;
>  		new_fpl->user = get_uid(fpl->user);
> diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c
> index 0391f66546a6..ea7bac18a781 100644
> --- a/net/unix/af_unix.c
> +++ b/net/unix/af_unix.c
> @@ -1956,8 +1956,10 @@ static void scm_stat_add(struct sock *sk, struct sk_buff *skb)
>  	struct scm_fp_list *fp = UNIXCB(skb).fp;
>  	struct unix_sock *u = unix_sk(sk);
>  
> -	if (unlikely(fp && fp->count))
> +	if (unlikely(fp && fp->count)) {
>  		atomic_add(fp->count, &u->scm_stat.nr_fds);
> +		unix_add_edges(fp, u);
> +	}
>  }
>  
>  static void scm_stat_del(struct sock *sk, struct sk_buff *skb)
> @@ -1965,8 +1967,10 @@ static void scm_stat_del(struct sock *sk, struct sk_buff *skb)
>  	struct scm_fp_list *fp = UNIXCB(skb).fp;
>  	struct unix_sock *u = unix_sk(sk);
>  
> -	if (unlikely(fp && fp->count))
> +	if (unlikely(fp && fp->count)) {
>  		atomic_sub(fp->count, &u->scm_stat.nr_fds);
> +		unix_del_edges(fp);
> +	}
>  }
>  
>  /*
> diff --git a/net/unix/garbage.c b/net/unix/garbage.c
> index 6a3572e43b9f..572ac0994c69 100644
> --- a/net/unix/garbage.c
> +++ b/net/unix/garbage.c
> @@ -110,6 +110,58 @@ void unix_init_vertex(struct unix_sock *u)
>  	INIT_LIST_HEAD(&vertex->entry);
>  }
>  
> +DEFINE_SPINLOCK(unix_gc_lock);
> +static LIST_HEAD(unix_unvisited_vertices);
> +
> +void unix_add_edges(struct scm_fp_list *fpl, struct unix_sock *receiver)
> +{
> +	int i = 0, j = 0;
> +
> +	spin_lock(&unix_gc_lock);
> +
> +	while (i < fpl->count_unix) {
> +		struct unix_sock *inflight = unix_get_socket(fpl->fp[j++]);
> +		struct unix_edge *edge;
> +
> +		if (!inflight)
> +			continue;
> +
> +		edge = fpl->edges + i++;
> +		edge->predecessor = &inflight->vertex;
> +		edge->successor = &receiver->vertex;
> +
> +		if (!edge->predecessor->out_degree++)
> +			list_add_tail(&edge->predecessor->entry, &unix_unvisited_vertices);
> +
> +		INIT_LIST_HEAD(&edge->entry);

Here  'edge->predecessor->entry' and 'edge->entry' refer to different
object types right ? edge vs vertices. Perhaps using different field
names could clarify the code a bit? 

Also the edge->entry initialization just before the list_add_tail below
looks strange/suspect. Perhaps it would be better to the init at
allocation time?

Thanks!

Paolo


  reply	other threads:[~2024-02-20 12:06 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 [this message]
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 ` [PATCH v1 net-next 05/16] af_unix: Fix up unix_edge.successor " Kuniyuki Iwashima
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=82046b6cf70823e8c17e102c8233f1cb219bb9f5.camel@redhat.com \
    --to=pabeni@redhat.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=kuba@kernel.org \
    --cc=kuni1840@gmail.com \
    --cc=kuniyu@amazon.com \
    --cc=netdev@vger.kernel.org \
    /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).