Received: by 2002:a05:6a10:c604:0:0:0:0 with SMTP id y4csp918352pxt; Fri, 6 Aug 2021 18:00:32 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwy6YfuthBthgOeOmOtautQIfrv2KZ5oFdNdjhkqz4YJJQ/I+mOmQimqGF9ENsdXf7KORD/ X-Received: by 2002:a05:6638:e92:: with SMTP id p18mr12278303jas.57.1628298032391; Fri, 06 Aug 2021 18:00:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628298032; cv=none; d=google.com; s=arc-20160816; b=GpOlCqZocx6krgd5Ks/rIsyeV0DDqHeZJSECk1hFyFjGZxF5F9R36T141Lxa1FoxDc HvHYhsrc9QUrOYockBJcD9ahMVhUx5hni3quVbDQdePykdMbwbDg3n9OsaNF8shWMw15 HJRS1A0Uw1EjgNc7MQtdaAs+t6SlScpfRWIRe+hAJeoD5Ylnvcxuq+Tfg1CRi66uH6i2 zGsg5ZIvftt7y8S57ZfdAjTrRBblsDZsBCEpdoU0XwhgzKKsXysAPFeL5abSKe95qs1R CCd81GQ8FVn4T/4c6vxv8kNdUhCCCQy+JKEm9lOHXdoQkmw4OKy301OefzS3cLUnd4K/ Fcxg== 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; bh=kXyh+YvuqmeyTN+YddthHlH2OkRxWpiNC9Cc27aitG0=; b=JR7k/NYVzlG0wyCh2Mp4T+sjQgY0xkcalXLiVPhgx83pyGpV7EUnIothNvmFLlbq+2 MzzqCn1dgkwFHkV87CESLXDGmDm3pP6FKBeyK16O+Gn3eL3RT+jPxkt0e3LLnCIpbJD0 xAUloMmqSCZIHu6BoRP37Xvib6fOkDzlFIGUs/VFX+mA041UjEiCEp8Hvrqtejx2cQ1V HsePLKr07DhoAUQ0kyVlS0RD3WjKK7W++z56+miI1ETQPqpttajbooGlFIzA8UBFJzxJ 50F1S5kvG/Xx7iyGA4AClaHR5yIxcX5UyUy73dKlCvitnhnifolaTViGAvVAD+HaGy9Z kGEw== 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 f14si9662693jar.87.2021.08.06.18.00.20; Fri, 06 Aug 2021 18:00:32 -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 S229862AbhHGA6x (ORCPT + 99 others); Fri, 6 Aug 2021 20:58:53 -0400 Received: from foss.arm.com ([217.140.110.172]:42778 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229758AbhHGA6w (ORCPT ); Fri, 6 Aug 2021 20:58:52 -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 12ABE113E; Fri, 6 Aug 2021 17:58:36 -0700 (PDT) Received: from e113632-lin.cambridge.arm.com (e113632-lin.cambridge.arm.com [10.1.194.46]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 3353F3F66F; Fri, 6 Aug 2021 17:58:33 -0700 (PDT) From: Valentin Schneider To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, rcu@vger.kernel.org, linux-rt-users@vger.kernel.org Cc: 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: [PATCH v2 2/4] sched: Introduce is_pcpu_safe() Date: Sat, 7 Aug 2021 01:58:05 +0100 Message-Id: <20210807005807.1083943-3-valentin.schneider@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210807005807.1083943-1-valentin.schneider@arm.com> References: <20210807005807.1083943-1-valentin.schneider@arm.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 +} + /* 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