2023-01-17 06:55:44

by Yu Kuai

[permalink] [raw]
Subject: [PATCH v4 0/5] blk-iocost: random bugfix

From: Yu Kuai <[email protected]>

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


2023-01-17 06:56:39

by Yu Kuai

[permalink] [raw]
Subject: [PATCH v4 1/5] blk-iocost: check return value of match_u64()

From: Yu Kuai <[email protected]>

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

Signed-off-by: Yu Kuai <[email protected]>
Acked-by: Tejun Heo <[email protected]>
---
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

2023-01-17 06:59:51

by Yu Kuai

[permalink] [raw]
Subject: [PATCH v4 2/5] blk-iocost: don't allow to configure bio based device

From: Yu Kuai <[email protected]>

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 <[email protected]>
Reviewed-by: Christoph Hellwig <[email protected]>
Acked-by: Tejun Heo <[email protected]>
---
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

2023-01-17 06:59:57

by Yu Kuai

[permalink] [raw]
Subject: [PATCH v4 4/5] blk-iocost: fix divide by 0 error in calc_lcoefs()

From: Li Nan <[email protected]>

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 <[email protected]>
Signed-off-by: Yu Kuai <[email protected]>
---
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

2023-01-17 07:22:08

by Yu Kuai

[permalink] [raw]
Subject: [PATCH v4 3/5] blk-iocost: read params inside lock in sysfs apis

From: Yu Kuai <[email protected]>

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

Signed-off-by: Yu Kuai <[email protected]>
Acked-by: Tejun Heo <[email protected]>
---
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

2023-01-18 18:36:56

by Tejun Heo

[permalink] [raw]
Subject: Re: [PATCH v4 4/5] blk-iocost: fix divide by 0 error in calc_lcoefs()

On Tue, Jan 17, 2023 at 03:08:05PM +0800, Yu Kuai wrote:
> From: Li Nan <[email protected]>
>
> 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 <[email protected]>
> Signed-off-by: Yu Kuai <[email protected]>

Acked-by: Tejun Heo <[email protected]>

Thanks.

--
tejun

2023-01-20 16:14:19

by Jens Axboe

[permalink] [raw]
Subject: Re: [PATCH v4 0/5] blk-iocost: random bugfix


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