mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Cong Wang <xiyou.wangcong@gmail.com>
To: linux-kernel@vger.kernel.org
Cc: Andy Lutomirski <luto@amacapital.net>,
	Kees Cook <kees@kernel.org>, Will Drewry <wad@chromium.org>,
	Christian Brauner <brauner@kernel.org>
Subject: [RFC PATCH 5/9] seccomp: factor out the user notification queue and wait
Date: Mon, 19 Oct 2026 17:11:20 -0700	[thread overview]
Message-ID: <20261020001124.974063-6-xiyou.wangcong@gmail.com> (raw)
In-Reply-To: <20261020001124.974063-1-xiyou.wangcong@gmail.com>

From: Cong Wang <cwang@multikernel.io>

Split seccomp_do_user_notification() into helpers that queue a
notification, wait for its reply and take it off the list again. The
next patch queues a second kind of notification where a system call's
result is reported, and reuses all three.

No functional change.

Signed-off-by: Cong Wang <cwang@multikernel.io>
---
 kernel/seccomp.c | 153 ++++++++++++++++++++++++++++-------------------
 1 file changed, 92 insertions(+), 61 deletions(-)

diff --git a/kernel/seccomp.c b/kernel/seccomp.c
index 336a7c28e1d6..793efcf0e626 100644
--- a/kernel/seccomp.c
+++ b/kernel/seccomp.c
@@ -1235,6 +1235,91 @@ static bool should_sleep_killable(struct seccomp_filter *match,
 	return match->wait_killable_recv && n->state >= SECCOMP_NOTIFY_SENT;
 }
 
