Skip to contentSkip to conversation

block: don't allocate a payload for discard request

RFC PATCH In review Christoph Hellwig 9 participants 57 messages Jun 18, 2010 – Jul 1, 2010

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 block: don't allocate a payload for discard request

Allocating a fixed payload for discard requests always was a horrible hack, and it's not coming to byte us when adding support for discard in DM/MD.

So change the code to leave the allocation of a payload to the lowlevel driver. Unfortunately that means we'll need another hack, which allows us to update the various block layer length fields indicating that we have a payload. Instead of hiding this in sd.c, which we already partially do for UNMAP support add a documented helper in the core block layer for it.

block/blk-lib.c +6 -27
Index: linux-2.6/block/blk-lib.c
===================================================================
@@ -19,7 +19,6 @@ static void blkdev_discard_end_io(struct
 
 	if (bio->bi_private)
 		complete(bio->bi_private);
-	__free_page(bio_page(bio));
 
 	bio_put(bio);
 }
@@ -43,7 +42,6 @@ int blkdev_issue_discard(struct block_de
 	int type = flags & BLKDEV_IFL_BARRIER ?
 		DISCARD_BARRIER : DISCARD_NOBARRIER;
 	struct bio *bio;
-	struct page *page;
 	int ret = 0;
 
 	if (!q)
@@ -53,35 +51,21 @@ int blkdev_issue_discard(struct block_de
 		return -EOPNOTSUPP;
 
 	while (nr_sects && !ret) {
-		unsigned int sector_size = q->limits.logical_block_size;
 		unsigned int max_discard_sectors =
 			min(q->limits.max_discard_sectors, UINT_MAX >> 9);
 
 		bio = bio_alloc(gfp_mask, 1);
-		if (!bio)
-			goto out;
+		if (!bio) {
+			ret = -ENOMEM;
+			break;
+		}
+
 		bio->bi_sector = sector;
 		bio->bi_end_io = blkdev_discard_end_io;
 		bio->bi_bdev = bdev;
 		if (flags & BLKDEV_IFL_WAIT)
 			bio->bi_private = &wait;
 
-		/*
-		 * Add a zeroed one-sector payload as that's what
-		 * our current implementations need.  If we'll ever need
-		 * more the interface will need revisiting.
-		 */
-		page = alloc_page(gfp_mask | __GFP_ZERO);
-		if (!page)
-			goto out_free_bio;
-		if (bio_add_pc_page(q, bio, page, sector_size, 0) < sector_size)
-			goto out_free_page;
-
-		/*
-		 * And override the bio size - the way discard works we
-		 * touch many more blocks on disk than the actual payload
-		 * length.
-		 */
 		if (nr_sects > max_discard_sectors) {
 			bio->bi_size = max_discard_sectors << 9;
 			nr_sects -= max_discard_sectors;
@@ -103,13 +87,8 @@ int blkdev_issue_discard(struct block_de
 			ret = -EIO;
 		bio_put(bio);
 	}
+
 	return ret;
-out_free_page:
-	__free_page(page);
-out_free_bio:
-	bio_put(bio);
-out:
-	return -ENOMEM;
 }
 EXPORT_SYMBOL(blkdev_issue_discard);
 
drivers/scsi/sd.c +34 -18

103 lines not on this page · raw message

block/blk-core.c +32 -0
Index: linux-2.6/block/blk-core.c
===================================================================
@@ -1135,6 +1135,38 @@ void blk_put_request(struct request *req
 }
 EXPORT_SYMBOL(blk_put_request);
 
+/**
+ * blk_add_request_payload - add a payload to a request
+ * @rq: request to update
+ * @page: page backing the payload
+ * @len: length of the payload.
+ *
+ * This allows to later add a payload to an already submitted request by
+ * a block driver.  The driver needs to take care of freeing the payload
+ * itself.
+ *
+ * Note that this is a quite horrible hack and nothing but handling of
+ * discard requests should ever use it.
+ */
+void blk_add_request_payload(struct request *rq, struct page *page,
+		unsigned int len)
+{
+	struct bio *bio = rq->bio;
+
+	bio->bi_io_vec->bv_page = page;
+	bio->bi_io_vec->bv_offset = 0;
+	bio->bi_io_vec->bv_len = len;
+
+	bio->bi_size = len;
+	bio->bi_vcnt = 1;
+	bio->bi_phys_segments = 1;
+
+	rq->__data_len = rq->resid_len = len;
+	rq->nr_phys_segments = 1;
+	rq->buffer = bio_data(bio);
+}
+EXPORT_SYMBOL_GPL(blk_add_request_payload);
+
 void init_request_from_bio(struct request *req, struct bio *bio)
 {
 	req->cpu = bio->bi_comp_cpu;
include/linux/blkdev.h +2 -0
Index: linux-2.6/include/linux/blkdev.h
===================================================================
@@ -702,6 +702,8 @@ extern struct request *blk_make_request(
 					gfp_t);
 extern void blk_insert_request(struct request_queue *, struct request *, int, void *);
 extern void blk_requeue_request(struct request_queue *, struct request *);
+extern void blk_add_request_payload(struct request *rq, struct page *page,
+		unsigned int len);
 extern int blk_rq_check_limits(struct request_queue *q, struct request *rq);
 extern int blk_lld_busy(struct request_queue *q);
 extern int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
Acked-by on v1 1/1
#1 lore raw
Quoted 11 lines · Christoph Hellwig

Allocating a fixed payload for discard requests always was a horrible hack, and it's not coming to byte us when adding support for discard in DM/MD.

So change the code to leave the allocation of a payload to the lowlevel driver. Unfortunately that means we'll need another hack, which allows us to update the various block layer length fields indicating that we have a payload. Instead of hiding this in sd.c, which we already partially do for UNMAP support add a documented helper in the core block layer for it.

I've built on your patch to successfully implement discard support for DM (this includes splitting discards that span targets; only the linear and striped DM targets are supported so far).

The patchset is still a work in progress but is available here (against 2.6.34 at the moment):
http://people.redhat.com/msnitzer/patches/dm-discard/latest/

#2 lore raw
Quoted 13 lines · Mike Snitzer
Quoted 10 lines · Christoph Hellwig

Allocating a fixed payload for discard requests always was a horrible hack, and it's not coming to byte us when adding support for discard in DM/MD.

So change the code to leave the allocation of a payload to the lowlevel driver. Unfortunately that means we'll need another hack, which allows us to update the various block layer length fields indicating that we have a payload. Instead of hiding this in sd.c, which we already partially do for UNMAP support add a documented helper in the core block layer for it.

Quoted 7 lines
Quoted 1 line

I've built on your patch to successfully implement discard support for DM (this includes splitting discards that span targets; only the linear and striped DM targets are supported so far).

I must retract my Acked-by because further testing has shown that this change results in OOM (on 2.6.34 with postmark benchmarking against ext4 w/ -o discard).

The patch is _very_ desirable (simplifies DM's discard support, etc) but there is more work needed to get it stable. I'll continue tracking this OOM down.

Mem-Info:
Node 0 DMA per-cpu:
CPU 0: hi: 0, btch: 1 usd: 0
CPU 1: hi: 0, btch: 1 usd: 0
Node 0 DMA32 per-cpu:
CPU 0: hi: 186, btch: 31 usd: 91
CPU 1: hi: 186, btch: 31 usd: 92
active_anon:52 inactive_anon:65 isolated_anon:0
active_file:525 inactive_file:1275 isolated_file:0 unevictable:0 dirty:802 writeback:0 unstable:0
free:3411 slab_reclaimable:5386 slab_unreclaimable:7672 mapped:86 shmem:0 pagetables:576 bounce:0
Node 0 DMA free:8040kB min:40kB low:48kB high:60kB active_anon:0kB inactive_anon:0kB active_file:12kB inactive_file:0kB unevictable:0kB isolated(anon):0kB isolated(file):0kB present:15768kB mlocked:0kB dirty:0kB writeback:0kB mapped:8kB shmem:0kB slab_reclaimable:28kB slab_unreclaimable:16kB kernel_stack:0kB pagetables:0kB unstable:0kB bounce:0kB writeback_tmp:0kB pages_scanned:22 all_unreclaimable? yes lowmem_reserve[]: 0 2003 2003 2003
Node 0 DMA32 free:5604kB min:5704kB low:7128kB high:8556kB active_anon:208kB inactive_anon:260kB active_file:2088kB inactive_file:4976kB unevictable:0kB isolated(anon):0kB isolated(file):128kB present:2052068kB mlocked:0kB dirty:3208kB writeback:0kB mapped:336kB shmem:0kB slab_reclaimable:21516kB slab_unreclaimable:30672kB kernel_stack:816kB pagetables:2304kB unstable:0kB bounce:0kB writeback_tmp:0kB pages_scanned:608 all_unreclaimable? no
lowmem_reserve[]: 0 0 0 0
Node 0 DMA: 4*4kB 3*8kB 1*16kB 2*32kB 2*64kB 3*128kB 3*256kB 3*512kB 3*1024kB 1*2048kB 0*4096kB = 8056kB
Node 0 DMA32: 422*4kB 0*8kB 0*16kB 3*32kB 1*64kB 1*128kB 0*256kB 1*512kB 1*1024kB 1*2048kB 0*4096kB = 5560kB
1914 total pagecache pages
106 pages in swap cache
Swap cache stats: add 499740, delete 499634, find 61395/208276 Free swap = 4177680kB
Total swap = 4194300kB
524224 pages RAM
79718 pages reserved
1622 pages shared
438890 pages non-shared
Out of memory: kill process 2672 (sshd) score 2515 or a child Killed process 3013 (sshd) vsz:97340kB, anon-rss:0kB, file-rss:4kB postmark used greatest stack depth: 2560 bytes left

I identified one potential leak, on an error path, through code inspection but I still hit OOM even with the following patch:

---
 block/blk-core.c       |   24 ++++++++++++++++++++++++
 drivers/scsi/sd.c      |    9 ++++++++-
 include/linux/blkdev.h |    1 +
 3 files changed, 33 insertions(+), 1 deletion(-)
drivers/scsi/sd.c +8 -1
Index: linux-2.6/drivers/scsi/sd.c
===================================================================
@@ -424,6 +424,7 @@ static int scsi_setup_discard_cmnd(struc
 	sector_t sector = bio->bi_sector;
 	unsigned int nr_sectors = bio_sectors(bio);
 	unsigned int len;
+	int ret;
 	struct page *page;
 
 	if (sdkp->device->sector_size == 4096) {
@@ -464,7 +465,13 @@ static int scsi_setup_discard_cmnd(struc
 	}
 
 	blk_add_request_payload(rq, page, len);
-	return scsi_setup_blk_pc_cmnd(sdp, rq);
+	ret = scsi_setup_blk_pc_cmnd(sdp, rq);
+	if (ret != BLKPREP_OK) {
+		blk_clear_request_payload(rq);
+		__free_page(page);
+	}
+
+	return ret;
 }
 
 /**
block/blk-core.c +24 -0
Index: linux-2.6/block/blk-core.c
===================================================================
@@ -1139,6 +1139,30 @@ void blk_add_request_payload(struct requ
 }
 EXPORT_SYMBOL_GPL(blk_add_request_payload);
 
+/**
+ * blk_add_request_payload - add a payload to a request
+ * @rq: request to update
+ *
+ * This clears a request's payload.  The driver needs to take care of
+ * freeing the payload itself.
+ */
+void blk_clear_request_payload(struct request *rq)
+{
+	struct bio *bio = rq->bio;
+
+	rq->__data_len = rq->resid_len = 0;
+	rq->nr_phys_segments = 0;
+	rq->buffer = NULL;
+
+	bio->bi_size = 0;
+	bio->bi_vcnt = 0;
+	bio->bi_phys_segments = 0;
+
+	bio->bi_io_vec->bv_page = NULL;
+	bio->bi_io_vec->bv_len = 0;
+}
+EXPORT_SYMBOL_GPL(blk_clear_request_payload);
+
 void init_request_from_bio(struct request *req, struct bio *bio)
 {
 	req->cpu = bio->bi_comp_cpu;
include/linux/blkdev.h +1 -0
Index: linux-2.6/include/linux/blkdev.h
===================================================================
@@ -779,6 +779,7 @@ extern void blk_insert_request(struct re
 extern void blk_requeue_request(struct request_queue *, struct request *);
 extern void blk_add_request_payload(struct request *rq, struct page *page,
 		unsigned int len);
+extern void blk_clear_request_payload(struct request *rq);
 extern int blk_rq_check_limits(struct request_queue *q, struct request *rq);
 extern int blk_lld_busy(struct request_queue *q);
 extern int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
#3 lore raw

PATCH 1/2 block: fix leaks associated with discard request payload

Fix leaks introduced via "block: don't allocate a payload for discard request" commit a1d949f5f44.

sd_done() is not called for REQ_TYPE_BLOCK_PC commands so cleanup discard request's payload directly in scsi_finish_command().

Also cleanup page allocated for discard payload in scsi_setup_discard_cmnd's scsi_setup_blk_pc_cmnd error path.

---
 block/blk-core.c       |   23 +++++++++++++++++++++++
 drivers/scsi/scsi.c    |    8 ++++++++
 drivers/scsi/sd.c      |   18 ++++++++----------
 include/linux/blkdev.h |    1 +
 4 files changed, 40 insertions(+), 10 deletions(-)
block/blk-core.c +23 -0
index 98b4cee..07925aa 100644
@@ -1167,6 +1167,29 @@ void blk_add_request_payload(struct request *rq, struct page *page,
 }
 EXPORT_SYMBOL_GPL(blk_add_request_payload);
 
+/**
+ * blk_clear_request_payload - clear a request's payload
+ * @rq: request to update
+ *
+ * The driver needs to take care of freeing the payload itself.
+ */
+void blk_clear_request_payload(struct request *rq)
+{
+	struct bio *bio = rq->bio;
+
+	rq->__data_len = rq->resid_len = 0;
+	rq->nr_phys_segments = 0;
+	rq->buffer = NULL;
+
+	bio->bi_size = 0;
+	bio->bi_vcnt = 0;
+	bio->bi_phys_segments = 0;
+
+	bio->bi_io_vec->bv_page = NULL;
+	bio->bi_io_vec->bv_len = 0;
+}
+EXPORT_SYMBOL_GPL(blk_clear_request_payload);
+
 void init_request_from_bio(struct request *req, struct bio *bio)
 {
 	req->cpu = bio->bi_comp_cpu;
drivers/scsi/scsi.c +8 -0
index ad0ed21..69c7ea4 100644
@@ -851,6 +851,14 @@ void scsi_finish_command(struct scsi_cmnd *cmd)
 		 */
 		if (good_bytes == old_good_bytes)
 			good_bytes -= scsi_get_resid(cmd);
+	} else if (cmd->request->cmd_flags & REQ_DISCARD) {
+		/*
+		 * If this is a discard request that originated from the kernel
+		 * we need to free our payload here.  Note that we need to check
+		 * the request flag as the normal payload rules apply for
+		 * pass-through UNMAP / WRITE SAME requests.
+		 */
+		__free_page(bio_page(cmd->request->bio));
 	}
 	scsi_io_completion(cmd, good_bytes);
 }
drivers/scsi/sd.c +8 -10
index 86da819..9b81dda 100644
@@ -425,6 +425,7 @@ static int scsi_setup_discard_cmnd(struct scsi_device *sdp, struct request *rq)
 	sector_t sector = bio->bi_sector;
 	unsigned int nr_sectors = bio_sectors(bio);
 	unsigned int len;
+	int ret;
 	struct page *page;
 
 	if (sdkp->device->sector_size == 4096) {
@@ -465,7 +466,13 @@ static int scsi_setup_discard_cmnd(struct scsi_device *sdp, struct request *rq)
 	}
 
 	blk_add_request_payload(rq, page, len);
-	return scsi_setup_blk_pc_cmnd(sdp, rq);
+	ret = scsi_setup_blk_pc_cmnd(sdp, rq);
+	if (ret != BLKPREP_OK) {
+		blk_clear_request_payload(rq);
+		__free_page(page);
+	}
+
+	return ret;
 }
 
 /**
@@ -1170,15 +1177,6 @@ static int sd_done(struct scsi_cmnd *SCpnt)
 	int sense_valid = 0;
 	int sense_deferred = 0;
 
-	/*
-	 * If this is a discard request that originated from the kernel
-	 * we need to free our payload here.  Note that we need to check
-	 * the request flag as the normal payload rules apply for
-	 * pass-through UNMAP / WRITE SAME requests.
-	 */
-	if (SCpnt->request->cmd_flags & REQ_DISCARD)
-		__free_page(bio_page(SCpnt->request->bio));
-
 	if (result) {
 		sense_valid = scsi_command_normalize_sense(SCpnt, &sshdr);
 		if (sense_valid)
include/linux/blkdev.h +1 -0
index 204fbe2..fdeef47 100644
@@ -707,6 +707,7 @@ extern void blk_insert_request(struct request_queue *, struct request *, int, vo
 extern void blk_requeue_request(struct request_queue *, struct request *);
 extern void blk_add_request_payload(struct request *rq, struct page *page,
 		unsigned int len);
+extern void blk_clear_request_payload(struct request *rq);
 extern int blk_rq_check_limits(struct request_queue *q, struct request *rq);
 extern int blk_lld_busy(struct request_queue *q);
 extern int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
Signature · 1 line
1.6.5.rc2
#4 lore raw

Re: [PATCH 1/2] block: fix leaks associated with discard request payload

Quoted 5 lines · On Sat, 26 Jun 2010 15:56:50 -0400 Mike Snitzer <[email protected]> wrote:

Fix leaks introduced via "block: don't allocate a payload for discard request" commit a1d949f5f44.

sd_done() is not called for REQ_TYPE_BLOCK_PC commands so cleanup discard request's payload directly in scsi_finish_command().

Instead of adding another discard hack to scsi_finish_command(), how about converting discard to REQ_TYPE_FS request? discard is FS request from the perspective of the block layer. It also fixes a problem that discard isn't retried in the case of UNIT ATTENTION.

I think that we can get more cleaner code if we handle discard as normal (fs) request in the block layer (and scsi-ml). We need more changes but this patch is the first step.

This patch depends on the patchset to convert flush request to REQ_TYPE_FS:

http://marc.info/?l=linux-kernel&m=127730591527424&w=2

The git tree is also available:

git://git.kernel.org/pub/scm/linux/kernel/git/tomo/linux-2.6-misc.git flush

Quoted 2 lines

Also cleanup page allocated for discard payload in scsi_setup_discard_cmnd's scsi_setup_blk_pc_cmnd error path.

This patch doesn't address on this (since it's a different problem). I think that scsi_setup_discard_cmnd() sees if it already allocates a page before allocating a page. That's similar to how other prep functions work, I think.

=
From: FUJITA Tomonori <[email protected]>
Subject: [PATCH] convert discard request to REQ_TYPE_FS instead of BLOCK_PC

---
 block/blk-lib.c   |    4 ++++
 drivers/scsi/sd.c |   11 +----------
 2 files changed, 5 insertions(+), 10 deletions(-)
block/blk-lib.c +4 -0
index e16185b..9e15c46 100644
@@ -20,6 +20,10 @@ static void blkdev_discard_end_io(struct bio *bio, int err)
 	if (bio->bi_private)
 		complete(bio->bi_private);
 
+	/* free the page that the lower layer allocated */
+	if (bio_page(bio))
+		__free_page(bio_page(bio));
+
 	bio_put(bio);
 }
 
drivers/scsi/sd.c +1 -10
index d447726..1b182de 100644
@@ -432,7 +432,7 @@ static int scsi_setup_discard_cmnd(struct scsi_device *sdp, struct request *rq)
 		nr_sectors >>= 3;
 	}
 
-	rq->cmd_type = REQ_TYPE_BLOCK_PC;
+	rq->cmd_type = REQ_TYPE_FS;
 	rq->timeout = SD_TIMEOUT;
 
 	memset(rq->cmd, 0, rq->cmd_len);
@@ -1177,15 +1177,6 @@ static int sd_done(struct scsi_cmnd *SCpnt)
 	int sense_valid = 0;
 	int sense_deferred = 0;
 
-	/*
-	 * If this is a discard request that originated from the kernel
-	 * we need to free our payload here.  Note that we need to check
-	 * the request flag as the normal payload rules apply for
-	 * pass-through UNMAP / WRITE SAME requests.
-	 */
-	if (SCpnt->request->cmd_flags & REQ_DISCARD)
-		__free_page(bio_page(SCpnt->request->bio));
-
 	if (result) {
 		sense_valid = scsi_command_normalize_sense(SCpnt, &sshdr);
 		if (sense_valid)
Signature · 1 line
1.6.5
#5 lore raw

Re: [PATCH 1/2] block: fix leaks associated with discard request payload

Quoted 9 lines · FUJITA Tomonori
Quoted 5 lines · On Sat, 26 Jun 2010 15:56:50 -0400 Mike Snitzer <[email protected]> wrote:

Fix leaks introduced via "block: don't allocate a payload for discard request" commit a1d949f5f44.

sd_done() is not called for REQ_TYPE_BLOCK_PC commands so cleanup discard request's payload directly in scsi_finish_command().

Quoted 8 lines

Instead of adding another discard hack to scsi_finish_command(), how about converting discard to REQ_TYPE_FS request? discard is FS request from the perspective of the block layer. It also fixes a problem that discard isn't retried in the case of UNIT ATTENTION.

I think that we can get more cleaner code if we handle discard as normal (fs) request in the block layer (and scsi-ml). We need more changes but this patch is the first step.

Making discard a REQ_TYPE_FS inside scsi (it already is before entering sd_prep_fn) means we'll need to special case it all over the I/O submission and completion path. Having the payload length not matching the transfer length is something we don't expect for FS requests.

block/blk-lib.c static void blkdev_discard_end_io(struct bio *bio, int err) · line 26

Quoted 11 lines

index e16185b..9e15c46 100644

block/blk-lib.c +4 -0
@@ -20,6 +20,10 @@ static void blkdev_discard_end_io(struct bio *bio, int err)
 	if (bio->bi_private)
 		complete(bio->bi_private);
 
+	/* free the page that the lower layer allocated */
+	if (bio_page(bio))
+		__free_page(bio_page(bio));
+

This is exactly what this patchkit gets rid off. Having a payload page that the caller tracks (previously fully, with this patch only for freeing) makes DM's life a lot harder. Remember we don't actually store any payload in there before entering sd_prep_fn - it's just that the scsi commands implementing discards need some payload - either a sector sizes zero filled buffer for WRITE SAME, or an LBA/len encoding inside the payload for UNMAP.

block/blk-lib.c static void blkdev_discard_end_io(struct bio *bio, int err) · line 27

Quoted 2 lines
block/blk-lib.c +1 -1
-	rq->cmd_type = REQ_TYPE_BLOCK_PC;
+	rq->cmd_type = REQ_TYPE_FS;

No need to set REQ_TYPE_FS here, it's already set up that way.

#6 lore raw

Re: [PATCH 1/2] block: fix leaks associated with discard request payload

Quoted 15 lines · On Sun, 27 Jun 2010 11:26:52 +0200 Christoph Hellwig <[email protected]> wrote:
Quoted 14 lines · FUJITA Tomonori
Quoted 5 lines · On Sat, 26 Jun 2010 15:56:50 -0400 Mike Snitzer <[email protected]> wrote:

Fix leaks introduced via "block: don't allocate a payload for discard request" commit a1d949f5f44.

sd_done() is not called for REQ_TYPE_BLOCK_PC commands so cleanup discard request's payload directly in scsi_finish_command().

Instead of adding another discard hack to scsi_finish_command(), how about converting discard to REQ_TYPE_FS request? discard is FS request from the perspective of the block layer. It also fixes a problem that discard isn't retried in the case of UNIT ATTENTION.

Quoted 7 lines
Quoted 3 lines

I think that we can get more cleaner code if we handle discard as normal (fs) request in the block layer (and scsi-ml). We need more changes but this patch is the first step.

Making discard a REQ_TYPE_FS inside scsi (it already is before entering sd_prep_fn) means we'll need to special case it all over the I/O submission and completion path. Having the payload length not matching

Hmm, my patch doesn't add any special case in scsi submission and completion. sd_prep_fn already has a hack for discard to set bi->bi_size to rq->__data_size so scsi can tell the block layer to finish discard requests.

Adding another special case for discard to scsi_io_completion() doesn't look good.

About the block layer, we already have special case for discard everywhere (rq->cmd_flags & REQ_DISCARD).

Quoted 1 line

the transfer length is something we don't expect for FS requests.

Yeah, that's tricky. I'm not sure yet which is better; change how the block layer handles the transfer length or let the lower layer to add pages (as we do now).

Quoted 7 lines
Quoted 7 lines

index e16185b..9e15c46 100644

block/blk-lib.c +0 -0
@@ -20,6 +20,10 @@ static void blkdev_discard_end_io(struct bio *bio, int err)
 	if (bio->bi_private)
 		complete(bio->bi_private);
 
Quoted 12 lines

block/blk-lib.c static void blkdev_discard_end_io(struct bio *bio, int err) · line 26

Quoted 4 lines
block/blk-lib.c +4 -0
+	/* free the page that the lower layer allocated */
+	if (bio_page(bio))
+		__free_page(bio_page(bio));
+

This is exactly what this patchkit gets rid off. Having a payload page that the caller tracks (previously fully, with this patch only for freeing) makes DM's life a lot harder. Remember we don't actually store any payload in there before entering sd_prep_fn - it's just that the scsi commands implementing discards need some payload - either a sector sizes zero filled buffer for WRITE SAME, or an LBA/len encoding inside the payload for UNMAP.

It's so bad if the block layer frees pages that the lower layer allocates? I thought it's ok if the block layer doesn't allocate.

It's better if sd_done() frees a page? As my patch does, if we handle discard as FS in scsi-ml, sd_done() is called.

Quoted 4 lines

block/blk-lib.c static void blkdev_discard_end_io(struct bio *bio, int err) · line 27

Quoted 2 lines
block/blk-lib.c +1 -1
-	rq->cmd_type = REQ_TYPE_BLOCK_PC;
+	rq->cmd_type = REQ_TYPE_FS;

No need to set REQ_TYPE_FS here, it's already set up that way.

Oops, sure.

#7 lore raw

Re: [PATCH 1/2] block: fix leaks associated with discard request payload

Quoted 65 lines · On Sun, 27 Jun 2010 19:01:33 +0900 FUJITA Tomonori…
Quoted 15 lines · On Sun, 27 Jun 2010 11:26:52 +0200 Christoph Hellwig <[email protected]> wrote:
Quoted 14 lines · FUJITA Tomonori
Quoted 5 lines · On Sat, 26 Jun 2010 15:56:50 -0400 Mike Snitzer <[email protected]> wrote:

Fix leaks introduced via "block: don't allocate a payload for discard request" commit a1d949f5f44.

sd_done() is not called for REQ_TYPE_BLOCK_PC commands so cleanup discard request's payload directly in scsi_finish_command().

Instead of adding another discard hack to scsi_finish_command(), how about converting discard to REQ_TYPE_FS request? discard is FS request from the perspective of the block layer. It also fixes a problem that discard isn't retried in the case of UNIT ATTENTION.

Quoted 7 lines
Quoted 3 lines

I think that we can get more cleaner code if we handle discard as normal (fs) request in the block layer (and scsi-ml). We need more changes but this patch is the first step.

Making discard a REQ_TYPE_FS inside scsi (it already is before entering sd_prep_fn) means we'll need to special case it all over the I/O submission and completion path. Having the payload length not matching

Hmm, my patch doesn't add any special case in scsi submission and completion. sd_prep_fn already has a hack for discard to set bi->bi_size to rq->__data_size so scsi can tell the block layer to finish discard requests.

Adding another special case for discard to scsi_io_completion() doesn't look good.

About the block layer, we already have special case for discard everywhere (rq->cmd_flags & REQ_DISCARD).

Quoted 1 line

the transfer length is something we don't expect for FS requests.

Yeah, that's tricky. I'm not sure yet which is better; change how the block layer handles the transfer length or let the lower layer to add pages (as we do now).

Quoted 7 lines
Quoted 7 lines

index e16185b..9e15c46 100644

block/blk-lib.c +0 -0
@@ -20,6 +20,10 @@ static void blkdev_discard_end_io(struct bio *bio, int err)
 	if (bio->bi_private)
 		complete(bio->bi_private);
 
Quoted 12 lines

block/blk-lib.c static void blkdev_discard_end_io(struct bio *bio, int err) · line 26

Quoted 4 lines
block/blk-lib.c +4 -0
+	/* free the page that the lower layer allocated */
+	if (bio_page(bio))
+		__free_page(bio_page(bio));
+

This is exactly what this patchkit gets rid off. Having a payload page that the caller tracks (previously fully, with this patch only for freeing) makes DM's life a lot harder. Remember we don't actually store any payload in there before entering sd_prep_fn - it's just that the scsi commands implementing discards need some payload - either a sector sizes zero filled buffer for WRITE SAME, or an LBA/len encoding inside the payload for UNMAP.

Quoted 5 lines

It's so bad if the block layer frees pages that the lower layer allocates? I thought it's ok if the block layer doesn't allocate.

It's better if sd_done() frees a page? As my patch does, if we handle discard as FS in scsi-ml, sd_done() is called.

How about this?

=
From: FUJITA Tomonori <[email protected]>
Subject: [PATCH] convert discard to REQ_TYPE_FS instead of REQ_TYPE_BLOCK_PC

Fixes the two issues:

- leak of pages that scsi_setup_discard_cmnd() allocates (because we don't call sd_done for pc requets).

- discard requests aren't retried when possible (e.g. UNIT ATTENTION).

---
 drivers/scsi/sd.c |    1 -
 1 files changed, 0 insertions(+), 1 deletions(-)
drivers/scsi/sd.c +0 -1
index d447726..056c8e1 100644
@@ -432,7 +432,6 @@ static int scsi_setup_discard_cmnd(struct scsi_device *sdp, struct request *rq)
 		nr_sectors >>= 3;
 	}
 
-	rq->cmd_type = REQ_TYPE_BLOCK_PC;
 	rq->timeout = SD_TIMEOUT;
 
 	memset(rq->cmd, 0, rq->cmd_len);
Signature · 1 line
1.6.5
#8 lore raw

Re: [PATCH 1/2] block: fix leaks associated with discard request payload

Quoted 1 line

How about this?

As I tried to explain before this utterly confuses the I/O completion path. With the patch applied even a simple mkfs.xfs that issues discard just hangs.

#9 lore raw

Re: [PATCH 1/2] block: fix leaks associated with discard request payload

Quoted 5 lines · On Sun, 27 Jun 2010 13:07:12 +0200 Christoph Hellwig <[email protected]> wrote:
Quoted 1 line

How about this?

As I tried to explain before this utterly confuses the I/O completion path. With the patch applied even a simple mkfs.xfs that issues discard just hangs.

Wired. I just tried mkfs.xfs against scsi_debug with my block patches (I saw one discard command). Seemed that it worked fine.

#10 lore raw

Re: [PATCH 1/2] block: fix leaks associated with discard request payload

Quoted 11 lines · FUJITA Tomonori
Quoted 5 lines · On Sun, 27 Jun 2010 13:07:12 +0200 Christoph Hellwig <[email protected]> wrote:
Quoted 1 line

How about this?

As I tried to explain before this utterly confuses the I/O completion path. With the patch applied even a simple mkfs.xfs that issues discard just hangs.

Wired. I just tried mkfs.xfs against scsi_debug with my block patches (I saw one discard command). Seemed that it worked fine.

My leak fixes have been tested extensively against all permuations of devices with discards (ATA trim, SCSI UNMAP, SCSI WRTIE SAME w/ unmap=1).

I think we need to get Christoph's discard payload transformation complete by fixing the leaks _without_ trying to rework how discard commands are tagged, etc. E.g. fix what Jens already has staged in linux-2.6-block's 'for-next' and 'for-2.6.36'.

With that sorted out we can then look at longer term changes to cleanup discard request processing.

Regards,
Mike

#11 lore raw

Re: [PATCH 1/2] block: fix leaks associated with discard request payload

Quoted 10 lines · Mike Snitzer

My leak fixes have been tested extensively against all permuations of devices with discards (ATA trim, SCSI UNMAP, SCSI WRTIE SAME w/ unmap=1).

I think we need to get Christoph's discard payload transformation complete by fixing the leaks _without_ trying to rework how discard commands are tagged, etc. E.g. fix what Jens already has staged in linux-2.6-block's 'for-next' and 'for-2.6.36'.

With that sorted out we can then look at longer term changes to cleanup discard request processing.

I tend to agree. I'll look into getting rid of treating discards as BLOCK_PC commands ontop of you patchset.

#12 lore raw

Re: [PATCH 1/2] block: fix leaks associated with discard request payload

Quoted 9 lines · On Sun, 27 Jun 2010 17:35:45 +0200 Christoph Hellwig <[email protected]> wrote:
Quoted 8 lines · Mike Snitzer

My leak fixes have been tested extensively against all permuations of devices with discards (ATA trim, SCSI UNMAP, SCSI WRTIE SAME w/ unmap=1).

I think we need to get Christoph's discard payload transformation complete by fixing the leaks _without_ trying to rework how discard commands are tagged, etc. E.g. fix what Jens already has staged in linux-2.6-block's 'for-next' and 'for-2.6.36'.

Quoted 5 lines
Quoted 2 lines

With that sorted out we can then look at longer term changes to cleanup discard request processing.

I tend to agree. I'll look into getting rid of treating discards as BLOCK_PC commands ontop of you patchset.

Let's start from jens' 2.6.36 tree (or it would be better if Jens can drop Christoph's patch).

We don't need to start on the top of new discard hacks (specially, the hack in scsi_finish_command looks terrible). We should not merge them into mainline. We are still in rc3.

I'll see how things work with other configurations.

#13 lore raw

Re: [PATCH 1/2] block: fix leaks associated with discard request payload

In reply to FUJITA Tomonori
Quoted 11 lines · FUJITA Tomonori
Quoted 5 lines · On Sun, 27 Jun 2010 13:07:12 +0200 Christoph Hellwig <[email protected]> wrote:
Quoted 1 line

How about this?

As I tried to explain before this utterly confuses the I/O completion path. With the patch applied even a simple mkfs.xfs that issues discard just hangs.

Wired. I just tried mkfs.xfs against scsi_debug with my block patches (I saw one discard command). Seemed that it worked fine.

Doesn't work for me against either a real SSD or WRITE SAME support in qemu. But I think I didn't actually have your barrier patches applied, so I'll try again with a clean tree.

#14 lore raw

Re: [PATCH 1/2] block: fix leaks associated with discard request payload

In reply to FUJITA Tomonori
Quoted 11 lines · FUJITA Tomonori
Quoted 5 lines · On Sun, 27 Jun 2010 13:07:12 +0200 Christoph Hellwig <[email protected]> wrote:
Quoted 1 line

How about this?

As I tried to explain before this utterly confuses the I/O completion path. With the patch applied even a simple mkfs.xfs that issues discard just hangs.

Wired. I just tried mkfs.xfs against scsi_debug with my block patches (I saw one discard command). Seemed that it worked fine.

I've tracked it down to the call to scsi_requeue_command in scsi_end_request. When the command is marked BLOCK_PC we'll just get it back as such in ->prep_fn next time, but now it's reverting to the previous state. While I see the problems with leaking ressources in that case I still can't quite explain the hang I see.

#15 lore raw

Re: [PATCH 1/2] block: fix leaks associated with discard request payload

Quoted 10 lines · On Mon, 28 Jun 2010 09:57:38 +0200 Christoph Hellwig <[email protected]> wrote:
Quoted 9 lines · FUJITA Tomonori
Quoted 5 lines · On Sun, 27 Jun 2010 13:07:12 +0200 Christoph Hellwig <[email protected]> wrote:
Quoted 1 line

How about this?

As I tried to explain before this utterly confuses the I/O completion path. With the patch applied even a simple mkfs.xfs that issues discard just hangs.

Quoted 6 lines
Quoted 2 lines

Wired. I just tried mkfs.xfs against scsi_debug with my block patches (I saw one discard command). Seemed that it worked fine.

I've tracked it down to the call to scsi_requeue_command in scsi_end_request. When the command is marked BLOCK_PC we'll just get it back as such in ->prep_fn next time, but now it's reverting to the previous state.

If scsi_end_request() calls scsi_requeue_command(), the command has a left over (i.e. hasn't finished all the data), right? You hit such condition with discard commands?

BLOCK_PC requests don't hit this case since blk_end_request() always return false for PC.

Quoted 2 lines

While I see the problems with leaking ressources in that case I still can't quite explain the hang I see.

Any way to reproduce the hang without ssd drives?

maintainer
#16 lore raw

Re: [PATCH 1/2] block: fix leaks associated with discard request payload

Quoted 20 lines · FUJITA Tomonori
Quoted 10 lines · On Mon, 28 Jun 2010 09:57:38 +0200 Christoph Hellwig <[email protected]> wrote:
Quoted 9 lines · FUJITA Tomonori
Quoted 5 lines · On Sun, 27 Jun 2010 13:07:12 +0200 Christoph Hellwig <[email protected]> wrote:
Quoted 1 line

How about this?

As I tried to explain before this utterly confuses the I/O completion path. With the patch applied even a simple mkfs.xfs that issues discard just hangs.

Quoted 6 lines
Quoted 2 lines

Wired. I just tried mkfs.xfs against scsi_debug with my block patches (I saw one discard command). Seemed that it worked fine.

I've tracked it down to the call to scsi_requeue_command in scsi_end_request. When the command is marked BLOCK_PC we'll just get it back as such in ->prep_fn next time, but now it's reverting to the previous state.

Quoted 6 lines

If scsi_end_request() calls scsi_requeue_command(), the command has a left over (i.e. hasn't finished all the data), right? You hit such condition with discard commands?

BLOCK_PC requests don't hit this case since blk_end_request() always return false for PC.

You can get requeues on the ->queuecommand() path as well, for a variety of reasons, and that would be what Christoph is hitting.

Signature · 1 line
Jens Axboe
#17 lore raw

Re: [PATCH 1/2] block: fix leaks associated with discard request payload

Quoted 25 lines · On Mon, 28 Jun 2010 10:18:48 +0200 Jens Axboe <[email protected]> wrote:
Quoted 24 lines · FUJITA Tomonori
Quoted 10 lines · On Mon, 28 Jun 2010 09:57:38 +0200 Christoph Hellwig <[email protected]> wrote:
Quoted 9 lines · FUJITA Tomonori
Quoted 5 lines · On Sun, 27 Jun 2010 13:07:12 +0200 Christoph Hellwig <[email protected]> wrote:
Quoted 1 line

How about this?

As I tried to explain before this utterly confuses the I/O completion path. With the patch applied even a simple mkfs.xfs that issues discard just hangs.

Quoted 6 lines
Quoted 2 lines

Wired. I just tried mkfs.xfs against scsi_debug with my block patches (I saw one discard command). Seemed that it worked fine.

I've tracked it down to the call to scsi_requeue_command in scsi_end_request. When the command is marked BLOCK_PC we'll just get it back as such in ->prep_fn next time, but now it's reverting to the previous state.

If scsi_end_request() calls scsi_requeue_command(), the command has a left over (i.e. hasn't finished all the data), right? You hit such condition with discard commands?

Quoted 5 lines
Quoted 2 lines

BLOCK_PC requests don't hit this case since blk_end_request() always return false for PC.

You can get requeues on the ->queuecommand() path as well, for a variety of reasons, and that would be what Christoph is hitting.

Probably, that's would be fine (we need to fix memory leak in that path). I guess that requeue with the partial completion commands might cause problems.

#18 lore raw

Re: [PATCH 1/2] block: fix leaks associated with discard request payload

In reply to FUJITA Tomonori
Quoted 4 lines · FUJITA Tomonori
Quoted 2 lines

While I see the problems with leaking ressources in that case I still can't quite explain the hang I see.

Any way to reproduce the hang without ssd drives?

Actually the SSDs don't fully hang, they just causes lots of I/O errors and hit the error handler hard. The hard hang is when running under qemu. Apply the patch below, then create an if=scsi drive that resides on an XFS filesystem, and you'll have scsi TP support in the guest:

hw/scsi-disk.c +34 -0
Index: qemu/hw/scsi-disk.c
===================================================================
@@ -397,6 +397,7 @@ static int scsi_disk_emulate_inquiry(SCS
         }
         case 0xb0: /* block device characteristics */
         {
+            static const int trim_sectors = (2 * 1024 * 1024) / 512;
             unsigned int min_io_size =
                     s->qdev.conf.min_io_size / s->qdev.blocksize;
             unsigned int opt_io_size =
@@ -416,6 +417,12 @@ static int scsi_disk_emulate_inquiry(SCS
             outbuf[13] = (opt_io_size >> 16) & 0xff;
             outbuf[14] = (opt_io_size >> 8) & 0xff;
             outbuf[15] = opt_io_size & 0xff;
+
+            /* optimal unmap granularity */
+            outbuf[28] = (trim_sectors >> 24) & 0xff;
+            outbuf[29] = (trim_sectors >> 16) & 0xff;
+            outbuf[30] = (trim_sectors >> 8) & 0xff;
+            outbuf[31] = trim_sectors & 0xff;
             break;
         }
         default:
@@ -824,6 +831,11 @@ static int scsi_disk_emulate_command(SCS
             outbuf[11] = 0;
             outbuf[12] = 0;
             outbuf[13] = get_physical_block_exp(&s->qdev.conf);
+
+            /* set TPE and TPRZ bits if the format supports discard */
+            if (bdrv_is_discardable(s->bs))
+                outbuf[14] = 0x80 | 0x40;
+
             /* Protection, exponent and lowest lba field left blank. */
             buflen = req->cmd.xfer;
             break;
@@ -988,6 +1000,28 @@ static int32_t scsi_send_command(SCSIDev
         r->sector_count = len * s->cluster_size;
         is_write = 1;
         break;
+    case WRITE_SAME_16:
+//        printf("WRITE SAME(16) (sector %" PRId64 ", count %d)\n", lba, len);
+
+//        if (lba + len > s->max_lba)
+        if (lba > s->max_lba)
+            goto illegal_lba; // check the condition code
+        /*
+         * We only support WRITE SAME with the unmap bit set for now.
+         */
+        if (!(buf[1] & 0x8))
+            goto fail;
+
+        rc = bdrv_discard(s->bs, lba * s->cluster_size, len * s->cluster_size);
+        if (rc < 0) {
+            /* XXX: better error code ?*/
+            goto fail;
+        }
+
+        scsi_req_set_status(&r->req, GOOD, NO_SENSE);
+        scsi_req_complete(&r->req);
+        scsi_remove_request(r);
+        return 0;
     default:
 	DPRINTF("Unknown SCSI command (%2.2x)\n", buf[0]);
     fail:
hw/scsi-defs.h +1 -0
Index: qemu/hw/scsi-defs.h
===================================================================
@@ -84,6 +84,7 @@
 #define MODE_SENSE_10         0x5a
 #define PERSISTENT_RESERVE_IN 0x5e
 #define PERSISTENT_RESERVE_OUT 0x5f
+#define WRITE_SAME_16         0x93
 #define MAINTENANCE_IN        0xa3
 #define MAINTENANCE_OUT       0xa4
 #define MOVE_MEDIUM           0xa5
block.c +12 -0
Index: qemu/block.c
===================================================================
@@ -1286,6 +1286,11 @@ int bdrv_is_sg(BlockDriverState *bs)
     return bs->sg;
 }
 
+int bdrv_is_discardable(BlockDriverState *bs)
+{
+    return bs->discardable;
+}
+
 int bdrv_enable_write_cache(BlockDriverState *bs)
 {
     return bs->enable_write_cache;
@@ -1431,6 +1436,13 @@ int bdrv_has_zero_init(BlockDriverState
     return 1;
 }
 
+int bdrv_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors)
+{
+    if (!bs->drv || !bs->drv->bdrv_discard)
+        return -ENOTSUP;
+    return bs->drv->bdrv_discard(bs, sector_num, nb_sectors);
+}
+
 /*
  * Returns true iff the specified sector is present in the disk image. Drivers
  * not implementing the functionality are assumed to not support backing files,
block.h +2 -0
Index: qemu/block.h
===================================================================
@@ -135,6 +135,7 @@ void bdrv_flush(BlockDriverState *bs);
 void bdrv_flush_all(void);
 void bdrv_close_all(void);
 
+int bdrv_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors);
 int bdrv_has_zero_init(BlockDriverState *bs);
 int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
 	int *pnum);
@@ -162,6 +163,7 @@ BlockErrorAction bdrv_get_on_error(Block
 int bdrv_is_removable(BlockDriverState *bs);
 int bdrv_is_read_only(BlockDriverState *bs);
 int bdrv_is_sg(BlockDriverState *bs);
+int bdrv_is_discardable(BlockDriverState *bs);
 int bdrv_enable_write_cache(BlockDriverState *bs);
 int bdrv_is_inserted(BlockDriverState *bs);
 int bdrv_media_changed(BlockDriverState *bs);
block/raw-posix.c +45 -0

75 lines not on this page · raw message

block_int.h +3 -0
Index: qemu/block_int.h
===================================================================
@@ -73,6 +73,8 @@ struct BlockDriver {
         BlockDriverCompletionFunc *cb, void *opaque);
     BlockDriverAIOCB *(*bdrv_aio_flush)(BlockDriverState *bs,
         BlockDriverCompletionFunc *cb, void *opaque);
+    int (*bdrv_discard)(BlockDriverState *bs, int64_t sector_num,
+                        int nb_sectors);
 
     int (*bdrv_aio_multiwrite)(BlockDriverState *bs, BlockRequest *reqs,
         int num_reqs);
@@ -141,6 +143,7 @@ struct BlockDriverState {
     int encrypted; /* if true, the media is encrypted */
     int valid_key; /* if true, a valid encryption key has been set */
     int sg;        /* if true, the device is a /dev/sg* */
+    int discardable;/* if true the device supports TRIM/UNMAP */
     /* event callback when inserting/removing */
     void (*change_cb)(void *opaque);
     void *change_opaque;
configure +26 -0
Index: qemu/configure
===================================================================
@@ -272,6 +272,7 @@ xen=""
 linux_aio=""
 attr=""
 vhost_net=""
+xfs=""
 
 gprof="no"
 debug_tcg="no"
@@ -1284,6 +1285,27 @@ EOF
 fi
 
 ##########################################
+# xfsctl() probe, used for raw-posix
+if test "$xfs" != "no" ; then
+  cat > $TMPC << EOF
+#include <xfs/xfs.h>
+int main(void)
+{
+    xfsctl(NULL, 0, 0, NULL);
+    return 0;
+}
+EOF
+  if compile_prog "" "" ; then
+    xfs="yes"
+  else
+    if test "$xfs" = "yes" ; then
+      feature_not_found "uuid"
+    fi
+    xfs=no
+  fi
+fi
+
+##########################################
 # vde libraries probe
 if test "$vde" != "no" ; then
   vde_libs="-lvdeplug"
@@ -2115,6 +2137,7 @@ echo "preadv support    $preadv"
 echo "fdatasync         $fdatasync"
 echo "uuid support      $uuid"
 echo "vhost-net support $vhost_net"
+echo "xfsctl support    $xfs"
 
 if test $sdl_too_old = "yes"; then
 echo "-> Your SDL version is too old - please upgrade to have SDL support"
@@ -2235,6 +2258,9 @@ fi
 if test "$uuid" = "yes" ; then
   echo "CONFIG_UUID=y" >> $config_host_mak
 fi
+if test "$xfs" = "yes" ; then
+  echo "CONFIG_XFS=y" >> $config_host_mak
+fi
 qemu_version=`head $source_path/VERSION`
 echo "VERSION=$qemu_version" >>$config_host_mak
 echo "PKGVERSION=$pkgversion" >>$config_host_mak
block/raw.c +7 -0
Index: qemu/block/raw.c
===================================================================
@@ -6,6 +6,7 @@
 static int raw_open(BlockDriverState *bs, int flags)
 {
     bs->sg = bs->file->sg;
+    bs->discardable = bs->file->discardable;
     return 0;
 }
 
@@ -65,6 +66,11 @@ static int raw_probe(const uint8_t *buf,
    return 1; /* everything can be opened as raw image */
 }
 
+static int raw_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors)
+{
+    return bdrv_discard(bs->file, sector_num, nb_sectors);
+}
+
 static int raw_is_inserted(BlockDriverState *bs)
 {
     return bdrv_is_inserted(bs->file);
@@ -125,6 +131,7 @@ static BlockDriver bdrv_raw = {
     .bdrv_aio_readv     = raw_aio_readv,
     .bdrv_aio_writev    = raw_aio_writev,
     .bdrv_aio_flush     = raw_aio_flush,
+    .bdrv_discard	= raw_discard,
 
     .bdrv_is_inserted   = raw_is_inserted,
     .bdrv_eject         = raw_eject,
#19 lore raw

Re: [PATCH 1/2] block: fix leaks associated with discard request payload

Quoted 10 lines · On Mon, 28 Jun 2010 17:25:36 +0200 Christoph Hellwig <[email protected]> wrote:
Quoted 4 lines · FUJITA Tomonori
Quoted 2 lines

While I see the problems with leaking ressources in that case I still can't quite explain the hang I see.

Any way to reproduce the hang without ssd drives?

Actually the SSDs don't fully hang, they just causes lots of I/O errors and hit the error handler hard. The hard hang is when running under qemu. Apply the patch below, then create an if=scsi drive that resides on an XFS filesystem, and you'll have scsi TP support in the guest:

Ok, I figured out what's wrong.

As I suspected, it's due to the partial completion.

qemu scsi driver tells that the WRITE_SAME command was successful but somehow the command has resid. So we retry it again and again (and leak some memory).

I don't know yet why qemu scsi driver is broken. Maybe there is a bug in it or converting discard to FS sends broken commands to the driver.

I'll try to figure out it tomorrow.

I've put a patch to complete discard command in the all-or-nothing manner:

git://git.kernel.org/pub/scm/linux/kernel/git/tomo/linux-2.6-misc.git discard

At least, the guest kernel doesn't hang for me.

#20 lore raw

Re: [PATCH 1/2] block: fix leaks associated with discard request payload

Quoted 16 lines · On Wed, 30 Jun 2010 20:55:09 +0900 FUJITA Tomonori…
Quoted 10 lines · On Mon, 28 Jun 2010 17:25:36 +0200 Christoph Hellwig <[email protected]> wrote:
Quoted 4 lines · FUJITA Tomonori
Quoted 2 lines

While I see the problems with leaking ressources in that case I still can't quite explain the hang I see.

Any way to reproduce the hang without ssd drives?

Actually the SSDs don't fully hang, they just causes lots of I/O errors and hit the error handler hard. The hard hang is when running under qemu. Apply the patch below, then create an if=scsi drive that resides on an XFS filesystem, and you'll have scsi TP support in the guest:

Ok, I figured out what's wrong.

Quoted 8 lines

As I suspected, it's due to the partial completion.

qemu scsi driver tells that the WRITE_SAME command was successful but somehow the command has resid. So we retry it again and again (and leak some memory).

I don't know yet why qemu scsi driver is broken. Maybe there is a bug in it or converting discard to FS sends broken commands to the driver.

looks like your qemu WRITE_SAME patch isn't completed :)

You implement WRITE_SAME as if it doesn't do any data transfer. So qemu scsi driver gets resid.

The reason why WRITE_SAME works now is that scsi-ml doesn't care about resid with PC commands but it cares with FS commands.

I confirmed that qemu scsi driver gets the identical command with both PC and FS commands and qemu calls xfsctl.

Quoted 4 lines

I've put a patch to complete discard command in the all-or-nothing manner:

git://git.kernel.org/pub/scm/linux/kernel/git/tomo/linux-2.6-misc.git discard

Seems that I finished discard FS conversion. I'll update it on the top of James' uprep patchset soon.

author Reviewed-by on v1 1/2
#21 lore raw

Re: [PATCH 1/2] block: fix leaks associated with discard request payload

In reply to Mike Snitzer
Quoted 8 lines · Mike Snitzer

Fix leaks introduced via "block: don't allocate a payload for discard request" commit a1d949f5f44.

sd_done() is not called for REQ_TYPE_BLOCK_PC commands so cleanup discard request's payload directly in scsi_finish_command().

Also cleanup page allocated for discard payload in scsi_setup_discard_cmnd's scsi_setup_blk_pc_cmnd error path.

This looks good - I've tested an equivalent patch doing xfstests runs for more than 24hours now without seeing leaks.

On the long run I'd prefer not having the knowledge of the payload freeing inside the core scsi code, but that requires either calling ->done for block pc requests, or treating discards as fs requests all the way, which will require much larger changes.

So for now:

#22 lore raw

Re: [PATCH 1/2] block: fix leaks associated with discard request payload

In reply to Mike Snitzer

linux-scsi cc added, since it's a SCSI patch.

Quoted 57 lines · Mike Snitzer

Fix leaks introduced via "block: don't allocate a payload for discard request" commit a1d949f5f44.

sd_done() is not called for REQ_TYPE_BLOCK_PC commands so cleanup discard request's payload directly in scsi_finish_command().

Also cleanup page allocated for discard payload in scsi_setup_discard_cmnd's scsi_setup_blk_pc_cmnd error path.

---
 block/blk-core.c       |   23 +++++++++++++++++++++++
 drivers/scsi/scsi.c    |    8 ++++++++
 drivers/scsi/sd.c      |   18 ++++++++----------
 include/linux/blkdev.h |    1 +
 4 files changed, 40 insertions(+), 10 deletions(-)
block/blk-core.c +23 -0
index 98b4cee..07925aa 100644
@@ -1167,6 +1167,29 @@ void blk_add_request_payload(struct request *rq, struct page *page,
 }
 EXPORT_SYMBOL_GPL(blk_add_request_payload);
 
+/**
+ * blk_clear_request_payload - clear a request's payload
+ * @rq: request to update
+ *
+ * The driver needs to take care of freeing the payload itself.
+ */
+void blk_clear_request_payload(struct request *rq)
+{
+	struct bio *bio = rq->bio;
+
+	rq->__data_len = rq->resid_len = 0;
+	rq->nr_phys_segments = 0;
+	rq->buffer = NULL;
+
+	bio->bi_size = 0;
+	bio->bi_vcnt = 0;
+	bio->bi_phys_segments = 0;
+
+	bio->bi_io_vec->bv_page = NULL;
+	bio->bi_io_vec->bv_len = 0;
+}
+EXPORT_SYMBOL_GPL(blk_clear_request_payload);
+
 void init_request_from_bio(struct request *req, struct bio *bio)
 {
 	req->cpu = bio->bi_comp_cpu;
drivers/scsi/scsi.c +0 -0
index ad0ed21..69c7ea4 100644
@@ -851,6 +851,14 @@ void scsi_finish_command(struct scsi_cmnd *cmd)
 		 */

drivers/scsi/scsi.c void scsi_finish_command(struct scsi_cmnd *cmd) · line 861

Quoted 10 lines
drivers/scsi/scsi.c +8 -0
 		if (good_bytes == old_good_bytes)
 			good_bytes -= scsi_get_resid(cmd);
+	} else if (cmd->request->cmd_flags & REQ_DISCARD) {
+		/*
+		 * If this is a discard request that originated from the kernel
+		 * we need to free our payload here.  Note that we need to check
+		 * the request flag as the normal payload rules apply for
+		 * pass-through UNMAP / WRITE SAME requests.
+		 */
+		__free_page(bio_page(cmd->request->bio));

This is another layering violation: the page is allocated in the Upper layer and freed in the mid-layer.

I really hate these growing contortions for discard. They're a clear signal that we haven't implemented it right.

So let's first work out how it should be done. I really like Tomo's idea of doing discard through the normal REQ_TYPE_FS route, which means we can control the setup in prep and the tear down in done, all confined to the ULD.

Where I think I'm at is partially what Christoph says: The command transformation belongs in the ULD so that's where the allocation and deallocation should be, and partly what Tomo says in that we should eliminate the special case paths.

The payload vs actual request size should be a red herring if we've got everything correct: only the ULD cares about the request parameters. Once we've got everything set up, the mid layer and LLD should only care about the parameters in the command, so we can confine the size changing part to the ULD doing the discard.

Could someone take a stab at this and see if it works without layering violations or any other problematic signals?

Thanks,

James

#23 lore raw

Re: [PATCH 1/2] block: fix leaks associated with discard request payload

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"James" == James Bottomley <[email protected]> writes:

James> I really hate these growing contortions for discard. They're a James> clear signal that we haven't implemented it right.

James> So let's first work out how it should be done. I really like James> Tomo's idea of doing discard through the normal REQ_TYPE_FS James> route, which means we can control the setup in prep and the tear James> down in done, all confined to the ULD.

Yeah, this is what I was trying to do a couple of months ago. Trying to make discard and write same filesystem class requests so we can split, merge, etc. like READs and WRITEs. I still think this is how we should do it but it's a lot of work.

There are several challenges involved. I was doing the "payload" allocation at request allocation time by permitting a buffer trailing struct request (size defined by ULD depending on req type). However, we have a few places in the stack where we memcpy requests and assume them to be the same size. That needs to be fixed. That's also the roadblock I ran into wrt. 32-byte CDB allocation so for that I ended up allocating the command in sd.

Also, another major headache of mine is WRITE SAME/UNMAP to DSM TRIM conversion. Because of the limitations of the TRIM command format a single WRITE SAME can turn into effectively hundreds of TRIM commands to be issued. I tried to limit this by using UNMAP translation instead. But we can still get into cases where we need to either loop or allocate a bunch of TRIMs in the translation layer. That leaves two options:
Either pass really conservative limits up the stack and loop up there. Or deal with the allocation/translation stuff at the bottom of the pile. None of my attempts in these departments turned out to be very nice. I'm still dreaming of the day where libata moves out from under SCSI so we don't have to translate square pegs into round holes...

Signature · 1 line
Martin K. Petersen	Oracle Linux Engineering
#24 lore raw

Re: [PATCH 1/2] block: fix leaks associated with discard request payload

Quoted 3 lines · Martin K. Petersen

I'm still dreaming of the day where libata moves out from under SCSI so we don't have to translate square pegs into round holes...

Hi Martin.

Is that on the Radar still? what would you say are the major issues holding it back?

* Is it just the missing new ULD, Will we be using a new ULD?
* Is it the ata midlayer that would duplicate some of scsi-midlayer. (Not so much these days, lots of good code went into blk_rq_)
* Is it the distro's setup procedures changing yet again back to the hd vs sd times.

Thanks
Boaz

maintainer
#25 lore raw

Re: [dm-devel] [PATCH 1/2] block: fix leaks associated with discard request payload

In reply to James Bottomley
Quoted 93 lines · James Bottomley

linux-scsi cc added, since it's a SCSI patch.

Quoted 57 lines · Mike Snitzer

Fix leaks introduced via "block: don't allocate a payload for discard request" commit a1d949f5f44.

sd_done() is not called for REQ_TYPE_BLOCK_PC commands so cleanup discard request's payload directly in scsi_finish_command().

Also cleanup page allocated for discard payload in scsi_setup_discard_cmnd's scsi_setup_blk_pc_cmnd error path.

---
 block/blk-core.c       |   23 +++++++++++++++++++++++
 drivers/scsi/scsi.c    |    8 ++++++++
 drivers/scsi/sd.c      |   18 ++++++++----------
 include/linux/blkdev.h |    1 +
 4 files changed, 40 insertions(+), 10 deletions(-)
block/blk-core.c +23 -0
index 98b4cee..07925aa 100644
@@ -1167,6 +1167,29 @@ void blk_add_request_payload(struct request *rq, struct page *page,
 }
 EXPORT_SYMBOL_GPL(blk_add_request_payload);
 
+/**
+ * blk_clear_request_payload - clear a request's payload
+ * @rq: request to update
+ *
+ * The driver needs to take care of freeing the payload itself.
+ */
+void blk_clear_request_payload(struct request *rq)
+{
+	struct bio *bio = rq->bio;
+
+	rq->__data_len = rq->resid_len = 0;
+	rq->nr_phys_segments = 0;
+	rq->buffer = NULL;
+
+	bio->bi_size = 0;
+	bio->bi_vcnt = 0;
+	bio->bi_phys_segments = 0;
+
+	bio->bi_io_vec->bv_page = NULL;
+	bio->bi_io_vec->bv_len = 0;
+}
+EXPORT_SYMBOL_GPL(blk_clear_request_payload);
+
 void init_request_from_bio(struct request *req, struct bio *bio)
 {
 	req->cpu = bio->bi_comp_cpu;
drivers/scsi/scsi.c +0 -0
index ad0ed21..69c7ea4 100644
@@ -851,6 +851,14 @@ void scsi_finish_command(struct scsi_cmnd *cmd)
 		 */

drivers/scsi/scsi.c void scsi_finish_command(struct scsi_cmnd *cmd) · line 861

Quoted 10 lines
drivers/scsi/scsi.c +8 -0
 		if (good_bytes == old_good_bytes)
 			good_bytes -= scsi_get_resid(cmd);
+	} else if (cmd->request->cmd_flags & REQ_DISCARD) {
+		/*
+		 * If this is a discard request that originated from the kernel
+		 * we need to free our payload here.  Note that we need to check
+		 * the request flag as the normal payload rules apply for
+		 * pass-through UNMAP / WRITE SAME requests.
+		 */
+		__free_page(bio_page(cmd->request->bio));

This is another layering violation: the page is allocated in the Upper layer and freed in the mid-layer.

I really hate these growing contortions for discard. They're a clear signal that we haven't implemented it right.

So let's first work out how it should be done. I really like Tomo's idea of doing discard through the normal REQ_TYPE_FS route, which means we can control the setup in prep and the tear down in done, all confined to the ULD.

Where I think I'm at is partially what Christoph says: The command transformation belongs in the ULD so that's where the allocation and deallocation should be, and partly what Tomo says in that we should eliminate the special case paths.

The payload vs actual request size should be a red herring if we've got everything correct: only the ULD cares about the request parameters. Once we've got everything set up, the mid layer and LLD should only care about the parameters in the command, so we can confine the size changing part to the ULD doing the discard.

Quoted 6 lines

Could someone take a stab at this and see if it works without layering violations or any other problematic signals?

Thanks,

James

Well, I think that you overestimate the importance of scsi code too much.

There is a layering violation in the code. So what --- you either fix the layering violation or let it be there and grind your teeth on it. But in either case, that layering violation won't affect anyone except scsi developers.

On the other hand, if you say "because we want to avoid layering violation in SCSI, every issuer of discard request must supply an empty page", you create havoc all over the Linux codebase. md, dm, drbd, xvd, virtio --- whatever you think of, will be allocating a dummy page when constructing a discard request.

If the layering violation spans only scsi code, it can be eventually fixed, but this, much worse "layering violation" that will be spanning all block device midlayers, won't ever be fixed.

Imagine for example --- a discard request arrivers at a dm-snapshot device. The driver splits it into chunks, remaps each chunk to the physical chunk, submits the requests, the elevator merges adjacent requests and submits fewer bigger requests to the device. Now, if you had to allocate a zeroed page each time you are splitting the request, that would exhaust memory and burn cpu needlessly. You delete a 100MB file? --- fine, allocate a 100MB of zeroed pages.

So I say --- let there be a layering violation in the scsi code, but don't put this problem with a page allocation to all the other bio midlayer developers.

Mikulas

#26 lore raw

Re: [dm-devel] [PATCH 1/2] block: fix leaks associated with discard request payload

Quoted 96 lines · Mikulas Patocka
Quoted 93 lines · James Bottomley

linux-scsi cc added, since it's a SCSI patch.

Quoted 57 lines · Mike Snitzer

Fix leaks introduced via "block: don't allocate a payload for discard request" commit a1d949f5f44.

sd_done() is not called for REQ_TYPE_BLOCK_PC commands so cleanup discard request's payload directly in scsi_finish_command().

Also cleanup page allocated for discard payload in scsi_setup_discard_cmnd's scsi_setup_blk_pc_cmnd error path.

---
 block/blk-core.c       |   23 +++++++++++++++++++++++
 drivers/scsi/scsi.c    |    8 ++++++++
 drivers/scsi/sd.c      |   18 ++++++++----------
 include/linux/blkdev.h |    1 +
 4 files changed, 40 insertions(+), 10 deletions(-)
block/blk-core.c +23 -0
index 98b4cee..07925aa 100644
@@ -1167,6 +1167,29 @@ void blk_add_request_payload(struct request *rq, struct page *page,
 }
 EXPORT_SYMBOL_GPL(blk_add_request_payload);
 
+/**
+ * blk_clear_request_payload - clear a request's payload
+ * @rq: request to update
+ *
+ * The driver needs to take care of freeing the payload itself.
+ */
+void blk_clear_request_payload(struct request *rq)
+{
+	struct bio *bio = rq->bio;
+
+	rq->__data_len = rq->resid_len = 0;
+	rq->nr_phys_segments = 0;
+	rq->buffer = NULL;
+
+	bio->bi_size = 0;
+	bio->bi_vcnt = 0;
+	bio->bi_phys_segments = 0;
+
+	bio->bi_io_vec->bv_page = NULL;
+	bio->bi_io_vec->bv_len = 0;
+}
+EXPORT_SYMBOL_GPL(blk_clear_request_payload);
+
 void init_request_from_bio(struct request *req, struct bio *bio)
 {
 	req->cpu = bio->bi_comp_cpu;
drivers/scsi/scsi.c +0 -0
index ad0ed21..69c7ea4 100644
@@ -851,6 +851,14 @@ void scsi_finish_command(struct scsi_cmnd *cmd)
 		 */

drivers/scsi/scsi.c void scsi_finish_command(struct scsi_cmnd *cmd) · line 861

Quoted 10 lines
drivers/scsi/scsi.c +8 -0
 		if (good_bytes == old_good_bytes)
 			good_bytes -= scsi_get_resid(cmd);
+	} else if (cmd->request->cmd_flags & REQ_DISCARD) {
+		/*
+		 * If this is a discard request that originated from the kernel
+		 * we need to free our payload here.  Note that we need to check
+		 * the request flag as the normal payload rules apply for
+		 * pass-through UNMAP / WRITE SAME requests.
+		 */
+		__free_page(bio_page(cmd->request->bio));

This is another layering violation: the page is allocated in the Upper layer and freed in the mid-layer.

I really hate these growing contortions for discard. They're a clear signal that we haven't implemented it right.

So let's first work out how it should be done. I really like Tomo's idea of doing discard through the normal REQ_TYPE_FS route, which means we can control the setup in prep and the tear down in done, all confined to the ULD.

Where I think I'm at is partially what Christoph says: The command transformation belongs in the ULD so that's where the allocation and deallocation should be, and partly what Tomo says in that we should eliminate the special case paths.

The payload vs actual request size should be a red herring if we've got everything correct: only the ULD cares about the request parameters. Once we've got everything set up, the mid layer and LLD should only care about the parameters in the command, so we can confine the size changing part to the ULD doing the discard.

Quoted 8 lines
Quoted 6 lines

Could someone take a stab at this and see if it works without layering violations or any other problematic signals?

Thanks,

James

Well, I think that you overestimate the importance of scsi code too much.

Not, I think, a deadly sin for a SCSI maintainer.

Quoted 4 lines

There is a layering violation in the code. So what --- you either fix the layering violation or let it be there and grind your teeth on it. But in either case, that layering violation won't affect anyone except scsi developers.

A layering violation is a signal of bad design wherever it occurs, so that wasn't a SCSI centric argument.

Quoted 5 lines

On the other hand, if you say "because we want to avoid layering violation in SCSI, every issuer of discard request must supply an empty page", you create havoc all over the Linux codebase. md, dm, drbd, xvd, virtio --- whatever you think of, will be allocating a dummy page when constructing a discard request.

Since I didn't actually say any of that, I suggest you re-read text you quoted above. The phrase "The command transformation belongs in the ULD so that's where the allocation and deallocation should be" might be a relevant one to concentrate on.

Quoted 11 lines

If the layering violation spans only scsi code, it can be eventually fixed, but this, much worse "layering violation" that will be spanning all block device midlayers, won't ever be fixed.

Imagine for example --- a discard request arrivers at a dm-snapshot device. The driver splits it into chunks, remaps each chunk to the physical chunk, submits the requests, the elevator merges adjacent requests and submits fewer bigger requests to the device. Now, if you had to allocate a zeroed page each time you are splitting the request, that would exhaust memory and burn cpu needlessly. You delete a 100MB file? --- fine, allocate a 100MB of zeroed pages.

This is a straw man: You've tried to portray a position I've never taken as mine then attack it ... with what is effectively another bogus argument.

It's not an either/or choice. I've asked the relevant parties to combine the approaches and see if a REQ_TYPE_FS path that does the allocations in the appropriate place, likely the ULD, produces a good design.

Quoted 3 lines

So I say --- let there be a layering violation in the scsi code, but don't put this problem with a page allocation to all the other bio midlayer developers.

Thanks for explaining that you have nothing to contribute, I'll make sure you're not on my list of relevant parties.

James

#27 lore raw

Re: [PATCH 1/2] block: fix leaks associated with discard request payload

Quoted 100 lines · James Bottomley
Quoted 99 lines · Mikulas Patocka
Quoted 96 lines · James Bottomley

linux-scsi cc added, since it's a SCSI patch.

Quoted 57 lines · Mike Snitzer

Fix leaks introduced via "block: don't allocate a payload for discard request" commit a1d949f5f44.

sd_done() is not called for REQ_TYPE_BLOCK_PC commands so cleanup discard request's payload directly in scsi_finish_command().

Also cleanup page allocated for discard payload in scsi_setup_discard_cmnd's scsi_setup_blk_pc_cmnd error path.

---
 block/blk-core.c       |   23 +++++++++++++++++++++++
 drivers/scsi/scsi.c    |    8 ++++++++
 drivers/scsi/sd.c      |   18 ++++++++----------
 include/linux/blkdev.h |    1 +
 4 files changed, 40 insertions(+), 10 deletions(-)
block/blk-core.c +23 -0
index 98b4cee..07925aa 100644
@@ -1167,6 +1167,29 @@ void blk_add_request_payload(struct request *rq, struct page *page,
 }
 EXPORT_SYMBOL_GPL(blk_add_request_payload);
 
+/**
+ * blk_clear_request_payload - clear a request's payload
+ * @rq: request to update
+ *
+ * The driver needs to take care of freeing the payload itself.
+ */
+void blk_clear_request_payload(struct request *rq)
+{
+	struct bio *bio = rq->bio;
+
+	rq->__data_len = rq->resid_len = 0;
+	rq->nr_phys_segments = 0;
+	rq->buffer = NULL;
+
+	bio->bi_size = 0;
+	bio->bi_vcnt = 0;
+	bio->bi_phys_segments = 0;
+
+	bio->bi_io_vec->bv_page = NULL;
+	bio->bi_io_vec->bv_len = 0;
+}
+EXPORT_SYMBOL_GPL(blk_clear_request_payload);
+
 void init_request_from_bio(struct request *req, struct bio *bio)
 {
 	req->cpu = bio->bi_comp_cpu;
drivers/scsi/scsi.c +0 -0
index ad0ed21..69c7ea4 100644
@@ -851,6 +851,14 @@ void scsi_finish_command(struct scsi_cmnd *cmd)
 		 */

drivers/scsi/scsi.c void scsi_finish_command(struct scsi_cmnd *cmd) · line 861

Quoted 10 lines
drivers/scsi/scsi.c +8 -0
 		if (good_bytes == old_good_bytes)
 			good_bytes -= scsi_get_resid(cmd);
+	} else if (cmd->request->cmd_flags & REQ_DISCARD) {
+		/*
+		 * If this is a discard request that originated from the kernel
+		 * we need to free our payload here.  Note that we need to check
+		 * the request flag as the normal payload rules apply for
+		 * pass-through UNMAP / WRITE SAME requests.
+		 */
+		__free_page(bio_page(cmd->request->bio));

This is another layering violation: the page is allocated in the Upper layer and freed in the mid-layer.

I really hate these growing contortions for discard. They're a clear signal that we haven't implemented it right.

So let's first work out how it should be done. I really like Tomo's idea of doing discard through the normal REQ_TYPE_FS route, which means we can control the setup in prep and the tear down in done, all confined to the ULD.

Where I think I'm at is partially what Christoph says: The command transformation belongs in the ULD so that's where the allocation and deallocation should be, and partly what Tomo says in that we should eliminate the special case paths.

The payload vs actual request size should be a red herring if we've got everything correct: only the ULD cares about the request parameters. Once we've got everything set up, the mid layer and LLD should only care about the parameters in the command, so we can confine the size changing part to the ULD doing the discard.

Could someone take a stab at this and see if it works without layering violations or any other problematic signals?

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

Thanks,

James

Well, I think that you overestimate the importance of scsi code too much.

Not, I think, a deadly sin for a SCSI maintainer.

Indeed ;)

Quoted 14 lines
Quoted 4 lines

There is a layering violation in the code. So what --- you either fix the layering violation or let it be there and grind your teeth on it. But in either case, that layering violation won't affect anyone except scsi developers.

A layering violation is a signal of bad design wherever it occurs, so that wasn't a SCSI centric argument.

Quoted 5 lines

On the other hand, if you say "because we want to avoid layering violation in SCSI, every issuer of discard request must supply an empty page", you create havoc all over the Linux codebase. md, dm, drbd, xvd, virtio --- whatever you think of, will be allocating a dummy page when constructing a discard request.

Quoted 4 lines

Since I didn't actually say any of that, I suggest you re-read text you quoted above. The phrase "The command transformation belongs in the ULD so that's where the allocation and deallocation should be" might be a relevant one to concentrate on.

Right, freeing the page, that was allocated in SCSI's ULD, from the SCSI midlayer is a SCSI layering violation. I think Mikulas was reacting to the desire to maintain the existing, arguably more problematic, layering violation that spans the block and SCSI layers.

Quoted 12 lines
Quoted 11 lines

If the layering violation spans only scsi code, it can be eventually fixed, but this, much worse "layering violation" that will be spanning all block device midlayers, won't ever be fixed.

Imagine for example --- a discard request arrivers at a dm-snapshot device. The driver splits it into chunks, remaps each chunk to the physical chunk, submits the requests, the elevator merges adjacent requests and submits fewer bigger requests to the device. Now, if you had to allocate a zeroed page each time you are splitting the request, that would exhaust memory and burn cpu needlessly. You delete a 100MB file? --- fine, allocate a 100MB of zeroed pages.

Quoted 8 lines

This is a straw man: You've tried to portray a position I've never taken as mine then attack it ... with what is effectively another bogus argument.

It's not an either/or choice. I've asked the relevant parties to combine the approaches and see if a REQ_TYPE_FS path that does the allocations in the appropriate place, likely the ULD, produces a good design.

If in the end we can fix up SCSI properly then everyone is happy. So lets just keep working toward that. The various attempts to convert discard over to REQ_TYPE_FS have fallen short. Hopefully we'll have a break through shortly.

Thanks for your guidance James,
Mike

maintainer
#28 lore raw

Re: [dm-devel] [PATCH 1/2] block: fix leaks associated with discard request payload

In reply to James Bottomley
Quoted 12 lines
Quoted 11 lines

If the layering violation spans only scsi code, it can be eventually fixed, but this, much worse "layering violation" that will be spanning all block device midlayers, won't ever be fixed.

Imagine for example --- a discard request arrivers at a dm-snapshot device. The driver splits it into chunks, remaps each chunk to the physical chunk, submits the requests, the elevator merges adjacent requests and submits fewer bigger requests to the device. Now, if you had to allocate a zeroed page each time you are splitting the request, that would exhaust memory and burn cpu needlessly. You delete a 100MB file? --- fine, allocate a 100MB of zeroed pages.

Quoted 5 lines

This is a straw man: You've tried to portray a position I've never taken as mine then attack it ... with what is effectively another bogus argument.

It's not an either/or choice.

It is either/or choice. If the interface isn't fixed NOW, the existing flawed zeroed-page-allocation interface gets into RHEL and I and others will have to support it for 7 years.

Quoted 4 lines

I've asked the relevant parties to
combine the approaches and see if a REQ_TYPE_FS path that does the allocations in the appropriate place, likely the ULD, produces a good design.

OK, but before you do this research, fix the interface.

Quoted 6 lines
Quoted 3 lines

So I say --- let there be a layering violation in the scsi code, but don't put this problem with a page allocation to all the other bio midlayer developers.

Thanks for explaining that you have nothing to contribute, I'll make sure you're not on my list of relevant parties.

You misunderstand what I meant. You admit that there are design problems in SCSI. So don't burden other developers with these problems. Don't force the others to allocate dummy pages just because you want a cleaner scsi code.

You intend to fix the design of SCSI and then remove the dummy pages. But by the time you finish it, it will be already late and there will be midlayer drivers allocating these dummy pages.

What I mean is that "layering violation" inside one driver is smaller problem than misdesigned interface between drivers. So accept the patch that creates "layering violation" but cleans up the interface.

Mikulas

#29 lore raw

Re: [dm-devel] [PATCH 1/2] block: fix leaks associated with discard request payload

Quoted 12 lines · Mikulas Patocka
Quoted 12 lines
Quoted 11 lines

If the layering violation spans only scsi code, it can be eventually fixed, but this, much worse "layering violation" that will be spanning all block device midlayers, won't ever be fixed.

Imagine for example --- a discard request arrivers at a dm-snapshot device. The driver splits it into chunks, remaps each chunk to the physical chunk, submits the requests, the elevator merges adjacent requests and submits fewer bigger requests to the device. Now, if you had to allocate a zeroed page each time you are splitting the request, that would exhaust memory and burn cpu needlessly. You delete a 100MB file? --- fine, allocate a 100MB of zeroed pages.

Quoted 8 lines
Quoted 5 lines

This is a straw man: You've tried to portray a position I've never taken as mine then attack it ... with what is effectively another bogus argument.

It's not an either/or choice.

It is either/or choice. If the interface isn't fixed NOW, the existing flawed zeroed-page-allocation interface gets into RHEL

That's a false dichotomy. You might see an either apply this hack now or support the interface choice with RHEL, but upstream has the option to fix stuff correctly. RHEL has never needed my blessing to apply random crap to their kernel before ... why is this patch any different?

Quoted 1 line

and I and others will have to support it for 7 years.

It's called a business model ... I believe it's what they pay you for.

Quoted 6 lines
Quoted 4 lines

I've asked the relevant parties to
combine the approaches and see if a REQ_TYPE_FS path that does the allocations in the appropriate place, likely the ULD, produces a good design.

OK, but before you do this research, fix the interface.

So even in the RHEL world, I think you'd find that analysing the problem *before* comping up with a fix is a good way of doing things.

Quoted 9 lines
Quoted 6 lines
Quoted 3 lines

So I say --- let there be a layering violation in the scsi code, but don't put this problem with a page allocation to all the other bio midlayer developers.

Thanks for explaining that you have nothing to contribute, I'll make sure you're not on my list of relevant parties.

You misunderstand what I meant. You admit that there are design problems in SCSI.

No I didn't.

And the rest of this rubbish is based on that false premise. It might help you to take off your SCSI antipathy and see this as a system
problem: it actually originates in block and spills out from there. Thus it requires a system solution.

James

Quoted 13 lines

So don't burden other developers with these problems. Don't force the others to allocate dummy pages just because you want a cleaner scsi code.

You intend to fix the design of SCSI and then remove the dummy pages. But by the time you finish it, it will be already late and there will be midlayer drivers allocating these dummy pages.

What I mean is that "layering violation" inside one driver is smaller problem than misdesigned interface between drivers. So accept the patch that creates "layering violation" but cleans up the interface.

Mikulas

#30 lore raw

Re: [PATCH 1/2] block: fix leaks associated with discard request payload

Quoted 51 lines · James Bottomley
Quoted 12 lines · Mikulas Patocka
Quoted 12 lines
Quoted 11 lines

If the layering violation spans only scsi code, it can be eventually fixed, but this, much worse "layering violation" that will be spanning all block device midlayers, won't ever be fixed.

Imagine for example --- a discard request arrivers at a dm-snapshot device. The driver splits it into chunks, remaps each chunk to the physical chunk, submits the requests, the elevator merges adjacent requests and submits fewer bigger requests to the device. Now, if you had to allocate a zeroed page each time you are splitting the request, that would exhaust memory and burn cpu needlessly. You delete a 100MB file? --- fine, allocate a 100MB of zeroed pages.

Quoted 8 lines
Quoted 5 lines

This is a straw man: You've tried to portray a position I've never taken as mine then attack it ... with what is effectively another bogus argument.

It's not an either/or choice.

It is either/or choice. If the interface isn't fixed NOW, the existing flawed zeroed-page-allocation interface gets into RHEL

That's a false dichotomy. You might see an either apply this hack now or support the interface choice with RHEL, but upstream has the option to fix stuff correctly. RHEL has never needed my blessing to apply random crap to their kernel before ... why is this patch any different?

Quoted 1 line

and I and others will have to support it for 7 years.

It's called a business model ... I believe it's what they pay you for.

Quoted 6 lines
Quoted 4 lines

I've asked the relevant parties to
combine the approaches and see if a REQ_TYPE_FS path that does the allocations in the appropriate place, likely the ULD, produces a good design.

OK, but before you do this research, fix the interface.

So even in the RHEL world, I think you'd find that analysing the problem *before* comping up with a fix is a good way of doing things.

Quoted 9 lines
Quoted 6 lines
Quoted 3 lines

So I say --- let there be a layering violation in the scsi code, but don't put this problem with a page allocation to all the other bio midlayer developers.

Thanks for explaining that you have nothing to contribute, I'll make sure you're not on my list of relevant parties.

You misunderstand what I meant. You admit that there are design problems in SCSI.

Quoted 6 lines

No I didn't.

And the rest of this rubbish is based on that false premise. It might help you to take off your SCSI antipathy and see this as a system
problem: it actually originates in block and spills out from there. Thus it requires a system solution.

As fun as it is for the others monitoring these lists to see redhat.com vs suse.de banter I think framing this discussion like you (and Mikulas) continue to do is a complete distraction.

I tried to elevate (and defuse) the discussion yesterday. But simply
put: patches speak volumes. I look forward to working with Tomo, hch and anyone else who has something to contribute that moves us toward a real fix for discards.

Mike

#31 lore raw

Re: [PATCH 1/2] block: fix leaks associated with discard request payload

Quoted 56 lines · Mike Snitzer
Quoted 53 lines · James Bottomley
Quoted 12 lines · Mikulas Patocka
Quoted 12 lines
Quoted 11 lines

If the layering violation spans only scsi code, it can be eventually fixed, but this, much worse "layering violation" that will be spanning all block device midlayers, won't ever be fixed.

Imagine for example --- a discard request arrivers at a dm-snapshot device. The driver splits it into chunks, remaps each chunk to the physical chunk, submits the requests, the elevator merges adjacent requests and submits fewer bigger requests to the device. Now, if you had to allocate a zeroed page each time you are splitting the request, that would exhaust memory and burn cpu needlessly. You delete a 100MB file? --- fine, allocate a 100MB of zeroed pages.

Quoted 8 lines
Quoted 5 lines

This is a straw man: You've tried to portray a position I've never taken as mine then attack it ... with what is effectively another bogus argument.

It's not an either/or choice.

It is either/or choice. If the interface isn't fixed NOW, the existing flawed zeroed-page-allocation interface gets into RHEL

That's a false dichotomy. You might see an either apply this hack now or support the interface choice with RHEL, but upstream has the option to fix stuff correctly. RHEL has never needed my blessing to apply random crap to their kernel before ... why is this patch any different?

Quoted 1 line

and I and others will have to support it for 7 years.

It's called a business model ... I believe it's what they pay you for.

Quoted 6 lines
Quoted 4 lines

I've asked the relevant parties to
combine the approaches and see if a REQ_TYPE_FS path that does the allocations in the appropriate place, likely the ULD, produces a good design.

OK, but before you do this research, fix the interface.

So even in the RHEL world, I think you'd find that analysing the problem *before* comping up with a fix is a good way of doing things.

Quoted 9 lines
Quoted 6 lines
Quoted 3 lines

So I say --- let there be a layering violation in the scsi code, but don't put this problem with a page allocation to all the other bio midlayer developers.

Thanks for explaining that you have nothing to contribute, I'll make sure you're not on my list of relevant parties.

You misunderstand what I meant. You admit that there are design problems in SCSI.

No I didn't.

Quoted 8 lines
Quoted 4 lines

And the rest of this rubbish is based on that false premise. It might help you to take off your SCSI antipathy and see this as a system
problem: it actually originates in block and spills out from there. Thus it requires a system solution.

As fun as it is for the others monitoring these lists to see redhat.com vs suse.de banter I think framing this discussion like you (and Mikulas) continue to do is a complete distraction.

Well, it's not SUSE v Red Hat, it's upstream v Enterprise ... and it's partly my job to explain why upstream does correct fixes not enterprise workarounds (whether the enterprise is RHEL or SLES). But I agree it's becoming a pointless distraction.

Quoted 4 lines

I tried to elevate (and defuse) the discussion yesterday. But simply
put: patches speak volumes. I look forward to working with Tomo, hch and anyone else who has something to contribute that moves us toward a real fix for discards.

Right, so thanks for that.

Most of our problem is tied up in the fact that we need to allocate in the prepare path, but we don't have a corresponding clear unprepare path to do the deallocation in. Introducing that into block might sort out this tangle better ... error handling on the backend is very convoluted and we can't really free the page until after it's complete (and the ->done function doesn't mark completion of error handling terms). I think the bones of a solution to this might be that scsi_unprep_request() needs to call into block (instead of setting the flags itself), say blk_unprep_request. Block also needs to call blk_unprep_request based on the REQ_DONTPREP status in its completion path. This would then give us a hook to hang the deallocation correctly on.

James

maintainer
#32 lore raw

Re: [dm-devel] [PATCH 1/2] block: fix leaks associated with discard request payload

In reply to James Bottomley
Quoted 7 lines
Quoted 2 lines

It is either/or choice. If the interface isn't fixed NOW, the existing flawed zeroed-page-allocation interface gets into RHEL

That's a false dichotomy. You might see an either apply this hack now or support the interface choice with RHEL, but upstream has the option to fix stuff correctly. RHEL has never needed my blessing to apply random crap to their kernel before ... why is this patch any different?

We can't apply non-upstream patches (except few exceptions such as dm-raid45). It makes sense, non-upstream patches have smaller test coverage.

Quoted 6 lines

And the rest of this rubbish is based on that false premise. It might help you to take off your SCSI antipathy and see this as a system
problem: it actually originates in block and spills out from there. Thus it requires a system solution.

James

Imagine this: I take a FPGA PCI board, I design a storage controller on it and this controller will need 3 pages to process a discard request. Now I
say: I refuse to allocate these 3 pages in the driver because the driver would look ugly --- instead, I demand that everyone in the Linux kernel who creates a discard request must attach 3 pages to the request for my driver.

Do you think it is correct behavior? Would you accept such a driver? I guess you wouldn't! But this is the same thing that you are doing with SCSI.

Now lets take it a bit further and I say "I may clean up the driver for my controller one day, when I do it, I remove that 3-page requirement --- and then, everyone who allocated those pages will have to change his code and remove the allocations".

And this is what you are intending to do with SCSI.

Mikulas

#33 lore raw

Re: [PATCH 1/2] block: fix leaks associated with discard request payload

Quoted 30 lines · Mikulas Patocka
Quoted 7 lines
Quoted 2 lines

It is either/or choice. If the interface isn't fixed NOW, the existing flawed zeroed-page-allocation interface gets into RHEL

That's a false dichotomy. You might see an either apply this hack now or support the interface choice with RHEL, but upstream has the option to fix stuff correctly. RHEL has never needed my blessing to apply random crap to their kernel before ... why is this patch any different?

We can't apply non-upstream patches (except few exceptions such as dm-raid45). It makes sense, non-upstream patches have smaller test coverage.

Quoted 6 lines

And the rest of this rubbish is based on that false premise. It might help you to take off your SCSI antipathy and see this as a system
problem: it actually originates in block and spills out from there. Thus it requires a system solution.

James

Imagine this: I take a FPGA PCI board, I design a storage controller on it and this controller will need 3 pages to process a discard request. Now I
say: I refuse to allocate these 3 pages in the driver because the driver would look ugly --- instead, I demand that everyone in the Linux kernel who creates a discard request must attach 3 pages to the request for my driver.

Do you think it is correct behavior? Would you accept such a driver? I guess you wouldn't! But this is the same thing that you are doing with SCSI.

Quoted 6 lines

Now lets take it a bit further and I say "I may clean up the driver for my controller one day, when I do it, I remove that 3-page requirement --- and then, everyone who allocated those pages will have to change his code and remove the allocations".

And this is what you are intending to do with SCSI.

Mikulas,

Jens has already queued up a comprehensive fix (3 patches) that James and Tomo developed. Please stop the hostility.. it has no place.

Others,
I'd encourage you to not respond to this thread further ;)

Regards,
Mike

maintainer
#34 lore raw

Re: [PATCH 1/2] block: fix leaks associated with discard request payload

Quoted 10 lines

Mikulas,

Jens has already queued up a comprehensive fix (3 patches) that James and Tomo developed. Please stop the hostility.. it has no place.

Others,
I'd encourage you to not respond to this thread further ;)

Regards,
Mike

I read mailing lists at longer intervals than my mailbox, so I read the James' message I responded to but missed the patches on the list. It's good that it's fixed and there is no need to argue about it anymore.

Mikulas

#35 lore raw

Re: [dm-devel] [PATCH 1/2] block: fix leaks associated with discard request payload

In reply to Mikulas Patocka
Quoted 1 line · Mikulas Patocka

But this is the same thing that you are doing with SCSI.

Have you read these patches, Mikulas? Stop digging.

Alasdair

#36 lore raw

Re: [PATCH 1/2] block: fix leaks associated with discard request payload

In reply to James Bottomley

On 06/27/2010 06:29 PM, James Bottomley wrote:
<snip>

Quoted 11 lines

drivers/scsi/scsi.c void scsi_finish_command(struct scsi_cmnd *cmd) · line 861

Quoted 7 lines
drivers/scsi/scsi.c +7 -0
+		/*
+		 * If this is a discard request that originated from the kernel
+		 * we need to free our payload here.  Note that we need to check
+		 * the request flag as the normal payload rules apply for
+		 * pass-through UNMAP / WRITE SAME requests.
+		 */
+		__free_page(bio_page(cmd->request->bio));

This is another layering violation: the page is allocated in the Upper layer and freed in the mid-layer.

May I ask a silly question? Why the dynamic allocation?

Why not have a const-static single global page at the block-layer somewhere that will be used for all discard-type operations and be done with it once and for all. A single page can be used for any size bio , any number of concurrent discards, any ZERO needed operation. It can also be used by other operations like padding and others. In fact isn't there one for the libsata padding?

(It could be dynamical allocated on first use for embedded system)

just my $0.017
Boaz

#37 lore raw

Re: [PATCH 1/2] block: fix leaks associated with discard request payload

Quoted 7 lines · Boaz Harrosh

May I ask a silly question? Why the dynamic allocation?

Why not have a const-static single global page at the block-layer somewhere that will be used for all discard-type operations and be done with it once and for all. A single page can be used for any size bio , any number of concurrent discards, any ZERO needed operation. It can also be used by other operations like padding and others. In fact isn't there one for the libsata padding?

for UNMAP we need to write into the payload. And for ATA TRIM we need to write into the WRITE SAME payload. That's another layering violation for those looking for them, btw..

#38 lore raw

Re: [PATCH 1/2] block: fix leaks associated with discard request payload

Quoted 11 lines · Christoph Hellwig
Quoted 7 lines · Boaz Harrosh

May I ask a silly question? Why the dynamic allocation?

Why not have a const-static single global page at the block-layer somewhere that will be used for all discard-type operations and be done with it once and for all. A single page can be used for any size bio , any number of concurrent discards, any ZERO needed operation. It can also be used by other operations like padding and others. In fact isn't there one for the libsata padding?

for UNMAP we need to write into the payload. And for ATA TRIM we need to write into the WRITE SAME payload.

OK, Thanks, I see. Is it one of these operations, (like we have in OSD) where the CDB information spills into the payload? like the scatter-gather and extent lists and such. Do we actually use a WRITE_SAME which is not zero? for what use?

Quoted 3 lines

That's another layering violation
for those looking for them, btw..

Agreed

#39 lore raw

Re: [PATCH 1/2] block: fix leaks associated with discard request payload

Quoted 3 lines · Boaz Harrosh

OK, Thanks, I see. Is it one of these operations, (like we have in OSD) where the CDB information spills into the payload? like the scatter-gather and extent lists and such.

For UNMAP the payload is a list of block number / length pairs, while the CDB itself doesn't contain any information like that. It's a rather awkward command.

Quoted 1 line

Do we actually use a WRITE_SAME which is not zero? for what use?

The kernel doesn't issue any WRITE SAME without the unmap bit set.

#40 lore raw

Re: [PATCH 1/2] block: fix leaks associated with discard request payload

Quoted 9 lines · Christoph Hellwig
Quoted 3 lines · Boaz Harrosh

OK, Thanks, I see. Is it one of these operations, (like we have in OSD) where the CDB information spills into the payload? like the scatter-gather and extent lists and such.

For UNMAP the payload is a list of block number / length pairs, while the CDB itself doesn't contain any information like that. It's a rather awkward command.

How big can that be? could we, maybe, use the sense_buffer, properly allocated already?

Quoted 3 lines
Quoted 1 line

Do we actually use a WRITE_SAME which is not zero? for what use?

The kernel doesn't issue any WRITE SAME without the unmap bit set.

So if the unmap bit is set then the page can just be zero, right?

I still think a static zero-page is a worth while optimization. And block-drivers can take care with special needs with a private mem_pool or something. For the discard-type user and generic block layer the page is just an implementation specific residue, No?

But don't mind me, I'm just babbling. Not that I'll do anything about it. Boaz

#41 lore raw

Re: [PATCH 1/2] block: fix leaks associated with discard request payload

Quoted 16 lines · Boaz Harrosh
Quoted 9 lines · Christoph Hellwig
Quoted 3 lines · Boaz Harrosh

OK, Thanks, I see. Is it one of these operations, (like we have in OSD) where the CDB information spills into the payload? like the scatter-gather and extent lists and such.

For UNMAP the payload is a list of block number / length pairs, while the CDB itself doesn't contain any information like that. It's a rather awkward command.

How big can that be? could we, maybe, use the sense_buffer, properly allocated already?

Quoted 2 lines
Quoted 1 line

Do we actually use a WRITE_SAME which is not zero? for what use?

Quoted 8 lines
Quoted 1 line

The kernel doesn't issue any WRITE SAME without the unmap bit set.

So if the unmap bit is set then the page can just be zero, right?

I still think a static zero-page is a worth while optimization. And block-drivers can take care with special needs with a private mem_pool or something. For the discard-type user and generic block layer the page is just an implementation specific residue, No?

Why should the block layer have any role in managing this page? Block layer doesn't care about it, SCSI does.

Mike

#42 lore raw

PATCH 2/2 block: defer the use of inline biovecs for discard requests

In reply to Mike Snitzer

Don't alloc discard bio with a biovec in blkdev_issue_discard. Doing so means bio_has_data() will not be true until the SCSI layer adds the payload to the discard request via blk_add_request_payload.

bio_{enable,disable}_inline_vecs are not expected to be widely used so they were exported using EXPORT_SYMBOL_GPL.

This patch avoids the need for the following VM accounting fix for
discards: http://lkml.org/lkml/2010/6/23/361
NOTE: Jens, you said you applied Tao Ma's fix but I cannot see it in your linux-2.6-block's for-next or for-2.6.36... as such I didn't revert it in this patch.

---
 block/blk-core.c    |    2 ++
 block/blk-lib.c     |    2 +-
 fs/bio.c            |   19 +++++++++++++++++--
 include/linux/bio.h |    3 +++
 4 files changed, 23 insertions(+), 3 deletions(-)
block/blk-core.c +2 -0
index 07925aa..457fc29 100644
@@ -1153,6 +1153,7 @@ void blk_add_request_payload(struct request *rq, struct page *page,
 {
 	struct bio *bio = rq->bio;
 
+	bio_enable_inline_vecs(bio);
 	bio->bi_io_vec->bv_page = page;
 	bio->bi_io_vec->bv_offset = 0;
 	bio->bi_io_vec->bv_len = len;
@@ -1187,6 +1188,7 @@ void blk_clear_request_payload(struct request *rq)
 
 	bio->bi_io_vec->bv_page = NULL;
 	bio->bi_io_vec->bv_len = 0;
+	bio_disable_inline_vecs(bio);
 }
 EXPORT_SYMBOL_GPL(blk_clear_request_payload);
 
block/blk-lib.c +1 -1
index e16185b..345a4b6 100644
@@ -54,7 +54,7 @@ int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
 		unsigned int max_discard_sectors =
 			min(q->limits.max_discard_sectors, UINT_MAX >> 9);
 
-		bio = bio_alloc(gfp_mask, 1);
+		bio = bio_alloc(gfp_mask, 0);
 		if (!bio) {
 			ret = -ENOMEM;
 			break;
fs/bio.c +17 -2
index 8abb2df..e47d0a6 100644
@@ -260,6 +260,21 @@ void bio_init(struct bio *bio)
 }
 EXPORT_SYMBOL(bio_init);
 
+void bio_enable_inline_vecs(struct bio *bio)
+{
+	bio->bi_flags |= BIO_POOL_NONE << BIO_POOL_OFFSET;
+	bio->bi_max_vecs = BIO_INLINE_VECS;
+	bio->bi_io_vec = bio->bi_inline_vecs;;
+}
+EXPORT_SYMBOL_GPL(bio_enable_inline_vecs);
+
+void bio_disable_inline_vecs(struct bio *bio)
+{
+	bio->bi_max_vecs = 0;
+	bio->bi_io_vec = NULL;
+}
+EXPORT_SYMBOL_GPL(bio_disable_inline_vecs);
+
 /**
  * bio_alloc_bioset - allocate a bio for I/O
  * @gfp_mask:   the GFP_ mask given to the slab allocator
@@ -293,8 +308,8 @@ struct bio *bio_alloc_bioset(gfp_t gfp_mask, int nr_iovecs, struct bio_set *bs)
 		goto out_set;
 
 	if (nr_iovecs <= BIO_INLINE_VECS) {
-		bvl = bio->bi_inline_vecs;
-		nr_iovecs = BIO_INLINE_VECS;
+		bio_enable_inline_vecs(bio);
+		return bio;
 	} else {
 		bvl = bvec_alloc_bs(gfp_mask, nr_iovecs, &idx, bs);
 		if (unlikely(!bvl))
include/linux/bio.h +3 -0
index 4d379c8..3135631 100644
@@ -403,6 +403,9 @@ extern struct bio *bio_clone(struct bio *, gfp_t);
 
 extern void bio_init(struct bio *);
 
+extern void bio_enable_inline_vecs(struct bio *);
+extern void bio_disable_inline_vecs(struct bio *);
+
 extern int bio_add_page(struct bio *, struct page *, unsigned int,unsigned int);
 extern int bio_add_pc_page(struct request_queue *, struct bio *, struct page *,
 			   unsigned int, unsigned int);
Signature · 1 line
1.6.5.rc2
#43 lore raw

Re: [PATCH 2/2] block: defer the use of inline biovecs for discard requests

Quoted 6 lines · Mike Snitzer

Don't alloc discard bio with a biovec in blkdev_issue_discard. Doing so means bio_has_data() will not be true until the SCSI layer adds the payload to the discard request via blk_add_request_payload.

bio_{enable,disable}_inline_vecs are not expected to be widely used so they were exported using EXPORT_SYMBOL_GPL.

Why do we need them exported at all? Also some comments on these functions would be useful.

Otherwise it looks good to me.

#44 lore raw

Re: [PATCH 2/2] block: defer the use of inline biovecs for discard requests

Quoted 9 lines · Christoph Hellwig
Quoted 6 lines · Mike Snitzer

Don't alloc discard bio with a biovec in blkdev_issue_discard. Doing so means bio_has_data() will not be true until the SCSI layer adds the payload to the discard request via blk_add_request_payload.

bio_{enable,disable}_inline_vecs are not expected to be widely used so they were exported using EXPORT_SYMBOL_GPL.

Why do we need them exported at all?

Hmm, good point!

Quoted 1 line

Also some comments on these functions would be useful.

OK.

Quoted 1 line

Otherwise it looks good to me.

Thanks, I'll get a v2 of this patch out.

#45 lore raw

PATCH v2 2/2 block: defer the use of inline biovecs for discard requests

In reply to Mike Snitzer

Don't alloc discard bio with a biovec in blkdev_issue_discard. Doing so means bio_has_data() will not be true until the SCSI layer adds the payload to the discard request via blk_add_request_payload.

This patch avoids the need for the following VM accounting fix for
discards: http://lkml.org/lkml/2010/6/23/361
NOTE: Jens, you said you applied Tao Ma's fix but I cannot see it in your linux-2.6-block's for-next or for-2.6.36... as such I didn't revert it in this patch.

---
 block/blk-core.c    |    2 ++
 block/blk-lib.c     |    2 +-
 fs/bio.c            |   25 +++++++++++++++++++++++--
 include/linux/bio.h |    3 +++
 4 files changed, 29 insertions(+), 3 deletions(-)
block/blk-core.c +2 -0
Index: linux-2.6-block/block/blk-core.c
===================================================================
@@ -1153,6 +1153,7 @@ void blk_add_request_payload(struct requ
 {
 	struct bio *bio = rq->bio;
 
+	bio_enable_inline_vecs(bio);
 	bio->bi_io_vec->bv_page = page;
 	bio->bi_io_vec->bv_offset = 0;
 	bio->bi_io_vec->bv_len = len;
@@ -1187,6 +1188,7 @@ void blk_clear_request_payload(struct re
 
 	bio->bi_io_vec->bv_page = NULL;
 	bio->bi_io_vec->bv_len = 0;
+	bio_disable_inline_vecs(bio);
 }
 EXPORT_SYMBOL_GPL(blk_clear_request_payload);
 
block/blk-lib.c +1 -1
Index: linux-2.6-block/block/blk-lib.c
===================================================================
@@ -54,7 +54,7 @@ int blkdev_issue_discard(struct block_de
 		unsigned int max_discard_sectors =
 			min(q->limits.max_discard_sectors, UINT_MAX >> 9);
 
-		bio = bio_alloc(gfp_mask, 1);
+		bio = bio_alloc(gfp_mask, 0);
 		if (!bio) {
 			ret = -ENOMEM;
 			break;
fs/bio.c +23 -2
Index: linux-2.6-block/fs/bio.c
===================================================================
@@ -261,6 +261,27 @@ void bio_init(struct bio *bio)
 EXPORT_SYMBOL(bio_init);
 
 /**
+ * bio_enable_inline_vecs - enable use of bio's inline iovecs
+ * @bio:   bio that will use its inline iovecs
+ */
+void bio_enable_inline_vecs(struct bio *bio)
+{
+	bio->bi_flags |= BIO_POOL_NONE << BIO_POOL_OFFSET;
+	bio->bi_max_vecs = BIO_INLINE_VECS;
+	bio->bi_io_vec = bio->bi_inline_vecs;;
+}
+
+/**
+ * bio_disable_inline_vecs - disable use of bio's inline iovecs
+ * @bio:   bio that will no longer use its inline iovecs
+ */
+void bio_disable_inline_vecs(struct bio *bio)
+{
+	bio->bi_max_vecs = 0;
+	bio->bi_io_vec = NULL;
+}
+
+/**
  * bio_alloc_bioset - allocate a bio for I/O
  * @gfp_mask:   the GFP_ mask given to the slab allocator
  * @nr_iovecs:	number of iovecs to pre-allocate
@@ -293,8 +314,8 @@ struct bio *bio_alloc_bioset(gfp_t gfp_m
 		goto out_set;
 
 	if (nr_iovecs <= BIO_INLINE_VECS) {
-		bvl = bio->bi_inline_vecs;
-		nr_iovecs = BIO_INLINE_VECS;
+		bio_enable_inline_vecs(bio);
+		return bio;
 	} else {
 		bvl = bvec_alloc_bs(gfp_mask, nr_iovecs, &idx, bs);
 		if (unlikely(!bvl))
include/linux/bio.h +3 -0
Index: linux-2.6-block/include/linux/bio.h
===================================================================
@@ -403,6 +403,9 @@ extern struct bio *bio_clone(struct bio 
 
 extern void bio_init(struct bio *);
 
+extern void bio_enable_inline_vecs(struct bio *);
+extern void bio_disable_inline_vecs(struct bio *);
+
 extern int bio_add_page(struct bio *, struct page *, unsigned int,unsigned int);
 extern int bio_add_pc_page(struct request_queue *, struct bio *, struct page *,
 			   unsigned int, unsigned int);
#47 lore raw

Re: [PATCH 2/2] block: defer the use of inline biovecs for discard requests

In reply to Mike Snitzer
Quoted 9 lines · On Sat, 26 Jun 2010 15:56:51 -0400 Mike Snitzer <[email protected]> wrote:

Don't alloc discard bio with a biovec in blkdev_issue_discard. Doing so means bio_has_data() will not be true until the SCSI layer adds the payload to the discard request via blk_add_request_payload.

bio_{enable,disable}_inline_vecs are not expected to be widely used so they were exported using EXPORT_SYMBOL_GPL.

This patch avoids the need for the following VM accounting fix for
discards: http://lkml.org/lkml/2010/6/23/361

Why do we need to avoid the above fix? Surely, the above fix is hacky but much simpler than this patch.

Quoted 11 lines

NOTE: Jens, you said you applied Tao Ma's fix but I cannot see it in your linux-2.6-block's for-next or for-2.6.36... as such I didn't revert it in this patch.

---
 block/blk-core.c    |    2 ++
 block/blk-lib.c     |    2 +-
 fs/bio.c            |   19 +++++++++++++++++--
 include/linux/bio.h |    3 +++
 4 files changed, 23 insertions(+), 3 deletions(-)
#48 lore raw

Re: [PATCH 2/2] block: defer the use of inline biovecs for discard requests

Quoted 7 lines · FUJITA Tomonori
Quoted 4 lines · On Sat, 26 Jun 2010 15:56:51 -0400 Mike Snitzer <[email protected]> wrote:

Don't alloc discard bio with a biovec in blkdev_issue_discard. Doing so means bio_has_data() will not be true until the SCSI layer adds the payload to the discard request via blk_add_request_payload.

Quoted 7 lines
Quoted 5 lines

bio_{enable,disable}_inline_vecs are not expected to be widely used so they were exported using EXPORT_SYMBOL_GPL.

This patch avoids the need for the following VM accounting fix for
discards: http://lkml.org/lkml/2010/6/23/361

Why do we need to avoid the above fix?

We don't _need_ to. We avoid the need for it as a side-effect of the cleanup that my patch provides.

Quoted 1 line

Surely, the above fix is hacky but much simpler than this patch.

My patch wasn't meant as an alternative to Tao Ma's patch. Again, it just obviates the need for it.

Your tolerance for "hacky" is difficult to understand. On the one-hand (PATCH 1/2) you have no tolerance for "hacky" fixes for leaks (that introduce a short-term SCSI layering violation). But in this case you're perfectly fine with BIO_RW_DISCARD special casing?

Mike

#49 lore raw

Re: [PATCH 2/2] block: defer the use of inline biovecs for discard requests

Quoted 21 lines · On Mon, 28 Jun 2010 08:29:55 -0400 Mike Snitzer <[email protected]> wrote:
Quoted 7 lines · FUJITA Tomonori
Quoted 4 lines · On Sat, 26 Jun 2010 15:56:51 -0400 Mike Snitzer <[email protected]> wrote:

Don't alloc discard bio with a biovec in blkdev_issue_discard. Doing so means bio_has_data() will not be true until the SCSI layer adds the payload to the discard request via blk_add_request_payload.

Quoted 7 lines
Quoted 5 lines

bio_{enable,disable}_inline_vecs are not expected to be widely used so they were exported using EXPORT_SYMBOL_GPL.

This patch avoids the need for the following VM accounting fix for
discards: http://lkml.org/lkml/2010/6/23/361

Why do we need to avoid the above fix?

We don't _need_ to. We avoid the need for it as a side-effect of the cleanup that my patch provides.

Quoted 8 lines
Quoted 1 line

Surely, the above fix is hacky but much simpler than this patch.

My patch wasn't meant as an alternative to Tao Ma's patch. Again, it just obviates the need for it.

Your tolerance for "hacky" is difficult to understand. On the one-hand (PATCH 1/2) you have no tolerance for "hacky" fixes for leaks (that introduce a short-term SCSI layering violation).

Sorry, if not clear enough.

- SCSI layering violation is bad.

- A 'short term' solution always turns out to be a long solution. We should have a clean solution from the start.

- Complicating the SCSI I/O completion is bad (already complicated enough).

...

And the 'leaks' bug is still in -next. No need to fix it in a hacky way. We can just drop it from -next.

Quoted 2 lines

But in this case
you're perfectly fine with BIO_RW_DISCARD special casing?

BIO_RW_DISCARD special is already everywhere in the block layer. I prefer to have the less. However as long as it's in the block layer, I can live with it. After all, that's the block layer thing.

At least, it looks much better this patch. This patch is really hacky (as Jens said).

#50 lore raw

Re: [PATCH 2/2] block: defer the use of inline biovecs for discard requests

Quoted 52 lines · FUJITA Tomonori
Quoted 21 lines · On Mon, 28 Jun 2010 08:29:55 -0400 Mike Snitzer <[email protected]> wrote:
Quoted 7 lines · FUJITA Tomonori
Quoted 4 lines · On Sat, 26 Jun 2010 15:56:51 -0400 Mike Snitzer <[email protected]> wrote:

Don't alloc discard bio with a biovec in blkdev_issue_discard. Doing so means bio_has_data() will not be true until the SCSI layer adds the payload to the discard request via blk_add_request_payload.

Quoted 7 lines
Quoted 5 lines

bio_{enable,disable}_inline_vecs are not expected to be widely used so they were exported using EXPORT_SYMBOL_GPL.

This patch avoids the need for the following VM accounting fix for
discards: http://lkml.org/lkml/2010/6/23/361

Why do we need to avoid the above fix?

We don't _need_ to. We avoid the need for it as a side-effect of the cleanup that my patch provides.

Quoted 8 lines
Quoted 1 line

Surely, the above fix is hacky but much simpler than this patch.

My patch wasn't meant as an alternative to Tao Ma's patch. Again, it just obviates the need for it.

Your tolerance for "hacky" is difficult to understand. On the one-hand (PATCH 1/2) you have no tolerance for "hacky" fixes for leaks (that introduce a short-term SCSI layering violation).

Sorry, if not clear enough.

- SCSI layering violation is bad.

- A 'short term' solution always turns out to be a long solution. We should have a clean solution from the start.

- Complicating the SCSI I/O completion is bad (already complicated enough).

...

And the 'leaks' bug is still in -next. No need to fix it in a hacky way. We can just drop it from -next.

Quoted 2 lines

But in this case
you're perfectly fine with BIO_RW_DISCARD special casing?

Quoted 6 lines

BIO_RW_DISCARD special is already everywhere in the block layer. I prefer to have the less. However as long as it's in the block layer, I can live with it. After all, that's the block layer thing.

At least, it looks much better this patch. This patch is really hacky (as Jens said).

Christoph more clearly conveyed the intent of my patch. Its focus was _not_ to eliminate the need for Tao Ma's vm accounting patch.

I was attempting to have the SCSI layer more comprehensively manage the allocation and use of biovec associated with the discard payload (that the SCSI layer was now also managing rather than relying on the block layer). It is as simple as that.

Berating me with "really hacky" critiques doesn't change the fact that both the block layer _and_ the SCSI layer need serious help on their implementation of discard support. The entirety of Linux's current discard support is "really hacky".

I think we can all agree on that; so if any good came of the discussion over the past 24 hours it is: we now know work is needed to make Linux's discard support more capable (select few knew this, but many more are aware of that fact now).

And the SCSI layer has a significant role in improving Linux's discard capabilities. So relying on all discard changes to be in the block layer isn't an option ;)

Regards,
Mike

maintainer
#51 lore raw

Re: [PATCH 2/2] block: defer the use of inline biovecs for discard requests

In reply to Mike Snitzer
Quoted 3 lines · Mike Snitzer

Don't alloc discard bio with a biovec in blkdev_issue_discard. Doing so means bio_has_data() will not be true until the SCSI layer adds the payload to the discard request via blk_add_request_payload.

Sorry, this looks horrible. What is the point of this?!

Quoted 2 lines

bio_{enable,disable}_inline_vecs are not expected to be widely used so they were exported using EXPORT_SYMBOL_GPL.

Never export anything that doesn't have an in-kernel modular user.

Quoted 5 lines

This patch avoids the need for the following VM accounting fix for
discards: http://lkml.org/lkml/2010/6/23/361
NOTE: Jens, you said you applied Tao Ma's fix but I cannot see it in your linux-2.6-block's for-next or for-2.6.36... as such I didn't revert it in this patch.

It's in the for-linus branch, that is stuff headed for the current kernel.

Signature · 1 line
Jens Axboe
#52 lore raw

Re: [PATCH 2/2] block: defer the use of inline biovecs for discard requests

Quoted 6 lines · Jens Axboe
Quoted 3 lines · Mike Snitzer

Don't alloc discard bio with a biovec in blkdev_issue_discard. Doing so means bio_has_data() will not be true until the SCSI layer adds the payload to the discard request via blk_add_request_payload.

Sorry, this looks horrible.

Your judgment isn't giving me much to work with... not sure where I go with "horrible".

Quoted 1 line

What is the point of this?!

Enables discard requests with _not_ return true for bio_has_data().

Quoted 4 lines
Quoted 2 lines

bio_{enable,disable}_inline_vecs are not expected to be widely used so they were exported using EXPORT_SYMBOL_GPL.

Never export anything that doesn't have an in-kernel modular user.

Yeap, v2 removed the exports.

Quoted 8 lines
Quoted 5 lines

This patch avoids the need for the following VM accounting fix for
discards: http://lkml.org/lkml/2010/6/23/361
NOTE: Jens, you said you applied Tao Ma's fix but I cannot see it in your linux-2.6-block's for-next or for-2.6.36... as such I didn't revert it in this patch.

It's in the for-linus branch, that is stuff headed for the current kernel.

OK.

Mike

maintainer
#53 lore raw

Re: [PATCH 2/2] block: defer the use of inline biovecs for discard requests

Quoted 4 lines · Mike Snitzer
Quoted 1 line · Jens Axboe

On 2010-06-26 21:56, Mike Snitzer wrote:

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

Don't alloc discard bio with a biovec in blkdev_issue_discard. Doing so means bio_has_data() will not be true until the SCSI layer adds the payload to the discard request via blk_add_request_payload.

Sorry, this looks horrible.

Your judgment isn't giving me much to work with... not sure where I go with "horrible".

The horrible part is working around that issue by fiddling with the assignment of the internal vec. THAT looks like a horrible solution to that problem.

How about just adding a check to bio_has_data() for non-zero bio->bi_vcnt?

Signature · 1 line
Jens Axboe
#54 lore raw

Re: [PATCH 2/2] block: defer the use of inline biovecs for discard requests

Quoted 6 lines · Jens Axboe

The horrible part is working around that issue by fiddling with the assignment of the internal vec. THAT looks like a horrible solution to that problem.

How about just adding a check to bio_has_data() for non-zero bio->bi_vcnt?

The question is how a discard request from the block layer should look like. With Mike's patch we have the same situation as for a barrier
request: absolutely no data transferred and no indicator of it. IHMO that's much better than any partially constructed request. And yes, that means enabling the payload later in the driver.

The other option would be to not reuse the request at all and just allocate a new request and use that from sd_prep_fn. That's what I tried to implement first, but I couldn't get it to work. Given all the issue we have with the current approach I'm almost tempted to try that again.

maintainer
#55 lore raw

Re: [PATCH 2/2] block: defer the use of inline biovecs for discard requests

Quoted 5 lines · Christoph Hellwig
Quoted 4 lines · Jens Axboe

The horrible part is working around that issue by fiddling with the assignment of the internal vec. THAT looks like a horrible solution to that problem.

Quoted 8 lines
Quoted 2 lines

How about just adding a check to bio_has_data() for non-zero bio->bi_vcnt?

The question is how a discard request from the block layer should look like. With Mike's patch we have the same situation as for a barrier
request: absolutely no data transferred and no indicator of it. IHMO that's much better than any partially constructed request. And yes, that means enabling the payload later in the driver.

With a barrier, it's more clear I think - if it carries data, then you account that. If it's an empty barrier, then there's nothing to account. There will be an impact on the io stream, but that is indicated in blktrace for instance.

Quoted 5 lines

The other option would be to not reuse the request at all and just allocate a new request and use that from sd_prep_fn. That's what I tried to implement first, but I couldn't get it to work. Given all the issue we have with the current approach I'm almost tempted to try that again.

That sounds way cleaner...

Signature · 1 line
Jens Axboe
#56 lore raw

Re: [PATCH 2/2] block: defer the use of inline biovecs for discard requests

In reply to Jens Axboe
Quoted 14 lines · Jens Axboe
Quoted 4 lines · Mike Snitzer
Quoted 1 line · Jens Axboe

On 2010-06-26 21:56, Mike Snitzer wrote:

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

Don't alloc discard bio with a biovec in blkdev_issue_discard. Doing so means bio_has_data() will not be true until the SCSI layer adds the payload to the discard request via blk_add_request_payload.

Sorry, this looks horrible.

Your judgment isn't giving me much to work with... not sure where I go with "horrible".

Quoted 6 lines

The horrible part is working around that issue by fiddling with the assignment of the internal vec. THAT looks like a horrible solution to that problem.

How about just adding a check to bio_has_data() for non-zero bio->bi_vcnt?

Sure, that works.