mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v4 0/5] blk-iocost: random bugfix
@ 2023-01-17  7:08 Yu Kuai
  2023-01-17  7:08 ` [PATCH v4 1/5] blk-iocost: check return value of match_u64() Yu Kuai
                   ` (5 more replies)
  0 siblings, 6 replies; 8+ messages in thread
From: Yu Kuai @ 2023-01-17  7:08 UTC (permalink / raw)
  To: tj, josef, axboe
  Cc: cgroups, linux-block, linux-kernel, yukuai3, yukuai1, yi.zhang,
	yangerkun

From: Yu Kuai <yukuai3@huawei.com>

changes in v4:
 - a litter code optimization to make it easier to understand in patch
 4.

changes in v3:
 - move some patches into separate patchset
 - don't return other error number for match_u64() in patch 1
 - instead of checking user input separately, set page directly if
 'bps + IOC_PAGE_SIZE' will overflow.

Li Nan (2):
  blk-iocost: fix divide by 0 error in calc_lcoefs()
  blk-iocost: change div64_u64 to DIV64_U64_ROUND_UP in
    ioc_refresh_params()

Yu Kuai (3):
  blk-iocost: check return value of match_u64()
  blk-iocost: don't allow to configure bio based device
  blk-iocost: read params inside lock in sysfs apis

 block/blk-iocost.c | 32 ++++++++++++++++++++++++++------
 1 file changed, 26 insertions(+), 6 deletions(-)

-- 
2.31.1


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

* [PATCH v4 1/5] blk-iocost: check return value of match_u64()
  2023-01-17  7:08 [PATCH v4 0/5] blk-iocost: random bugfix Yu Kuai
@ 2023-01-17  7:08 ` Yu Kuai
  2023-01-17  7:08 ` [PATCH v4 2/5] blk-iocost: don't allow to configure bio based device Yu Kuai
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 8+ messages in thread
From: Yu Kuai @ 2023-01-17  7:08 UTC (permalink / raw)
  To: tj, josef, axboe
  Cc: cgroups, linux-block, linux-kernel, yukuai3, yukuai1, yi.zhang,
	yangerkun

From: Yu Kuai <yukuai3@huawei.com>

This patch fixs that the return value of match_u64() from ioc_qos_write()
is not checked,

Signed-off-by: Yu Kuai <yukuai3@huawei.com>
Acked-by: Tejun Heo <tj@kernel.org>
---
 block/blk-iocost.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/block/blk-iocost.c b/block/blk-iocost.c
