From: "Jan Beulich" Subject: [PATCH] linux/blkfront: use blk_rq_map_sg to generate ring entries Date: Wed, 04 Mar 2009 11:56:25 +0000 Message-ID: <49AE7A79.76E4.0078.0@novell.com> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Return-path: Content-Disposition: inline List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xensource.com Errors-To: xen-devel-bounces@lists.xensource.com To: xen-devel@lists.xensource.com List-Id: xen-devel@lists.xenproject.org From: Jens Axboe Subject: xen/blkfront: use blk_rq_map_sg to generate ring entries On occasion, the request will apparently have more segments than we fit into the ring. Jens says: > The second problem is that the block layer then appears to create one > too many segments, but from the dump it has rq->nr_phys_segments =3D=3D > BLKIF_MAX_SEGMENTS_PER_REQUEST. I suspect the latter is due to > xen-blkfront not handling the merging on its own. It should check that > the new page doesn't form part of the previous page. The > rq_for_each_segment() iterates all single bits in the request, not dma > segments. The "easiest" way to do this is to call blk_rq_map_sg() and > then iterate the mapped sg list. That will give you what you are > looking for. > Here's a test patch, compiles but otherwise untested. I spent more > time figuring out how to enable XEN than to code it up, so YMMV! > Probably the sg list wants to be put inside the ring and only > initialized on allocation, then you can get rid of the sg on stack and > sg_init_table() loop call in the function. I'll leave that, and the > testing, to you. [Moved sg array into info structure, and initialize once. -J] Signed-off-by: Jens Axboe Signed-off-by: Jeremy Fitzhardinge After testing on 2.6.27.19, made apply to 2.6.18 tree. Signed-off-by: Jan Beulich --- sle11-2009-03-04.orig/drivers/xen/blkfront/blkfront.c 2009-03-04 = 11:38:21.000000000 +0100 +++ sle11-2009-03-04/drivers/xen/blkfront/blkfront.c 2009-03-04 = 11:38:33.000000000 +0100 @@ -40,6 +40,7 @@ #include #include #include +#include #include #include #include @@ -232,6 +233,8 @@ static int setup_blkring(struct xenbus_d SHARED_RING_INIT(sring); FRONT_RING_INIT(&info->ring, sring, PAGE_SIZE); =20 + sg_init_table(info->sg, BLKIF_MAX_SEGMENTS_PER_REQUEST); + err =3D xenbus_grant_ring(dev, virt_to_mfn(info->ring.sring)); if (err < 0) { free_page((unsigned long)sring); @@ -593,13 +596,11 @@ static int blkif_queue_request(struct re struct blkfront_info *info =3D req->rq_disk->private_data; unsigned long buffer_mfn; blkif_request_t *ring_req; - struct bio *bio; - struct bio_vec *bvec; - int idx; unsigned long id; unsigned int fsect, lsect; - int ref; + int i, ref; grant_ref_t gref_head; + struct scatterlist *sg; =20 if (unlikely(info->connected !=3D BLKIF_STATE_CONNECTED)) return 1; @@ -629,14 +631,13 @@ static int blkif_queue_request(struct re if (blk_barrier_rq(req)) ring_req->operation =3D BLKIF_OP_WRITE_BARRIER; =20 - ring_req->nr_segments =3D 0; - rq_for_each_bio (bio, req) { - bio_for_each_segment (bvec, bio, idx) { - BUG_ON(ring_req->nr_segments - =3D=3D BLKIF_MAX_SEGMENTS_PER_REQUEST); - buffer_mfn =3D page_to_phys(bvec->bv_page) >> = PAGE_SHIFT; - fsect =3D bvec->bv_offset >> 9; - lsect =3D fsect + (bvec->bv_len >> 9) - 1; + ring_req->nr_segments =3D blk_rq_map_sg(req->q, req, info->sg); + BUG_ON(ring_req->nr_segments > BLKIF_MAX_SEGMENTS_PER_REQUEST); + for (i =3D 0; i < ring_req->nr_segments; ++i) { + sg =3D info->sg + i; + buffer_mfn =3D page_to_phys(sg->page) >> PAGE_SHIFT= ; + fsect =3D sg->offset >> 9; + lsect =3D fsect + (sg->length >> 9) - 1; /* install a grant reference. */ ref =3D gnttab_claim_grant_reference(&gref_head); BUG_ON(ref =3D=3D -ENOSPC); @@ -646,17 +648,12 @@ static int blkif_queue_request(struct re buffer_mfn, rq_data_dir(req) ? GTF_readonly : 0 ); =20 - info->shadow[id].frame[ring_req->nr_segments] =3D - mfn_to_pfn(buffer_mfn); - - ring_req->seg[ring_req->nr_segments] =3D + info->shadow[id].frame[i] =3D mfn_to_pfn(buffer_mfn= ); + ring_req->seg[i] =3D (struct blkif_request_segment) { .gref =3D ref, .first_sect =3D fsect, .last_sect =3D lsect }; - - ring_req->nr_segments++; - } } =20 info->ring.req_prod_pvt++; --- sle11-2009-03-04.orig/drivers/xen/blkfront/block.h 2009-03-04 = 11:38:21.000000000 +0100 +++ sle11-2009-03-04/drivers/xen/blkfront/block.h 2009-03-04 = 11:38:33.000000000 +0100 @@ -102,6 +102,7 @@ struct blkfront_info int connected; int ring_ref; blkif_front_ring_t ring; + struct scatterlist sg[BLKIF_MAX_SEGMENTS_PER_REQUEST]; unsigned int irq; struct xlbd_major_info *mi; request_queue_t *rq;