From patchwork Thu Oct 20 01:10:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrei Vagin X-Patchwork-Id: 5947 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp1504wrs; Wed, 19 Oct 2022 18:13:01 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7g7GVKqqAzzMkdG8MweW3/OuCfDb1E3AyDAr8dMr0EZ4oqXifS4NaVKJjl3tD3sBUabTew X-Received: by 2002:a17:903:1109:b0:179:d220:1f55 with SMTP id n9-20020a170903110900b00179d2201f55mr11164473plh.42.1666228381570; Wed, 19 Oct 2022 18:13:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666228381; cv=none; d=google.com; s=arc-20160816; b=MCFCWNL50oI19dBtPyGBwzCe8O17eLKSudq6KPDuHv8LCAvaEaYP2Dr+7nczlxBoln 5qDuVQsKqvyt18xS9kjXYkpiW302i8oOfqZ2YSvfjT8WVtroV65AZqJgEyyRbeGYSZMN StVgx2/tzWy4yjWCixg1koPuc8nk/f0BfBl09FjSmYzUxDYI6hTPap4bAHgzEiW+S9Mz Qqnkeg31F/jAOVVxOU9Df13ZdbcHvAAaFulgzeDFyCC/546oTGM1Ow6U6ke7Aj0sGlCI lfw3IgySRnFd7nYcG8NNzWNQH90O9uzjgzt2HUM4iV80z11PDRobtzqqg3q6meqsTOUj /5Xw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=MSR840Eg0KSO7RPBBBPHvoR30CLZFIHcH+FNf/fc5cI=; b=CKFhdlnwTT4sIpBTP4uR/PMQ1aXWKUR1phEuYlTHg8hSstypjCfiTZh1ajYiHgG8vr 7A2zmNTMyKm7cYG079vpUMTb9P5ry/J7J/9ylqPtR760k5N6Ugv0Zyi99V8/kLOcFLoL r0aNAsIASGoC7J2cwQLkt2EsVPXF32oR8UBWiMKAG5KR04/whMlv4FDCDHyjTuhlkkcd HTLfFRNxeAKaYkXpg/mbCkTSj2EWTkL0UjAuUsTJ9md4SmWareZW5fl5SKvB4ZcybU3j OSa8tuRoEII7qJzd2QqqmtgpC9NTBzXLQUR9zarhYEtevPEZDHG1xTuKzsmg0oYgjLej awiw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=l+CJQs4p; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s19-20020a056a00195300b0056183db44afsi21913155pfk.102.2022.10.19.18.12.49; Wed, 19 Oct 2022 18:13:01 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=l+CJQs4p; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229930AbiJTBLF (ORCPT + 99 others); Wed, 19 Oct 2022 21:11:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59534 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229755AbiJTBLC (ORCPT ); Wed, 19 Oct 2022 21:11:02 -0400 Received: from mail-pl1-x62d.google.com (mail-pl1-x62d.google.com [IPv6:2607:f8b0:4864:20::62d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 95B481645EB for ; Wed, 19 Oct 2022 18:11:01 -0700 (PDT) Received: by mail-pl1-x62d.google.com with SMTP id c24so18897775plo.3 for ; Wed, 19 Oct 2022 18:11:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=MSR840Eg0KSO7RPBBBPHvoR30CLZFIHcH+FNf/fc5cI=; b=l+CJQs4pADn878a0a4dqqLU5neAcFyZiJHhd1n/aek+VbEfNXwAY+ShQNj0RZ7zeHS H6ubAfJtsH7bNV13I9MtI2TdwsoRnDZyvxNNMuCY9ZaWhJ56bySUCz+WiMjvAqIA/6sN S/YHGxVxcCKbAc7IwxuVeeq3XwULZCC0gciZeeYeLb0B0p3E98bGhDHd1HddknTO+TeS FeNCt8WoP1HTb4IBmY23D2fGJfrVfh0bbVjNbyFI+MZIX+xUa368nTUHgq+YgZjwkRwr ThgEm7olehdHIDyvegW74ncxM8QSHMNvGHtvAnTDyL65QY8l5LDd26vt2A3sbXxbO0XI i8OA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=MSR840Eg0KSO7RPBBBPHvoR30CLZFIHcH+FNf/fc5cI=; b=stZU4b9xmux3s1u3+ERRvLAClWa83dssai5PsXaMx7yvpfSfj5xMnosYbeDxHchkp7 g05S8lC/35kdrl8K4MWFxR8nuluiVyOzwnEk4FJulroqg+9pCgymIyU+gM5r+8t8LAc8 aBzJ4z2gbxigfaWafbQqqR0MdHt4smM7xovKizYU+YI4Ty4gGAN6CF9n9F9mfBQwUl4k 0bgeKIWD0uSrjHd+xQECZmF4RTYXBq54q3SAAkHGYgQqenKMZRsbbKkD73K7+tWiMjee sydo6dSi755SNCA+PxhiL81eQ4PVgU08esTeT+jE70djE+fo/IHxdSp9hmUCo7QRWXme O6zA== X-Gm-Message-State: ACrzQf2ikJ8RRbN9OMwmwxZHLnoeOa+hsiol3Axoxfq6N4hOduqZ0rr3 2XKPGn37G53W+MJlWIwUfwwegOjLas5tyw== X-Received: by 2002:a17:90b:4fcc:b0:20f:81ca:ec18 with SMTP id qa12-20020a17090b4fcc00b0020f81caec18mr12706080pjb.176.1666228260538; Wed, 19 Oct 2022 18:11:00 -0700 (PDT) Received: from laptop.hsd1.wa.comcast.net ([2601:600:8500:5f14:d627:c51e:516e:a105]) by smtp.gmail.com with ESMTPSA id b14-20020a170902650e00b00174fa8cbf31sm11242938plk.303.2022.10.19.18.10.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Oct 2022 18:10:59 -0700 (PDT) From: Andrei Vagin To: linux-kernel@vger.kernel.org Cc: Andrei Vagin , Andy Lutomirski , Christian Brauner , Dietmar Eggemann , Kees Cook , Ingo Molnar , Juri Lelli , Peter Oskolkov , Peter Zijlstra , Tycho Andersen , Will Drewry , Vincent Guittot Subject: [PATCH 1/5] seccomp: don't use semaphore and wait_queue together Date: Wed, 19 Oct 2022 18:10:44 -0700 Message-Id: <20221020011048.156415-2-avagin@gmail.com> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20221020011048.156415-1-avagin@gmail.com> References: <20221020011048.156415-1-avagin@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747167091339970293?= X-GMAIL-MSGID: =?utf-8?q?1747167091339970293?= Here is no reason to use two different primitives that do similar things. 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 e9852d1b4a5e..876022e9c88c 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_syscall, list_add_tail(&n.list, &match->notif->notifications); INIT_LIST_HEAD(&n.addfd); - up(&match->notif->request); + atomic_add(1, &match->notif->requests); wake_up_poll(&match->wqh, EPOLLIN | EPOLLRDNORM); /* @@ -1450,6 +1450,37 @@ find_notification(struct seccomp_filter *filter, u64 id) return NULL; } +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_add_unless(&filter->notif->requests, -1, 0) != 0) + return 0; + + for (;;) { + ret = prepare_to_wait_event(&filter->wqh, &wait, TASK_INTERRUPTIBLE); + + if (atomic_add_unless(&filter->notif->requests, -1, 0) != 0) + break; + + if (ret) + return ret; + + schedule(); + } + finish_wait(&filter->wqh, &wait); + return 0; +} 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, memset(&unotif, 0, sizeof(unotif)); - ret = down_interruptible(&filter->notif->request); + ret = recv_wait_event(filter); if (ret < 0) return ret; @@ -1515,7 +1546,8 @@ static long seccomp_notify_recv(struct seccomp_filter *filter, if (should_sleep_killable(filter, knotif)) complete(&knotif->ready); knotif->state = SECCOMP_NOTIFY_INIT; - up(&filter->notif->request); + atomic_add(1, &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_filter *filter) if (!filter->notif) goto out; - sema_init(&filter->notif->request, 0); filter->notif->next_id = get_random_u64(); INIT_LIST_HEAD(&filter->notif->notifications); From patchwork Thu Oct 20 01:10:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrei Vagin X-Patchwork-Id: 5948 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp1542wrs; Wed, 19 Oct 2022 18:13:08 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6WvetCgBq5FeBgvaBohU/NKtP0UbhZ+rwAW/l/FjtuHNsxlmHJHXwNw4aG5BRspHI+2PzX X-Received: by 2002:aa7:9e0f:0:b0:562:7faf:e281 with SMTP id y15-20020aa79e0f000000b005627fafe281mr11448759pfq.58.1666228387762; Wed, 19 Oct 2022 18:13:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666228387; cv=none; d=google.com; s=arc-20160816; b=dmltPnnIq6NWMfU7yvLIB7zuIm/ow2iEiNfkn8XsEHlilO7ttChbr4co6U4aAWMfct wRNqTpTYL4asSZcdlR4BhoOIG0FrSiR0r6qsUES7UsuSBr5Cz3YI1Mt3L1JhTAUWcYHA +0HTZVonwT1nN9oMOuoVyH+WYizTdobql4XneNSUPpFhgipUICTT964AgZT2O7A3hde6 VPaVs8xm7q33xzzvSRb4JjN0xuGC3q83k4axU/Te5EbmXqfrF4OqvRnVijXnj1HWgAwQ c6Oj2mu/8K9X3b65fxzJDnjHLZAz/Ihd5iuGlMPwcK33zFSXU4DBY+TGFE08b0S80pst p0RQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=4NSOyseBFnL3nwsXsl+tqfsopwFipdmUNqJP3ooKECg=; b=rSh3w0db8jqMdRUT/aPDt8c0Bzjoh8VLOjf3xU8hsbOOVT0ggXC7hhMNAFteqqTpi0 tFhPwfonX7Sjai7KgemUebgtubH4TO/u/RFhKMkbcj7Swtza4SfKzO3TtWin/dwGQ/Ls NBtwhWTdllOdPazVyYpElDjh466P6I0+pwTGYHxUdOZCVGlmPTBdC8wsALERJFhCNNIk YfKoYRIHW4SEQLvFZBiv3C3YC6RQV4l26xs7hZnhdvsKIUxs9YaivMJ4MmyDK7EYfOwB hZg301+4L6KpUk6LGAFWQuz6sPvTIbLsGlt9ummKSRPvDQ6N2YE7YYcTzELuD7V92slr mTrQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=E9Oa32ke; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q24-20020a635c18000000b0043945685064si19548329pgb.26.2022.10.19.18.12.55; Wed, 19 Oct 2022 18:13:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=E9Oa32ke; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230130AbiJTBLJ (ORCPT + 99 others); Wed, 19 Oct 2022 21:11:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59610 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229905AbiJTBLE (ORCPT ); Wed, 19 Oct 2022 21:11:04 -0400 Received: from mail-pf1-x42a.google.com (mail-pf1-x42a.google.com [IPv6:2607:f8b0:4864:20::42a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 534801645F9 for ; Wed, 19 Oct 2022 18:11:03 -0700 (PDT) Received: by mail-pf1-x42a.google.com with SMTP id 204so18834965pfx.10 for ; Wed, 19 Oct 2022 18:11:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=4NSOyseBFnL3nwsXsl+tqfsopwFipdmUNqJP3ooKECg=; b=E9Oa32keD+q4jeVumDMAg41KTc4XBw8kCovcEgAwRbI6e2hr/jPOoUNhMAsOBk0ioI 09YwP40DDIeO/2fFJ9e3ENAdOuYPo3Z3ehexjpoNyhntwp8bnjeZQ8rGAYA4Wzc3+ceR 60FBoamG5nr24SMb/49lFPTSy+riRMpt+A3uA7AiURI3pgGv/6yvL4YUU9K8/fGndHby T+1FIiviiFWZVNNE61IjHStUuoh20XoSmZFnX1IITkpV3Eo14RNdMY/FGUrqVMKlSWXq BaYrE5XkB+PY0yewZeZmm51CpzIfcMERDTqc52+dhBTk1BAJsaSJBa0GzanRacGbxeYl fdAg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=4NSOyseBFnL3nwsXsl+tqfsopwFipdmUNqJP3ooKECg=; b=vQZDuPJHojAw8omEz3vdacbuG4xH0LhHrWfiC3f/SvS4GDmKSWM/phhWRdasUe4w02 J7Wpq1qvt5CEmorJJp7oZpqikg1tiOaGm5frqknR/9RMr8OHbtZnWhDtqDvsIQseJoaw b35pl3adG72DS6sImw5LZsrvao86S+gYp0Q00iF7rfDYEkwyn4PPr2Yem/bzbZiTZvBo ez0n5v1qcVEwmF+Mt4XRXHlvfESbSmUR7SqzxGSi1n6pfGL7GWEKL57vB9y9RjpG4cVC cp1qeda6mWs3IJE6SFGqnnEvpPrZ6UFYw08M/mU2oWriWYHo8+c+Y3hUTNVY+EnrXaR1 plWg== X-Gm-Message-State: ACrzQf1Oz9oTY7IegztHyI8YQZkDX6WZLd8n7H/TzEfZEyA6vlFycm21 HNvxvwJjvom2TmXetid3hST/FOy3Q682gw== X-Received: by 2002:a63:4904:0:b0:439:e6a4:a3b5 with SMTP id w4-20020a634904000000b00439e6a4a3b5mr9269287pga.182.1666228262156; Wed, 19 Oct 2022 18:11:02 -0700 (PDT) Received: from laptop.hsd1.wa.comcast.net ([2601:600:8500:5f14:d627:c51e:516e:a105]) by smtp.gmail.com with ESMTPSA id b14-20020a170902650e00b00174fa8cbf31sm11242938plk.303.2022.10.19.18.11.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Oct 2022 18:11:01 -0700 (PDT) From: Andrei Vagin To: linux-kernel@vger.kernel.org Cc: Andrei Vagin , Andy Lutomirski , Christian Brauner , Dietmar Eggemann , Kees Cook , Ingo Molnar , Juri Lelli , Peter Oskolkov , Peter Zijlstra , Tycho Andersen , Will Drewry , Vincent Guittot Subject: [PATCH 2/5] sched: add WF_CURRENT_CPU and externise ttwu Date: Wed, 19 Oct 2022 18:10:45 -0700 Message-Id: <20221020011048.156415-3-avagin@gmail.com> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20221020011048.156415-1-avagin@gmail.com> References: <20221020011048.156415-1-avagin@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747167097878333974?= X-GMAIL-MSGID: =?utf-8?q?1747167097878333974?= From: Peter Oskolkov Add WF_CURRENT_CPU wake flag that advices the scheduler to move the wakee to the current CPU. This is useful for fast on-CPU context switching use cases such as UMCG. In addition, make ttwu external rather than static so that the flag could be passed to it from outside of sched/core.c. Signed-off-by: Peter Oskolkov Signed-off-by: Andrei Vagin --- kernel/sched/core.c | 3 +-- kernel/sched/fair.c | 4 ++++ kernel/sched/sched.h | 13 ++++++++----- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 5800b0623ff3..cffa8f314c9a 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -4039,8 +4039,7 @@ bool ttwu_state_match(struct task_struct *p, unsigned int state, int *success) * Return: %true if @p->state changes (an actual wakeup was done), * %false otherwise. */ -static int -try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags) +int try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags) { unsigned long flags; int cpu, success = 0; diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index e4a0b8bd941c..4ebe7222664c 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -7204,6 +7204,10 @@ select_task_rq_fair(struct task_struct *p, int prev_cpu, int wake_flags) if (wake_flags & WF_TTWU) { record_wakee(p); + if ((wake_flags & WF_CURRENT_CPU) && + cpumask_test_cpu(cpu, p->cpus_ptr)) + return cpu; + if (sched_energy_enabled()) { new_cpu = find_energy_efficient_cpu(p, prev_cpu); if (new_cpu >= 0) diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h index 1644242ecd11..ee24141c4942 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -2071,12 +2071,13 @@ static inline int task_on_rq_migrating(struct task_struct *p) } /* Wake flags. The first three directly map to some SD flag value */ -#define WF_EXEC 0x02 /* Wakeup after exec; maps to SD_BALANCE_EXEC */ -#define WF_FORK 0x04 /* Wakeup after fork; maps to SD_BALANCE_FORK */ -#define WF_TTWU 0x08 /* Wakeup; maps to SD_BALANCE_WAKE */ +#define WF_EXEC 0x02 /* Wakeup after exec; maps to SD_BALANCE_EXEC */ +#define WF_FORK 0x04 /* Wakeup after fork; maps to SD_BALANCE_FORK */ +#define WF_TTWU 0x08 /* Wakeup; maps to SD_BALANCE_WAKE */ -#define WF_SYNC 0x10 /* Waker goes to sleep after wakeup */ -#define WF_MIGRATED 0x20 /* Internal use, task got migrated */ +#define WF_SYNC 0x10 /* Waker goes to sleep after wakeup */ +#define WF_MIGRATED 0x20 /* Internal use, task got migrated */ +#define WF_CURRENT_CPU 0x40 /* Prefer to move the wakee to the current CPU. */ #ifdef CONFIG_SMP static_assert(WF_EXEC == SD_BALANCE_EXEC); @@ -3161,6 +3162,8 @@ static inline bool is_per_cpu_kthread(struct task_struct *p) extern void swake_up_all_locked(struct swait_queue_head *q); extern void __prepare_to_swait(struct swait_queue_head *q, struct swait_queue *wait); +extern int try_to_wake_up(struct task_struct *tsk, unsigned int state, int wake_flags); + #ifdef CONFIG_PREEMPT_DYNAMIC extern int preempt_dynamic_mode; extern int sched_dynamic_mode(const char *str); From patchwork Thu Oct 20 01:10:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrei Vagin X-Patchwork-Id: 5951 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp1903wrs; Wed, 19 Oct 2022 18:14:26 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5oVDatzVtTFylc+SLLvcEHLJu4Sd+CEK6gDVy8U1+lekJaqi86Jy1kKmrBOjwoTnkYnyqI X-Received: by 2002:a17:90b:4f46:b0:20d:1fe8:dcd2 with SMTP id pj6-20020a17090b4f4600b0020d1fe8dcd2mr48355600pjb.235.1666228466143; Wed, 19 Oct 2022 18:14:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666228466; cv=none; d=google.com; s=arc-20160816; b=Qvby/5fg0g7QSDoiAZ2oKpVKFRRu8eaKl7S4dzViixpjkCnZf3nbq7adDCnk1rwRfY YakR6R1ffa/WTKu4hERj98bWws/h4LN1xovI/7Abn10josyO9gZAqiWwMqfJF1h+SJOU mR0fCGyzfNkO5kPI0Si5okBHuWHp9n+ayzkh53ZTF44KF1U5D8vpNJm5/kn6yuw+u5bI yiiE4i6PgsFtX+9JRne3fFtkqc0AR9OcRJxUvcPrPLrRudtHRtjqaFuDmzcr7xY24OhL Wz7l5URaD7IkFRCo0A9/hD/Oa4+8xCJTyQXlH8qhqyLexKY6xLqqkWAjzFXVWA0dHbLq KBQg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=bJohSBMpko54dLX8P/51bY6y3oGzs1tMcw4aH/Yvzng=; b=lDveq/xb3x7heiyOE/bLPafqcZbbb0CPQQff0wNoefkbG2zbZmCsaOnv90hP+dkkeW bNGX9Nw4y+N9faBEAZ51k+fU38I6H0R9bisfiXf/7cqhXj2Xl/b+8MjtYoUDrafRnnp1 TZNakV6vxRj85VpLXXFvZJN7eI37SgtE+VgwY7tNjnW0wBPFO0UpuZ8zDsdLpHCYMdPi hazOsB719EoAhMddMI9jVhZnt/unE00h0rpmavWQN0rAMZ5EmPjA04eev55HKe5Buwr9 vadIGSG7/OWJ2qs/LhxKKHiXgbfLEXrivqZUZ4Y1uAPRtAWz+pkOSnG0eyqc99K6sjJr FM4g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=Aq+lPDV4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e8-20020a170902ef4800b00176b7e66c9asi20159731plx.155.2022.10.19.18.14.12; Wed, 19 Oct 2022 18:14:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=Aq+lPDV4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230298AbiJTBLV (ORCPT + 99 others); Wed, 19 Oct 2022 21:11:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59696 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230037AbiJTBLG (ORCPT ); Wed, 19 Oct 2022 21:11:06 -0400 Received: from mail-pj1-x1031.google.com (mail-pj1-x1031.google.com [IPv6:2607:f8b0:4864:20::1031]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4010A16552F for ; Wed, 19 Oct 2022 18:11:05 -0700 (PDT) Received: by mail-pj1-x1031.google.com with SMTP id ez6so850313pjb.1 for ; Wed, 19 Oct 2022 18:11:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=bJohSBMpko54dLX8P/51bY6y3oGzs1tMcw4aH/Yvzng=; b=Aq+lPDV4PUAgvfmt2l+zK/WTvDukCwcSf/iEiizuVXAIUeFOQ9iS+Fh9ro4ADsXeJb hTyoIEHWrhYmmG1dtB0BM2qr7N80PPdl+FiFzju9NUoNw/QLnG7Iyiq6NMPkIPBRyW4n SRJuWhyrjIdi+8KwjNFo7bq1f1BlFvXJr4oik70p5h6q7a3xfm4kaarWblPWYWD7bfMk f+QEivOdBsJ569IEApREbc96II3Q//letX9fhTwdV3VoHWoN1DUe0GM940IoEXbJJjf9 UJUU3qdpt4XiT8ktx9zIIxcoRm8EbHlm5oy3eXbMt11tPkbKKdCIYxj6CnzcL+uPa7fG CgIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=bJohSBMpko54dLX8P/51bY6y3oGzs1tMcw4aH/Yvzng=; b=6hBeFTgF4t+9BrU4CuktKg16iepID2BWagcW5rkvf8AXapRhlbVvHv7ZV47ubUKVAG 3D7pdU9n32Q1+mGqbD7cBxjz7s55FxeTAPlaylVryidrWctNMAFAYQ1+ZXX4sm6Y13Vq /59koQ11GKdAcMQ7mRPLL/nLeIa4OgDjUuXWWe4L+jQj5+crP1pLhzRr/QajabOFAhWL +AJo4WvUrfC3ZpQMzq8LzdGi9T5Kui7e8+MzT8HOWVhVRq7PXVylxhigWKlYEn1z+4yq KK60nUcyWafEw2+Nzjhm0sgQoL7Bwra4BCM0M7fUQ9wc78nfPTTHesiwYZuGZRASccqS u3TA== X-Gm-Message-State: ACrzQf2eJEuLBSqGK/5dN0fFbNq7HXNMuykp9xmjNy8/geEuhXBwr1T5 xYPd9E9Jsjt3qfNCAPi66W0/tngeucbhDg== X-Received: by 2002:a17:90b:2250:b0:20d:9587:45c4 with SMTP id hk16-20020a17090b225000b0020d958745c4mr12621755pjb.17.1666228264084; Wed, 19 Oct 2022 18:11:04 -0700 (PDT) Received: from laptop.hsd1.wa.comcast.net ([2601:600:8500:5f14:d627:c51e:516e:a105]) by smtp.gmail.com with ESMTPSA id b14-20020a170902650e00b00174fa8cbf31sm11242938plk.303.2022.10.19.18.11.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Oct 2022 18:11:03 -0700 (PDT) From: Andrei Vagin To: linux-kernel@vger.kernel.org Cc: Andrei Vagin , Andy Lutomirski , Christian Brauner , Dietmar Eggemann , Kees Cook , Ingo Molnar , Juri Lelli , Peter Oskolkov , Peter Zijlstra , Tycho Andersen , Will Drewry , Vincent Guittot Subject: [PATCH 3/5] sched: add a few helpers to wake up tasks on the current cpu Date: Wed, 19 Oct 2022 18:10:46 -0700 Message-Id: <20221020011048.156415-4-avagin@gmail.com> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20221020011048.156415-1-avagin@gmail.com> References: <20221020011048.156415-1-avagin@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747167179976857397?= X-GMAIL-MSGID: =?utf-8?q?1747167179976857397?= Add complete_on_current_cpu, wake_up_poll_on_current_cpu helpers to wake up processes on the current CPU. Signed-off-by: Andrei Vagin --- include/linux/completion.h | 1 + include/linux/swait.h | 1 + include/linux/wait.h | 3 +++ kernel/sched/completion.c | 12 ++++++++++++ kernel/sched/core.c | 2 +- kernel/sched/swait.c | 11 +++++++++++ kernel/sched/wait.c | 5 +++++ 7 files changed, 34 insertions(+), 1 deletion(-) diff --git a/include/linux/completion.h b/include/linux/completion.h index 62b32b19e0a8..fb2915676574 100644 --- a/include/linux/completion.h +++ b/include/linux/completion.h @@ -116,6 +116,7 @@ extern bool try_wait_for_completion(struct completion *x); extern bool completion_done(struct completion *x); extern void complete(struct completion *); +extern void complete_on_current_cpu(struct completion *x); extern void complete_all(struct completion *); #endif diff --git a/include/linux/swait.h b/include/linux/swait.h index 6a8c22b8c2a5..1f27b254adf5 100644 --- a/include/linux/swait.h +++ b/include/linux/swait.h @@ -147,6 +147,7 @@ static inline bool swq_has_sleeper(struct swait_queue_head *wq) extern void swake_up_one(struct swait_queue_head *q); extern void swake_up_all(struct swait_queue_head *q); extern void swake_up_locked(struct swait_queue_head *q); +extern void swake_up_locked_on_current_cpu(struct swait_queue_head *q); extern void prepare_to_swait_exclusive(struct swait_queue_head *q, struct swait_queue *wait, int state); extern long prepare_to_swait_event(struct swait_queue_head *q, struct swait_queue *wait, int state); diff --git a/include/linux/wait.h b/include/linux/wait.h index 7f5a51aae0a7..c7d3e78a500d 100644 --- a/include/linux/wait.h +++ b/include/linux/wait.h @@ -210,6 +210,7 @@ __remove_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_entry *wq } void __wake_up(struct wait_queue_head *wq_head, unsigned int mode, int nr, void *key); +void __wake_up_on_current_cpu(struct wait_queue_head *wq_head, unsigned int mode, void *key); void __wake_up_locked_key(struct wait_queue_head *wq_head, unsigned int mode, void *key); void __wake_up_locked_key_bookmark(struct wait_queue_head *wq_head, unsigned int mode, void *key, wait_queue_entry_t *bookmark); @@ -237,6 +238,8 @@ void __wake_up_pollfree(struct wait_queue_head *wq_head); #define key_to_poll(m) ((__force __poll_t)(uintptr_t)(void *)(m)) #define wake_up_poll(x, m) \ __wake_up(x, TASK_NORMAL, 1, poll_to_key(m)) +#define wake_up_poll_on_current_cpu(x, m) \ + __wake_up_on_current_cpu(x, TASK_NORMAL, poll_to_key(m)) #define wake_up_locked_poll(x, m) \ __wake_up_locked_key((x), TASK_NORMAL, poll_to_key(m)) #define wake_up_interruptible_poll(x, m) \ diff --git a/kernel/sched/completion.c b/kernel/sched/completion.c index d57a5c1c1cd9..a1931a79c05a 100644 --- a/kernel/sched/completion.c +++ b/kernel/sched/completion.c @@ -38,6 +38,18 @@ void complete(struct completion *x) } EXPORT_SYMBOL(complete); +void complete_on_current_cpu(struct completion *x) +{ + unsigned long flags; + + raw_spin_lock_irqsave(&x->wait.lock, flags); + + if (x->done != UINT_MAX) + x->done++; + swake_up_locked_on_current_cpu(&x->wait); + raw_spin_unlock_irqrestore(&x->wait.lock, flags); +} + /** * complete_all: - signals all threads waiting on this completion * @x: holds the state of this particular completion diff --git a/kernel/sched/core.c b/kernel/sched/core.c index cffa8f314c9a..1412470216c3 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -6822,7 +6822,7 @@ asmlinkage __visible void __sched preempt_schedule_irq(void) int default_wake_function(wait_queue_entry_t *curr, unsigned mode, int wake_flags, void *key) { - WARN_ON_ONCE(IS_ENABLED(CONFIG_SCHED_DEBUG) && wake_flags & ~WF_SYNC); + WARN_ON_ONCE(IS_ENABLED(CONFIG_SCHED_DEBUG) && wake_flags & ~(WF_SYNC|WF_CURRENT_CPU)); return try_to_wake_up(curr->private, mode, wake_flags); } EXPORT_SYMBOL(default_wake_function); diff --git a/kernel/sched/swait.c b/kernel/sched/swait.c index 76b9b796e695..9ebe23868942 100644 --- a/kernel/sched/swait.c +++ b/kernel/sched/swait.c @@ -31,6 +31,17 @@ void swake_up_locked(struct swait_queue_head *q) } EXPORT_SYMBOL(swake_up_locked); +void swake_up_locked_on_current_cpu(struct swait_queue_head *q) +{ + struct swait_queue *curr; + + if (list_empty(&q->task_list)) + return; + + curr = list_first_entry(&q->task_list, typeof(*curr), task_list); + try_to_wake_up(curr->task, TASK_NORMAL, WF_CURRENT_CPU); + list_del_init(&curr->task_list); +} /* * Wake up all waiters. This is an interface which is solely exposed for * completions and not for general usage. diff --git a/kernel/sched/wait.c b/kernel/sched/wait.c index 9860bb9a847c..9a78bca79419 100644 --- a/kernel/sched/wait.c +++ b/kernel/sched/wait.c @@ -157,6 +157,11 @@ void __wake_up(struct wait_queue_head *wq_head, unsigned int mode, } EXPORT_SYMBOL(__wake_up); +void __wake_up_on_current_cpu(struct wait_queue_head *wq_head, unsigned int mode, void *key) +{ + __wake_up_common_lock(wq_head, mode, 1, WF_CURRENT_CPU, key); +} + /* * Same as __wake_up but called with the spinlock in wait_queue_head_t held. */ From patchwork Thu Oct 20 01:10:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrei Vagin X-Patchwork-Id: 5952 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp1937wrs; Wed, 19 Oct 2022 18:14:39 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4CxEgNsprxzHfUPxre635ZzAtBqK777xQoXRg4iSrDulgXgse334vX69AH4Dbud0XEphEk X-Received: by 2002:a05:6a02:20d:b0:430:3886:59e8 with SMTP id bh13-20020a056a02020d00b00430388659e8mr9584680pgb.516.1666228479558; Wed, 19 Oct 2022 18:14:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666228479; cv=none; d=google.com; s=arc-20160816; b=KlyY09oClZyWpAyvfANXxK6A7lApN0l1Gfb9Uo1fkIaF2YghffiUF43JZLmJ4jbG0R X1EDWYnTdFAldsETFi2JeJErO1sl56QyLR2jnqG3ZEgUASVe/rnijPcXlz2TwvFxaifd 3PEGJM6AJVCTgnxxb5o6tYjWYWkC6nWuJJt/V/45Jr/o7AAZsi4FrUTkIaWvySqs29hK pyj+dhRW6BkjKCqOqQfNnqN86nWJ/81G6e0nNpcgTvSFauDcZUbEchFhQQ5vSw3PEw+Y jwCLqBlxw09lXlxg4V3/tnV43HN1/xOmrMeAYeacuZre5YRSB+l1RZCnFmLlyfNCUP6R qfNQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=3p8cEHauNcqkL4kG4wAOXzGDd6JhummGET3RKuVhg5o=; b=p3XJ49EPb2SLXndqDhBNthC01uUmdjfvPmRQeuSvXBbAZHGZ6LzEqeIc8KMkuHojpo OLn6eE/u21wGe9R6KX4FbQOQjcSE8gOCSj1hL7r35xcrkJ2eRjCoL2fmjNEsyMJ+5Ew3 mwG065Wq2btQ/iWJ0owomFZ16jjyfQoAFGajVRhUEQZaqKUE8BTIRLTu8YvWykVKLkPw FSvioS71+VvU+0YEq7Fgww93BVzxaa2VBz3OxyhaP8xiIkJAQ0cp1spr1JAv8dUXrTnB qCxMiAka4+W5cvM9odLBaHZbwgcVGxHHRZ+/EC5aDr80Z4dyOCKBY0M5EMxAiN4GFdPF 7hVw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=KpqBtiFv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d16-20020a170903231000b0017f762e2dc6si23314808plh.613.2022.10.19.18.14.26; Wed, 19 Oct 2022 18:14:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=KpqBtiFv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229612AbiJTBL1 (ORCPT + 99 others); Wed, 19 Oct 2022 21:11:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59808 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230122AbiJTBLJ (ORCPT ); Wed, 19 Oct 2022 21:11:09 -0400 Received: from mail-pj1-x1029.google.com (mail-pj1-x1029.google.com [IPv6:2607:f8b0:4864:20::1029]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C2F351669A6 for ; Wed, 19 Oct 2022 18:11:06 -0700 (PDT) Received: by mail-pj1-x1029.google.com with SMTP id t10-20020a17090a4e4a00b0020af4bcae10so1569025pjl.3 for ; Wed, 19 Oct 2022 18:11:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=3p8cEHauNcqkL4kG4wAOXzGDd6JhummGET3RKuVhg5o=; b=KpqBtiFvdg/rxYhqM8UMKgbw4wfAZWNEAYm6f/68Q4ilz5ADiqoLnrVN0BBGvFgXei r/+yi4zJTHczr6toKOde2bi66paH32nklVclydKHdR0lWfYuPU0coxQDSs+gLyFFK3MS 5zvTuLbDFjyxYhd0agNdb5v9azLZai/Qmp7QI8/y4lPClYkC3VyF1uWG3UQu+Li41DOY sVHj7uwTaQV48gMmayCCWArAVMjDdzw/fzXdYlRmwtoPbPvzrTeWFnDU7bkVWvvrSXt4 C71MEv4mxVHvceGaWpiW49DgdVzhJ9YZiU3oG0aRzHhHcvY1oiq2XAaIHT6JG9weW0nK O+sA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=3p8cEHauNcqkL4kG4wAOXzGDd6JhummGET3RKuVhg5o=; b=kLC4TLG2SfxO7wv/qwbNVNPNxiJEBwuI8YGWKUOftwnHPFWvpf929F8ACJSbKebum2 kMRy3+dnVDfOho5gPa+5pmJjAdAk4b8LqdmGLBBRpHP8Hv2EgTGVusPiM41TRsbr41DF JRP8rfseycEPgnUHY3cifRNLre3DnQGNRqbXLpXcJ96PC041Cu0bz8k4VdJdIKVz8Rgm HN/CK/eQzrnANYlgR4TrcNjDbjtcBuAdee6Vu4lBiDkUmMRCu7kG0HptkFSgkdQxTmCA dPWIEG4A7Vg5um7ItO/jW3v/+2jP9zdm+utDcOw3xhXLqfz1qQLiVOxgUBHBSUZlckjb orvA== X-Gm-Message-State: ACrzQf1UwYHixHOsq1EA8Xox293m9IvFqiB2HT724hEXh1dxOuXeHKer bLzHWDYTgJYsU4xNh01uIGxluTslSLwKKA== X-Received: by 2002:a17:903:2344:b0:17f:7dc9:cde5 with SMTP id c4-20020a170903234400b0017f7dc9cde5mr11207629plh.47.1666228265632; Wed, 19 Oct 2022 18:11:05 -0700 (PDT) Received: from laptop.hsd1.wa.comcast.net ([2601:600:8500:5f14:d627:c51e:516e:a105]) by smtp.gmail.com with ESMTPSA id b14-20020a170902650e00b00174fa8cbf31sm11242938plk.303.2022.10.19.18.11.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Oct 2022 18:11:05 -0700 (PDT) From: Andrei Vagin To: linux-kernel@vger.kernel.org Cc: Andrei Vagin , Andy Lutomirski , Christian Brauner , Dietmar Eggemann , Kees Cook , Ingo Molnar , Juri Lelli , Peter Oskolkov , Peter Zijlstra , Tycho Andersen , Will Drewry , Vincent Guittot Subject: [PATCH 4/5] seccomp: add the synchronous mode for seccomp_unotify Date: Wed, 19 Oct 2022 18:10:47 -0700 Message-Id: <20221020011048.156415-5-avagin@gmail.com> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20221020011048.156415-1-avagin@gmail.com> References: <20221020011048.156415-1-avagin@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747167075929175378?= X-GMAIL-MSGID: =?utf-8?q?1747167194626662505?= seccomp_unotify allows more privileged processes does actions on behalf of less privileged processes. In many cases, the workflow is fully synchronous. It means a target process triggers a system call and passes controls to a supervisor process that handles the system call and returns controls to the target process. In this context, "synchronous" means that only one process is running and another one is waiting. There is the WF_CURRENT_CPU flag that is used to advise the scheduler to move the wakee to the current CPU. For such synchronous workflows, it makes context switches a few times faster. Right now, each interaction takes 12µs. With this patch, it takes about 3µs. This change introduce the SECCOMP_USER_NOTIF_FD_SYNC_WAKE_UP flag that it used to enable the sync mode. Signed-off-by: Andrei Vagin --- include/uapi/linux/seccomp.h | 4 ++++ kernel/seccomp.c | 31 +++++++++++++++++++++++++++++-- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/include/uapi/linux/seccomp.h b/include/uapi/linux/seccomp.h index 0fdc6ef02b94..dbfc9b37fcae 100644 --- a/include/uapi/linux/seccomp.h +++ b/include/uapi/linux/seccomp.h @@ -115,6 +115,8 @@ struct seccomp_notif_resp { __u32 flags; }; +#define SECCOMP_USER_NOTIF_FD_SYNC_WAKE_UP (1UL << 0) + /* valid flags for seccomp_notif_addfd */ #define SECCOMP_ADDFD_FLAG_SETFD (1UL << 0) /* Specify remote fd */ #define SECCOMP_ADDFD_FLAG_SEND (1UL << 1) /* Addfd and return it, atomically */ @@ -150,4 +152,6 @@ struct seccomp_notif_addfd { #define SECCOMP_IOCTL_NOTIF_ADDFD SECCOMP_IOW(3, \ struct seccomp_notif_addfd) +#define SECCOMP_IOCTL_NOTIF_SET_FLAGS SECCOMP_IOW(4, __u64) + #endif /* _UAPI_LINUX_SECCOMP_H */ diff --git a/kernel/seccomp.c b/kernel/seccomp.c index 876022e9c88c..0a62d44f4898 100644 --- a/kernel/seccomp.c +++ b/kernel/seccomp.c @@ -143,9 +143,12 @@ struct seccomp_kaddfd { * filter->notify_lock. * @next_id: The id of the next request. * @notifications: A list of struct seccomp_knotif elements. + * @flags: A set of SECCOMP_USER_NOTIF_FD_* flags. */ + struct notification { atomic_t requests; + u32 flags; u64 next_id; struct list_head notifications; }; @@ -1117,7 +1120,10 @@ static int seccomp_do_user_notification(int this_syscall, INIT_LIST_HEAD(&n.addfd); atomic_add(1, &match->notif->requests); - wake_up_poll(&match->wqh, EPOLLIN | EPOLLRDNORM); + 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); /* * This is where we wait for a reply from userspace. @@ -1593,7 +1599,10 @@ static long seccomp_notify_send(struct seccomp_filter *filter, knotif->error = resp.error; knotif->val = resp.val; knotif->flags = resp.flags; - complete(&knotif->ready); + if (filter->notif->flags & SECCOMP_USER_NOTIF_FD_SYNC_WAKE_UP) + complete_on_current_cpu(&knotif->ready); + else + complete(&knotif->ready); out: mutex_unlock(&filter->notify_lock); return ret; @@ -1623,6 +1632,22 @@ static long seccomp_notify_id_valid(struct seccomp_filter *filter, return ret; } +static long seccomp_notify_set_flags(struct seccomp_filter *filter, + unsigned long flags) +{ + long ret; + + if (flags & ~SECCOMP_USER_NOTIF_FD_SYNC_WAKE_UP) + return -EINVAL; + + ret = mutex_lock_interruptible(&filter->notify_lock); + if (ret < 0) + return ret; + filter->notif->flags = flags; + mutex_unlock(&filter->notify_lock); + return 0; +} + static long seccomp_notify_addfd(struct seccomp_filter *filter, struct seccomp_notif_addfd __user *uaddfd, unsigned int size) @@ -1752,6 +1777,8 @@ static long seccomp_notify_ioctl(struct file *file, unsigned int cmd, case SECCOMP_IOCTL_NOTIF_ID_VALID_WRONG_DIR: case SECCOMP_IOCTL_NOTIF_ID_VALID: return seccomp_notify_id_valid(filter, buf); + case SECCOMP_IOCTL_NOTIF_SET_FLAGS: + return seccomp_notify_set_flags(filter, arg); } /* Extensible Argument ioctls */ From patchwork Thu Oct 20 01:10:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrei Vagin X-Patchwork-Id: 5949 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp1873wrs; Wed, 19 Oct 2022 18:14:17 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6kcUGgePB6fOPFWbZLwqX8pYJhQyvUcHwEbH0fE7TyMzGjGPNWCx2EFa2OepbPNvOQ3A7L X-Received: by 2002:a17:902:76c3:b0:17a:68:767d with SMTP id j3-20020a17090276c300b0017a0068767dmr11381337plt.109.1666228457289; Wed, 19 Oct 2022 18:14:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666228457; cv=none; d=google.com; s=arc-20160816; b=WRmZPvNUz12GS4p5yDOoEeoqwuTUyh2EbL5t8TdgGk9JzaJTQpWTD3Ael4y6BV1RrX U49rTVjKVTOmgXm6/ugUCb6D568vLdDo1ZwWXEJhfSLxToZGksvUdyF79222n36HR7mq /WylgQSQ+ZuHpC1EzmVGfVT8jWV/jEgVIt3qRyVCzNejo03t06+w7xcNEQYrIneupX9k UZTEahpphLIaRFScFu/oLqiYwsr+KM0oaddE2jeWfr8joD1TImmTGkrlrn68Fe4ayt3B wrCiUNJ9MzYzdX56YLIb2PPWHGsOfzXT+qCqSe1qbQngwkL+atpATVtPSvyvAx2adoEq Tq8g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=q+qrgR/MUitgGFEN28UdJxxU/RHOuYqlke4g/Y2iteA=; b=K86kWoSCWmdCVM9nc/PRgnSY2jWj5V8M1CMAiDsXpLedv4SgVhjHPkuB2bawcU0ph2 UDs/1HfC1FeM1f8i84gZOARyQrDSR2/FyD1zckqIkJbWbg4/iYDg0iX4f0HBf0WVh2y6 DTaXjeVtgJ7WEBeQ37aCZK9y4h8wZ8C5uBsEWqAh5yuh/CKJm1WxwIZx3a8aXlYvKmVE Ejd+TY+nAs3V+8WfNaHVXALpK/4+VxpgCP0GCW4EDqPf/2oEQJr+ww5AM5M9CuKdVXvR eYaud4tYHOZlSbyOUS857V8VEKr2hlktWK0cWKNenHdJI1KewXzw41Z2pH+N90jkfB3R i2Iw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=PItxHKiL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id cm9-20020a056a00338900b0056771c032f4si3886962pfb.28.2022.10.19.18.14.04; Wed, 19 Oct 2022 18:14:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=PItxHKiL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230377AbiJTBLb (ORCPT + 99 others); Wed, 19 Oct 2022 21:11:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59876 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230150AbiJTBLK (ORCPT ); Wed, 19 Oct 2022 21:11:10 -0400 Received: from mail-pl1-x62a.google.com (mail-pl1-x62a.google.com [IPv6:2607:f8b0:4864:20::62a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0DAD4169128 for ; Wed, 19 Oct 2022 18:11:08 -0700 (PDT) Received: by mail-pl1-x62a.google.com with SMTP id z20so18850604plb.10 for ; Wed, 19 Oct 2022 18:11:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=q+qrgR/MUitgGFEN28UdJxxU/RHOuYqlke4g/Y2iteA=; b=PItxHKiLj7J1/b4y394GFbYMnhiM8mYdu7R5SmT7+rG7QCwJLP9WX/xaTofobo5zcF BFFbGFIOcE56vQVKV+aLrB/kkys2OJPRGMdh3LkbM7ILqHzQSPTpMkpsfYrJjbBFozb9 CrVUWsRr2XPaeINndMHVm+D5mafY3zu1gKOhP9/xXI79mRF3gpEfoBG3+6G2Zh0X1r+6 CPSXlEkiXzPX6WaTdZmS89/mnmTJQqxrwe9Kvfpkh2emabi/3l4P/FzsupCLxw/b7Qh3 dpQOFRpXpHkug6mZQP+WZPn9VDBHONioWZqI5Ic5YwAnmWQ1Pi+dRlDNUXYEWECgtbpA wGZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=q+qrgR/MUitgGFEN28UdJxxU/RHOuYqlke4g/Y2iteA=; b=sQkk0obZrz+uD3bHXJ0nXpTOVF161sYG/qKb6XBqEEA5RcIntX2vEjfwmp13DeRLSE DjYzEVKjLAJgDS1FYPH5Q/r0KoNb7zAfOwgHC9vrMTlLWYJecarGJ3SAhDCOXqawDxrn N9+m1YzPeXp1IxJhYuoiugPW3fGHn+9h7K0jzbozfrQifqEqpMVu3qN2hvOcyfNxYcFK iPe+aThZ9ZipwF6RWgolBiMjfICwzn3TZdvq+y8U8gZWyQOFJBc0pu7hUsn0g+DojGcW svLftBVEghQI1V4KzJEVSRDr9lQRmKoLj/g1dUUwbbXB4O4EYMwRU4elzOW9DSiTHRwE 12XQ== X-Gm-Message-State: ACrzQf0wcKxA99Psp6Qp5CeAT4nqcpiWsTTFHXR4OUMXwerPFihVsxFh 0hC1IWqUYSblxUt9sYEmquUr63gxtQU0kQ== X-Received: by 2002:a17:90a:1c02:b0:1e0:df7:31f2 with SMTP id s2-20020a17090a1c0200b001e00df731f2mr47334791pjs.222.1666228266940; Wed, 19 Oct 2022 18:11:06 -0700 (PDT) Received: from laptop.hsd1.wa.comcast.net ([2601:600:8500:5f14:d627:c51e:516e:a105]) by smtp.gmail.com with ESMTPSA id b14-20020a170902650e00b00174fa8cbf31sm11242938plk.303.2022.10.19.18.11.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Oct 2022 18:11:06 -0700 (PDT) From: Andrei Vagin To: linux-kernel@vger.kernel.org Cc: Andrei Vagin , Andy Lutomirski , Christian Brauner , Dietmar Eggemann , Kees Cook , Ingo Molnar , Juri Lelli , Peter Oskolkov , Peter Zijlstra , Tycho Andersen , Will Drewry , Vincent Guittot Subject: [PATCH 5/5] selftest/seccomp: add a new test for the sync mode of seccomp_user_notify Date: Wed, 19 Oct 2022 18:10:48 -0700 Message-Id: <20221020011048.156415-6-avagin@gmail.com> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20221020011048.156415-1-avagin@gmail.com> References: <20221020011048.156415-1-avagin@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747167170495990575?= X-GMAIL-MSGID: =?utf-8?q?1747167170495990575?= Test output: RUN global.user_notification_sync ... seccomp_bpf.c:4279:user_notification_sync:basic: 8655 nsec/syscall seccomp_bpf.c:4279:user_notification_sync:sync: 2919 nsec/syscall OK global.user_notification_sync Signed-off-by: Andrei Vagin --- tools/testing/selftests/seccomp/seccomp_bpf.c | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/tools/testing/selftests/seccomp/seccomp_bpf.c b/tools/testing/selftests/seccomp/seccomp_bpf.c index 4ae6c8991307..01f872415c17 100644 --- a/tools/testing/selftests/seccomp/seccomp_bpf.c +++ b/tools/testing/selftests/seccomp/seccomp_bpf.c @@ -4241,6 +4241,86 @@ TEST(user_notification_addfd_rlimit) close(memfd); } +/* USER_NOTIF_BENCH_TIMEOUT is 100 miliseconds. */ +#define USER_NOTIF_BENCH_TIMEOUT 100000000ULL +#define NSECS_PER_SEC 1000000000ULL + +#ifndef SECCOMP_USER_NOTIF_FD_SYNC_WAKE_UP +#define SECCOMP_USER_NOTIF_FD_SYNC_WAKE_UP (1UL << 0) +#define SECCOMP_IOCTL_NOTIF_SET_FLAGS SECCOMP_IOW(4, __u64) +#endif + +static void user_notification_sync_loop(struct __test_metadata *_metadata, + char *test_name, int listener) +{ + struct timespec ts; + uint64_t start, end, nr; + struct seccomp_notif req = {}; + struct seccomp_notif_resp resp = {}; + + clock_gettime(CLOCK_MONOTONIC, &ts); + start = ts.tv_nsec + ts.tv_sec * NSECS_PER_SEC; + for (end = start, nr = 0; end - start < USER_NOTIF_BENCH_TIMEOUT; nr++) { + memset(&req, 0, sizeof(req)); + req.pid = 0; + EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_RECV, &req), 0); + + EXPECT_EQ(req.data.nr, __NR_getppid); + + resp.id = req.id; + resp.error = 0; + resp.val = USER_NOTIF_MAGIC; + resp.flags = 0; + EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_SEND, &resp), 0); + + clock_gettime(CLOCK_MONOTONIC, &ts); + end = ts.tv_nsec + ts.tv_sec * NSECS_PER_SEC; + } + TH_LOG("%s:\t%lld nsec/syscall", test_name, USER_NOTIF_BENCH_TIMEOUT / nr); +} + +TEST(user_notification_sync) +{ + pid_t pid; + long ret; + int status, listener; + + ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); + ASSERT_EQ(0, ret) { + TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!"); + } + + listener = user_notif_syscall(__NR_getppid, + SECCOMP_FILTER_FLAG_NEW_LISTENER); + ASSERT_GE(listener, 0); + + pid = fork(); + ASSERT_GE(pid, 0); + + if (pid == 0) { + while (1) { + ret = syscall(__NR_getppid); + if (ret == USER_NOTIF_MAGIC) + continue; + break; + } + _exit(1); + } + + user_notification_sync_loop(_metadata, "basic", listener); + + EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_SET_FLAGS, + SECCOMP_USER_NOTIF_FD_SYNC_WAKE_UP, 0), 0); + + user_notification_sync_loop(_metadata, "sync", listener); + + kill(pid, SIGKILL); + EXPECT_EQ(waitpid(pid, &status, 0), pid); + EXPECT_EQ(true, WIFSIGNALED(status)); + EXPECT_EQ(SIGKILL, WTERMSIG(status)); +} + + /* Make sure PTRACE_O_SUSPEND_SECCOMP requires CAP_SYS_ADMIN. */ FIXTURE(O_SUSPEND_SECCOMP) { pid_t pid;