diff mbox series

[V5] blk-mq: don't schedule block kworker on isolated CPUs

Message ID 20240322021244.1056223-1-ming.lei@redhat.com (mailing list archive)
State New, archived
Headers show
Series [V5] blk-mq: don't schedule block kworker on isolated CPUs | expand

Commit Message

Ming Lei March 22, 2024, 2:12 a.m. UTC
Kernel parameter of `isolcpus=` or 'nohz_full=' are used to isolate CPUs
for specific task, and it isn't expected to let block IO disturb these CPUs.
blk-mq kworker shouldn't be scheduled on isolated CPUs. Also if isolated
CPUs is run for blk-mq kworker, long block IO latency can be caused.

Kernel workqueue only respects CPU isolation for WQ_UNBOUND, for bound
WQ, the responsibility is on user because CPU is specified as WQ API
parameter, such as mod_delayed_work_on(cpu), queue_delayed_work_on(cpu)
and queue_work_on(cpu).

So not run blk-mq kworker on isolated CPUs by removing isolated CPUs
from hctx->cpumask. Meantime use queue map to check if all CPUs in this
hw queue are offline instead of hctx->cpumask, this way can avoid any
cost in fast IO code path, and is safe since hctx->cpumask are only
used in the two cases.

Cc: Tim Chen <tim.c.chen@linux.intel.com>
Cc: Juri Lelli <juri.lelli@redhat.com>
Cc: Andrew Theurer <atheurer@redhat.com>
Cc: Joe Mario <jmario@redhat.com>
Cc: Sebastian Jug <sejug@redhat.com>
Cc: Frederic Weisbecker <frederic@kernel.org>
Cc: Bart Van Assche <bvanassche@acm.org>
Cc: Tejun Heo <tj@kernel.org>
Tesed-by: Joe Mario <jmario@redhat.com>
Signed-off-by: Ming Lei <ming.lei@redhat.com>
---
V5:
	- fix comment
	- add one helper for document benefit
	- all are suggested by Jens
V4:
	- improve comment & commit log as suggested by Tim
V3:
	- avoid to check invalid cpu as reported by Bart
	- take current cpu(to be offline, not done yet) into account
	- simplify blk_mq_hctx_has_online_cpu()

V2:
	- remove module parameter, meantime use queue map to check if
	all cpus in one hctx are offline


 block/blk-mq.c | 57 +++++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 47 insertions(+), 10 deletions(-)

Comments

