All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3 0/5] blk-iocost: random bugfix
@ 2022-12-26  8:58 Yu Kuai
  2022-12-26  8:58   ` Yu Kuai
                   ` (4 more replies)
  0 siblings, 5 replies; 16+ messages in thread
From: Yu Kuai @ 2022-12-26  8:58 UTC (permalink / raw)
  To: tj, josef, axboe
  Cc: cgroups, linux-block, linux-kernel, yukuai3, yukuai1, yi.zhang

From: Yu Kuai <yukuai3@huawei.com>

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 | 31 +++++++++++++++++++++++++------
 1 file changed, 25 insertions(+), 6 deletions(-)

-- 
2.31.1


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

* [PATCH v3 1/5] blk-iocost: check return value of match_u64()
@ 2022-12-26  8:58   ` Yu Kuai
  0 siblings, 0 replies; 16+ messages in thread
From: Yu Kuai @ 2022-12-26  8:58 UTC (permalink / raw)
  To: tj, josef, axboe
  Cc: cgroups, linux-block, linux-kernel, yukuai3, yukuai1, yi.zhang

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>
---
 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 549ddc9e0c6f..93b938517be3 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 related	[flat|nested] 16+ messages in thread

* [PATCH v3 1/5] blk-iocost: check return value of match_u64()
@ 2022-12-26  8:58   ` Yu Kuai
  0 siblings, 0 replies; 16+ messages in thread
From: Yu Kuai @ 2022-12-26  8:58 UTC (permalink / raw)
  To: tj-DgEjT+Ai2ygdnm+yROfE0A, josef-DigfWCa+lFGyeJad7bwFQA,
	axboe-tSWWG44O7X1aa/9Udqfwiw
  Cc: cgroups-u79uwXL29TY76Z2rM5mHXA,
	linux-block-u79uwXL29TY76Z2rM5mHXA,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	yukuai3-hv44wF8Li93QT0dZR+AlfA, yukuai1-XF6JlduFytWkHkcT6e4Xnw,
	yi.zhang-hv44wF8Li93QT0dZR+AlfA

From: Yu Kuai <yukuai3-hv44wF8Li93QT0dZR+AlfA@public.gmane.org>

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

Signed-off-by: Yu Kuai <yukuai3-hv44wF8Li93QT0dZR+AlfA@public.gmane.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 549ddc9e0c6f..93b938517be3 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 related	[flat|nested] 16+ messages in thread

* [PATCH v3 2/5] blk-iocost: don't allow to configure bio based device
  2022-12-26  8:58 [PATCH v3 0/5] blk-iocost: random bugfix Yu Kuai
  2022-12-26  8:58   ` Yu Kuai
@ 2022-12-26  8:58 ` Yu Kuai
  2022-12-26  8:58   ` Yu Kuai
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 16+ messages in thread
From: Yu Kuai @ 2022-12-26  8:58 UTC (permalink / raw)
  To: tj, josef, axboe
  Cc: cgroups, linux-block, linux-kernel, yukuai3, yukuai1, yi.zhang

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 93b938517be3..ce4eafbada56 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 related	[flat|nested] 16+ messages in thread

* [PATCH v3 3/5] blk-iocost: read params inside lock in sysfs apis
@ 2022-12-26  8:58   ` Yu Kuai
  0 siblings, 0 replies; 16+ messages in thread
From: Yu Kuai @ 2022-12-26  8:58 UTC (permalink / raw)
  To: tj, josef, axboe
  Cc: cgroups, linux-block, linux-kernel, yukuai3, yukuai1, yi.zhang

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 ce4eafbada56..f8726e20da20 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 related	[flat|nested] 16+ messages in thread