index 6955605629e4..f1166582fb64 100644
--- a/block/blk-iocost.c
+++ b/block/blk-iocost.c
@@ -3212,7 +3212,8 @@ static ssize_t ioc_qos_write(struct kernfs_open_file *of, char *input,
 
 		switch (match_token(p, qos_ctrl_tokens, args)) {
 		case QOS_ENABLE:
-			match_u64(&args[0], &v);
+			if (match_u64(&args[0], &v))
+				goto einval;
 			enable = v;
 			continue;
 		case QOS_CTRL:
-- 
2.31.1


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

* [PATCH v4 2/5] blk-iocost: don't allow to configure bio based device
  2023-01-17  7:08 [PATCH v4 0/5] blk-iocost: random bugfix Yu Kuai
  2023-01-17  7:08 ` [PATCH v4 1/5] blk-iocost: check return value of match_u64() Yu Kuai
@ 2023-01-17  7:08 ` Yu Kuai
  2023-01-17  7:08 ` [PATCH v4 3/5] blk-iocost: read params inside lock in sysfs apis Yu Kuai
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 8+ messages in thread
From: Yu Kuai @ 2023-01-17  7:08 UTC (permalink / raw)
  To: tj, josef, axboe
  Cc: cgroups, linux-block, linux-kernel, yukuai3, yukuai1, yi.zhang,
	yangerkun

From: Yu Kuai <yukuai3@huawei.com>

iocost is based on rq_qos, which can only work for request based device,
thus it doesn't make sense to configure iocost for bio based device.

Signed-off-by: Yu Kuai <yukuai3@huawei.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Acked-by: Tejun Heo <tj@kernel.org>
---
 block/blk-iocost.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/block/blk-iocost.c b/block/blk-iocost.c
index f1166582fb64..d6256731cae4 100644
--- a/block/blk-iocost.c
+++ b/block/blk-iocost.c
@@ -3185,6 +3185,11 @@ static ssize_t ioc_qos_write(struct kernfs_open_file *of, char *input,
 		return PTR_ERR(bdev);
 
 	disk = bdev->bd_disk;
+	if (!queue_is_mq(disk->queue)) {
+		ret = -EOPNOTSUPP;
+		goto err;
+	}
+
 	ioc = q_to_ioc(disk->queue);
 	if (!ioc) {
 		ret = blk_iocost_init(disk);
@@ -3365,6 +3370,11 @@ static ssize_t ioc_cost_model_write(struct kernfs_open_file *of, char *input,
 		return PTR_ERR(bdev);
 
 	q = bdev_get_queue(bdev);
+	if (!queue_is_mq(q)) {
+		ret = -EOPNOTSUPP;
+		goto err;
+	}
+
 	ioc = q_to_ioc(q);
 	if (!ioc) {
 		ret = blk_iocost_init(bdev->bd_disk);
-- 
2.31.1


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

* [PATCH v4 3/5] blk-iocost: read params inside lock in sysfs apis
  2023-01-17  7:08 [PATCH v4 0/5] blk-iocost: random bugfix Yu Kuai
  2023-01-17  7:08 ` [PATCH v4 1/5] blk-iocost: check return value of match_u64() Yu Kuai
  2023-01-17  7:08 ` [PATCH v4 2/5] blk-iocost: don't allow to configure bio based device Yu Kuai
@ 2023-01-17  7:08 ` Yu Kuai
  2023-01-17  7:08 ` [PATCH v4 4/5] blk-iocost: fix divide by 0 error in calc_lcoefs() Yu Kuai
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 8+ messages in thread
From: Yu Kuai @ 2023-01-17  7:08 UTC (permalink / raw)
  To: tj, josef, axboe
  Cc: cgroups, linux-block, linux-kernel, yukuai3, yukuai1, yi.zhang,
	yangerkun

From: Yu Kuai <yukuai3@huawei.com>

Otherwise, user might get abnormal values if params is updated
concurrently.

Signed-off-by: Yu Kuai <yukuai3@huawei.com>
Acked-by: Tejun Heo <tj@kernel.org>
---
 block/blk-iocost.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/block/blk-iocost.c b/block/blk-iocost.c
index d6256731cae4..d6f692d2283c 100644
--- a/block/blk-iocost.c
+++ b/block/blk-iocost.c
@@ -3129,6 +3129,7 @@ static u64 ioc_qos_prfill(struct seq_file *sf, struct blkg_policy_data *pd,
 	if (!dname)
 		return 0;
 
+	spin_lock_irq(&ioc->lock);
 	seq_printf(sf, "%s enable=%d ctrl=%s rpct=%u.%02u rlat=%u wpct=%u.%02u wlat=%u min=%u.%02u max=%u.%02u\n",
 		   dname, ioc->enabled, ioc->user_qos_params ? "user" : "auto",
 		   ioc->params.qos[QOS_RPPM] / 10000,
@@ -3141,6 +3142,7 @@ static u64 ioc_qos_prfill(struct seq_file *sf, struct blkg_policy_data *pd,
 		   ioc->params.qos[QOS_MIN] % 10000 / 100,
 		   ioc->params.qos[QOS_MAX] / 10000,
 		   ioc->params.qos[QOS_MAX] % 10000 / 100);
+	spin_unlock_irq(&ioc->lock);
 	return 0;
 }
 
@@ -3320,12 +3322,14 @@ static u64 ioc_cost_model_prfill(struct seq_file *sf,
 	if (!dname)
 		return 0;
 
+	spin_lock_irq(&ioc->lock);
 	seq_printf(sf, "%s ctrl=%s model=linear "
 		   "rbps=%llu rseqiops=%llu rrandiops=%llu "
 		   "wbps=%llu wseqiops=%llu wrandiops=%llu\n",
 		   dname, ioc->user_cost_model ? "user" : "auto",
 		   u[I_LCOEF_RBPS], u[I_LCOEF_RSEQIOPS], u[I_LCOEF_RRANDIOPS],
 		   u[I_LCOEF_WBPS], u[I_LCOEF_WSEQIOPS], u[I_LCOEF_WRANDIOPS]);
+	spin_unlock_irq(&ioc->lock);
 	return 0;
 }
 
-- 
2.31.1


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

* [PATCH v4 4/5] blk-iocost: fix divide by 0 error in calc_lcoefs()
  2023-01-17  7:08 [PATCH v4 0/5] blk-iocost: random bugfix Yu Kuai
                   ` (2 preceding siblings ...)
  2023-01-17  7:08 ` [PATCH v4 3/5] blk-iocost: read params inside lock in sysfs apis Yu Kuai
@ 2023-01-17  7:08 ` Yu Kuai
  2023-01-18 17:18   ` Tejun Heo
  2023-01-17  7:08 ` [PATCH v4 5/5] blk-iocost: change div64_u64 to DIV64_U64_ROUND_UP in ioc_refresh_params() Yu Kuai
  2023-01-20 15:40 ` [PATCH v4 0/5] blk-iocost: random bugfix Jens Axboe
  5 siblings, 1 reply; 8+ messages in thread
From: Yu Kuai @ 2023-01-17  7:08 UTC (permalink / raw)
  To: tj, josef, axboe
  Cc: cgroups, linux-block, linux-kernel, yukuai3, yukuai1, yi.zhang,
	yangerkun

From: Li Nan <linan122@huawei.com>

echo max of u64 to cost.model can cause divide by 0 error.

  # echo 8:0 rbps=18446744073709551615 > /sys/fs/cgroup/io.cost.model

  divide error: 0000 [#1] PREEMPT SMP
  RIP: 0010:calc_lcoefs+0x4c/0xc0
  Call Trace:
   <TASK>
   ioc_refresh_params+0x2b3/0x4f0
   ioc_cost_model_write+0x3cb/0x4c0
   ? _copy_from_iter+0x6d/0x6c0
   ? kernfs_fop_write_iter+0xfc/0x270
   cgroup_file_write+0xa0/0x200
   kernfs_fop_write_iter+0x17d/0x270
   vfs_write+0x414/0x620
   ksys_write+0x73/0x160
   __x64_sys_write+0x1e/0x30
   do_syscall_64+0x35/0x80
   entry_SYSCALL_64_after_hwframe+0x63/0xcd

calc_lcoefs() uses the input value of cost.model in DIV_ROUND_UP_ULL,
overflow would happen if bps plus IOC_PAGE_SIZE is greater than
ULLONG_MAX, it can cause divide by 0 error.

Fix the problem by setting basecost

Signed-off-by: Li Nan <linan122@huawei.com>
Signed-off-by: Yu Kuai <yukuai3@huawei.com>
---
 block/blk-iocost.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/block/blk-iocost.c b/block/blk-iocost.c
index d6f692d2283c..4cac0e7bb7cc 100644
--- a/block/blk-iocost.c
+++ b/block/blk-iocost.c
@@ -866,9 +866,14 @@ static void calc_lcoefs(u64 bps, u64 seqiops, u64 randiops,
 
 	*page = *seqio = *randio = 0;
 
-	if (bps)
-		*page = DIV64_U64_ROUND_UP(VTIME_PER_SEC,
-					   DIV_ROUND_UP_ULL(bps, IOC_PAGE_SIZE));
+	if (bps) {
+		u64 bps_pages = DIV_ROUND_UP_ULL(bps, IOC_PAGE_SIZE);
+
+		if (bps_pages)
+			*page = DIV64_U64_ROUND_UP(VTIME_PER_SEC, bps_pages);
+		else
+			*page = 1;
+	}
 
 	if (seqiops) {
 		v = DIV64_U64_ROUND_UP(VTIME_PER_SEC, seqiops);
-- 
2.31.1


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

* [PATCH v4 5/5] blk-iocost: change div64_u64 to DIV64_U64_ROUND_UP in ioc_refresh_params()
  2023-01-17  7:08 [PATCH v4 0/5] blk-iocost: random bugfix Yu Kuai
                   ` (3 preceding siblings ...)
  2023-01-17  7:08 ` [PATCH v4 4/5] blk-iocost: fix divide by 0 error in calc_lcoefs() Yu Kuai
@ 2023-01-17  7:08 ` Yu Kuai
  2023-01-20 15:40 ` [PATCH v4 0/5] blk-iocost: random bugfix Jens Axboe
  5 siblings, 0 replies; 8+ messages in thread
From: Yu Kuai @ 2023-01-17  7:08 UTC (permalink / raw)
  To: tj, josef, axboe
  Cc: cgroups, linux-block, linux-kernel, yukuai3, yukuai1, yi.zhang,
	yangerkun

From: Li Nan <linan122@huawei.com>

vrate_min is calculated by DIV64_U64_ROUND_UP, but vrate_max is calculated
by div64_u64. Vrate_min may be 1 greater than vrate_max if the input
values min and max of cost.qos are equal.

Signed-off-by: Li Nan <linan122@huawei.com>
Signed-off-by: Yu Kuai <yukuai3@huawei.com>
Acked-by: Tejun Heo <tj@kernel.org>
---
 block/blk-iocost.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/block/blk-iocost.c b/block/blk-iocost.c
index 4cac0e7bb7cc..1f23480a7a01 100644
--- a/block/blk-iocost.c
+++ b/block/blk-iocost.c
@@ -931,8 +931,8 @@ static bool ioc_refresh_params(struct ioc *ioc, bool force)
 
 	ioc->vrate_min = DIV64_U64_ROUND_UP((u64)ioc->params.qos[QOS_MIN] *
 					    VTIME_PER_USEC, MILLION);
-	ioc->vrate_max = div64_u64((u64)ioc->params.qos[QOS_MAX] *
-				   VTIME_PER_USEC, MILLION);
+	ioc->vrate_max = DIV64_U64_ROUND_UP((u64)ioc->params.qos[QOS_MAX] *
+					    VTIME_PER_USEC, MILLION);
 
 	return true;
 }
-- 
2.31.1


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

* Re: [PATCH v4 4/5] blk-iocost: fix divide by 0 error in calc_lcoefs()
  2023-01-17  7:08 ` [PATCH v4 4/5] blk-iocost: fix divide by 0 error in calc_lcoefs() Yu Kuai
@ 2023-01-18 17:18   ` Tejun Heo
  0 siblings, 0 replies; 8+ messages in thread
From: Tejun Heo @ 2023-01-18 17:18 UTC (permalink / raw)
  To: Yu Kuai
  Cc: josef, axboe, cgroups, linux-block, linux-kernel, yukuai3,
	yi.zhang, yangerkun

On Tue, Jan 17, 2023 at 03:08:05PM +0800, Yu Kuai wrote:
> From: Li Nan <linan122@huawei.com>
> 
> echo max of u64 to cost.model can cause divide by 0 error.
> 
>   # echo 8:0 rbps=18446744073709551615 > /sys/fs/cgroup/io.cost.model
> 
>   divide error: 0000 [#1] PREEMPT SMP
>   RIP: 0010:calc_lcoefs+0x4c/0xc0
>   Call Trace:
>    <TASK>
>    ioc_refresh_params+0x2b3/0x4f0
>    ioc_cost_model_write+0x3cb/0x4c0
>    ? _copy_from_iter+0x6d/0x6c0
>    ? kernfs_fop_write_iter+0xfc/0x270
>    cgroup_file_write+0xa0/0x200
>    kernfs_fop_write_iter+0x17d/0x270
>    vfs_write+0x414/0x620
>    ksys_write+0x73/0x160
>    __x64_sys_write+0x1e/0x30
>    do_syscall_64+0x35/0x80
>    entry_SYSCALL_64_after_hwframe+0x63/0xcd
> 
> calc_lcoefs() uses the input value of cost.model in DIV_ROUND_UP_ULL,
> overflow would happen if bps plus IOC_PAGE_SIZE is greater than
> ULLONG_MAX, it can cause divide by 0 error.
> 
> Fix the problem by setting basecost
> 
> Signed-off-by: Li Nan <linan122@huawei.com>
> Signed-off-by: Yu Kuai <yukuai3@huawei.com>

Acked-by: Tejun Heo <tj@kernel.org>

Thanks.

-- 
tejun

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

* Re: [PATCH v4 0/5] blk-iocost: random bugfix
  2023-01-17  7:08 [PATCH v4 0/5] blk-iocost: random bugfix Yu Kuai
                   ` (4 preceding siblings ...)
  2023-01-17  7:08 ` [PATCH v4 5/5] blk-iocost: change div64_u64 to DIV64_U64_ROUND_UP in ioc_refresh_params() Yu Kuai
@ 2023-01-20 15:40 ` Jens Axboe
  5 siblings, 0 replies; 8+ messages in thread
From: Jens Axboe @ 2023-01-20 15:40 UTC (permalink / raw)
  To: tj, josef, Yu Kuai
  Cc: cgroups, linux-block, linux-kernel, yukuai3, yi.zhang, yangerkun


On Tue, 17 Jan 2023 15:08:01 +0800, Yu Kuai wrote:
> changes in v4:
>  - a litter code optimization to make it easier to understand in patch
>  4.
> 
> changes in v3:
>  - move some patches into separate patchset
>  - don't return other error number for match_u64() in patch 1
>  - instead of checking user input separately, set page directly if
>  'bps + IOC_PAGE_SIZE' will overflow.
> 
> [...]

Applied, thanks!

[1/5] blk-iocost: check return value of match_u64()
      commit: 7b6a2c89052bfaf750260691dee29c2e457c0929
[2/5] blk-iocost: don't allow to configure bio based device
      commit: 204a9e1eeb4b72cd260274f14e521162b130a978
[3/5] blk-iocost: read params inside lock in sysfs apis
      commit: 7b810b50390b53c2a81051adce8fcbff3200fc74
[4/5] blk-iocost: fix divide by 0 error in calc_lcoefs()
      commit: 4e952a32301a77faea0d36c540ae16847cf2a8ee
[5/5] blk-iocost: change div64_u64 to DIV64_U64_ROUND_UP in ioc_refresh_params()
      commit: ad5572498be17b800e75bc36f7d810dd3b673802

Best regards,
-- 
Jens Axboe




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

end of thread, other threads:[~2023-01-20 15:41 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-17  7:08 [PATCH v4 0/5] blk-iocost: random bugfix Yu Kuai
2023-01-17  7:08 ` [PATCH v4 1/5] blk-iocost: check return value of match_u64() Yu Kuai
2023-01-17  7:08 ` [PATCH v4 2/5] blk-iocost: don't allow to configure bio based device Yu Kuai
2023-01-17  7:08 ` [PATCH v4 3/5] blk-iocost: read params inside lock in sysfs apis Yu Kuai
2023-01-17  7:08 ` [PATCH v4 4/5] blk-iocost: fix divide by 0 error in calc_lcoefs() Yu Kuai
2023-01-18 17:18   ` Tejun Heo
2023-01-17  7:08 ` [PATCH v4 5/5] blk-iocost: change div64_u64 to DIV64_U64_ROUND_UP in ioc_refresh_params() Yu Kuai
2023-01-20 15:40 ` [PATCH v4 0/5] blk-iocost: random bugfix Jens Axboe

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

all inboxes | Powered by JetHome®