Ewan Milne March 25, 2024, 7:01 p.m. UTC | #1
On Thu, Mar 21, 2024 at 10:13 PM Ming Lei <ming.lei@redhat.com> wrote:
>
> Kernel parameter of `isolcpus=` or 'nohz_full=' are used to isolate CPUs
> for specific task, and it isn't expected to let block IO disturb these CPUs.
> blk-mq kworker shouldn't be scheduled on isolated CPUs. Also if isolated
> CPUs is run for blk-mq kworker, long block IO latency can be caused.
>
> Kernel workqueue only respects CPU isolation for WQ_UNBOUND, for bound
> WQ, the responsibility is on user because CPU is specified as WQ API
> parameter, such as mod_delayed_work_on(cpu), queue_delayed_work_on(cpu)
> and queue_work_on(cpu).
>
> So not run blk-mq kworker on isolated CPUs by removing isolated CPUs
> from hctx->cpumask. Meantime use queue map to check if all CPUs in this
> hw queue are offline instead of hctx->cpumask, this way can avoid any
> cost in fast IO code path, and is safe since hctx->cpumask are only
> used in the two cases.
>
> Cc: Tim Chen <tim.c.chen@linux.intel.com>
> Cc: Juri Lelli <juri.lelli@redhat.com>
> Cc: Andrew Theurer <atheurer@redhat.com>
> Cc: Joe Mario <jmario@redhat.com>
> Cc: Sebastian Jug <sejug@redhat.com>
> Cc: Frederic Weisbecker <frederic@kernel.org>
> Cc: Bart Van Assche <bvanassche@acm.org>
> Cc: Tejun Heo <tj@kernel.org>
> Tesed-by: Joe Mario <jmario@redhat.com>
> Signed-off-by: Ming Lei <ming.lei@redhat.com>
> ---
> V5:
>         - fix comment
>         - add one helper for document benefit
>         - all are suggested by Jens
> V4:
>         - improve comment & commit log as suggested by Tim
> V3:
>         - avoid to check invalid cpu as reported by Bart
>         - take current cpu(to be offline, not done yet) into account
>         - simplify blk_mq_hctx_has_online_cpu()
>
> V2:
>         - remove module parameter, meantime use queue map to check if
>         all cpus in one hctx are offline
>
>
>  block/blk-mq.c | 57 +++++++++++++++++++++++++++++++++++++++++---------
>  1 file changed, 47 insertions(+), 10 deletions(-)
>
> diff --git a/block/blk-mq.c b/block/blk-mq.c
> index 555ada922cf0..06cd9c8068fc 100644
> --- a/block/blk-mq.c
> +++ b/block/blk-mq.c
> @@ -28,6 +28,7 @@
>  #include <linux/prefetch.h>
>  #include <linux/blk-crypto.h>
>  #include <linux/part_stat.h>
> +#include <linux/sched/isolation.h>
>
>  #include <trace/events/block.h>
>
> @@ -2168,6 +2169,15 @@ static inline int blk_mq_first_mapped_cpu(struct blk_mq_hw_ctx *hctx)
>         return cpu;
>  }
>
> +/*
> + * ->next_cpu is always calculated from hctx->cpumask, so simply use
> + * it for speeding up the check
> + */
> +static bool blk_mq_hctx_empty_cpumask(struct blk_mq_hw_ctx *hctx)
> +{
> +        return hctx->next_cpu >= nr_cpu_ids;
> +}
> +
>  /*
>   * It'd be great if the workqueue API had a way to pass
>   * in a mask and had some smarts for more clever placement.
> @@ -2179,7 +2189,8 @@ static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx)
>         bool tried = false;
>         int next_cpu = hctx->next_cpu;
>
> -       if (hctx->queue->nr_hw_queues == 1)
> +       /* Switch to unbound if no allowable CPUs in this hctx */
> +       if (hctx->queue->nr_hw_queues == 1 || blk_mq_hctx_empty_cpumask(hctx))
>                 return WORK_CPU_UNBOUND;
>
>         if (--hctx->next_cpu_batch <= 0) {
> @@ -3488,14 +3499,30 @@ static bool blk_mq_hctx_has_requests(struct blk_mq_hw_ctx *hctx)
>         return data.has_rq;
>  }
>
> -static inline bool blk_mq_last_cpu_in_hctx(unsigned int cpu,
> -               struct blk_mq_hw_ctx *hctx)
> +static bool blk_mq_hctx_has_online_cpu(struct blk_mq_hw_ctx *hctx,
> +               unsigned int this_cpu)
>  {
> -       if (cpumask_first_and(hctx->cpumask, cpu_online_mask) != cpu)
> -               return false;
> -       if (cpumask_next_and(cpu, hctx->cpumask, cpu_online_mask) < nr_cpu_ids)
> -               return false;
> -       return true;
> +       enum hctx_type type = hctx->type;
> +       int cpu;
> +
> +       /*
> +        * hctx->cpumask has to rule out isolated CPUs, but userspace still
> +        * might submit IOs on these isolated CPUs, so use the queue map to
> +        * check if all CPUs mapped to this hctx are offline
> +        */
> +       for_each_online_cpu(cpu) {
> +               struct blk_mq_hw_ctx *h = blk_mq_map_queue_type(hctx->queue,
> +                               type, cpu);
> +
> +               if (h != hctx)
> +                       continue;
> +
> +               /* this hctx has at least one online CPU */
> +               if (this_cpu != cpu)
> +                       return true;
> +       }
> +
> +       return false;
>  }
>
>  static int blk_mq_hctx_notify_offline(unsigned int cpu, struct hlist_node *node)
> @@ -3503,8 +3530,7 @@ static int blk_mq_hctx_notify_offline(unsigned int cpu, struct hlist_node *node)
>         struct blk_mq_hw_ctx *hctx = hlist_entry_safe(node,
>                         struct blk_mq_hw_ctx, cpuhp_online);
>
> -       if (!cpumask_test_cpu(cpu, hctx->cpumask) ||
> -           !blk_mq_last_cpu_in_hctx(cpu, hctx))
> +       if (blk_mq_hctx_has_online_cpu(hctx, cpu))
>                 return 0;
>
>         /*
> @@ -3912,6 +3938,8 @@ static void blk_mq_map_swqueue(struct request_queue *q)
>         }
>
>         queue_for_each_hw_ctx(q, hctx, i) {
> +               int cpu;
> +
>                 /*
>                  * If no software queues are mapped to this hardware queue,
>                  * disable it and free the request entries.
> @@ -3938,6 +3966,15 @@ static void blk_mq_map_swqueue(struct request_queue *q)
>                  */
>                 sbitmap_resize(&hctx->ctx_map, hctx->nr_ctx);
>
> +               /*
> +                * Rule out isolated CPUs from hctx->cpumask to avoid
> +                * running block kworker on isolated CPUs
> +                */
> +               for_each_cpu(cpu, hctx->cpumask) {
> +                       if (cpu_is_isolated(cpu))
> +                               cpumask_clear_cpu(cpu, hctx->cpumask);
> +               }
> +
>                 /*
>                  * Initialize batch roundrobin counts
>                  */
> --
> 2.41.0
>
>

Reviewed-by: Ewan D. Milne <emilne@redhat.com>
Jens Axboe March 27, 2024, 4:29 p.m. UTC | #2
On Fri, 22 Mar 2024 10:12:44 +0800, Ming Lei wrote:
> Kernel parameter of `isolcpus=` or 'nohz_full=' are used to isolate CPUs
> for specific task, and it isn't expected to let block IO disturb these CPUs.
> blk-mq kworker shouldn't be scheduled on isolated CPUs. Also if isolated
> CPUs is run for blk-mq kworker, long block IO latency can be caused.
> 
> Kernel workqueue only respects CPU isolation for WQ_UNBOUND, for bound
> WQ, the responsibility is on user because CPU is specified as WQ API
> parameter, such as mod_delayed_work_on(cpu), queue_delayed_work_on(cpu)
> and queue_work_on(cpu).
> 
> [...]

