All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] cpumask: add cpumask_any_and_but()
@ 2017-02-06 11:38 Mark Rutland
  2017-02-06 13:48 ` Peter Zijlstra
  2017-02-08  0:38 ` Rusty Russell
  0 siblings, 2 replies; 3+ messages in thread
From: Mark Rutland @ 2017-02-06 11:38 UTC (permalink / raw)
  To: linux-kernel
  Cc: Mark Rutland, Thomas Gleixner, Andrew Morton, Peter Zijlstra,
	Rusty Russell

In some cases, it's useful to be able to select a random cpu from the
intersection of two masks, excluding a particular CPU.

For example, in some systems an uncore PMU is shared by a subset of
CPUs, and management of this PMU is assigned to some arbitrary CPU in
this set. Whenever the management CPU is hotplugged out, we wish to
migrate responsibility to another arbitrary CPU which is both in this
set and online.

Today we can use cpumask_any_and() to select an arbitrary CPU in the
intersection of two masks. We can also use cpumask_any_but() to select
any arbitrary cpu in a mask excluding, a particular CPU.

To do both, we either need to use a temporary cpumask, which is
wasteful, or use some lower-level cpumask helpers, which can be unclear.

This patch adds a new cpumask_any_and_but() to cater for these cases.

Signed-off-by: Mark Rutland <mark.rutland@arm.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Rusty Russell <rusty@rustcorp.com.au>
Cc: linux-kernel@vger.kernel.org
---
 include/linux/cpumask.h |  3 +++
 lib/cpumask.c           | 23 +++++++++++++++++++++++
 2 files changed, 26 insertions(+)

This patch would help in cases like the Qualcomm L2 cache PMU driver [1]. If
people are happy with this patch, I'd like to take it along with that patch
(modified to use the new helper). I'm also happy to leave this as a subsequent
cleanup.

Thanks,
Mark.

[1] http://lists.infradead.org/pipermail/linux-arm-kernel/2017-February/485762.html

diff --git a/include/linux/cpumask.h b/include/linux/cpumask.h
index c717f5e..99dc550 100644
--- a/include/linux/cpumask.h
+++ b/include/linux/cpumask.h
@@ -210,6 +210,9 @@ static inline unsigned int cpumask_next_zero(int n, const struct cpumask *srcp)
 
 int cpumask_next_and(int n, const struct cpumask *, const struct cpumask *);
 int cpumask_any_but(const struct cpumask *mask, unsigned int cpu);
+int cpumask_any_and_but(const struct cpumask *mask1,
+			const struct cpumask *mask2,
+			unsigned int cpu);
 unsigned int cpumask_local_spread(unsigned int i, int node);
 
 /**
diff --git a/lib/cpumask.c b/lib/cpumask.c
index 81dedaa..641b526 100644
--- a/lib/cpumask.c
+++ b/lib/cpumask.c
@@ -43,6 +43,29 @@ int cpumask_any_but(const struct cpumask *mask, unsigned int cpu)
 }
 EXPORT_SYMBOL(cpumask_any_but);
 
+/**
+ * cpumask_any_and_but - pick a "random" cpu from *mask1 & *mask2, but not this one.
+ * @mask1: the first input cpumask
+ * @mask2: the second input cpumask
+ * @cpu: the cpu to ignore
+ *
+ * Returns >= nr_cpu_ids if no cpus set.
+ */
+int cpumask_any_and_but(const struct cpumask *mask1,
+			const struct cpumask *mask2,
+			unsigned int cpu)
+{
+	unsigned int i;
+
+	cpumask_check(cpu);
+	i = cpumask_first_and(mask1, mask2);
+	if (i != cpu)
+		return i;
+
+	return cpumask_next_and(cpu, mask1, mask2);
+}
+EXPORT_SYMBOL(cpumask_any_and_but);
+
 /* These are not inline because of header tangles. */
 #ifdef CONFIG_CPUMASK_OFFSTACK
 /**
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] cpumask: add cpumask_any_and_but()
  2017-02-06 11:38 [PATCH] cpumask: add cpumask_any_and_but() Mark Rutland
@ 2017-02-06 13:48 ` Peter Zijlstra
  2017-02-08  0:38 ` Rusty Russell
  1 sibling, 0 replies; 3+ messages in thread
From: Peter Zijlstra @ 2017-02-06 13:48 UTC (permalink / raw)
  To: Mark Rutland; +Cc: linux-kernel, Thomas Gleixner, Andrew Morton, Rusty Russell

On Mon, Feb 06, 2017 at 11:38:52AM +0000, Mark Rutland wrote:
> In some cases, it's useful to be able to select a random cpu from the
> intersection of two masks, excluding a particular CPU.
> 
> For example, in some systems an uncore PMU is shared by a subset of
> CPUs, and management of this PMU is assigned to some arbitrary CPU in
> this set. Whenever the management CPU is hotplugged out, we wish to
> migrate responsibility to another arbitrary CPU which is both in this
> set and online.
> 
> Today we can use cpumask_any_and() to select an arbitrary CPU in the
> intersection of two masks. We can also use cpumask_any_but() to select
> any arbitrary cpu in a mask excluding, a particular CPU.
> 
> To do both, we either need to use a temporary cpumask, which is
> wasteful, or use some lower-level cpumask helpers, which can be unclear.
> 
> This patch adds a new cpumask_any_and_but() to cater for these cases.
> 
> Signed-off-by: Mark Rutland <mark.rutland@arm.com>
> Cc: Thomas Gleixner <tglx@linutronix.de>
> Cc: Andrew Morton <akpm@linux-foundation.org>
> Cc: Peter Zijlstra <peterz@infradead.org>
> Cc: Rusty Russell <rusty@rustcorp.com.au>
> Cc: linux-kernel@vger.kernel.org
> ---
>  include/linux/cpumask.h |  3 +++
>  lib/cpumask.c           | 23 +++++++++++++++++++++++
>  2 files changed, 26 insertions(+)
> 
> This patch would help in cases like the Qualcomm L2 cache PMU driver [1]. If
> people are happy with this patch, I'd like to take it along with that patch
> (modified to use the new helper). I'm also happy to leave this as a subsequent
> cleanup.

Have at; looks ok I suppose.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] cpumask: add cpumask_any_and_but()
  2017-02-06 11:38 [PATCH] cpumask: add cpumask_any_and_but() Mark Rutland
  2017-02-06 13:48 ` Peter Zijlstra
@ 2017-02-08  0:38 ` Rusty Russell
  1 sibling, 0 replies; 3+ messages in thread
From: Rusty Russell @ 2017-02-08  0:38 UTC (permalink / raw)
  To: Mark Rutland, linux-kernel
  Cc: Mark Rutland, Thomas Gleixner, Andrew Morton, Peter Zijlstra

Mark Rutland <mark.rutland@arm.com> writes:
> In some cases, it's useful to be able to select a random cpu from the
> intersection of two masks, excluding a particular CPU.

Acked-by: Rusty Russell <rusty@rustcorp.com.au>

Thanks,
Rusty.

> For example, in some systems an uncore PMU is shared by a subset of
> CPUs, and management of this PMU is assigned to some arbitrary CPU in
> this set. Whenever the management CPU is hotplugged out, we wish to
> migrate responsibility to another arbitrary CPU which is both in this
> set and online.
>
> Today we can use cpumask_any_and() to select an arbitrary CPU in the
> intersection of two masks. We can also use cpumask_any_but() to select
> any arbitrary cpu in a mask excluding, a particular CPU.
>
> To do both, we either need to use a temporary cpumask, which is
> wasteful, or use some lower-level cpumask helpers, which can be unclear.
>
> This patch adds a new cpumask_any_and_but() to cater for these cases.
>
> Signed-off-by: Mark Rutland <mark.rutland@arm.com>
> Cc: Thomas Gleixner <tglx@linutronix.de>
> Cc: Andrew Morton <akpm@linux-foundation.org>
> Cc: Peter Zijlstra <peterz@infradead.org>
> Cc: Rusty Russell <rusty@rustcorp.com.au>
> Cc: linux-kernel@vger.kernel.org
> ---
>  include/linux/cpumask.h |  3 +++
>  lib/cpumask.c           | 23 +++++++++++++++++++++++
>  2 files changed, 26 insertions(+)
>
> This patch would help in cases like the Qualcomm L2 cache PMU driver [1]. If
> people are happy with this patch, I'd like to take it along with that patch
> (modified to use the new helper). I'm also happy to leave this as a subsequent
> cleanup.
>
> Thanks,
> Mark.
>
> [1] http://lists.infradead.org/pipermail/linux-arm-kernel/2017-February/485762.html
>
> diff --git a/include/linux/cpumask.h b/include/linux/cpumask.h
> index c717f5e..99dc550 100644
> --- a/include/linux/cpumask.h
> +++ b/include/linux/cpumask.h
> @@ -210,6 +210,9 @@ static inline unsigned int cpumask_next_zero(int n, const struct cpumask *srcp)
>  
>  int cpumask_next_and(int n, const struct cpumask *, const struct cpumask *);
>  int cpumask_any_but(const struct cpumask *mask, unsigned int cpu);
> +int cpumask_any_and_but(const struct cpumask *mask1,
> +			const struct cpumask *mask2,
> +			unsigned int cpu);
>  unsigned int cpumask_local_spread(unsigned int i, int node);
>  
>  /**
> diff --git a/lib/cpumask.c b/lib/cpumask.c
> index 81dedaa..641b526 100644
> --- a/lib/cpumask.c
> +++ b/lib/cpumask.c
> @@ -43,6 +43,29 @@ int cpumask_any_but(const struct cpumask *mask, unsigned int cpu)
>  }
>  EXPORT_SYMBOL(cpumask_any_but);
>  
> +/**
> + * cpumask_any_and_but - pick a "random" cpu from *mask1 & *mask2, but not this one.
> + * @mask1: the first input cpumask
> + * @mask2: the second input cpumask
> + * @cpu: the cpu to ignore
> + *
> + * Returns >= nr_cpu_ids if no cpus set.
> + */
> +int cpumask_any_and_but(const struct cpumask *mask1,
> +			const struct cpumask *mask2,
> +			unsigned int cpu)
> +{
> +	unsigned int i;
> +
> +	cpumask_check(cpu);
> +	i = cpumask_first_and(mask1, mask2);
> +	if (i != cpu)
> +		return i;
> +
> +	return cpumask_next_and(cpu, mask1, mask2);
> +}
> +EXPORT_SYMBOL(cpumask_any_and_but);
> +
>  /* These are not inline because of header tangles. */
>  #ifdef CONFIG_CPUMASK_OFFSTACK
>  /**
> -- 
> 1.9.1

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2017-02-08  1:04 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-02-06 11:38 [PATCH] cpumask: add cpumask_any_and_but() Mark Rutland
2017-02-06 13:48 ` Peter Zijlstra
2017-02-08  0:38 ` Rusty Russell

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.