Netdev List
 help / color / mirror / Atom feed
From: Paolo Abeni <pabeni@redhat.com>
To: Nam Cao <namcao@linutronix.de>,
	Kuniyuki Iwashima <kuniyu@google.com>,
	"David S . Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@google.com>,
	Jakub Kicinski <kuba@kernel.org>, Simon Horman <horms@kernel.org>,
	netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-rt-devel@lists.linux.dev
Subject: Re: [PATCH net-next v3 1/3] af_unix: Schedule the garbage collector at task exit
Date: Tue, 28 Jul 2026 11:50:31 +0200	[thread overview]
Message-ID: <e9709a13-9fd9-4ee3-88b0-9cc56a1ca07c@redhat.com> (raw)
In-Reply-To: <ba4ed2717e5225b7b77ef928fb97ee5544632811.1784712370.git.namcao@linutronix.de>

On 7/22/26 11:31 AM, Nam Cao wrote:
> When a task exits while still having dead cyclic reference AF_UNIX sockets,
> those sockets stay behind indefinitely until the garbage collector gets
> scheduled by an unrelated reason. This can be observed with the program
> below.
> 
> Resolve this issue by scheduling the garbage collector during task exit,
> after the task's file descriptors have been closed.
> 
>  #include <sys/mount.h>
>  #include <sys/socket.h>
>  #include <sys/un.h>
>  #include <sys/wait.h>
> 
>  #include <errno.h>
>  #include <fcntl.h>
>  #include <stdio.h>
>  #include <stdlib.h>
>  #include <string.h>
>  #include <unistd.h>
> 
> static int send_fd(int unix_fd, int fd)
> {
>         struct msghdr msgh;
>         struct cmsghdr *cmsg;
>         char buf[CMSG_SPACE(sizeof(fd))];
> 
>         memset(&msgh, 0, sizeof(msgh));
> 
>         memset(buf, 0, sizeof(buf));
>         msgh.msg_control = buf;
>         msgh.msg_controllen = sizeof(buf);
> 
>         cmsg = CMSG_FIRSTHDR(&msgh);
>         cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
>         cmsg->cmsg_level = SOL_SOCKET;
>         cmsg->cmsg_type = SCM_RIGHTS;
> 
>         msgh.msg_controllen = cmsg->cmsg_len;
> 
>         memcpy(CMSG_DATA(cmsg), &fd, sizeof(fd));
>         return sendmsg(unix_fd, &msgh, 0);
> }
> 
> int main(int argc, char *argv[])
> {
> 	int fd[2];
> 	int i;
> 
> 	for (int n = 0; n < 100; ++n) {
> 		if (socketpair(PF_UNIX, SOCK_SEQPACKET, 0, fd) == -1)
> 			goto out_error;
> 
> 		for (i = 0; i < 100; ++i) {
> 			if (send_fd(fd[0], fd[0]) == -1)
> 				goto out_error;
> 
> 			if (send_fd(fd[1], fd[1]) == -1)
> 				goto out_error;
> 		}
> 	}
> 
> 	return 0;
> 
> out_error:
> 	fprintf(stderr, "error: %s\n", strerror(errno));
> }
> 
> Signed-off-by: Nam Cao <namcao@linutronix.de>
> ---
>  include/net/af_unix.h | 5 +++++
>  kernel/exit.c         | 7 +++++++
>  net/unix/af_unix.h    | 1 -
>  3 files changed, 12 insertions(+), 1 deletion(-)
> 
> diff --git a/include/net/af_unix.h b/include/net/af_unix.h
> index 34f53dde65ce..686f6f1d1c21 100644
> --- a/include/net/af_unix.h
> +++ b/include/net/af_unix.h
> @@ -14,11 +14,16 @@
>  
>  #if IS_ENABLED(CONFIG_UNIX)
>  struct unix_sock *unix_get_socket(struct file *filp);
> +void unix_schedule_gc(struct user_struct *user);
>  #else
>  static inline struct unix_sock *unix_get_socket(struct file *filp)
>  {
>  	return NULL;
>  }
> +
> +static inline void unix_schedule_gc(struct user_struct *user)
> +{
> +}
>  #endif
>  
>  struct unix_address {
> diff --git a/kernel/exit.c b/kernel/exit.c
> index 2c0b1c02920f..593ac4b0105f 100644
> --- a/kernel/exit.c
> +++ b/kernel/exit.c
> @@ -71,6 +71,7 @@
>  #include <linux/unwind_deferred.h>
>  #include <linux/uaccess.h>
>  #include <linux/pidfs.h>
> +#include <net/af_unix.h>
>  
>  #include <uapi/linux/wait.h>
>  
> @@ -1009,6 +1010,12 @@ void __noreturn do_exit(long code)
>  	exit_task_work(tsk);
>  	exit_thread(tsk);
>  
> +	/*
> +	 * Must be after exit_files() and exit_task_work(tsk) to ensure that
> +	 * the task's AF_UNIX sockets have all been closed.
> +	 */
> +	unix_schedule_gc(NULL);

Sashiko noted this can cause relevant regressions:

https://sashiko.dev/#/patchset/cover.1784712370.git.namcao%40linutronix.de

Generally speaking hooking the unix GC at task exit time does not feel
right (layering violation). Perhaps scheduling the GC with a reasonable
timeout in unix_update_graph() would be better?

/P


  reply	other threads:[~2026-07-28  9:50 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-22  9:31 [PATCH net-next v3 0/3] af_unix: Fix priority inversion issue Nam Cao
2026-07-22  9:31 ` [PATCH net-next v3 1/3] af_unix: Schedule the garbage collector at task exit Nam Cao
2026-07-28  9:50   ` Paolo Abeni [this message]
2026-07-22  9:31 ` [PATCH net-next v3 2/3] af_unix: Do not wait for garbage collector in sendmsg() Nam Cao
2026-07-22  9:31 ` [PATCH net-next v3 3/3] af_unix: Clean up unix_schedule_gc() Nam Cao

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=e9709a13-9fd9-4ee3-88b0-9cc56a1ca07c@redhat.com \
    --to=pabeni@redhat.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=horms@kernel.org \
    --cc=kuba@kernel.org \
    --cc=kuniyu@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-rt-devel@lists.linux.dev \
    --cc=namcao@linutronix.de \
    --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