Received: by 2002:a05:6a10:c604:0:0:0:0 with SMTP id y4csp3422881pxt; Tue, 10 Aug 2021 03:21:53 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz9RJXPRSykIM0qpVFwjFq/fYG3tVJiFCQpI4xB+qUgUYGhJZeFPjuYzBd6uY1MBr5Y44QY X-Received: by 2002:a05:6402:5242:: with SMTP id t2mr4097919edd.200.1628590913244; Tue, 10 Aug 2021 03:21:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628590913; cv=none; d=google.com; s=arc-20160816; b=D6kewRjps1pv6IV871ueX3WZ2JjxZ6I+BwfDbuuBQN+y14tEl0buHR+Fo++tH9WljS G9ayEsYTCCUGkllkaaHlv2yeEAJTLO3/v6PcIG/7hPPnOcdEiQ0/T7p17oi5eeXP2lxk /52wTjoNJ3PLZDvZTLEIGhyVPI4mY0OdoQIkx9sUSm7cVPGB1FsgKsioC8zrej6Xt+gu y5iod2xF6V1gxntaXx8CgQ0Q3KOiO2+Ck9ZiHYcFLdiWdZWbIDHxdxSofRP5iOupeNjX F+BNJ6tn2ikp4SPuyZ7ql1DoUTHDkkMTRIQ64ESP8sCAk+/9jOGwyKlF0Wy0DGcSu913 ZlOg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:date:references :in-reply-to:subject:cc:to:from; bh=+PbAoPbFSzvV0TyJ90av8HZydAjCbs86XiQl3ODKjSo=; b=AO3+9iNjqs2VqzcYzaVezig5eTAlz5JFCKgxWHu9D7hONaX0Y7vYVGpXyDGKEPy00W 50regCQmRUn8A+pBQ/3CBoiog+1Qkw5CEqaXfhJ5QV3fWWNjTWQ10s/qlH8aqMRvwWqk 9fDAFI8JrD3bYuGfqNm0IHoOYSMaAovoMCspLjCzmQElOpEbM85ftfOP58/eXdwijsRd PLQIWdDWoW7otFzFwhsfMx71RThLrO1e2pX1bA8YNwbf3iEbyo3rOydsTK8q+2C0Z5Pj PIk7j/xMHeLBOBbEj//4libnMlmC/D12UcjywvSqnkWp7AhHrikN2SndnX/BSUna5kf1 oA0Q== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id u7si18863837edx.92.2021.08.10.03.21.27; Tue, 10 Aug 2021 03:21:53 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238996AbhHJJ0p (ORCPT + 99 others); Tue, 10 Aug 2021 05:26:45 -0400 Received: from foss.arm.com ([217.140.110.172]:51870 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236851AbhHJJ0o (ORCPT ); Tue, 10 Aug 2021 05:26:44 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 9FE7DD6E; Tue, 10 Aug 2021 02:26:22 -0700 (PDT) Received: from e113632-lin (usa-sjc-imap-foss1.foss.arm.com [10.121.207.14]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id BFF863F718; Tue, 10 Aug 2021 02:26:19 -0700 (PDT) From: Valentin Schneider To: Boqun Feng 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() In-Reply-To: References: <20210807005807.1083943-1-valentin.schneider@arm.com> <20210807005807.1083943-3-valentin.schneider@arm.com> Date: Tue, 10 Aug 2021 10:26:15 +0100 Message-ID: <871r71abwo.mognet@arm.com> MIME-Version: 1.0 Content-Type: text/plain Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi, On 10/08/21 10:42, Boqun Feng wrote: > 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()? > You're absolutely right. migrate_disable() protects the thread against being migrated due to hotplug, but pure CPU affinity doesn't at all. kthread_is_per_cpu() doesn't work either, because parking is not the only approach to hotplug for those (e.g. per-CPU workqueue threads unbind themselves on hotplug, as in your example). One could hold cpus_read_lock(), but I don't see much point here. So that has to be return !preemptible() || current->migration_disabled; Thanks!