Received: by 2002:a05:6a10:c604:0:0:0:0 with SMTP id y4csp3340641pxt; Tue, 10 Aug 2021 00:59:54 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwk7nfAVHJKjhPoahob5NdiFe4wPJMI/rshNfT10W5OPwtB1u9mMC6BCCHGswiL6GI49Ljf X-Received: by 2002:a05:6602:20d2:: with SMTP id 18mr270817ioz.85.1628582394746; Tue, 10 Aug 2021 00:59:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628582394; cv=none; d=google.com; s=arc-20160816; b=YChAQTR60pKZojIT3oY3/Re6SQmSDzNE81Fh/B7I3nP28HDqsq0BQ81cjRUgJYSXsI +9f64m0ArOwfQ+rgf2fzupZUKHurqwnuBnGOoLXixCDv9LT55OKLLjhZyywMjM1Ukbec IB01te4jr6ChXdYmof4dic1zrEO4QmONU9Ap8LWwVGI+s0PeoLU4rCXIAW2nE8f13U5c T7C8UhCUkyNRO4kV3SmNuOgYe7nT+79B/k6/9APVAbQWcE2VWsbuxNpFsJ3CosC5eQmR i55UXfsOZ2tX/0XzKjwNKrgQKxWe+B1CywKb3meq1b7bbVf6zvXjbEiIFmGWxeY60hH6 cH2w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=2r1eKT0GHVa73VDvbo2DptlCFZJjgR5mWxSgpJNvXcE=; b=Fd2Ur2fdg7nZEp1uD6bQqdomj41YC74+BzgslrxA+UWYuvzege1M/f1nMZ7LL2Fm0Z yEKOkth134yC+tXKA7Ddni914Bc0l+b01M7J258ouZ+oZNqOnu4B50YeqNU04P2l/n7m wMszxomQwBQPgQnz+7Hn7jqfCB17oORoMILxdpiGvihmrny9eiTp0ucYOd4vttEk4owp ZeICNAO5IZ42/JQSWVOcTQJBwblMFQWw4zX7XyGxf8GCNmgUQSV4d6AaSy/xgJKEnCDp gdTyKkDx+cfRv71C87SdMlKLnZzNfO1060oY0/t7RSNk3fso6hhouCT2kmUqigGUYkhJ rvBA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=SQz9WL37; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id q17si22072621ilm.64.2021.08.10.00.59.40; Tue, 10 Aug 2021 00:59:54 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=SQz9WL37; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S234749AbhHJCmz (ORCPT + 99 others); Mon, 9 Aug 2021 22:42:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42062 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231609AbhHJCmz (ORCPT ); Mon, 9 Aug 2021 22:42:55 -0400 Received: from mail-io1-xd31.google.com (mail-io1-xd31.google.com [IPv6:2607:f8b0:4864:20::d31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E20A5C0613D3; Mon, 9 Aug 2021 19:42:33 -0700 (PDT) Received: by mail-io1-xd31.google.com with SMTP id l20so28068522iom.4; Mon, 09 Aug 2021 19:42:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=2r1eKT0GHVa73VDvbo2DptlCFZJjgR5mWxSgpJNvXcE=; b=SQz9WL37YY+hOktCyV+T3ZifD8c/qTgY2yw7hUxMTTBollxEtPnQH8HrRhNnEeNaoJ AJnhVNpfe5f8a/ddQSoSimMdF+b1MCT4Kroo3sVK2YjFnhgxo0sKBE9Zt5v2wXIi23+x eiSAS0pPdiZdA9QvD6DwvJ9eV0/QN3+UsfWFdF6P2C5q5VIOsIOERx1j6PcwQJ+mURiH AmbO7TksgekQ5Ywet/JxYTiLHkVXL6wbTvOksHuzc1NiBwY1XLAFGksVkIKfOCQBgYzu x8pgcG3/HlNVCjCSwx9MHClTkqXFYymfycZwKxpjO+SkObBNUXa2FOd07Pgo7b+y0BjX b7fg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=2r1eKT0GHVa73VDvbo2DptlCFZJjgR5mWxSgpJNvXcE=; b=BGD/TN6x3KhZJloVE1T4MauK9KwW5iy4/FEiZLBcE+Z/GEnpEaC79o4RourkF0pbQV D9A6zePGHCHqSVbHlAoJBRm14UIL0xJJHi3NSfFVTl6tTn0qkKFG/kJl9XQdZgV/RMsc /tvTS7VidaWXtfaZWwiEKzSKAILIshKxByvzvdu6YhQVLloiDcr+7VJTiOsrtRbxHttS Yb4uCLUtgQCRdd9dkZ79MYFYmXGgZCwzYouwp9I8YaEWS8HYTotyb/c8DO06eGEiSlu4 ug0z2nsy2aVEPEj88aRYXmC4Y4dOHE0tL5Pz5lR7zD3Qsk574Lq3EH04PY2dI1B1rku5 sUxg== X-Gm-Message-State: AOAM531OTGFCPCtf7+aAxs9cc3fFqgExytB/grSyUBn36y/mvaFQt0m8 zCEP89xqnDjfobQAb0dM9+A= X-Received: by 2002:a02:8206:: with SMTP id o6mr25016213jag.92.1628563352967; Mon, 09 Aug 2021 19:42:32 -0700 (PDT) Received: from auth2-smtp.messagingengine.com (auth2-smtp.messagingengine.com. [66.111.4.228]) by smtp.gmail.com with ESMTPSA id i11sm10268401ilb.15.2021.08.09.19.42.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Aug 2021 19:42:32 -0700 (PDT) Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailauth.nyi.internal (Postfix) with ESMTP id 23C3327C0054; Mon, 9 Aug 2021 22:42:30 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Mon, 09 Aug 2021 22:42:31 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvtddrjeekgdeivdcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpeffhffvuffkfhggtggujgesthdtredttddtvdenucfhrhhomhepuehoqhhunhcu hfgvnhhguceosghoqhhunhdrfhgvnhhgsehgmhgrihhlrdgtohhmqeenucggtffrrghtth gvrhhnpedvleeigedugfegveejhfejveeuveeiteejieekvdfgjeefudehfefhgfegvdeg jeenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegsoh hquhhnodhmvghsmhhtphgruhhthhhpvghrshhonhgrlhhithihqdeiledvgeehtdeigedq udejjeekheehhedvqdgsohhquhhnrdhfvghngheppehgmhgrihhlrdgtohhmsehfihigmh gvrdhnrghmvg X-ME-Proxy: Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 9 Aug 2021 22:42:28 -0400 (EDT) Date: Tue, 10 Aug 2021 10:42:21 +0800 From: Boqun Feng To: Valentin Schneider Cc: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, rcu@vger.kernel.org, linux-rt-users@vger.kernel.org, Catalin Marinas , Will Deacon , Ingo Molnar , Peter Zijlstra , Thomas Gleixner , Steven Rostedt , Daniel Bristot de Oliveira , Sebastian Andrzej Siewior , "Paul E. McKenney" , Frederic Weisbecker , Josh Triplett , Mathieu Desnoyers , Davidlohr Bueso , Lai Jiangshan , Joel Fernandes , Anshuman Khandual , Vincenzo Frascino , Steven Price , Ard Biesheuvel Subject: Re: [PATCH v2 2/4] sched: Introduce is_pcpu_safe() Message-ID: References: <20210807005807.1083943-1-valentin.schneider@arm.com> <20210807005807.1083943-3-valentin.schneider@arm.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210807005807.1083943-3-valentin.schneider@arm.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi, On Sat, Aug 07, 2021 at 01:58:05AM +0100, Valentin Schneider wrote: > Some areas use preempt_disable() + preempt_enable() to safely access > per-CPU data. The PREEMPT_RT folks have shown this can also be done by > keeping preemption enabled and instead disabling migration (and acquiring a > sleepable lock, if relevant). > > Introduce a helper which checks whether the current task can safely access > per-CPU data, IOW if the task's context guarantees the accesses will target > a single CPU. This accounts for preemption, CPU affinity, and migrate > disable - note that the CPU affinity check also mandates the presence of > PF_NO_SETAFFINITY, as otherwise userspace could concurrently render the > upcoming per-CPU access(es) unsafe. > > Signed-off-by: Valentin Schneider > --- > include/linux/sched.h | 10 ++++++++++ > 1 file changed, 10 insertions(+) > > diff --git a/include/linux/sched.h b/include/linux/sched.h > index debc960f41e3..b77d65f677f6 100644 > --- a/include/linux/sched.h > +++ b/include/linux/sched.h > @@ -1715,6 +1715,16 @@ static inline bool is_percpu_thread(void) > #endif > } > > +/* Is the current task guaranteed not to be migrated elsewhere? */ > +static inline bool is_pcpu_safe(void) > +{ > +#ifdef CONFIG_SMP > + return !preemptible() || is_percpu_thread() || current->migration_disabled; > +#else > + return true; > +#endif > +} I wonder whether the following can happen, say thread A is a worker thread for CPU 1, so it has the flag PF_NO_SETAFFINITY set. { percpu variable X on CPU 2 is initially 0 } thread A ======== if (is_pcpu_safe()) { // nr_cpus_allowed == 1, so return true. unbinder_workers(1); // A->cpus_mask becomes cpu_possible_mask __this_cpu_inc(X); tmp = X; // tmp == 0 this_cpu_inc(X); // X becomes 1 X = tmp + 1; // race! } if so, then is_percpu_thread() doesn't indicate is_pcpu_safe()? Regards, Boqun > + > /* Per-process atomic flags. */ > #define PFA_NO_NEW_PRIVS 0 /* May not gain new privileges. */ > #define PFA_SPREAD_PAGE 1 /* Spread page cache over cpuset */ > -- > 2.25.1 >