From nobody Sat Nov 2 18:18:21 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 922E8C64EC4 for ; Wed, 8 Mar 2023 07:32:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229558AbjCHHcX (ORCPT ); Wed, 8 Mar 2023 02:32:23 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55584 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229786AbjCHHcS (ORCPT ); Wed, 8 Mar 2023 02:32:18 -0500 Received: from mail-pf1-x44a.google.com (mail-pf1-x44a.google.com [IPv6:2607:f8b0:4864:20::44a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 856D9A838D for ; Tue, 7 Mar 2023 23:32:12 -0800 (PST) Received: by mail-pf1-x44a.google.com with SMTP id w3-20020aa78583000000b005d244af158eso8586521pfn.23 for ; Tue, 07 Mar 2023 23:32:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1678260732; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=KdP6YpLIkrgOw0lYy0bdzpscqpNI4s/RcyKWmelvr0E=; b=QLqdFxWJJTCcjLfJJw26wi68d2otuqveE486Q1LDW5sQla52Ywi+1Tl4jv3D4AxLUw 3avLGCXQb+mv1yWcYc08B7I0oRGzwex3vI06gsV9j61XaiBQsgKuYpRXvXpAalOSSNev 9uW+1N9swUVFYiNNUrLahwCYNt8pSOAyNrjbAuij0LyQiw2yMX2MGn3B10pJ9i8bTgk8 pmgTIp307skmeFk5+T/6dvS64gc5D0SRFQVxDOobzth79IinQln6GNfwHB1AE9clAVAn dBp7xSgYLwfXWfxPMz/dmFQ/DMUnjXmUJAoQl3bjX5c4uxyGngzH+SlozLWaWG8rRxiF LfNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678260732; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=KdP6YpLIkrgOw0lYy0bdzpscqpNI4s/RcyKWmelvr0E=; b=QFiuRgX2xCLHr9YfL6uOxZiJ8F1YwAawOOYt2Xzm8/B/hKkW5k4UnluRs6YLUQmMHS CDqDa4zXovp10OK3jiJVCHmXG7uH2UKd5W9JHvEEPfkgd40P06WM5EYsHmFXAdfPiTHE 1gEzFP2FQ0rbIJsCx6K72w0yThWQMEqWRlk5IUgnmsymYvXYqpcN++0FzEes7RnRrTub tMSmjtaxzT+ddK8utoouvNH4HdWu5iCDKhZFof3fQkKAGaGnHa/R///Dfc42QPWGzgfy 1xk3NrmhT3WdIOEtfIH6tXyaVAR19NOekq+8ToEt77XtHYqytMNrKGd/O962ZaEDKKBU POuQ== X-Gm-Message-State: AO0yUKUDaHwfIb8Eau5/KHuPxT6LBvSPghaxDu2HxRZjn41z03bAdpBe GaYo8t/jTARp86P6FN2qWHJeLEV9ID0= X-Google-Smtp-Source: AK7set8B3jj38rn8ptglrW7yWvEHIdy5OdCXZtLU8YjO4V8UaFgGNiU2Ro861eQ0I/L5FK8DARZmS6xetmo= X-Received: from avagin.kir.corp.google.com ([2620:0:1008:11:b53:99a6:b4fe:b30b]) (user=avagin job=sendgmr) by 2002:a17:902:f783:b0:19c:140d:aada with SMTP id q3-20020a170902f78300b0019c140daadamr6499087pln.2.1678260731766; Tue, 07 Mar 2023 23:32:11 -0800 (PST) Date: Tue, 7 Mar 2023 23:31:56 -0800 In-Reply-To: <20230308073201.3102738-1-avagin@google.com> Mime-Version: 1.0 References: <20230308073201.3102738-1-avagin@google.com> X-Mailer: git-send-email 2.40.0.rc0.216.gc4246ad0f0-goog Message-ID: <20230308073201.3102738-2-avagin@google.com> Subject: [PATCH 1/6] seccomp: don't use semaphore and wait_queue together From: Andrei Vagin To: Kees Cook , Peter Zijlstra Cc: linux-kernel@vger.kernel.org, Christian Brauner , Chen Yu , avagin@gmail.com, Andrei Vagin , Andy Lutomirski , Dietmar Eggemann , Ingo Molnar , Juri Lelli , Peter Oskolkov , Tycho Andersen , Will Drewry , Vincent Guittot Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The main reason is to use new wake_up helpers that will be added in the following patches. But here are a few other reasons: * if we use two different ways, we always need to call them both. This patch fixes seccomp_notify_recv where we forgot to call wake_up_poll in the error path. * If we use one primitive, we can control how many waiters are woken up for each request. Our goal is to wake up just one that will handle a request. Right now, wake_up_poll can wake up one waiter and up(&match->notif->request) can wake up one more. Signed-off-by: Andrei Vagin --- kernel/seccomp.c | 41 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/kernel/seccomp.c b/kernel/seccomp.c index cebf26445f9e..9fca9345111c 100644 --- a/kernel/seccomp.c +++ b/kernel/seccomp.c @@ -145,7 +145,7 @@ struct seccomp_kaddfd { * @notifications: A list of struct seccomp_knotif elements. */ struct notification { - struct semaphore request; + atomic_t requests; u64 next_id; struct list_head notifications; }; @@ -1116,7 +1116,7 @@ static int seccomp_do_user_notification(int this_sysc= all, list_add_tail(&n.list, &match->notif->notifications); INIT_LIST_HEAD(&n.addfd); =20 - up(&match->notif->request); + atomic_inc(&match->notif->requests); wake_up_poll(&match->wqh, EPOLLIN | EPOLLRDNORM); =20 /* @@ -1450,6 +1450,37 @@ find_notification(struct seccomp_filter *filter, u64= id) return NULL; } =20 +static int recv_wake_function(wait_queue_entry_t *wait, unsigned int mode,= int sync, + void *key) +{ + /* Avoid a wakeup if event not interesting for us. */ + if (key && !(key_to_poll(key) & (EPOLLIN | EPOLLERR))) + return 0; + return autoremove_wake_function(wait, mode, sync, key); +} + +static int recv_wait_event(struct seccomp_filter *filter) +{ + DEFINE_WAIT_FUNC(wait, recv_wake_function); + int ret; + + if (atomic_dec_if_positive(&filter->notif->requests) >=3D 0) + return 0; + + for (;;) { + ret =3D prepare_to_wait_event(&filter->wqh, &wait, TASK_INTERRUPTIBLE); + + if (atomic_dec_if_positive(&filter->notif->requests) >=3D 0) + break; + + if (ret) + return ret; + + schedule(); + } + finish_wait(&filter->wqh, &wait); + return 0; +} =20 static long seccomp_notify_recv(struct seccomp_filter *filter, void __user *buf) @@ -1467,7 +1498,7 @@ static long seccomp_notify_recv(struct seccomp_filter= *filter, =20 memset(&unotif, 0, sizeof(unotif)); =20 - ret =3D down_interruptible(&filter->notif->request); + ret =3D recv_wait_event(filter); if (ret < 0) return ret; =20 @@ -1515,7 +1546,8 @@ static long seccomp_notify_recv(struct seccomp_filter= *filter, if (should_sleep_killable(filter, knotif)) complete(&knotif->ready); knotif->state =3D SECCOMP_NOTIFY_INIT; - up(&filter->notif->request); + atomic_inc(&filter->notif->requests); + wake_up_poll(&filter->wqh, EPOLLIN | EPOLLRDNORM); } mutex_unlock(&filter->notify_lock); } @@ -1777,7 +1809,6 @@ static struct file *init_listener(struct seccomp_filt= er *filter) if (!filter->notif) goto out; =20 - sema_init(&filter->notif->request, 0); filter->notif->next_id =3D get_random_u64(); INIT_LIST_HEAD(&filter->notif->notifications); =20 --=20 2.40.0.rc0.216.gc4246ad0f0-goog