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 00/16] af_unix: Reimplment GC.
Date: Fri, 2 Feb 2024 19:00:42 -0800	[thread overview]
Message-ID: <20240203030058.60750-1-kuniyu@amazon.com> (raw)

When we pass a file descriptor to an AF_UNIX socket via SCM_RIGTHS,
the underlying struct file of the inflight fd gets its refcount bumped.
If the fd is of an AF_UNIX socket, we need to track it in case it forms
cyclic references.

Let's say we send a fd of AF_UNIX socket A to B and vice versa and
close() both sockets.

When created, each socket's struct file initially has one reference.
After the fd exchange, both refcounts are bumped up to 2.  Then, close()
decreases both to 1.  From this point on, no one can touch the file/socket.

However, the struct file has one refcount and thus never calls the
release() function of the AF_UNIX socket.

That's why we need to track all inflight AF_UNIX sockets and run garbage
collection.

This series replaces the current GC implementation that locks each inflight
socket's receive queue and requires trickiness in other places.

The new GC does not lock each socket's queue to minimise its effect and
tries to be lightweight if there is no cyclic reference or no update in
the shape of the inflight fd graph.

The new implementation is based on Tarjan's Strongly Connected Components
algorithm, and we consider each inflight file descriptor of AF_UNIX sockets
as an edge in a directed graph.

If socket B has the fd of socket A, socket B holds refcount of A, and then
we consider it as A -> B.

For the details, please see each patch.

  patch 1  -  5 : Add struct to express graphs
  patch       6 : Optimse inflight fd counters
  patch       7 : Group SCC
  patch 8  - 10 : Make GC lightweight
  patch 11 - 12 : Detect dead cycle reference
  patch      13 : Replace GC algorithm
  patch 14 - 15 : Remove confusing tricks
  patch      16 : selftest


Kuniyuki Iwashima (16):
  af_unix: Add struct unix_vertex in struct unix_sock.
  af_unix: Allocate struct unix_edge for each inflight AF_UNIX fd.
  af_unix: Link struct unix_edge when queuing skb.
  af_unix: Save listener for embryo socket.
  af_unix: Fix up unix_edge.successor for embryo socket.
  af_unix: Bulk update unix_tot_inflight/unix_inflight when queuing skb.
  af_unix: Detect Strongly Connected Components.
  af_unix: Save O(n) setup of Tarjan's algo.
  af_unix: Avoid Tarjan's algorithm if unnecessary.
  af_unix: Skip GC if no cycle exists.
  af_unix: Assign a unique index to SCC.
  af_unix: Detect dead SCC.
  af_unix: Replace garbage collection algorithm.
  af_unix: Remove scm_fp_dup() in unix_attach_fds().
  af_unix: Remove lock dance in unix_peek_fds().
  selftest: af_unix: Test GC for SCM_RIGHTS.

 include/net/af_unix.h                         |  34 +-
 include/net/scm.h                             |   8 +
 net/core/scm.c                                |   5 +
 net/unix/af_unix.c                            |  78 +--
 net/unix/garbage.c                            | 459 +++++++++++-------
 tools/testing/selftests/net/.gitignore        |   1 +
 tools/testing/selftests/net/af_unix/Makefile  |   2 +-
 .../selftests/net/af_unix/scm_rights.c        | 242 +++++++++
 8 files changed, 580 insertions(+), 249 deletions(-)
 create mode 100644 tools/testing/selftests/net/af_unix/scm_rights.c

-- 
2.30.2


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

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-03  3:00 Kuniyuki Iwashima [this message]
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 ` [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=20240203030058.60750-1-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).