From 45789c236bf1893d6d85b73ee11abbd17bc41f6a Mon Sep 17 00:00:00 2001
From: Valentin Schneider <>
Date: Wed, 11 Aug 2021 21:13:52 +0100
Subject: [PATCH 002/166] sched: Introduce migratable()

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 be migrated
elsewhere, IOW if it is pinned to its local CPU in the current
context. This can help determining if per-CPU properties can be safely

Note that CPU affinity is not checked here, as a preemptible task can have
its affinity changed at any given time (including if it has
PF_NO_SETAFFINITY, when hotplug gets involved).

Signed-off-by: Valentin Schneider <>
[bigeasy: Return false on UP, call it is_migratable().]
Signed-off-by: Sebastian Andrzej Siewior <>
 include/linux/sched.h |   10 ++++++++++
 1 file changed, 10 insertions(+)

Index: linux-5.15.32-rt39/include/linux/sched.h
@ linux-5.15.32-rt39/include/linux/sched.h:1741 @ static __always_inline bool is_percpu_th
+/* Is the current task guaranteed to stay on its current CPU? */
+static inline bool is_migratable(void)
+#ifdef CONFIG_SMP
+	return preemptible() && !current->migration_disabled;
+	return false;
 /* 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 */