Skip to contentSkip to conversation

blk-mq: fix nr_requests wrong value when modify it from sysfs

PATCH Superseded weiping zhang 5 participants 9 messages Sep 3, 2017 – Sep 21, 2017 Block layer

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 blk-mq: fix nr_requests wrong value when modify it from sysfs

if blk-mq use "none" io scheduler, nr_request get a wrong value when input a number > tag_set->queue_depth. blk_mq_tag_update_depth will get the smaller one min(nr, set->queue_depth), and then q->nr_request get a wrong value.

Reproduce:

echo none > /sys/block/nvme0n1/queue/ioscheduler echo 1000000 > /sys/block/nvme0n1/queue/nr_requests cat /sys/block/nvme0n1/queue/nr_requests 1000000

---
 block/blk-mq.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
block/blk-mq.c +5 -2
index f84d145..8303e5e 100644
@@ -2622,8 +2622,11 @@ int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr)
 		 * queue depth. This is similar to what the old code would do.
 		 */
 		if (!hctx->sched_tags) {
-			ret = blk_mq_tag_update_depth(hctx, &hctx->tags,
-							min(nr, set->queue_depth),
+			if (nr > set->queue_depth) {
+				nr = set->queue_depth;
+				pr_warn("reduce nr_request to %u\n", nr);
+			}
+			ret = blk_mq_tag_update_depth(hctx, &hctx->tags, nr,
 							false);
 		} else {
 			ret = blk_mq_tag_update_depth(hctx, &hctx->sched_tags,
Signature · 1 line
2.9.4
Reviewed-by on v1 1/1
#1 lore raw
Quoted 25 lines · weiping zhang

if blk-mq use "none" io scheduler, nr_request get a wrong value when input a number > tag_set->queue_depth. blk_mq_tag_update_depth will get the smaller one min(nr, set->queue_depth), and then q->nr_request get a wrong value.

Reproduce:

echo none > /sys/block/nvme0n1/queue/ioscheduler echo 1000000 > /sys/block/nvme0n1/queue/nr_requests cat /sys/block/nvme0n1/queue/nr_requests 1000000

---
 block/blk-mq.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
block/blk-mq.c +0 -0
index f84d145..8303e5e 100644
@@ -2622,8 +2622,11 @@ int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr)
                 * queue depth. This is similar to what the old code would do.
                 */
                if (!hctx->sched_tags) {

block/blk-mq.c int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr) · line 2632

Quoted 10 lines
block/blk-mq.c +5 -2
-                       ret = blk_mq_tag_update_depth(hctx, &hctx->tags,
-                                                       min(nr, set->queue_depth),
+                       if (nr > set->queue_depth) {
+                               nr = set->queue_depth;
+                               pr_warn("reduce nr_request to %u\n", nr);
+                       }
+                       ret = blk_mq_tag_update_depth(hctx, &hctx->tags, nr,
                                                        false);
                } else {
                        ret = blk_mq_tag_update_depth(hctx, &hctx->sched_tags,

Not sure if the pr_warn() is useful, but the idea is correct:

Signature · 1 line
Ming Lei
#2 lore raw
In reply to weiping zhang

T24gU3VuLCAyMDE3LTA5LTAzIGF0IDIxOjQ2ICswODAwLCB3ZWlwaW5nIHpoYW5nIHdyb3RlOg0K PiBpZiBibGstbXEgdXNlICJub25lIiBpbyBzY2hlZHVsZXIsIG5yX3JlcXVlc3QgZ2V0IGEgd3Jv bmcgdmFsdWUgd2hlbg0KPiBpbnB1dCBhIG51bWJlciA+IHRhZ19zZXQtPnF1ZXVlX2RlcHRoLiBi bGtfbXFfdGFnX3VwZGF0ZV9kZXB0aCB3aWxsIGdldA0KPiB0aGUgc21hbGxlciBvbmUgbWluKG5y LCBzZXQtPnF1ZXVlX2RlcHRoKSwgYW5kIHRoZW4gcS0+bnJfcmVxdWVzdCBnZXQgYQ0KPiB3cm9u ZyB2YWx1ZS4NCj4gDQo+IFJlcHJvZHVjZToNCj4gDQo+IGVjaG8gbm9uZSA+IC9zeXMvYmxvY2sv bnZtZTBuMS9xdWV1ZS9pb3NjaGVkdWxlcg0KPiBlY2hvIDEwMDAwMDAgPiAvc3lzL2Jsb2NrL252 bWUwbjEvcXVldWUvbnJfcmVxdWVzdHMNCj4gY2F0IC9zeXMvYmxvY2svbnZtZTBuMS9xdWV1ZS9u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GN0eCwgJmhjdHgtPnRhZ3MsIG5y LA0KPiAgCQkJCQkJCWZhbHNlKTsNCj4gIAkJfSBlbHNlIHsNCj4gIAkJCXJldCA9IGJsa19tcV90 YWdfdXBkYXRlX2RlcHRoKGhjdHgsICZoY3R4LT5zY2hlZF90YWdzLA0KDQpTaG91bGRuJ3QgdGhp cyBjb2RlIHJldHVybiAtRUlOVkFMIG9yIC1FUkFOR0UgaWYgJ25yJyBpcyB0b28gbGFyZ2U/IFRo YXQgd2lsbCBoZWxwIHRvDQprZWVwIHVzZXIgc3BhY2UgY29kZSBzaW1wbGUgdGhhdCB1cGRhdGVz IHRoZSBxdWV1ZSBkZXB0aC4NCg0KVGhhbmtzLA0KDQpCYXJ0Lg==

#3 lore raw
Quoted 31 lines · Bart Van Assche
Quoted 30 lines · weiping zhang

if blk-mq use "none" io scheduler, nr_request get a wrong value when input a number > tag_set->queue_depth. blk_mq_tag_update_depth will get the smaller one min(nr, set->queue_depth), and then q->nr_request get a wrong value.

Reproduce:

echo none > /sys/block/nvme0n1/queue/ioscheduler echo 1000000 > /sys/block/nvme0n1/queue/nr_requests cat /sys/block/nvme0n1/queue/nr_requests 1000000

---
 block/blk-mq.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
block/blk-mq.c +3 -2
index f84d145..8303e5e 100644
@@ -2622,8 +2622,11 @@ int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr)
 		 * queue depth. This is similar to what the old code would do.
 		 */
 		if (!hctx->sched_tags) {
-			ret = blk_mq_tag_update_depth(hctx, &hctx->tags,
-							min(nr, set->queue_depth),
+			if (nr > set->queue_depth) {
+				nr = set->queue_depth;
+				pr_warn("reduce nr_request to %u\n", nr);
Quoted 8 lines

block/blk-mq.c int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr) · line 2632

Quoted 5 lines
block/blk-mq.c +2 -0
+			}
+			ret = blk_mq_tag_update_depth(hctx, &hctx->tags, nr,
 							false);
 		} else {
 			ret = blk_mq_tag_update_depth(hctx, &hctx->sched_tags,

Shouldn't this code return -EINVAL or -ERANGE if 'nr' is too large? That will help to keep user space code simple that updates the queue depth.

Hi Bart,

The reason why not return -EINVAL is keeping alin with minimum checking in queue_requests_store, if you insist return -EINVAL/-ERANGE, minimum checking should also keep
same behavior. Both return error meesage and quietly changing are okey
for me. Which way do you prefer ?

static ssize_t
queue_requests_store(struct request_queue *q, const char *page, size_t count)

{
	unsigned long nr;
	int ret, err;

	if (!q->request_fn && !q->mq_ops)
		return -EINVAL;

	ret = queue_var_store(&nr, page, count);
	if (ret < 0)
		return ret;

	if (nr < BLKDEV_MIN_RQ)
		nr = BLKDEV_MIN_RQ;

Thanks

#4 lore raw

T24gV2VkLCAyMDE3LTA5LTA2IGF0IDE1OjM0ICswODAwLCB3ZWlwaW5nIHpoYW5nIHdyb3RlOg0K PiBPbiBUdWUsIFNlcCAwNSwgMjAxNyBhdCAwMzo0Mjo0NVBNICswMDAwLCBCYXJ0IFZhbiBBc3Nj aGUgd3JvdGU6DQo+ID4gT24gU3VuLCAyMDE3LTA5LTAzIGF0IDIxOjQ2ICswODAwLCB3ZWlwaW5n IHpoYW5nIHdyb3RlOg0KPiA+ID4gaWYgYmxrLW1xIHVzZSAibm9uZSIgaW8gc2NoZWR1bGVyLCBu cl9yZXF1ZXN0IGdldCBhIHdyb25nIHZhbHVlIHdoZW4NCj4gPiA+IGlucHV0IGEgbnVtYmVyID4g dGFnX3NldC0+cXVldWVfZGVwdGguIGJsa19tcV90YWdfdXBkYXRlX2RlcHRoIHdpbGwgZ2V0DQo+ ID4gPiB0aGUgc21hbGxlciBvbmUgbWluKG5yLCBzZXQtPnF1ZXVlX2RlcHRoKSwgYW5kIHRoZW4g cS0+bnJfcmVxdWVzdCBnZXQgYQ0KPiA+ID4gd3JvbmcgdmFsdWUuDQo+ID4gPiANCj4gPiA+IFJl cHJvZHVjZToNCj4gPiA+IA0KPiA+ID4gZWNobyBub25lID4gL3N5cy9ibG9jay9udm1lMG4xL3F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j4gc3RhdGljIHNzaXplX3QN Cj4gcXVldWVfcmVxdWVzdHNfc3RvcmUoc3RydWN0IHJlcXVlc3RfcXVldWUgKnEsIGNvbnN0IGNo YXIgKnBhZ2UsIHNpemVfdCBjb3VudCkNCj4gew0KPiAJdW5zaWduZWQgbG9uZyBucjsNCj4gCWlu dCByZXQsIGVycjsNCj4gDQo+IAlpZiAoIXEtPnJlcXVlc3RfZm4gJiYgIXEtPm1xX29wcykNCj4g CQlyZXR1cm4gLUVJTlZBTDsNCj4gDQo+IAlyZXQgPSBxdWV1ZV92YXJfc3RvcmUoJm5yLCBwYWdl LCBjb3VudCk7DQo+IAlpZiAocmV0IDwgMCkNCj4gCQlyZXR1cm4gcmV0Ow0KPiANCj4gCWlmIChu ciA8IEJMS0RFVl9NSU5fUlEpDQo+IAkJbnIgPSBCTEtERVZfTUlOX1JROw0KDQpIZWxsbyBKZW5z LA0KDQpEbyB5b3UgcGVyaGFwcyBoYXZlIGEgcHJlZmVyZW5jZSBmb3Igb25lIG9mIHRoZSBhcHBy b2FjaGVzIHRoYXQgaGF2ZSBiZWVuIGRpc2N1c3NlZA0KaW4gdGhpcyBlLW1haWwgdGhyZWFkPw0K DQpUaGFua3MsDQoNCkJhcnQu

#5 lore raw
Quoted 65 lines · Bart Van Assche
Quoted 50 lines · weiping zhang
Quoted 31 lines · Bart Van Assche
Quoted 30 lines · weiping zhang

if blk-mq use "none" io scheduler, nr_request get a wrong value when input a number > tag_set->queue_depth. blk_mq_tag_update_depth will get the smaller one min(nr, set->queue_depth), and then q->nr_request get a wrong value.

Reproduce:

echo none > /sys/block/nvme0n1/queue/ioscheduler echo 1000000 > /sys/block/nvme0n1/queue/nr_requests cat /sys/block/nvme0n1/queue/nr_requests 1000000

---
 block/blk-mq.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
block/blk-mq.c +3 -2
index f84d145..8303e5e 100644
@@ -2622,8 +2622,11 @@ int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr)
 		 * queue depth. This is similar to what the old code would do.
 		 */
 		if (!hctx->sched_tags) {
-			ret = blk_mq_tag_update_depth(hctx, &hctx->tags,
-							min(nr, set->queue_depth),
+			if (nr > set->queue_depth) {
+				nr = set->queue_depth;
+				pr_warn("reduce nr_request to %u\n", nr);
Quoted 8 lines

block/blk-mq.c int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr) · line 2632

Quoted 5 lines
block/blk-mq.c +2 -0
+			}
+			ret = blk_mq_tag_update_depth(hctx, &hctx->tags, nr,
 							false);
 		} else {
 			ret = blk_mq_tag_update_depth(hctx, &hctx->sched_tags,

Shouldn't this code return -EINVAL or -ERANGE if 'nr' is too large? That will help to keep user space code simple that updates the queue depth.

Hi Bart,

The reason why not return -EINVAL is keeping alin with minimum checking in queue_requests_store, if you insist return -EINVAL/-ERANGE, minimum checking should also keep
same behavior. Both return error meesage and quietly changing are okey
for me. Which way do you prefer ?

static ssize_t
queue_requests_store(struct request_queue *q, const char *page, size_t count)

Quoted 13 lines
{
	unsigned long nr;
	int ret, err;

	if (!q->request_fn && !q->mq_ops)
		return -EINVAL;

	ret = queue_var_store(&nr, page, count);
	if (ret < 0)
		return ret;

	if (nr < BLKDEV_MIN_RQ)
		nr = BLKDEV_MIN_RQ;
Quoted 8 lines

Hello Jens,

Do you perhaps have a preference for one of the approaches that have been discussed in this e-mail thread?

Thanks,

Bart.

Hello Jens,

Would you please give some comments about this patch,

Thanks
Weiping.

#6 lore raw
Quoted 66 lines · weiping zhang
Quoted 65 lines · Bart Van Assche
Quoted 63 lines · weiping zhang
Quoted 31 lines · Bart Van Assche
Quoted 30 lines · weiping zhang

if blk-mq use "none" io scheduler, nr_request get a wrong value when input a number > tag_set->queue_depth. blk_mq_tag_update_depth will get the smaller one min(nr, set->queue_depth), and then q->nr_request get a wrong value.

Reproduce:

echo none > /sys/block/nvme0n1/queue/ioscheduler echo 1000000 > /sys/block/nvme0n1/queue/nr_requests cat /sys/block/nvme0n1/queue/nr_requests 1000000

---
 block/blk-mq.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
block/blk-mq.c +3 -2
index f84d145..8303e5e 100644
@@ -2622,8 +2622,11 @@ int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr)
 		 * queue depth. This is similar to what the old code would do.
 		 */
 		if (!hctx->sched_tags) {
-			ret = blk_mq_tag_update_depth(hctx, &hctx->tags,
-							min(nr, set->queue_depth),
+			if (nr > set->queue_depth) {
+				nr = set->queue_depth;
+				pr_warn("reduce nr_request to %u\n", nr);
Quoted 8 lines

block/blk-mq.c int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr) · line 2632

Quoted 5 lines
block/blk-mq.c +2 -0
+			}
+			ret = blk_mq_tag_update_depth(hctx, &hctx->tags, nr,
 							false);
 		} else {
 			ret = blk_mq_tag_update_depth(hctx, &hctx->sched_tags,

Shouldn't this code return -EINVAL or -ERANGE if 'nr' is too large? That will help to keep user space code simple that updates the queue depth.

Hi Bart,

The reason why not return -EINVAL is keeping alin with minimum checking in queue_requests_store, if you insist return -EINVAL/-ERANGE, minimum checking should also keep
same behavior. Both return error meesage and quietly changing are okey
for me. Which way do you prefer ?

static ssize_t
queue_requests_store(struct request_queue *q, const char *page, size_t count)

{
	unsigned long nr;
	int ret, err;

	if (!q->request_fn && !q->mq_ops)
		return -EINVAL;

	ret = queue_var_store(&nr, page, count);
	if (ret < 0)
		return ret;

	if (nr < BLKDEV_MIN_RQ)
		nr = BLKDEV_MIN_RQ;
Quoted 9 lines
Quoted 8 lines

Hello Jens,

Do you perhaps have a preference for one of the approaches that have been discussed in this e-mail thread?

Thanks,

Bart.

Hello Jens,

Would you please give some comments about this patch,

Thanks
Weiping.

maintainer
#7 lore raw
Quoted 72 lines · weiping zhang
Quoted 71 lines · weiping zhang
Quoted 70 lines · Bart Van Assche
Quoted 50 lines · weiping zhang
Quoted 31 lines · Bart Van Assche
Quoted 30 lines · weiping zhang

if blk-mq use "none" io scheduler, nr_request get a wrong value when input a number > tag_set->queue_depth. blk_mq_tag_update_depth will get the smaller one min(nr, set->queue_depth), and then q->nr_request get a wrong value.

Reproduce:

echo none > /sys/block/nvme0n1/queue/ioscheduler echo 1000000 > /sys/block/nvme0n1/queue/nr_requests cat /sys/block/nvme0n1/queue/nr_requests 1000000

---
 block/blk-mq.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
block/blk-mq.c +3 -2
index f84d145..8303e5e 100644
@@ -2622,8 +2622,11 @@ int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr)
 		 * queue depth. This is similar to what the old code would do.
 		 */
 		if (!hctx->sched_tags) {
-			ret = blk_mq_tag_update_depth(hctx, &hctx->tags,
-							min(nr, set->queue_depth),
+			if (nr > set->queue_depth) {
+				nr = set->queue_depth;
+				pr_warn("reduce nr_request to %u\n", nr);
Quoted 8 lines

block/blk-mq.c int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr) · line 2632

Quoted 5 lines
block/blk-mq.c +2 -0
+			}
+			ret = blk_mq_tag_update_depth(hctx, &hctx->tags, nr,
 							false);
 		} else {
 			ret = blk_mq_tag_update_depth(hctx, &hctx->sched_tags,

Shouldn't this code return -EINVAL or -ERANGE if 'nr' is too large? That will help to keep user space code simple that updates the queue depth.

Hi Bart,

The reason why not return -EINVAL is keeping alin with minimum checking in queue_requests_store, if you insist return -EINVAL/-ERANGE, minimum checking should also keep
same behavior. Both return error meesage and quietly changing are okey
for me. Which way do you prefer ?

static ssize_t
queue_requests_store(struct request_queue *q, const char *page, size_t count)

Quoted 13 lines
{
	unsigned long nr;
	int ret, err;

	if (!q->request_fn && !q->mq_ops)
		return -EINVAL;

	ret = queue_var_store(&nr, page, count);
	if (ret < 0)
		return ret;

	if (nr < BLKDEV_MIN_RQ)
		nr = BLKDEV_MIN_RQ;

Hello Jens,

Do you perhaps have a preference for one of the approaches that have been discussed in this e-mail thread?

Quoted 7 lines
Quoted 4 lines
Quoted 3 lines

Thanks,

Bart.

Hello Jens,

Would you please give some comments about this patch,

If someone writes a value that's too large, return -EINVAL and don't set it. Don't add weird debug printks.

Signature · 1 line
Jens Axboe
#8 lore raw
Quoted 75 lines · Jens Axboe
Quoted 74 lines · weiping zhang
Quoted 73 lines · weiping zhang
Quoted 72 lines · Bart Van Assche
Quoted 50 lines · weiping zhang
Quoted 31 lines · Bart Van Assche
Quoted 30 lines · weiping zhang

if blk-mq use "none" io scheduler, nr_request get a wrong value when input a number > tag_set->queue_depth. blk_mq_tag_update_depth will get the smaller one min(nr, set->queue_depth), and then q->nr_request get a wrong value.

Reproduce:

echo none > /sys/block/nvme0n1/queue/ioscheduler echo 1000000 > /sys/block/nvme0n1/queue/nr_requests cat /sys/block/nvme0n1/queue/nr_requests 1000000

---
 block/blk-mq.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
block/blk-mq.c +3 -2
index f84d145..8303e5e 100644
@@ -2622,8 +2622,11 @@ int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr)
 		 * queue depth. This is similar to what the old code would do.
 		 */
 		if (!hctx->sched_tags) {
-			ret = blk_mq_tag_update_depth(hctx, &hctx->tags,
-							min(nr, set->queue_depth),
+			if (nr > set->queue_depth) {
+				nr = set->queue_depth;
+				pr_warn("reduce nr_request to %u\n", nr);
Quoted 8 lines

block/blk-mq.c int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr) · line 2632

Quoted 5 lines
block/blk-mq.c +2 -0
+			}
+			ret = blk_mq_tag_update_depth(hctx, &hctx->tags, nr,
 							false);
 		} else {
 			ret = blk_mq_tag_update_depth(hctx, &hctx->sched_tags,

Shouldn't this code return -EINVAL or -ERANGE if 'nr' is too large? That will help to keep user space code simple that updates the queue depth.

Hi Bart,

The reason why not return -EINVAL is keeping alin with minimum checking in queue_requests_store, if you insist return -EINVAL/-ERANGE, minimum checking should also keep
same behavior. Both return error meesage and quietly changing are okey
for me. Which way do you prefer ?

static ssize_t
queue_requests_store(struct request_queue *q, const char *page, size_t count)

Quoted 13 lines
{
	unsigned long nr;
	int ret, err;

	if (!q->request_fn && !q->mq_ops)
		return -EINVAL;

	ret = queue_var_store(&nr, page, count);
	if (ret < 0)
		return ret;

	if (nr < BLKDEV_MIN_RQ)
		nr = BLKDEV_MIN_RQ;

Hello Jens,

Do you perhaps have a preference for one of the approaches that have been discussed in this e-mail thread?

Thanks,

Quoted 10 lines
Quoted 5 lines
Quoted 2 lines
Quoted 1 line

Bart.

Hello Jens,

Would you please give some comments about this patch,

If someone writes a value that's too large, return -EINVAL and don't set it. Don't add weird debug printks.

OK, I send patch V2 soon.