blk-iocost: fix NULL iocg deref from racing against initialization
authorTejun Heo <tj@kernel.org>
Tue, 5 Jan 2021 17:37:23 +0000 (12:37 -0500)
committerJens Axboe <axboe@kernel.dk>
Tue, 5 Jan 2021 18:33:32 +0000 (11:33 -0700)
commitd16baa3f1453c14d680c5fee01cd122a22d0e0ce
tree3aa54c0344d3efa92d717dd609b500e2d992c950
parent19cd3403cb0d522dd5e10188eef85817de29e26e
blk-iocost: fix NULL iocg deref from racing against initialization

When initializing iocost for a queue, its rqos should be registered before
the blkcg policy is activated to allow policy data initiailization to lookup
the associated ioc. This unfortunately means that the rqos methods can be
called on bios before iocgs are attached to all existing blkgs.

While the race is theoretically possible on ioc_rqos_throttle(), it mostly
happened in ioc_rqos_merge() due to the difference in how they lookup ioc.
The former determines it from the passed in @rqos and then bails before
dereferencing iocg if the looked up ioc is disabled, which most likely is
the case if initialization is still in progress. The latter looked up ioc by
dereferencing the possibly NULL iocg making it a lot more prone to actually
triggering the bug.

* Make ioc_rqos_merge() use the same method as ioc_rqos_throttle() to look
  up ioc for consistency.

* Make ioc_rqos_throttle() and ioc_rqos_merge() test for NULL iocg before
  dereferencing it.

* Explain the danger of NULL iocgs in blk_iocost_init().

Signed-off-by: Tejun Heo <tj@kernel.org>
Reported-by: Jonathan Lemon <bsd@fb.com>
Cc: stable@vger.kernel.org # v5.4+
Signed-off-by: Jens Axboe <axboe@kernel.dk>
block/blk-iocost.c