* [PATCH v3 3/5] blk-iocost: read params inside lock in sysfs apis
@ 2022-12-26  8:58   ` Yu Kuai
  0 siblings, 0 replies; 16+ messages in thread
From: Yu Kuai @ 2022-12-26  8:58 UTC (permalink / raw)
  To: tj-DgEjT+Ai2ygdnm+yROfE0A, josef-DigfWCa+lFGyeJad7bwFQA,
	axboe-tSWWG44O7X1aa/9Udqfwiw
  Cc: cgroups-u79uwXL29TY76Z2rM5mHXA,
	linux-block-u79uwXL29TY76Z2rM5mHXA,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	yukuai3-hv44wF8Li93QT0dZR+AlfA, yukuai1-XF6JlduFytWkHkcT6e4Xnw,
	yi.zhang-hv44wF8Li93QT0dZR+AlfA

From: Yu Kuai <yukuai3-hv44wF8Li93QT0dZR+AlfA@public.gmane.org>

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

Signed-off-by: Yu Kuai <yukuai3-hv44wF8Li93QT0dZR+AlfA@public.gmane.org>
Acked-by: Tejun Heo <tj-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
---
 block/blk-iocost.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/block/blk-iocost.c b/block/blk-iocost.c
index ce4eafbada56..f8726e20da20 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 related	[flat|nested] 16+ messages in thread

* [PATCH v3 4/5] blk-iocost: fix divide by 0 error in calc_lcoefs()
@ 2022-12-26  8:58   ` Yu Kuai
  0 siblings, 0 replies; 16+ messages in thread
From: Yu Kuai @ 2022-12-26  8:58 UTC (permalink / raw)
  To: tj, josef, axboe
  Cc: cgroups, linux-block, linux-kernel, yukuai3, yukuai1, yi.zhang

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 | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/block/blk-iocost.c b/block/blk-iocost.c
index f8726e20da20..c6b39024117b 100644
--- a/block/blk-iocost.c
+++ b/block/blk-iocost.c
@@ -866,9 +866,13 @@ 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) {
+		if (bps >= U64_MAX - IOC_PAGE_SIZE)
+			*page = 1;
+		else
+			*page = DIV64_U64_ROUND_UP(VTIME_PER_SEC,
+					DIV_ROUND_UP_ULL(bps, IOC_PAGE_SIZE));
+	}
 
 	if (seqiops) {
 		v = DIV64_U64_ROUND_UP(VTIME_PER_SEC, seqiops);
-- 
2.31.1


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

* [PATCH v3 4/5] blk-iocost: fix divide by 0 error in calc_lcoefs()
@ 2022-12-26  8:58   ` Yu Kuai
  0 siblings, 0 replies; 16+ messages in thread
From: Yu Kuai @ 2022-12-26  8:58 UTC (permalink / raw)
  To: tj-DgEjT+Ai2ygdnm+yROfE0A, josef-DigfWCa+lFGyeJad7bwFQA,
	axboe-tSWWG44O7X1aa/9Udqfwiw
  Cc: cgroups-u79uwXL29TY76Z2rM5mHXA,
	linux-block-u79uwXL29TY76Z2rM5mHXA,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	yukuai3-hv44wF8Li93QT0dZR+AlfA, yukuai1-XF6JlduFytWkHkcT6e4Xnw,
	yi.zhang-hv44wF8Li93QT0dZR+AlfA

From: Li Nan <linan122-hv44wF8Li93QT0dZR+AlfA@public.gmane.org>

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-hv44wF8Li93QT0dZR+AlfA@public.gmane.org>
Signed-off-by: Yu Kuai <yukuai3-hv44wF8Li93QT0dZR+AlfA@public.gmane.org>
---
 block/blk-iocost.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/block/blk-iocost.c b/block/blk-iocost.c
index f8726e20da20..c6b39024117b 100644
--- a/block/blk-iocost.c
+++ b/block/blk-iocost.c
@@ -866,9 +866,13 @@ 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) {
+		if (bps >= U64_MAX - IOC_PAGE_SIZE)
+			*page = 1;
+		else
+			*page = DIV64_U64_ROUND_UP(VTIME_PER_SEC,
+					DIV_ROUND_UP_ULL(bps, IOC_PAGE_SIZE));
+	}
 
 	if (seqiops) {
 		v = DIV64_U64_ROUND_UP(VTIME_PER_SEC, seqiops);
-- 
2.31.1


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

* [PATCH v3 5/5] blk-iocost: change div64_u64 to DIV64_U64_ROUND_UP in ioc_refresh_params()
  2022-12-26  8:58 [PATCH v3 0/5] blk-iocost: random bugfix Yu Kuai
                   ` (3 preceding siblings ...)
  2022-12-26  8:58   ` Yu Kuai
@ 2022-12-26  8:58 ` Yu Kuai
  2023-01-04 21:48     ` Tejun Heo
  4 siblings, 1 reply; 16+ messages in thread
From: Yu Kuai @ 2022-12-26  8:58 UTC (permalink / raw)
  To: tj, josef, axboe
  Cc: cgroups, linux-block, linux-kernel, yukuai3, yukuai1, yi.zhang

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>
---
 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 c6b39024117b..7a0d754b9eb2 100644
--- a/block/blk-iocost.c
+++ b/block/blk-iocost.c
@@ -930,8 +930,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 related	[flat|nested] 16+ messages in thread

* Re: [PATCH v3 1/5] blk-iocost: check return value of match_u64()
@ 2023-01-04 21:48     ` Tejun Heo
  0 siblings, 0 replies; 16+ messages in thread
From: Tejun Heo @ 2023-01-04 21:48 UTC (permalink / raw)
  To: Yu Kuai
  Cc: josef, axboe, cgroups, linux-block, linux-kernel, yukuai3, yi.zhang

On Mon, Dec 26, 2022 at 04:58:55PM +0800, Yu Kuai wrote:
> 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>

Thanks.

-- 
tejun

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

* Re: [PATCH v3 1/5] blk-iocost: check return value of match_u64()
@ 2023-01-04 21:48     ` Tejun Heo
  0 siblings, 0 replies; 16+ messages in thread
From: Tejun Heo @ 2023-01-04 21:48 UTC (permalink / raw)
  To: Yu Kuai
  Cc: josef-DigfWCa+lFGyeJad7bwFQA, axboe-tSWWG44O7X1aa/9Udqfwiw,
	cgroups-u79uwXL29TY76Z2rM5mHXA,
	linux-block-u79uwXL29TY76Z2rM5mHXA,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	yukuai3-hv44wF8Li93QT0dZR+AlfA, yi.zhang-hv44wF8Li93QT0dZR+AlfA

On Mon, Dec 26, 2022 at 04:58:55PM +0800, Yu Kuai wrote:
> From: Yu Kuai <yukuai3-hv44wF8Li93QT0dZR+AlfA@public.gmane.org>
> 
> This patch fixs that the return value of match_u64() from ioc_qos_write()
> is not checked,
> 
> Signed-off-by: Yu Kuai <yukuai3-hv44wF8Li93QT0dZR+AlfA@public.gmane.org>

Acked-by: Tejun Heo <tj-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>

Thanks.

-- 
tejun

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

* Re: [PATCH v3 5/5] blk-iocost: change div64_u64 to DIV64_U64_ROUND_UP in ioc_refresh_params()
@ 2023-01-04 21:48     ` Tejun Heo
  0 siblings, 0 replies; 16+ messages in thread
From: Tejun Heo @ 2023-01-04 21:48 UTC (permalink / raw)
  To: Yu Kuai
  Cc: josef, axboe, cgroups, linux-block, linux-kernel, yukuai3, yi.zhang

On Mon, Dec 26, 2022 at 04:58:59PM +0800, Yu Kuai wrote:
> 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>

Thanks.

-- 
tejun

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

* Re: [PATCH v3 5/5] blk-iocost: change div64_u64 to DIV64_U64_ROUND_UP in ioc_refresh_params()
@ 2023-01-04 21:48     ` Tejun Heo
  0 siblings, 0 replies; 16+ messages in thread
From: Tejun Heo @ 2023-01-04 21:48 UTC (permalink / raw)
  To: Yu Kuai
  Cc: josef-DigfWCa+lFGyeJad7bwFQA, axboe-tSWWG44O7X1aa/9Udqfwiw,
	cgroups-u79uwXL29TY76Z2rM5mHXA,
	linux-block-u79uwXL29TY76Z2rM5mHXA,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	yukuai3-hv44wF8Li93QT0dZR+AlfA, yi.zhang-hv44wF8Li93QT0dZR+AlfA

On Mon, Dec 26, 2022 at 04:58:59PM +0800, Yu Kuai wrote:
> From: Li Nan <linan122-hv44wF8Li93QT0dZR+AlfA@public.gmane.org>
> 
> 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-hv44wF8Li93QT0dZR+AlfA@public.gmane.org>
> Signed-off-by: Yu Kuai <yukuai3-hv44wF8Li93QT0dZR+AlfA@public.gmane.org>

Acked-by: Tejun Heo <tj-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>

Thanks.

-- 
tejun

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

* Re: [PATCH v3 4/5] blk-iocost: fix divide by 0 error in calc_lcoefs()
  2022-12-26  8:58   ` Yu Kuai
  (?)
@ 2023-01-04 21:54   ` Tejun Heo
  2023-01-05  1:53       ` Yu Kuai
  -1 siblings, 1 reply; 16+ messages in thread
From: Tejun Heo @ 2023-01-04 21:54 UTC (permalink / raw)
  To: Yu Kuai
  Cc: josef, axboe, cgroups, linux-block, linux-kernel, yukuai3, yi.zhang

On Mon, Dec 26, 2022 at 04:58:58PM +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>
> ---
>  block/blk-iocost.c | 10 +++++++---
>  1 file changed, 7 insertions(+), 3 deletions(-)
> 
> diff --git a/block/blk-iocost.c b/block/blk-iocost.c
> index f8726e20da20..c6b39024117b 100644
> --- a/block/blk-iocost.c
> +++ b/block/blk-iocost.c
> @@ -866,9 +866,13 @@ 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) {
> +		if (bps >= U64_MAX - IOC_PAGE_SIZE)
> +			*page = 1;
> +		else
> +			*page = DIV64_U64_ROUND_UP(VTIME_PER_SEC,
> +					DIV_ROUND_UP_ULL(bps, IOC_PAGE_SIZE));
> +	}

