Linux block layer
 help / color / mirror / Atom feed
* [PATCH] block: avoid to call blkg_free() in atomic context
@ 2022-03-22 16:12 Ming Lei
  2022-03-22 16:42 ` Tejun Heo
  2022-03-23  0:13 ` kernel test robot
  0 siblings, 2 replies; 4+ messages in thread
From: Ming Lei @ 2022-03-22 16:12 UTC (permalink / raw)
  To: Jens Axboe; +Cc: linux-block, Ming Lei, Tejun Heo, Christoph Hellwig

blkg_free() may be called in atomic context, either spin lock is held,
or run in rcu callback. Meantime either request queue's release handler
or ->pd_free_fn can sleep.

Fix the issue by scheduling work function for freeing blkcg_gq instance.

Cc: Tejun Heo <tj@kernel.org>
Fixes: 0a9a25ca7843 ("block: let blkcg_gq grab request queue's refcnt")
Reported-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Ming Lei <ming.lei@redhat.com>
---
 block/blk-cgroup.c         | 14 +++++++++++++-
 include/linux/blk-cgroup.h |  5 ++++-
 2 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/block/blk-cgroup.c b/block/blk-cgroup.c
index d53b0d69dd73..89a976d3ab74 100644
--- a/block/blk-cgroup.c
+++ b/block/blk-cgroup.c
@@ -71,8 +71,10 @@ static bool blkcg_policy_enabled(struct request_queue *q,
  *
  * Free @blkg which may be partially allocated.
  */
-static void blkg_free(struct blkcg_gq *blkg)
+static void blkg_free_workfn(struct work_struct *work)
 {
+	struct blkcg_gq *blkg = container_of(work, struct blkcg_gq,
+					     free_work);
 	int i;
 
 	if (!blkg)
@@ -89,6 +91,16 @@ static void blkg_free(struct blkcg_gq *blkg)
 	kfree(blkg);
 }
 
+static void blkg_free(struct blkcg_gq *blkg)
+{
+	/*
+	 * Both ->pd_free_fn() and request queue's release handler may
+	 * sleep, so free us by scheduling one work func
+	 */
+	INIT_WORK(&blkg->free_work, blkg_free_workfn);
+	schedule_work(&blkg->free_work);
+}
+
 static void __blkg_release(struct rcu_head *rcu)
 {
 	struct blkcg_gq *blkg = container_of(rcu, struct blkcg_gq, rcu_head);
diff --git a/include/linux/blk-cgroup.h b/include/linux/blk-cgroup.h
index f2ad8ed8f777..652cd05b0924 100644
--- a/include/linux/blk-cgroup.h
+++ b/include/linux/blk-cgroup.h
@@ -95,7 +95,10 @@ struct blkcg_gq {
 
 	spinlock_t			async_bio_lock;
 	struct bio_list			async_bios;
-	struct work_struct		async_bio_work;
+	union {
+		struct work_struct	async_bio_work;
+		struct work_struct	free_work;
+	};
 
 	atomic_t			use_delay;
 	atomic64_t			delay_nsec;
-- 
2.31.1


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

* Re: [PATCH] block: avoid to call blkg_free() in atomic context
  2022-03-22 16:12 [PATCH] block: avoid to call blkg_free() in atomic context Ming Lei
@ 2022-03-22 16:42 ` Tejun Heo
  2022-03-23  0:13 ` kernel test robot
  1 sibling, 0 replies; 4+ messages in thread
From: Tejun Heo @ 2022-03-22 16:42 UTC (permalink / raw)
  To: Ming Lei; +Cc: Jens Axboe, linux-block, Christoph Hellwig

On Wed, Mar 23, 2022 at 12:12:38AM +0800, Ming Lei wrote:
> blkg_free() may be called in atomic context, either spin lock is held,
> or run in rcu callback. Meantime either request queue's release handler
> or ->pd_free_fn can sleep.
> 
> Fix the issue by scheduling work function for freeing blkcg_gq instance.
> 
> Cc: Tejun Heo <tj@kernel.org>
> Fixes: 0a9a25ca7843 ("block: let blkcg_gq grab request queue's refcnt")
> Reported-by: Christoph Hellwig <hch@lst.de>
> Signed-off-by: Ming Lei <ming.lei@redhat.com>

Do you have an example of this being a problem?

Thanks.

-- 
tejun

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

* Re: [PATCH] block: avoid to call blkg_free() in atomic context
  2022-03-22 16:12 [PATCH] block: avoid to call blkg_free() in atomic context Ming Lei
  2022-03-22 16:42 ` Tejun Heo
@ 2022-03-23  0:13 ` kernel test robot
  2022-03-23  1:05   ` Ming Lei
  1 sibling, 1 reply; 4+ messages in thread
From: kernel test robot @ 2022-03-23  0:13 UTC (permalink / raw)
  To: Ming Lei, Jens Axboe
  Cc: llvm, kbuild-all, linux-block, Ming Lei, Tejun Heo,
	Christoph Hellwig

Hi Ming,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on axboe-block/for-next]
[also build test WARNING on v5.17 next-20220322]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Ming-Lei/block-avoid-to-call-blkg_free-in-atomic-context/20220323-001434
base:   https://git.kernel.org/pub/scm/linux/kernel/git/axboe/linux-block.git for-next
config: i386-randconfig-a005-20220321 (https://download.01.org/0day-ci/archive/20220323/202203230833.LMKQ6DdX-lkp@intel.com/config)
compiler: clang version 15.0.0 (https://github.com/llvm/llvm-project 902f4708fe1d03b0de7e5315ef875006a6adc319)
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # https://github.com/0day-ci/linux/commit/c40ac630dd1d94497e427b4933efad4dbfaa0b5b
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review Ming-Lei/block-avoid-to-call-blkg_free-in-atomic-context/20220323-001434
        git checkout c40ac630dd1d94497e427b4933efad4dbfaa0b5b
        # save the config file to linux build tree
        mkdir build_dir
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=i386 SHELL=/bin/bash

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>

All warnings (new ones prefixed by >>):

   block/blk-cgroup.c:75: warning: Function parameter or member 'work' not described in 'blkg_free_workfn'
>> block/blk-cgroup.c:75: warning: expecting prototype for blkg_free(). Prototype was for blkg_free_workfn() instead


vim +75 block/blk-cgroup.c

a2b1693bac45ea Tejun Heo 2012-04-13  67  
0381411e4b1a52 Tejun Heo 2012-03-05  68  /**
0381411e4b1a52 Tejun Heo 2012-03-05  69   * blkg_free - free a blkg
0381411e4b1a52 Tejun Heo 2012-03-05  70   * @blkg: blkg to free
0381411e4b1a52 Tejun Heo 2012-03-05  71   *
0381411e4b1a52 Tejun Heo 2012-03-05  72   * Free @blkg which may be partially allocated.
0381411e4b1a52 Tejun Heo 2012-03-05  73   */
c40ac630dd1d94 Ming Lei  2022-03-23  74  static void blkg_free_workfn(struct work_struct *work)
0381411e4b1a52 Tejun Heo 2012-03-05 @75  {
c40ac630dd1d94 Ming Lei  2022-03-23  76  	struct blkcg_gq *blkg = container_of(work, struct blkcg_gq,
c40ac630dd1d94 Ming Lei  2022-03-23  77  					     free_work);
e8989fae38d983 Tejun Heo 2012-03-05  78  	int i;
549d3aa872cd1a Tejun Heo 2012-03-05  79  
549d3aa872cd1a Tejun Heo 2012-03-05  80  	if (!blkg)
549d3aa872cd1a Tejun Heo 2012-03-05  81  		return;
549d3aa872cd1a Tejun Heo 2012-03-05  82  
db61367038dcd2 Tejun Heo 2013-05-14  83  	for (i = 0; i < BLKCG_MAX_POLS; i++)
001bea73e70efd Tejun Heo 2015-08-18  84  		if (blkg->pd[i])
001bea73e70efd Tejun Heo 2015-08-18  85  			blkcg_policy[i]->pd_free_fn(blkg->pd[i]);
e8989fae38d983 Tejun Heo 2012-03-05  86  
0a9a25ca78437b Ming Lei  2022-03-18  87  	if (blkg->q)
0a9a25ca78437b Ming Lei  2022-03-18  88  		blk_put_queue(blkg->q);
f73316482977ac Tejun Heo 2019-11-07  89  	free_percpu(blkg->iostat_cpu);
ef069b97feec11 Tejun Heo 2019-06-13  90  	percpu_ref_exit(&blkg->refcnt);
549d3aa872cd1a Tejun Heo 2012-03-05  91  	kfree(blkg);
0381411e4b1a52 Tejun Heo 2012-03-05  92  }
0381411e4b1a52 Tejun Heo 2012-03-05  93  

-- 
0-DAY CI Kernel Test Service
https://01.org/lkp

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

* Re: [PATCH] block: avoid to call blkg_free() in atomic context
  2022-03-23  0:13 ` kernel test robot
@ 2022-03-23  1:05   ` Ming Lei
  0 siblings, 0 replies; 4+ messages in thread
From: Ming Lei @ 2022-03-23  1:05 UTC (permalink / raw)
  To: kernel test robot
  Cc: Jens Axboe, llvm, kbuild-all, linux-block, Tejun Heo,
	Christoph Hellwig

On Wed, Mar 23, 2022 at 08:13:46AM +0800, kernel test robot wrote:
> Hi Ming,
> 
> Thank you for the patch! Perhaps something to improve:
> 
> [auto build test WARNING on axboe-block/for-next]
> [also build test WARNING on v5.17 next-20220322]
> [If your patch is applied to the wrong git tree, kindly drop us a note.
> And when submitting patch, we suggest to use '--base' as documented in
> https://git-scm.com/docs/git-format-patch]
> 
> url:    https://github.com/0day-ci/linux/commits/Ming-Lei/block-avoid-to-call-blkg_free-in-atomic-context/20220323-001434
> base:   https://git.kernel.org/pub/scm/linux/kernel/git/axboe/linux-block.git for-next
> config: i386-randconfig-a005-20220321 (https://download.01.org/0day-ci/archive/20220323/202203230833.LMKQ6DdX-lkp@intel.com/config)
> compiler: clang version 15.0.0 (https://github.com/llvm/llvm-project 902f4708fe1d03b0de7e5315ef875006a6adc319)
> reproduce (this is a W=1 build):
>         wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
>         chmod +x ~/bin/make.cross
>         # https://github.com/0day-ci/linux/commit/c40ac630dd1d94497e427b4933efad4dbfaa0b5b
>         git remote add linux-review https://github.com/0day-ci/linux
>         git fetch --no-tags linux-review Ming-Lei/block-avoid-to-call-blkg_free-in-atomic-context/20220323-001434
>         git checkout c40ac630dd1d94497e427b4933efad4dbfaa0b5b
>         # save the config file to linux build tree
>         mkdir build_dir
>         COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=i386 SHELL=/bin/bash
> 
> If you fix the issue, kindly add following tag as appropriate
> Reported-by: kernel test robot <lkp@intel.com>
> 
> All warnings (new ones prefixed by >>):
> 
>    block/blk-cgroup.c:75: warning: Function parameter or member 'work' not described in 'blkg_free_workfn'
> >> block/blk-cgroup.c:75: warning: expecting prototype for blkg_free(). Prototype was for blkg_free_workfn() instead

Not understand what the 'prototype' for blkg_freee() is. If it is type of
blkg_free(), the patch doesn't change that. If it is document, it can
be one issue.

Anyway, I have tested clang build on v2, and the above warning can't be
observed any more.


Thanks,
Ming


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

end of thread, other threads:[~2022-03-23  1:05 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-03-22 16:12 [PATCH] block: avoid to call blkg_free() in atomic context Ming Lei
2022-03-22 16:42 ` Tejun Heo
2022-03-23  0:13 ` kernel test robot
2022-03-23  1:05   ` Ming Lei

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox