md/raid10: fix the condition to call bio_end_io_acct()
authorLi Nan <linan122@huawei.com>
Fri, 9 Jun 2023 09:43:20 +0000 (17:43 +0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 19 Jul 2023 14:21:59 +0000 (16:21 +0200)
commit3781d0e6c11b5905f5c278d84018d7bb08af7170
treebfae01155728b182f87a79b17fdd5b3ea0a1bbb9
parent07e81c9208d9a3d51402ed981427d805c324a2a6
md/raid10: fix the condition to call bio_end_io_acct()

[ Upstream commit 125bfc7cd750e68c99f1d446e2c22abea08c237f ]

/sys/block/[device]/queue/iostats is used to control whether to count io
stat. Write 0 to it will clear queue_flags QUEUE_FLAG_IO_STAT which means
iostats is disabled. If we disable iostats and later endable it, the io
issued during this period will be counted incorrectly, inflight will be
decreased to -1.

  //T1 set iostats
  echo 0 > /sys/block/md0/queue/iostats
   clear QUEUE_FLAG_IO_STAT

//T2 issue io
if (QUEUE_FLAG_IO_STAT) -> false
 bio_start_io_acct
  inflight++

  echo 1 > /sys/block/md0/queue/iostats
   set QUEUE_FLAG_IO_STAT

//T3 io end
if (QUEUE_FLAG_IO_STAT) -> true
 bio_end_io_acct
  inflight-- -> -1

Also, if iostats is enabled while issuing io but disabled while io end,
inflight will never be decreased.

Fix it by checking start_time when io end. If start_time is not 0, call
bio_end_io_acct().

Fixes: 528bc2cf2fcc ("md/raid10: enable io accounting")
Signed-off-by: Li Nan <linan122@huawei.com>
Signed-off-by: Song Liu <song@kernel.org>
Link: https://lore.kernel.org/r/20230609094320.2397604-1-linan666@huaweicloud.com
Signed-off-by: Sasha Levin <sashal@kernel.org>
drivers/md/raid10.c