This is a nitpick but wouldn't something like the following be easier to
understand?

        if (bps) {
                u64 bps_pages = DIV_ROUND_UP_ULL(bps, IOC_PAGE_SIZE);

                if (bps_pages)
                        *pages = DIV64_U64_ROUND_UP(VTIME_PER_SEC, bps_pages);
                else
                        *pages = 1;
        }

Out of scope but this seems more like a bug in the DIV macros. The fact that
it returns 0 is an implementation artifact more than anything and a
surprising one at that as it ends up returning 0 for an input that a regular
division would handle just fine and the rounded up result fits well within
the result type.

Thanks.

-- 
tejun

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

* Re: [PATCH v3 4/5] blk-iocost: fix divide by 0 error in calc_lcoefs()
  2023-01-04 21:54   ` Tejun Heo
@ 2023-01-05  1:53       ` Yu Kuai
  0 siblings, 0 replies; 16+ messages in thread
From: Yu Kuai @ 2023-01-05  1:53 UTC (permalink / raw)
  To: Tejun Heo, Yu Kuai
  Cc: josef, axboe, cgroups, linux-block, linux-kernel, yi.zhang, yukuai (C)

Hi,

在 2023/01/05 5:54, Tejun Heo 写道:
> On Mon, Dec 26, 2022 at 04:58:58PM +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>
>> ---
>>   block/blk-iocost.c | 10 +++++++---
>>   1 file changed, 7 insertions(+), 3 deletions(-)
>>
>> diff --git a/block/blk-iocost.c b/block/blk-iocost.c
>> index f8726e20da20..c6b39024117b 100644
>> --- a/block/blk-iocost.c
>> +++ b/block/blk-iocost.c
>> @@ -866,9 +866,13 @@ 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) {
>> +		if (bps >= U64_MAX - IOC_PAGE_SIZE)
>> +			*page = 1;
>> +		else
>> +			*page = DIV64_U64_ROUND_UP(VTIME_PER_SEC,
>> +					DIV_ROUND_UP_ULL(bps, IOC_PAGE_SIZE));
>> +	}
> 
> This is a nitpick but wouldn't something like the following be easier to
> understand?
> 
>          if (bps) {
>                  u64 bps_pages = DIV_ROUND_UP_ULL(bps, IOC_PAGE_SIZE);
> 
>                  if (bps_pages)
>                          *pages = DIV64_U64_ROUND_UP(VTIME_PER_SEC, bps_pages);
>                  else
>                          *pages = 1;
>          }
> 
Yes, I agree that this is better to understand. I'll send a new version.

Thanks,
Kuai


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

* Re: [PATCH v3 4/5] blk-iocost: fix divide by 0 error in calc_lcoefs()
@ 2023-01-05  1:53       ` Yu Kuai
  0 siblings, 0 replies; 16+ messages in thread
From: Yu Kuai @ 2023-01-05  1:53 UTC (permalink / raw)
  To: Tejun Heo, Yu Kuai
  Cc: josef, axboe, cgroups, linux-block, linux-kernel, yi.zhang, yukuai (C)

Hi,

ÔÚ 2023/01/05 5:54, Tejun Heo дµÀ:
> On Mon, Dec 26, 2022 at 04:58:58PM +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>
>> ---
>>   block/blk-iocost.c | 10 +++++++---
>>   1 file changed, 7 insertions(+), 3 deletions(-)
>>
>> diff --git a/block/blk-iocost.c b/block/blk-iocost.c
>> index f8726e20da20..c6b39024117b 100644
>> --- a/block/blk-iocost.c
>> +++ b/block/blk-iocost.c
>> @@ -866,9 +866,13 @@ 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) {
>> +		if (bps >= U64_MAX - IOC_PAGE_SIZE)
>> +			*page = 1;
>> +		else
>> +			*page = DIV64_U64_ROUND_UP(VTIME_PER_SEC,
>> +					DIV_ROUND_UP_ULL(bps, IOC_PAGE_SIZE));
>> +	}
> 
> This is a nitpick but wouldn't something like the following be easier to
> understand?
> 
>          if (bps) {
>                  u64 bps_pages = DIV_ROUND_UP_ULL(bps, IOC_PAGE_SIZE);
> 
>                  if (bps_pages)
>                          *pages = DIV64_U64_ROUND_UP(VTIME_PER_SEC, bps_pages);
>                  else
>                          *pages = 1;
>          }
> 
Yes, I agree that this is better to understand. I'll send a new version.

Thanks,
Kuai


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

end of thread, other threads:[~2023-01-05  1:53 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-26  8:58 [PATCH v3 0/5] blk-iocost: random bugfix Yu Kuai
2022-12-26  8:58 ` [PATCH v3 1/5] blk-iocost: check return value of match_u64() Yu Kuai
2022-12-26  8:58   ` Yu Kuai
2023-01-04 21:48   ` Tejun Heo
2023-01-04 21:48     ` Tejun Heo
2022-12-26  8:58 ` [PATCH v3 2/5] blk-iocost: don't allow to configure bio based device Yu Kuai
2022-12-26  8:58 ` [PATCH v3 3/5] blk-iocost: read params inside lock in sysfs apis Yu Kuai
2022-12-26  8:58   ` Yu Kuai
2022-12-26  8:58 ` [PATCH v3 4/5] blk-iocost: fix divide by 0 error in calc_lcoefs() Yu Kuai
2022-12-26  8:58   ` Yu Kuai
2023-01-04 21:54   ` Tejun Heo
2023-01-05  1:53     ` Yu Kuai
2023-01-05  1:53       ` Yu Kuai
2022-12-26  8:58 ` [PATCH v3 5/5] blk-iocost: change div64_u64 to DIV64_U64_ROUND_UP in ioc_refresh_params() Yu Kuai
2023-01-04 21:48   ` Tejun Heo
2023-01-04 21:48     ` Tejun Heo

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.