Chaining sg lists for big IO commands v6
Subsystems (4)
cover Chaining sg lists for big IO commands v6
Hi,
Round 6 of the sg chaining patch set. Changes since v5:
- Support for more architectures. It should now run and work on i386, x86-64, sparc, sparc64, ia64, and ppc (32 and 64-bit). I have only tested i386/x86-64/ppc64 myself.
- Large command support is now automatically enabled by the SCSI mid layer, if the arch supports sg chaining. The debug queue entry in /sys/block/<dev>/queue/max_segments still exists if you want to tweak it further. The default limit is now 2048 entries, or at least 8mb ios. Remember that you still need to tweak the soft limit to allow the block layer to build large commands, that is the max_sectors_kb variable in the same directory.
- Add kerneldoc documentation to scatterlist.h
It's solid here, so please give it a spin. I'd like to get some performance results as well, if someone feels inclined to do so.
arch/ia64/hp/common/sba_iommu.c | 14 - arch/ia64/hp/sim/simscsi.c | 23 +- arch/ia64/sn/pci/pci_dma.c | 11 - arch/powerpc/kernel/dma_64.c | 5 arch/powerpc/kernel/ibmebus.c | 11 - arch/powerpc/kernel/iommu.c | 18 + arch/powerpc/platforms/ps3/system-bus.c | 5 arch/sparc/kernel/ioport.c | 25 +- arch/sparc/mm/io-unit.c | 12 - arch/sparc/mm/iommu.c | 10 arch/sparc/mm/sun4c.c | 10 arch/sparc64/kernel/pci_iommu.c | 39 ++- arch/sparc64/kernel/pci_sun4v.c | 32 +- arch/sparc64/kernel/sbus.c | 44 ++-- arch/x86_64/kernel/pci-calgary.c | 25 +- arch/x86_64/kernel/pci-gart.c | 60 ++--- arch/x86_64/kernel/pci-nommu.c | 5 block/ll_rw_blk.c | 42 +++ crypto/digest.c | 2 crypto/scatterwalk.c | 2 crypto/scatterwalk.h | 2 drivers/ata/libata-core.c | 30 +- drivers/ide/cris/ide-cris.c | 3 drivers/ide/ide-dma.c | 2 drivers/ide/ide-taskfile.c | 17 + drivers/ide/mips/au1xxx-ide.c | 2 drivers/ide/pci/sgiioc4.c | 2 drivers/ide/ppc/pmac.c | 2 drivers/infiniband/hw/ipath/ipath_dma.c | 9 drivers/infiniband/ulp/iser/iser_memory.c | 75 +++--- drivers/infiniband/ulp/srp/ib_srp.c | 22 +- drivers/message/fusion/mptscsih.c | 4 drivers/scsi/3w-9xxx.c | 8 drivers/scsi/3w-xxxx.c | 8 drivers/scsi/53c700.c | 16 - drivers/scsi/BusLogic.c | 7 drivers/scsi/NCR53c406a.c | 18 - drivers/scsi/a100u2w.c | 9 drivers/scsi/aacraid/aachba.c | 29 +- drivers/scsi/advansys.c | 21 - drivers/scsi/aha1542.c | 21 - drivers/scsi/aha1740.c | 8 drivers/scsi/aic7xxx/aic79xx_osm.c | 3 drivers/scsi/aic7xxx/aic7xxx_osm.c | 12 - drivers/scsi/aic94xx/aic94xx_task.c | 6 drivers/scsi/arcmsr/arcmsr_hba.c | 11 - drivers/scsi/dc395x.c | 7 drivers/scsi/dpt_i2o.c | 13 - drivers/scsi/eata.c | 8 drivers/scsi/esp_scsi.c | 5 drivers/scsi/gdth.c | 45 ++-- drivers/scsi/hptiop.c | 8 drivers/scsi/ibmmca.c | 11 - drivers/scsi/ibmvscsi/ibmvscsi.c | 4 drivers/scsi/ide-scsi.c | 31 +- drivers/scsi/initio.c | 12 - drivers/scsi/ipr.c | 9 drivers/scsi/ips.c | 74 +++--- drivers/scsi/iscsi_tcp.c | 43 ++- drivers/scsi/jazz_esp.c | 27 +- drivers/scsi/lpfc/lpfc_scsi.c | 9 drivers/scsi/mac53c94.c | 9 drivers/scsi/megaraid.c | 13 - drivers/scsi/megaraid/megaraid_mbox.c | 7 drivers/scsi/megaraid/megaraid_sas.c | 16 - drivers/scsi/mesh.c | 12 - drivers/scsi/ncr53c8xx.c | 7 drivers/scsi/nsp32.c | 9 drivers/scsi/pcmcia/sym53c500_cs.c | 18 - drivers/scsi/qla1280.c | 66 +++--- drivers/scsi/qla2xxx/qla_iocb.c | 9 drivers/scsi/qla4xxx/ql4_iocb.c | 8 drivers/scsi/qlogicfas408.c | 9 drivers/scsi/qlogicpti.c | 15 - drivers/scsi/scsi_debug.c | 29 +- drivers/scsi/scsi_lib.c | 239 ++++++++++++++++------ drivers/scsi/scsi_tgt_lib.c | 4 drivers/scsi/sg.c | 16 - drivers/scsi/sym53c416.c | 9 drivers/scsi/sym53c8xx_2/sym_glue.c | 7 drivers/scsi/u14-34f.c | 10 drivers/scsi/ultrastor.c | 10 drivers/scsi/wd7000.c | 7 drivers/usb/storage/alauda.c | 16 - drivers/usb/storage/datafab.c | 10 drivers/usb/storage/jumpshot.c | 10 drivers/usb/storage/protocol.c | 20 + drivers/usb/storage/protocol.h | 2 drivers/usb/storage/sddr09.c | 16 - drivers/usb/storage/sddr55.c | 16 - drivers/usb/storage/shuttle_usbat.c | 17 - include/asm-i386/dma-mapping.h | 13 - include/asm-i386/scatterlist.h | 2 include/asm-ia64/dma-mapping.h | 1 include/asm-ia64/scatterlist.h | 2 include/asm-powerpc/dma-mapping.h | 2 include/asm-powerpc/scatterlist.h | 2 include/asm-sparc/scatterlist.h | 2 include/asm-sparc64/scatterlist.h | 2 include/asm-x86_64/dma-mapping.h | 3 include/asm-x86_64/scatterlist.h | 2 include/linux/i2o.h | 2 include/linux/ide.h | 2 include/linux/libata.h | 16 - include/linux/scatterlist.h | 84 +++++++ include/scsi/scsi.h | 7 include/scsi/scsi_cmnd.h | 3 lib/swiotlb.c | 19 + 108 files changed, 1104 insertions(+), 744 deletions(-)
PATCH 1/10 crypto: don't pollute the global namespace with sg_next()
It's a subsystem function, prefix it as such.
--- crypto/digest.c | 2 +- crypto/scatterwalk.c | 2 +- crypto/scatterwalk.h | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-)
crypto/digest.c +1 -1
@@ -77,7 +77,7 @@ static int update2(struct hash_desc *desc,if (!nbytes)break ; - sg =sg_next (sg); + sg =scatterwalk_sg_next (sg); }return 0 ;
crypto/scatterwalk.c +1 -1
@@ -70,7 +70,7 @@ static void scatterwalk_pagedone(struct scatter_walk *walk, int out, walk->offset +=PAGE_SIZE -1 ; walk->offset &=PAGE_MASK ;if (walk->offset >= walk->sg->offset + walk->sg->length) -scatterwalk_start (walk,sg_next (walk->sg)); +scatterwalk_start (walk,scatterwalk_sg_next (walk->sg)); } }
crypto/scatterwalk.h +1 -1
@@ -20,7 +20,7 @@#include "internal.h" -static inline struct scatterlist *sg_next (struct scatterlist *sg) +static inline struct scatterlist *scatterwalk_sg_next (struct scatterlist *sg) {return (++sg)->length ? sg : (void *)sg->page; }
Signature · 1 line
1.5.2.rc1
PATCH 2/10 Add sg helpers for iterating over a scatterlist table
In reply to Jens AxboeFirst step to being able to change the scatterlist setup without having to modify drivers (a lot :-)
--- include/linux/scatterlist.h | 9 +++++++++ 1 files changed, 9 insertions(+), 0 deletions(-)
include/linux/scatterlist.h +9 -0
@@ -20,4 +20,13 @@ static inline void sg_init_one(struct scatterlist *sg, const void *buf, sg_set_buf(sg, buf, buflen); } +#define sg_next (sg) ((sg) + 1) +#define sg_last (sg, nents) (&(sg[nents - 1])) + +/* +* Loop over each sg element, following the pointer to a new list if necessary +*/ +#define for_each_sg (sglist, sg, nr, __i) \ + for (__i = 0, sg = (sglist); __i < nr; __i++, sg = sg_next(sg)) +#endif /* _LINUX_SCATTERLIST_H */
Signature · 1 line
1.5.2.rc1
PATCH 3/10 libata: convert to using sg helpers
In reply to Jens AxboeThis converts libata to using the sg helpers for looking up sg elements, instead of doing it manually.
--- drivers/ata/libata-core.c | 30 ++++++++++++++++-------------- include/linux/libata.h | 16 ++++++++++------ 2 files changed, 26 insertions(+), 20 deletions(-)
drivers/ata/libata-core.c +16 -14
@@ -1368,7 +1368,7 @@ static void ata_qc_complete_internal(struct ata_queued_cmd *qc)*/ unsigned ata_exec_internal_sg (struct ata_device *dev,struct ata_taskfile *tf,const u8 *cdb, -int dma_dir,struct scatterlist *sg, +int dma_dir,struct scatterlist *sgl,unsigned int n_elem) {struct ata_port *ap = dev->ap;
@@ -1426,11 +1426,12 @@ unsigned ata_exec_internal_sg(struct ata_device *dev, qc->dma_dir = dma_dir;if (dma_dir !=DMA_NONE ) {unsigned int i, buflen =0 ; +struct scatterlist *sg; -for (i =0 ; i < n_elem; i++) - buflen += sg[i].length; +for_each_sg (sgl, sg, n_elem, i) + buflen += sg->length; -ata_sg_init (qc, sg, n_elem); +ata_sg_init (qc, sgl, n_elem); qc->nbytes = buflen; }
@@ -3980,7 +3981,7 @@ void ata_sg_clean(struct ata_queued_cmd *qc)if (qc->n_elem)dma_unmap_sg (ap->dev, sg, qc->n_elem, dir);/* restore last sg */ - sg[qc->orig_n_elem -1 ].length += qc->pad_len; +sg_last (sg, qc->orig_n_elem)->length += qc->pad_len;if (pad_buf) {struct scatterlist *psg = &qc->pad_sgent;void *addr =kmap_atomic (psg->page,KM_IRQ0 );
@@ -4139,6 +4140,7 @@ void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen) qc->orig_n_elem =1 ; qc->buf_virt = buf; qc->nbytes = buflen; + qc->cursg = qc->__sg;sg_init_one (&qc->sgent, buf, buflen); }
@@ -4164,6 +4166,7 @@ void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg, qc->__sg = sg; qc->n_elem = n_elem; qc->orig_n_elem = n_elem; + qc->cursg = qc->__sg; }/**
@@ -4253,7 +4256,7 @@ static int ata_sg_setup(struct ata_queued_cmd *qc) {struct ata_port *ap = qc->ap;struct scatterlist *sg = qc->__sg; -struct scatterlist *lsg = &sg[qc->n_elem -1 ]; +struct scatterlist *lsg =sg_last (qc->__sg, qc->n_elem);int n_elem, pre_n_elem, dir, trim_sg =0 ;VPRINTK ("ENTER, ata%u\n" , ap->print_id);
@@ -4417,7 +4420,6 @@ void ata_data_xfer_noirq(struct ata_device *adev, unsigned char *buf,static void ata_pio_sector (struct ata_queued_cmd *qc) {int do_write = (qc->tf.flags &ATA_TFLAG_WRITE ); -struct scatterlist *sg = qc->__sg;struct ata_port *ap = qc->ap;struct page *page;unsigned int offset;
@@ -4426,8 +4428,8 @@ static void ata_pio_sector(struct ata_queued_cmd *qc)if (qc->curbytes == qc->nbytes - qc->sect_size) ap->hsm_task_state =HSM_ST_LAST ; - page = sg[qc->cursg].page; - offset = sg[qc->cursg].offset + qc->cursg_ofs; + page = qc->cursg->page; + offset = qc->cursg->offset + qc->cursg_ofs;/* get the current page and offset */ page =nth_page (page, (offset >>PAGE_SHIFT ));
@@ -4455,8 +4457,8 @@ static void ata_pio_sector(struct ata_queued_cmd *qc) qc->curbytes += qc->sect_size; qc->cursg_ofs += qc->sect_size; -if (qc->cursg_ofs == (&sg[qc->cursg])->length) { - qc->cursg++; +if (qc->cursg_ofs == qc->cursg->length) { + qc->cursg =sg_next (qc->cursg); qc->cursg_ofs =0 ; } }
@@ -4549,7 +4551,7 @@ static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes) ap->hsm_task_state =HSM_ST_LAST ; next_sg: -if (unlikely (qc->cursg >= qc->n_elem)) { +if (unlikely (qc->cursg ==sg_last (qc->__sg, qc->n_elem))) {/* * The end of qc->sg is reached and the device expects * more data to transfer. In order not to overrun qc->sg
@@ -4572,7 +4574,7 @@ next_sg:return ; } - sg = &qc->__sg[qc->cursg]; + sg = qc->cursg; page = sg->page; offset = sg->offset + qc->cursg_ofs;
@@ -4611,7 +4613,7 @@ next_sg: qc->cursg_ofs += count;if (qc->cursg_ofs == sg->length) { - qc->cursg++; + qc->cursg =sg_next (qc->cursg); qc->cursg_ofs =0 ; }
include/linux/libata.h +10 -6
@@ -30,7 +30,7 @@#include <linux/interrupt.h> #include <linux/pci.h> #include <linux/dma-mapping.h> -#include <asm/scatterlist.h> +#include <linux/scatterlist.h> #include <linux/io.h> #include <linux/ata.h> #include <linux/workqueue.h>
@@ -388,6 +388,7 @@ struct ata_queued_cmd {unsigned long flags;/* ATA_QCFLAG_xxx */ unsigned int tag;unsigned int n_elem; +unsigned int n_iter;unsigned int orig_n_elem;int dma_dir;
@@ -398,7 +399,7 @@ struct ata_queued_cmd {unsigned int nbytes;unsigned int curbytes; -unsigned int cursg; +struct scatterlist *cursg;unsigned int cursg_ofs;struct scatterlist sgent;
@@ -935,7 +936,7 @@ ata_sg_is_last(struct scatterlist *sg, struct ata_queued_cmd *qc)return 1 ;if (qc->pad_len)return 0 ; -if (((sg - qc->__sg) +1 ) == qc->n_elem) +if (qc->n_iter == qc->n_elem)return 1 ;return 0 ; }
@@ -943,6 +944,7 @@ ata_sg_is_last(struct scatterlist *sg, struct ata_queued_cmd *qc)static inline struct scatterlist *ata_qc_first_sg (struct ata_queued_cmd *qc) { + qc->n_iter =0 ;if (qc->n_elem)return qc->__sg;if (qc->pad_len)
@@ -955,8 +957,8 @@ ata_qc_next_sg(struct scatterlist *sg, struct ata_queued_cmd *qc) {if (sg == &qc->pad_sgent)return NULL ; -if (++sg - qc->__sg < qc->n_elem) -return sg; +if (++qc->n_iter < qc->n_elem) +return sg_next (sg);if (qc->pad_len)return &qc->pad_sgent;return NULL ;
@@ -1157,9 +1159,11 @@ static inline void ata_qc_reinit(struct ata_queued_cmd *qc) qc->dma_dir =DMA_NONE ; qc->__sg =NULL ; qc->flags =0 ; - qc->cursg = qc->cursg_ofs =0 ; + qc->cursg =NULL ; + qc->cursg_ofs =0 ; qc->nbytes = qc->curbytes =0 ; qc->n_elem =0 ; + qc->n_iter =0 ; qc->err_mask =0 ; qc->pad_len =0 ; qc->sect_size =ATA_SECT_SIZE ;
Signature · 1 line
1.5.2.rc1
PATCH 4/10 block: convert to using sg helpers
In reply to Jens AxboeConvert the main rq mapper (blk_rq_map_sg()) to the sg helper setup.
--- block/ll_rw_blk.c | 19 ++++++++++++------- 1 files changed, 12 insertions(+), 7 deletions(-)
block/ll_rw_blk.c +12 -7
@@ -30,6 +30,7 @@#include <linux/cpu.h> #include <linux/blktrace_api.h> #include <linux/fault-inject.h> +#include <linux/scatterlist.h> /* * for max sense size
@@ -1307,9 +1308,11 @@ static int blk_hw_contig_segment(request_queue_t *q, struct bio *bio,* map a request to scatterlist, return number of sg entries setup. Caller * must make sure sg can hold rq->nr_phys_segments entries */ -int blk_rq_map_sg (request_queue_t *q,struct request *rq,struct scatterlist *sg) +int blk_rq_map_sg (request_queue_t *q,struct request *rq, +struct scatterlist *sglist) {struct bio_vec *bvec, *bvprv; +struct scatterlist *next_sg, *sg;struct bio *bio;int nsegs, i, cluster;
@@ -1320,6 +1323,7 @@ int blk_rq_map_sg(request_queue_t *q, struct request *rq, struct scatterlist *sg* for each bio in rq */ bvprv =NULL ; + sg = next_sg = &sglist[0 ];rq_for_each_bio (bio, rq) {/* * for each segment in bio
@@ -1328,7 +1332,7 @@ int blk_rq_map_sg(request_queue_t *q, struct request *rq, struct scatterlist *sgint nbytes = bvec->bv_len;if (bvprv && cluster) { -if (sg[nsegs -1 ].length + nbytes > q->max_segment_size) +if (sg->length + nbytes > q->max_segment_size) goto new_segment;if (!BIOVEC_PHYS_MERGEABLE (bvprv, bvec))
@@ -1336,14 +1340,15 @@ int blk_rq_map_sg(request_queue_t *q, struct request *rq, struct scatterlist *sgif (!BIOVEC_SEG_BOUNDARY (q, bvprv, bvec)) goto new_segment; - sg[nsegs -1 ].length += nbytes; + sg->length += nbytes; }else { new_segment: -memset (&sg[nsegs],0 ,sizeof (struct scatterlist )); - sg[nsegs].page = bvec->bv_page; - sg[nsegs].length = nbytes; - sg[nsegs].offset = bvec->bv_offset; + sg = next_sg; + next_sg =sg_next (sg); + sg->page = bvec->bv_page; + sg->length = nbytes; + sg->offset = bvec->bv_offset; nsegs++; } bvprv = bvec;
Signature · 1 line
1.5.2.rc1
PATCH 5/10 scsi: convert to using sg helpers
In reply to Jens AxboeThis converts the SCSI mid layer to using the sg helpers for looking up sg elements, instead of doing it manually.
--- drivers/scsi/scsi_lib.c | 9 +++++---- 1 files changed, 5 insertions(+), 4 deletions(-)
drivers/scsi/scsi_lib.c +5 -4
@@ -302,14 +302,15 @@ static int scsi_req_map_sg(struct request *rq, struct scatterlist *sgl,struct request_queue *q = rq->q;int nr_pages = (bufflen + sgl[0 ].offset +PAGE_SIZE -1 ) >>PAGE_SHIFT ;unsigned int data_len =0 , len, bytes, off; +struct scatterlist *sg;struct page *page;struct bio *bio =NULL ;int i, err, nr_vecs =0 ; -for (i =0 ; i < nsegs; i++) { - page = sgl[i].page; - off = sgl[i].offset; - len = sgl[i].length; +for_each_sg (sgl, sg, nsegs, i) { + page = sg->page; + off = sg->offset; + len = sg->length; data_len += len;while (len >0 ) {
Signature · 1 line
1.5.2.rc1
PATCH 6/10 i386 dma_map_sg: convert to using sg helpers
In reply to Jens AxboeThe dma mapping helpers need to be converted to using sg helpers as well, so they will work with a chained sglist setup.
--- include/asm-i386/dma-mapping.h | 13 +++++++------ 1 files changed, 7 insertions(+), 6 deletions(-)
include/asm-i386/dma-mapping.h +7 -6
@@ -2,10 +2,10 @@#define _ASM_I386_DMA_MAPPING_H#include <linux/mm.h> +#include <linux/scatterlist.h> #include <asm/cache.h> #include <asm/io.h> -#include <asm/scatterlist.h> #include <asm/bug.h> #define dma_alloc_noncoherent (d, s, h, f) dma_alloc_coherent(d, s, h, f)
@@ -35,18 +35,19 @@ dma_unmap_single(struct device *dev, dma_addr_t dma_addr, size_t size, }static inline int -dma_map_sg (struct device *dev,struct scatterlist *sg,int nents, +dma_map_sg (struct device *dev,struct scatterlist *sglist,int nents,enum dma_data_direction direction) { +struct scatterlist *sg;int i;BUG_ON (!valid_dma_direction (direction)); -WARN_ON (nents ==0 || sg[0 ].length ==0 ); +WARN_ON (nents ==0 || sglist[0 ].length ==0 ); -for (i =0 ; i < nents; i++ ) { -BUG_ON (!sg[i].page); +for_each_sg (sglist, sg, nents, i) { +BUG_ON (!sg->page); - sg[i].dma_address =page_to_phys (sg[i].page) + sg[i].offset; + sg->dma_address =page_to_phys (sg->page) + sg->offset; }flush_write_buffers ();
Signature · 1 line
1.5.2.rc1
PATCH 7/10 i386 sg: add support for chaining scatterlists
In reply to Jens AxboeThe core of the patch - allow the last sg element in a scatterlist table to point to the start of a new table. This adds a pointer to the sglist structure, and defines sg_chain_ptr() which the generic code can use to look it up.
--- include/asm-i386/scatterlist.h | 4 ++++ include/linux/scatterlist.h | 37 ++++++++++++++++++++++++++++++++++--- 2 files changed, 38 insertions(+), 3 deletions(-)
include/asm-i386/scatterlist.h +4 -0
@@ -8,8 +8,11 @@ struct scatterlist {unsigned int offset;dma_addr_t dma_address;unsigned int length; +struct scatterlist *next; }; +#define ARCH_HAS_SG_CHAIN +/* These macros should be used after a pci_map_sg call has been done * to get bus addresses of each of the SG entries and their lengths. * You should only work with the number of sg entries pci_map_sg
@@ -17,6 +20,7 @@ struct scatterlist {*/ #define sg_dma_address (sg) ((sg)->dma_address)#define sg_dma_len (sg) ((sg)->length) +#define sg_chain_ptr (sg) ((sg)->next)#define ISA_DMA_THRESHOLD (0x00ffffff)
include/linux/scatterlist.h +34 -3
@@ -20,13 +20,44 @@ static inline void sg_init_one(struct scatterlist *sg, const void *buf, sg_set_buf(sg, buf, buflen); } -#define sg_next (sg) ((sg) + 1) -#define sg_last (sg, nents) (&(sg[nents - 1])) -/* * Loop over each sg element, following the pointer to a new list if necessary */ #define for_each_sg (sglist, sg, nr, __i) \ for (__i = 0, sg = (sglist); __i < nr; __i++, sg = sg_next(sg)) +#ifdef ARCH_HAS_SG_CHAIN +#define sg_next (sg) (sg_chain_ptr((sg)) ? : (sg) + 1) +/* +* Chain previous sglist to this one +*/ +static inline void sg_chain (struct scatterlist *prv,unsigned int nents, +struct scatterlist *sgl) +{ +sg_chain_ptr (&prv[nents -1 ]) = sgl; +} + +/* +* We could improve this by passing in the maximum size of an sglist, so +* we could jump directly to the last table. That would eliminate this +* (potentially) lengthy scan. +*/ +static inline struct scatterlist *sg_last (struct scatterlist *sgl, +unsigned int nents) +{ +struct scatterlist *sg, *ret =NULL ; +int i; + +for_each_sg (sgl, sg, nents, i) + ret = sg; + +return ret; +} +#else +#define sg_next (sg) ((sg) + 1) +#define sg_chain (prv, nents, sgl) BUG() +#define sg_chain_ptr (sg) NULL +#define sg_last (sg, nents) (&(sg[nents - 1])) +#endif +#endif /* _LINUX_SCATTERLIST_H */
Signature · 1 line
1.5.2.rc1
PATCH 8/10 scsi: simplify scsi_free_sgtable()
In reply to Jens AxboeJust pass in the command, no point in passing in the scatterlist and scatterlist pool index seperately.
--- drivers/scsi/scsi_lib.c | 9 +++++---- drivers/scsi/scsi_tgt_lib.c | 4 ++-- include/scsi/scsi_cmnd.h | 2 +- 3 files changed, 8 insertions(+), 7 deletions(-)
drivers/scsi/scsi_lib.c +5 -4
@@ -745,13 +745,14 @@ struct scatterlist *scsi_alloc_sgtable(struct scsi_cmnd *cmd, gfp_t gfp_mask)EXPORT_SYMBOL (scsi_alloc_sgtable); -void scsi_free_sgtable (struct scatterlist *sgl,int index) +void scsi_free_sgtable (struct scsi_cmnd *cmd) { +struct scatterlist *sgl = cmd->request_buffer;struct scsi_host_sg_pool *sgp; -BUG_ON (index >=SG_MEMPOOL_NR ); +BUG_ON (cmd->sglist_len >=SG_MEMPOOL_NR ); - sgp = scsi_sg_pools + index; + sgp = scsi_sg_pools + cmd->sglist_len;mempool_free (sgl, sgp->pool); }
@@ -777,7 +778,7 @@ EXPORT_SYMBOL(scsi_free_sgtable);static void scsi_release_buffers (struct scsi_cmnd *cmd) {if (cmd->use_sg) -scsi_free_sgtable (cmd->request_buffer, cmd->sglist_len); +scsi_free_sgtable (cmd);/* * Zero these out. They now point to freed memory, and it is
drivers/scsi/scsi_tgt_lib.c +2 -2
@@ -329,7 +329,7 @@ static void scsi_tgt_cmd_done(struct scsi_cmnd *cmd)scsi_tgt_uspace_send_status (cmd, tcmd->tag);if (cmd->request_buffer) -scsi_free_sgtable (cmd->request_buffer, cmd->sglist_len); +scsi_free_sgtable (cmd);queue_work (scsi_tgtd, &tcmd->work); }
@@ -370,7 +370,7 @@ static int scsi_tgt_init_cmd(struct scsi_cmnd *cmd, gfp_t gfp_mask) }eprintk ("cmd %p cnt %d\n" , cmd, cmd->use_sg); -scsi_free_sgtable (cmd->request_buffer, cmd->sglist_len); +scsi_free_sgtable (cmd);return -EINVAL ; }
include/scsi/scsi_cmnd.h +1 -1
@@ -133,6 +133,6 @@ extern void *scsi_kmap_atomic_sg(struct scatterlist *sg, int sg_count,extern void scsi_kunmap_atomic_sg (void *virt);extern struct scatterlist *scsi_alloc_sgtable (struct scsi_cmnd *,gfp_t ); -extern void scsi_free_sgtable (struct scatterlist *,int ); +extern void scsi_free_sgtable (struct scsi_cmnd *);#endif /* _SCSI_SCSI_CMND_H */
Signature · 1 line
1.5.2.rc1
PATCH 9/10 SCSI: support for allocating large scatterlists
In reply to Jens AxboeThis is what enables large commands. If we need to allocate an sgtable that doesn't fit in a single page, allocate several SCSI_MAX_SG_SEGMENTS sized tables and chain them together.
We default to the safe setup of NOT chaining, for now.
--- drivers/scsi/scsi_lib.c | 194 +++++++++++++++++++++++++++++++++++----------- include/scsi/scsi.h | 7 -- include/scsi/scsi_cmnd.h | 1 + 3 files changed, 148 insertions(+), 54 deletions(-)
drivers/scsi/scsi_lib.c +147 -47
@@ -29,39 +29,26 @@#include "scsi_priv.h" #include "scsi_logging.h" +#include <linux/scatterlist.h> #define SG_MEMPOOL_NR ARRAY_SIZE(scsi_sg_pools)#define SG_MEMPOOL_SIZE 2struct scsi_host_sg_pool {size_t size; -char *name; +char *name;struct kmem_cache *slab;mempool_t *pool; }; -#if (SCSI_MAX_PHYS_SEGMENTS <32 ) -#error SCSI_MAX_PHYS_SEGMENTS is too small -#endif - -#define SP (x) { x, "sgpool-" #x } +#define SP (x) { x, "sgpool-" #x }static struct scsi_host_sg_pool scsi_sg_pools[] = {SP (8 ),SP (16 ),SP (32 ), -#if (SCSI_MAX_PHYS_SEGMENTS > 32)SP (64 ), -#if (SCSI_MAX_PHYS_SEGMENTS > 64)SP (128 ), -#if (SCSI_MAX_PHYS_SEGMENTS > 128) - SP(256), -#if (SCSI_MAX_PHYS_SEGMENTS > 256) -#error SCSI_MAX_PHYS_SEGMENTS is too large -#endif -#endif -#endif -#endif -}; +};#undef SPstatic void scsi_run_queue (struct request_queue *q);
@@ -702,45 +689,116 @@ static struct scsi_cmnd *scsi_end_request(struct scsi_cmnd *cmd, int uptodate,return NULL ; } -struct scatterlist *scsi_alloc_sgtable (struct scsi_cmnd *cmd,gfp_t gfp_mask) -{ -struct scsi_host_sg_pool *sgp; -struct scatterlist *sgl; +/* +* Should fit within a single page, and must be a power-of-2. +*/ +#define SCSI_MAX_SG_SEGMENTS 128 -BUG_ON (!cmd->use_sg); +static inline unsigned int scsi_sgtable_index (unsigned short nents) +{ +unsigned int index; -switch (cmd->use_sg) { +switch (nents) { case 1 ... 8: - cmd->sglist_len =0 ; + index =0 ;break ; case 9 ... 16: - cmd->sglist_len =1 ; + index =1 ;break ; case 17 ... 32: - cmd->sglist_len =2 ; + index =2 ;break ; -#if (SCSI_MAX_PHYS_SEGMENTS >32 ) case 33 ... 64: - cmd->sglist_len =3 ; + index =3 ;break ; -#if (SCSI_MAX_PHYS_SEGMENTS >64 ) - case 65 ... 128: - cmd->sglist_len =4 ; + case 65 ... SCSI_MAX_SG_SEGMENTS: + index =4 ;break ; -#if (SCSI_MAX_PHYS_SEGMENTS >128 ) - case 129 ... 256: - cmd->sglist_len =5 ; -break ; -#endif -#endif -#endif default : -return NULL ; +printk (KERN_ERR "scsi: bad segment count=%d\n" , nents); +BUG (); } - sgp = scsi_sg_pools + cmd->sglist_len; - sgl =mempool_alloc (sgp->pool, gfp_mask); -return sgl; +return index; +} + +struct scatterlist *scsi_alloc_sgtable (struct scsi_cmnd *cmd,gfp_t gfp_mask) +{ +struct scsi_host_sg_pool *sgp; +struct scatterlist *sgl, *prev, *ret; +unsigned int index; +int this, left; + +BUG_ON (!cmd->use_sg); + + left = cmd->use_sg; + ret = prev =NULL ; +do { + this = left; +if (this >SCSI_MAX_SG_SEGMENTS ) { + this =SCSI_MAX_SG_SEGMENTS ; + index =SG_MEMPOOL_NR -1 ; + }else + index =scsi_sgtable_index (this); + + left -= this; + + sgp = scsi_sg_pools + index; + + sgl =mempool_alloc (sgp->pool, gfp_mask); +if (unlikely (!sgl)) + goto enomem; + +memset (sgl,0 ,sizeof (*sgl) * sgp->size); + +/* +* first loop through, set initial index and return value +*/ +if (!ret) { + cmd->sglist_len = index; + ret = sgl; + } + +/* +* chain previous sglist, if any. we know the previous +* sglist must be the biggest one, or we would not have +* ended up doing another loop. +*/ +if (prev) +sg_chain (prev,SCSI_MAX_SG_SEGMENTS , sgl); + +/* +* don't allow subsequent mempool allocs to sleep, it would +* violate the mempool principle. +*/ + gfp_mask &= ~__GFP_WAIT; + prev = sgl; + }while (left); + +/* +* ->use_sg may get modified after dma mapping has potentially +* shrunk the number of segments, so keep a copy of it for free. +*/ + cmd->__use_sg = cmd->use_sg; +return ret; +enomem: +if (ret) { +/* +* Free entries chained off ret. Since we were trying to +* allocate another sglist, we know that all entries are of +* the max size. +*/ + sgp = scsi_sg_pools +SG_MEMPOOL_NR -1 ; + prev = &ret[SCSI_MAX_SG_SEGMENTS -1 ]; + +while ((sgl =sg_chain_ptr (ret)) !=NULL ) { + ret = &sgl[SCSI_MAX_SG_SEGMENTS -1 ]; +mempool_free (sgl, sgp->pool); + } + +mempool_free (prev, sgp->pool); + } +return NULL ; }EXPORT_SYMBOL (scsi_alloc_sgtable);
@@ -752,6 +810,42 @@ void scsi_free_sgtable(struct scsi_cmnd *cmd)BUG_ON (cmd->sglist_len >=SG_MEMPOOL_NR ); +/* +* if this is the biggest size sglist, check if we have +* chained parts we need to free +*/ +if (cmd->__use_sg >SCSI_MAX_SG_SEGMENTS ) { +unsigned short this, left; +struct scatterlist *next; +unsigned int index; + + left = cmd->__use_sg -SCSI_MAX_SG_SEGMENTS ; + next =sg_chain_ptr (&sgl[SCSI_MAX_SG_SEGMENTS -1 ]); +do { + sgl = next; + this = left; +if (this >SCSI_MAX_SG_SEGMENTS ) { + this =SCSI_MAX_SG_SEGMENTS ; + index =SG_MEMPOOL_NR -1 ; + }else + index =scsi_sgtable_index (this); + + left -= this; + + sgp = scsi_sg_pools + index; + +if (left) + next =sg_chain_ptr (&sgl[sgp->size -1 ]); + +mempool_free (sgl, sgp->pool); + }while (left); + +/* +* Restore original, will be freed below +*/ + sgl = cmd->request_buffer; + } + sgp = scsi_sg_pools + cmd->sglist_len;mempool_free (sgl, sgp->pool); }
@@ -993,7 +1087,6 @@ EXPORT_SYMBOL(scsi_io_completion);static int scsi_init_io (struct scsi_cmnd *cmd) {struct request *req = cmd->request; -struct scatterlist *sgpnt;int count;/*
@@ -1006,14 +1099,13 @@ static int scsi_init_io(struct scsi_cmnd *cmd)/* * If sg table allocation fails, requeue request later. */ - sgpnt =scsi_alloc_sgtable (cmd,GFP_ATOMIC ); -if (unlikely (!sgpnt)) { + cmd->request_buffer =scsi_alloc_sgtable (cmd,GFP_ATOMIC ); +if (unlikely (!cmd->request_buffer)) {scsi_unprep_request (req);return BLKPREP_DEFER ; } req->buffer =NULL ; - cmd->request_buffer = (char *) sgpnt;if (blk_pc_request (req)) cmd->request_bufflen = req->data_len;else
@@ -1577,8 +1669,16 @@ struct request_queue *__scsi_alloc_queue(struct Scsi_Host *shost,if (!q)return NULL ; +/* +* this limit is imposed by hardware restrictions +*/ blk_queue_max_hw_segments (q, shost->sg_tablesize); -blk_queue_max_phys_segments (q,SCSI_MAX_PHYS_SEGMENTS ); + +/* +* we can chain scatterlists, so this limit is fairly arbitrary +*/ +blk_queue_max_phys_segments (q,SCSI_MAX_SG_SEGMENTS ); +blk_queue_max_sectors (q, shost->max_sectors);blk_queue_bounce_limit (q,scsi_calculate_bounce_limit (shost));blk_queue_segment_boundary (q, shost->dma_boundary);
include/scsi/scsi.h +0 -7
@@ -11,13 +11,6 @@#include <linux/types.h> /* -* The maximum sg list length SCSI can cope with -* (currently must be a power of 2 between 32 and 256) -*/ -#define SCSI_MAX_PHYS_SEGMENTS MAX_PHYS_SEGMENTS - - -/* * SCSI command lengths */
include/scsi/scsi_cmnd.h +1 -0
@@ -72,6 +72,7 @@ struct scsi_cmnd {/* These elements define the operation we ultimately want to perform */ unsigned short use_sg;/* Number of pieces of scatter-gather */ unsigned short sglist_len;/* size of malloc'd scatter-gather list */ +unsigned short __use_sg;unsigned underflow;/* Return error if less than this amount is transferred */
Signature · 1 line
1.5.2.rc1
PATCH 10/10 ll_rw_blk: temporarily enable max_segments tweaking
In reply to Jens AxboeExpose this setting for now, so that users can play with enabling large commands without defaulting it to on globally.
--- block/ll_rw_blk.c | 22 ++++++++++++++++++++++ 1 files changed, 22 insertions(+), 0 deletions(-)
block/ll_rw_blk.c +22 -0
@@ -3931,7 +3931,22 @@ static ssize_t queue_max_hw_sectors_show(struct request_queue *q, char *page)return queue_var_show (max_hw_sectors_kb, (page)); } +static ssize_t queue_max_segments_show (struct request_queue *q,char *page) +{ +return queue_var_show (q->max_phys_segments, page); +} + +static ssize_t queue_max_segments_store (struct request_queue *q,const char *page,size_t count) +{ +unsigned long segments; +ssize_t ret =queue_var_store (&segments, page, count); +spin_lock_irq (q->queue_lock); + q->max_phys_segments = segments; +spin_unlock_irq (q->queue_lock); + +return ret; +}static struct queue_sysfs_entry queue_requests_entry = { .attr = {.name ="nr_requests" , .mode =S_IRUGO |S_IWUSR }, .show = queue_requests_show,
@@ -3955,6 +3970,12 @@ static struct queue_sysfs_entry queue_max_hw_sectors_entry = { .show = queue_max_hw_sectors_show, }; +static struct queue_sysfs_entry queue_max_segments_entry = { + .attr = {.name ="max_segments" , .mode =S_IRUGO |S_IWUSR }, + .show = queue_max_segments_show, + .store = queue_max_segments_store, +}; +static struct queue_sysfs_entry queue_iosched_entry = { .attr = {.name ="scheduler" , .mode =S_IRUGO |S_IWUSR }, .show = elv_iosched_show,
@@ -3966,6 +3987,7 @@ static struct attribute *default_attrs[] = { &queue_ra_entry.attr, &queue_max_hw_sectors_entry.attr, &queue_max_sectors_entry.attr, + &queue_max_segments_entry.attr, &queue_iosched_entry.attr, NULL, };
Signature · 1 line
1.5.2.rc1