Applied, thanks!

[1/1] blk-mq: don't schedule block kworker on isolated CPUs
      commit: 5f27f3d892743312fce237958a4194859ef93d69

Best regards,
diff mbox series

Patch

diff --git a/block/blk-mq.c b/block/blk-mq.c
index 555ada922cf0..06cd9c8068fc 100644
--- a/block/blk-mq.c
+++ b/block/blk-mq.c
@@ -28,6 +28,7 @@ 
 #include <linux/prefetch.h>
 #include <linux/blk-crypto.h>
 #include <linux/part_stat.h>
+#include <linux/sched/isolation.h>
 
 #include <trace/events/block.h>
 
@@ -2168,6 +2169,15 @@  static inline int blk_mq_first_mapped_cpu(struct blk_mq_hw_ctx *hctx)
 	return cpu;
 }
 
+/*
+ * ->next_cpu is always calculated from hctx->cpumask, so simply use
+ * it for speeding up the check
+ */
+static bool blk_mq_hctx_empty_cpumask(struct blk_mq_hw_ctx *hctx)
+{
+        return hctx->next_cpu >= nr_cpu_ids;
+}
+
 /*
  * It'd be great if the workqueue API had a way to pass
  * in a mask and had some smarts for more clever placement.
@@ -2179,7 +2189,8 @@  static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx)
 	bool tried = false;
 	int next_cpu = hctx->next_cpu;
 
-	if (hctx->queue->nr_hw_queues == 1)
+	/* Switch to unbound if no allowable CPUs in this hctx */
+	if (hctx->queue->nr_hw_queues == 1 || blk_mq_hctx_empty_cpumask(hctx))
 		return WORK_CPU_UNBOUND;
 
 	if (--hctx->next_cpu_batch <= 0) {
@@ -3488,14 +3499,30 @@  static bool blk_mq_hctx_has_requests(struct blk_mq_hw_ctx *hctx)
 	return data.has_rq;
 }
 
-static inline bool blk_mq_last_cpu_in_hctx(unsigned int cpu,
-		struct blk_mq_hw_ctx *hctx)
+static bool blk_mq_hctx_has_online_cpu(struct blk_mq_hw_ctx *hctx,
+		unsigned int this_cpu)
 {
-	if (cpumask_first_and(hctx->cpumask, cpu_online_mask) != cpu)
-		return false;
-	if (cpumask_next_and(cpu, hctx->cpumask, cpu_online_mask) < nr_cpu_ids)
-		return false;
-	return true;
+	enum hctx_type type = hctx->type;
+	int cpu;
+
+	/*
+	 * hctx->cpumask has to rule out isolated CPUs, but userspace still
+	 * might submit IOs on these isolated CPUs, so use the queue map to
+	 * check if all CPUs mapped to this hctx are offline
+	 */
+	for_each_online_cpu(cpu) {
+		struct blk_mq_hw_ctx *h = blk_mq_map_queue_type(hctx->queue,
+				type, cpu);
+
+		if (h != hctx)
+			continue;
+
+		/* this hctx has at least one online CPU */
+		if (this_cpu != cpu)
+			return true;
+	}
+
+	return false;
 }
 
 static int blk_mq_hctx_notify_offline(unsigned int cpu, struct hlist_node *node)
@@ -3503,8 +3530,7 @@  static int blk_mq_hctx_notify_offline(unsigned int cpu, struct hlist_node *node)
 	struct blk_mq_hw_ctx *hctx = hlist_entry_safe(node,
 			struct blk_mq_hw_ctx, cpuhp_online);
 
-	if (!cpumask_test_cpu(cpu, hctx->cpumask) ||
-	    !blk_mq_last_cpu_in_hctx(cpu, hctx))
+	if (blk_mq_hctx_has_online_cpu(hctx, cpu))
 		return 0;
 
 	/*
@@ -3912,6 +3938,8 @@  static void blk_mq_map_swqueue(struct request_queue *q)
 	}
 
 	queue_for_each_hw_ctx(q, hctx, i) {
+		int cpu;
+
 		/*
 		 * If no software queues are mapped to this hardware queue,
 		 * disable it and free the request entries.
@@ -3938,6 +3966,15 @@  static void blk_mq_map_swqueue(struct request_queue *q)
 		 */
 		sbitmap_resize(&hctx->ctx_map, hctx->nr_ctx);
 
+		/*
+		 * Rule out isolated CPUs from hctx->cpumask to avoid
+		 * running block kworker on isolated CPUs
+		 */
+		for_each_cpu(cpu, hctx->cpumask) {
+			if (cpu_is_isolated(cpu))
+				cpumask_clear_cpu(cpu, hctx->cpumask);
+		}
+
 		/*
 		 * Initialize batch roundrobin counts
 		 */