+static void seccomp_notify_queue(struct seccomp_filter *match,
+				 struct seccomp_knotif *n)
+{
+	lockdep_assert_held(&match->notify_lock);
+
+	n->state = SECCOMP_NOTIFY_INIT;
+	init_completion(&n->ready);
+	list_add_tail(&n->list, &match->notif->notifications);
+	INIT_LIST_HEAD(&n->addfd);
+
+	atomic_inc(&match->notif->requests);
+	if (match->notif->flags & SECCOMP_USER_NOTIF_FD_SYNC_WAKE_UP)
+		wake_up_poll_on_current_cpu(&match->wqh, EPOLLIN | EPOLLRDNORM);
+	else
+		wake_up_poll(&match->wqh, EPOLLIN | EPOLLRDNORM);
+}
+
+/* Returns 0 once @n is replied to, or the error that interrupted the wait. */
+static int seccomp_notify_wait(struct seccomp_filter *match,
+			       struct seccomp_knotif *n)
+{
+	struct seccomp_kaddfd *addfd;
+	int err;
+
+	lockdep_assert_held(&match->notify_lock);
+
+	do {
+		bool wait_killable = should_sleep_killable(match, n);
+
+		mutex_unlock(&match->notify_lock);
+		if (wait_killable)
+			err = wait_for_completion_killable(&n->ready);
+		else
+			err = wait_for_completion_interruptible(&n->ready);
+		mutex_lock(&match->notify_lock);
+
+		if (err != 0) {
+			/*
+			 * Check to see whether we should switch to wait
+			 * killable. Only return the interrupted error if not.
+			 */
+			if (!(!wait_killable && should_sleep_killable(match, n)))
+				return err;
+		}
+
+		addfd = list_first_entry_or_null(&n->addfd,
+						 struct seccomp_kaddfd, list);
+		/* Check if we were woken up by a addfd message */
+		if (addfd)
+			seccomp_handle_addfd(addfd, n);
+
+	}  while (n->state != SECCOMP_NOTIFY_REPLIED);
+
+	return 0;
+}
+
+static void seccomp_notify_dequeue(struct seccomp_filter *match,
+				   struct seccomp_knotif *n)
+{
+	struct seccomp_kaddfd *addfd, *tmp;
+
+	lockdep_assert_held(&match->notify_lock);
+
+	/* If there were any pending addfd calls, clear them out */
+	list_for_each_entry_safe(addfd, tmp, &n->addfd, list) {
+		/* The process went away before we got a chance to handle it */
+		addfd->ret = -ESRCH;
+		list_del_init(&addfd->list);
+		complete(&addfd->completion);
+	}
+
+	/*
+	 * Note that it's possible the listener died in between the time when
+	 * we were notified of a response (or a signal) and when we were able to
+	 * re-acquire the lock, so only delete from the list if the
+	 * notification actually exists.
+	 *
+	 * Also note that this test is only valid because there's no way to
+	 * *reattach* to a notifier right now. If one is added, we'll need to
+	 * keep track of the notif itself and make sure they match here.
+	 */
+	if (match->notif)
+		list_del(&n->list);
+}
+
 /* Report the system call's outcome on the way back to user space. */
 static void seccomp_result_work(struct callback_head *work)
 {
@@ -1276,7 +1361,6 @@ static int seccomp_do_user_notification(int this_syscall,
 	u32 flags = 0;
 	long ret = 0;
 	struct seccomp_knotif n = {};
-	struct seccomp_kaddfd *addfd, *tmp;
 
 	mutex_lock(&match->notify_lock);
 	err = -ENOSYS;
@@ -1284,74 +1368,21 @@ static int seccomp_do_user_notification(int this_syscall,
 		goto out;
 
 	n.task = current;
-	n.state = SECCOMP_NOTIFY_INIT;
 	n.data = sd;
 	n.id = seccomp_next_notify_id(match);
-	init_completion(&n.ready);
-	list_add_tail(&n.list, &match->notif->notifications);
-	INIT_LIST_HEAD(&n.addfd);
-
-	atomic_inc(&match->notif->requests);
-	if (match->notif->flags & SECCOMP_USER_NOTIF_FD_SYNC_WAKE_UP)
-		wake_up_poll_on_current_cpu(&match->wqh, EPOLLIN | EPOLLRDNORM);
-	else
-		wake_up_poll(&match->wqh, EPOLLIN | EPOLLRDNORM);
+	seccomp_notify_queue(match, &n);
 
 	/*
 	 * This is where we wait for a reply from userspace.
 	 */
-	do {
-		bool wait_killable = should_sleep_killable(match, &n);
-
-		mutex_unlock(&match->notify_lock);
-		if (wait_killable)
-			err = wait_for_completion_killable(&n.ready);
-		else
-			err = wait_for_completion_interruptible(&n.ready);
-		mutex_lock(&match->notify_lock);
-
-		if (err != 0) {
-			/*
-			 * Check to see whether we should switch to wait
-			 * killable. Only return the interrupted error if not.
-			 */
-			if (!(!wait_killable && should_sleep_killable(match, &n)))
-				goto interrupted;
-		}
-
-		addfd = list_first_entry_or_null(&n.addfd,
-						 struct seccomp_kaddfd, list);
-		/* Check if we were woken up by a addfd message */
-		if (addfd)
-			seccomp_handle_addfd(addfd, &n);
-
-	}  while (n.state != SECCOMP_NOTIFY_REPLIED);
-
-	ret = n.val;
-	err = n.error;
-	flags = n.flags;
-
-interrupted:
-	/* If there were any pending addfd calls, clear them out */
-	list_for_each_entry_safe(addfd, tmp, &n.addfd, list) {
-		/* The process went away before we got a chance to handle it */
-		addfd->ret = -ESRCH;
-		list_del_init(&addfd->list);
-		complete(&addfd->completion);
+	err = seccomp_notify_wait(match, &n);
+	if (!err) {
+		ret = n.val;
+		err = n.error;
+		flags = n.flags;
 	}
 
-	/*
-	 * Note that it's possible the listener died in between the time when
-	 * we were notified of a response (or a signal) and when we were able to
-	 * re-acquire the lock, so only delete from the list if the
-	 * notification actually exists.
-	 *
-	 * Also note that this test is only valid because there's no way to
-	 * *reattach* to a notifier right now. If one is added, we'll need to
-	 * keep track of the notif itself and make sure they match here.
-	 */
-	if (match->notif)
-		list_del(&n.list);
+	seccomp_notify_dequeue(match, &n);
 out:
 	mutex_unlock(&match->notify_lock);
 
-- 
2.43.0


  parent reply	other threads:[~2026-10-20  0:11 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-20  0:11 [RFC PATCH 0/9] seccomp: report the result of continued system calls Cong Wang
2026-10-20  0:11 ` [RFC PATCH 1/9] seccomp: add result records to the notification listener Cong Wang
2026-10-20  0:11 ` [RFC PATCH 2/9] seccomp: add __NR_seccomp_* aliases for the fork family Cong Wang
2026-10-20  0:11 ` [RFC PATCH 3/9] seccomp: report the result of a continued system call Cong Wang
2026-10-20  0:11 ` [RFC PATCH 4/9] seccomp: report the result of a continued fork Cong Wang
2026-10-20  0:11 ` Cong Wang [this message]
2026-10-20  0:11 ` [RFC PATCH 6/9] seccomp: add SECCOMP_USER_NOTIF_FLAG_HOLD Cong Wang
2026-10-20  0:11 ` [RFC PATCH 7/9] selftests/seccomp: test fork result records Cong Wang
2026-10-20  0:11 ` [RFC PATCH 8/9] selftests/seccomp: test held fork results Cong Wang
2026-10-20  0:11 ` [RFC PATCH 9/9] Documentation: seccomp: describe system call result records Cong Wang

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=20261020001124.974063-6-xiyou.wangcong@gmail.com \
    --to=xiyou.wangcong@gmail.com \
    --cc=brauner@kernel.org \
    --cc=kees@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luto@amacapital.net \
    --cc=wad@chromium.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

all inboxes | Powered by JetHome®