From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.7 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id DE9B6C4338F for ; Tue, 10 Aug 2021 02:45:27 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 7D1DD60F25 for ; Tue, 10 Aug 2021 02:45:27 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 7D1DD60F25 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References: Message-ID:Subject:Cc:To:From:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=+YsxIpyG3pb/7sv7OM6FSCV1bZ6vE4R3MbeA7QW1y78=; b=GwteV85yKh52Uw 24ZuvaFC+qfEKNQPqBLZMbPwc93BiGEKg50MMkGshk3aE/qygKk0INH5OBkF37JqehMqWoAEkvcbw pCQK9fuyDK9RLhcYNW8XfmIUqnv3HcAWP5pQ8E1IS6XkUhVmYywYNWKMx8ViUCl+XUxHpMAcFkNAk i04AxVCeeReufOsMVQFSDstckXnhfqSXk47jHPAtydVjtR/JcW+PyUhc0CtKVTDaUGUl0ntgrXvTp UUw07wGlAssMynnm902vkCVoZznbEvREH8MXLog4ke3WNFfKjagyoch6e4wI8NAe18WaiyVJKA5qj dx47OdEtZ8L4JMfjF10Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mDHiv-002IFW-MV; Tue, 10 Aug 2021 02:42:37 +0000 Received: from mail-io1-xd2e.google.com ([2607:f8b0:4864:20::d2e]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mDHir-002IEs-US for linux-arm-kernel@lists.infradead.org; Tue, 10 Aug 2021 02:42:35 +0000 Received: by mail-io1-xd2e.google.com with SMTP id x10so19431133iop.13 for ; 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=tHbmJFVOAEmwDPr71A8tmzE08Lvq3TrDBHvvodMxNoZoEjLoKAdCrmOM8Dbt1Lko4i 2dnysRdk2AuCOGEOWXGSG3k3arQ6ooqygO+he2o+64JBKvVyCya92lIyCaBz21p8eY6a UipSNAwsc2S2JPQ4qCZgvX5XKaRdi8njuI73xyZ2RZyvrS62QcBTKwRLf3mARs5V85EV 1TVdhwoSqlMbYLJjxO5+kVFpsjGmqYfDEccN1l6dVllds+kuCbk7mCK8YeDi9+dP4roX DP6e6pTyj3Yl/gxA0oVjj75FZBvwnlAzdx9PqLYEltTw8moU9huO3j8hSdFj2JIpRcRl j+YQ== X-Gm-Message-State: AOAM531j7NC+eS6Otfoo7FhyFEuhZt7N5Ujkn7OS62ExYs0/i+NncRDl m3L30MKYO0E0KFM78Vz1+4w= X-Google-Smtp-Source: ABdhPJzJih5QO/F+2BNnmyBITFJ9dfwTTT/BfBBYWTVH6SE75HmgVfERCfi0lZY/88t+lYGkua3r6A== 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-Disposition: inline In-Reply-To: <20210807005807.1083943-3-valentin.schneider@arm.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210809_194234_015821_E5174A88 X-CRM114-Status: GOOD ( 24.48 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.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 > _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel