From: Peter Zijlstra <peterz@infradead.org>
To: Waiman Long <longman@redhat.com>
Cc: Ingo Molnar <mingo@redhat.com>,
Juri Lelli <juri.lelli@redhat.com>,
Vincent Guittot <vincent.guittot@linaro.org>,
Dietmar Eggemann <dietmar.eggemann@arm.com>,
Steven Rostedt <rostedt@goodmis.org>,
Ben Segall <bsegall@google.com>, Mel Gorman <mgorman@suse.de>,
Daniel Bristot de Oliveira <bristot@redhat.com>,
Valentin Schneider <vschneid@redhat.com>,
linux-kernel@vger.kernel.org, Phil Auld <pauld@redhat.com>,
Brent Rowsell <browsell@redhat.com>,
Peter Hunt <pehunt@redhat.com>,
Florian Weimer <fweimer@redhat.com>
Subject: Re: [PATCH v4] sched/core: Use zero length to reset cpumasks in sched_setaffinity()
Date: Wed, 4 Oct 2023 10:36:48 +0200 [thread overview]
Message-ID: <20231004083648.GI27267@noisy.programming.kicks-ass.net> (raw)
In-Reply-To: <20231003205735.2921964-1-longman@redhat.com>
On Tue, Oct 03, 2023 at 04:57:35PM -0400, Waiman Long wrote:
> Since commit 8f9ea86fdf99 ("sched: Always preserve the user requested
> cpumask"), user provided CPU affinity via sched_setaffinity(2) is
> perserved even if the task is being moved to a different cpuset. However,
> that affinity is also being inherited by any subsequently created child
> processes which may not want or be aware of that affinity.
>
> One way to solve this problem is to provide a way to back off from that
> user provided CPU affinity. This patch implements such a scheme by
> using an input cpumask length of 0 to signal a reset of the cpumasks
> to the default as allowed by the current cpuset. A non-NULL cpumask
> should still be provided to avoid problem with older kernel.
>
> If sched_setaffinity(2) has been called previously to set a user
> supplied cpumask, a value of 0 will be returned to indicate success.
> Otherwise, an error value of -EINVAL will be returned.
>
> We may have to update the sched_setaffinity(2) manpage to document
> this new side effect of passing in an input length of 0.
Bah.. so while this is less horrible than some of the previous hacks,
but I still think an all set mask is the sanest option.
Adding FreeBSD's CPU_FILL() to glibc() isn't the hardest thing ever, but
even without that, it's a single memset() away.
Would not the below two patches, one kernel, one glibc, be all it takes?
---
Subject: sched: Allow sched_setaffinity() to re-set the usermask
When userspace provides an all-set cpumask, take that to mean 'no
explicit affinity' and drop the usermask.
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
---
kernel/sched/core.c | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 779cdc7969c8..18124bbbb17c 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -8368,7 +8368,15 @@ long sched_setaffinity(pid_t pid, const struct cpumask *in_mask)
*/
user_mask = alloc_user_cpus_ptr(NUMA_NO_NODE);
if (user_mask) {
- cpumask_copy(user_mask, in_mask);
+ /*
+ * All-set user cpumask resets affinity and drops the explicit
+ * user mask.
+ */
+ cpumask_and(user_mask, in_mask, cpu_possible_mask);
+ if (cpumask_equal(user_mask, cpu_possible_mask)) {
+ kfree(user_mask);
+ user_mask = NULL;
+ }
} else if (IS_ENABLED(CONFIG_SMP)) {
return -ENOMEM;
}
---
Subject: sched: Add CPU_FILL()
Add the CPU_FILL() macros to easily create an all-set cpumask.
FreeBSD also provides this macro with this semantic.
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
---
posix/bits/cpu-set.h | 10 ++++++++++
posix/sched.h | 2 ++
2 files changed, 12 insertions(+)
diff --git a/posix/bits/cpu-set.h b/posix/bits/cpu-set.h
index 16037eae30..c65332461f 100644
--- a/posix/bits/cpu-set.h
+++ b/posix/bits/cpu-set.h
@@ -45,6 +45,8 @@ typedef struct
#if __GNUC_PREREQ (2, 91)
# define __CPU_ZERO_S(setsize, cpusetp) \
do __builtin_memset (cpusetp, '\0', setsize); while (0)
+# define __CPU_FILL_S(setsize, cpusetp) \
+ do __builtin_memset (cpusetp, 0xFF, setsize); while (0)
#else
# define __CPU_ZERO_S(setsize, cpusetp) \
do { \
@@ -54,6 +56,14 @@ typedef struct
for (__i = 0; __i < __imax; ++__i) \
__bits[__i] = 0; \
} while (0)
+# define __CPU_FILL_S(setsize, cpusetp) \
+ do { \
+ size_t __i; \
+ size_t __imax = (setsize) / sizeof (__cpu_mask); \
+ __cpu_mask *__bits = (cpusetp)->__bits; \
+ for (__i = 0; __i < __imax; ++__i) \
+ __bits[__i] = ~0UL; \
+ } while (0)
#endif
#define __CPU_SET_S(cpu, setsize, cpusetp) \
(__extension__ \
diff --git a/posix/sched.h b/posix/sched.h
index 9b254ae840..a7f6638353 100644
--- a/posix/sched.h
+++ b/posix/sched.h
@@ -94,6 +94,7 @@ extern int __REDIRECT_NTH (sched_rr_get_interval,
# define CPU_ISSET(cpu, cpusetp) __CPU_ISSET_S (cpu, sizeof (cpu_set_t), \
cpusetp)
# define CPU_ZERO(cpusetp) __CPU_ZERO_S (sizeof (cpu_set_t), cpusetp)
+# define CPU_FILL(cpusetp) __CPU_FILL_S (sizeof (cpu_set_t), cpusetp)
# define CPU_COUNT(cpusetp) __CPU_COUNT_S (sizeof (cpu_set_t), cpusetp)
# define CPU_SET_S(cpu, setsize, cpusetp) __CPU_SET_S (cpu, setsize, cpusetp)
@@ -101,6 +102,7 @@ extern int __REDIRECT_NTH (sched_rr_get_interval,
# define CPU_ISSET_S(cpu, setsize, cpusetp) __CPU_ISSET_S (cpu, setsize, \
cpusetp)
# define CPU_ZERO_S(setsize, cpusetp) __CPU_ZERO_S (setsize, cpusetp)
+# define CPU_FILL_S(setsize, cpusetp) __CPU_FILL_S (setsize, cpusetp)
# define CPU_COUNT_S(setsize, cpusetp) __CPU_COUNT_S (setsize, cpusetp)
# define CPU_EQUAL(cpusetp1, cpusetp2) \
next prev parent reply other threads:[~2023-10-04 8:37 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-10-03 20:57 [PATCH v4] sched/core: Use zero length to reset cpumasks in sched_setaffinity() Waiman Long
2023-10-04 8:36 ` Peter Zijlstra [this message]
2023-10-04 9:23 ` Ingo Molnar
2023-10-04 9:43 ` Peter Zijlstra
2023-10-04 10:06 ` Ingo Molnar
2023-10-04 12:19 ` Waiman Long
2023-10-04 12:34 ` Florian Weimer
2023-10-04 12:41 ` Waiman Long
2023-10-04 12:55 ` Florian Weimer
2023-10-04 16:23 ` Waiman Long
2023-10-04 13:52 ` Peter Zijlstra
2023-10-04 13:54 ` Peter Zijlstra
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20231004083648.GI27267@noisy.programming.kicks-ass.net \
--to=peterz@infradead.org \
--cc=bristot@redhat.com \
--cc=browsell@redhat.com \
--cc=bsegall@google.com \
--cc=dietmar.eggemann@arm.com \
--cc=fweimer@redhat.com \
--cc=juri.lelli@redhat.com \
--cc=linux-kernel@vger.kernel.org \
--cc=longman@redhat.com \
--cc=mgorman@suse.de \
--cc=mingo@redhat.com \
--cc=pauld@redhat.com \
--cc=pehunt@redhat.com \
--cc=rostedt@goodmis.org \
--cc=vincent.guittot@linaro.org \
--cc=vschneid@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox