10db9299fSJens Axboe /* 20db9299fSJens Axboe * Copyright (C) 2007 Jens Axboe <jens.axboe@oracle.com> 30db9299fSJens Axboe * 40db9299fSJens Axboe * Scatterlist handling helpers. 50db9299fSJens Axboe * 60db9299fSJens Axboe * This source code is licensed under the GNU General Public License, 70db9299fSJens Axboe * Version 2. See the file COPYING for more details. 80db9299fSJens Axboe */ 98bc3bcc9SPaul Gortmaker #include <linux/export.h> 105a0e3ad6STejun Heo #include <linux/slab.h> 110db9299fSJens Axboe #include <linux/scatterlist.h> 12b1adaf65SFUJITA Tomonori #include <linux/highmem.h> 13b94de9bbSChris Wilson #include <linux/kmemleak.h> 140db9299fSJens Axboe 150db9299fSJens Axboe /** 160db9299fSJens Axboe * sg_next - return the next scatterlist entry in a list 170db9299fSJens Axboe * @sg: The current sg entry 180db9299fSJens Axboe * 190db9299fSJens Axboe * Description: 200db9299fSJens Axboe * Usually the next entry will be @sg@ + 1, but if this sg element is part 210db9299fSJens Axboe * of a chained scatterlist, it could jump to the start of a new 220db9299fSJens Axboe * scatterlist array. 230db9299fSJens Axboe * 240db9299fSJens Axboe **/ 250db9299fSJens Axboe struct scatterlist *sg_next(struct scatterlist *sg) 260db9299fSJens Axboe { 270db9299fSJens Axboe #ifdef CONFIG_DEBUG_SG 280db9299fSJens Axboe BUG_ON(sg->sg_magic != SG_MAGIC); 290db9299fSJens Axboe #endif 300db9299fSJens Axboe if (sg_is_last(sg)) 310db9299fSJens Axboe return NULL; 320db9299fSJens Axboe 330db9299fSJens Axboe sg++; 340db9299fSJens Axboe if (unlikely(sg_is_chain(sg))) 350db9299fSJens Axboe sg = sg_chain_ptr(sg); 360db9299fSJens Axboe 370db9299fSJens Axboe return sg; 380db9299fSJens Axboe } 390db9299fSJens Axboe EXPORT_SYMBOL(sg_next); 400db9299fSJens Axboe 410db9299fSJens Axboe /** 422e484610SMaxim Levitsky * sg_nents - return total count of entries in scatterlist 432e484610SMaxim Levitsky * @sg: The scatterlist 442e484610SMaxim Levitsky * 452e484610SMaxim Levitsky * Description: 462e484610SMaxim Levitsky * Allows to know how many entries are in sg, taking into acount 472e484610SMaxim Levitsky * chaining as well 482e484610SMaxim Levitsky * 492e484610SMaxim Levitsky **/ 502e484610SMaxim Levitsky int sg_nents(struct scatterlist *sg) 512e484610SMaxim Levitsky { 52232f1b51SMaxim Levitsky int nents; 53232f1b51SMaxim Levitsky for (nents = 0; sg; sg = sg_next(sg)) 542e484610SMaxim Levitsky nents++; 552e484610SMaxim Levitsky return nents; 562e484610SMaxim Levitsky } 572e484610SMaxim Levitsky EXPORT_SYMBOL(sg_nents); 582e484610SMaxim Levitsky 592e484610SMaxim Levitsky 602e484610SMaxim Levitsky /** 610db9299fSJens Axboe * sg_last - return the last scatterlist entry in a list 620db9299fSJens Axboe * @sgl: First entry in the scatterlist 630db9299fSJens Axboe * @nents: Number of entries in the scatterlist 640db9299fSJens Axboe * 650db9299fSJens Axboe * Description: 660db9299fSJens Axboe * Should only be used casually, it (currently) scans the entire list 670db9299fSJens Axboe * to get the last entry. 680db9299fSJens Axboe * 690db9299fSJens Axboe * Note that the @sgl@ pointer passed in need not be the first one, 700db9299fSJens Axboe * the important bit is that @nents@ denotes the number of entries that 710db9299fSJens Axboe * exist from @sgl@. 720db9299fSJens Axboe * 730db9299fSJens Axboe **/ 740db9299fSJens Axboe struct scatterlist *sg_last(struct scatterlist *sgl, unsigned int nents) 750db9299fSJens Axboe { 760db9299fSJens Axboe #ifndef ARCH_HAS_SG_CHAIN 770db9299fSJens Axboe struct scatterlist *ret = &sgl[nents - 1]; 780db9299fSJens Axboe #else 790db9299fSJens Axboe struct scatterlist *sg, *ret = NULL; 800db9299fSJens Axboe unsigned int i; 810db9299fSJens Axboe 820db9299fSJens Axboe for_each_sg(sgl, sg, nents, i) 830db9299fSJens Axboe ret = sg; 840db9299fSJens Axboe 850db9299fSJens Axboe #endif 860db9299fSJens Axboe #ifdef CONFIG_DEBUG_SG 870db9299fSJens Axboe BUG_ON(sgl[0].sg_magic != SG_MAGIC); 880db9299fSJens Axboe BUG_ON(!sg_is_last(ret)); 890db9299fSJens Axboe #endif 900db9299fSJens Axboe return ret; 910db9299fSJens Axboe } 920db9299fSJens Axboe EXPORT_SYMBOL(sg_last); 930db9299fSJens Axboe 940db9299fSJens Axboe /** 950db9299fSJens Axboe * sg_init_table - Initialize SG table 960db9299fSJens Axboe * @sgl: The SG table 970db9299fSJens Axboe * @nents: Number of entries in table 980db9299fSJens Axboe * 990db9299fSJens Axboe * Notes: 1000db9299fSJens Axboe * If this is part of a chained sg table, sg_mark_end() should be 1010db9299fSJens Axboe * used only on the last table part. 1020db9299fSJens Axboe * 1030db9299fSJens Axboe **/ 1040db9299fSJens Axboe void sg_init_table(struct scatterlist *sgl, unsigned int nents) 1050db9299fSJens Axboe { 1060db9299fSJens Axboe memset(sgl, 0, sizeof(*sgl) * nents); 1070db9299fSJens Axboe #ifdef CONFIG_DEBUG_SG 1080db9299fSJens Axboe { 1090db9299fSJens Axboe unsigned int i; 1100db9299fSJens Axboe for (i = 0; i < nents; i++) 1110db9299fSJens Axboe sgl[i].sg_magic = SG_MAGIC; 1120db9299fSJens Axboe } 1130db9299fSJens Axboe #endif 1140db9299fSJens Axboe sg_mark_end(&sgl[nents - 1]); 1150db9299fSJens Axboe } 1160db9299fSJens Axboe EXPORT_SYMBOL(sg_init_table); 1170db9299fSJens Axboe 1180db9299fSJens Axboe /** 1190db9299fSJens Axboe * sg_init_one - Initialize a single entry sg list 1200db9299fSJens Axboe * @sg: SG entry 1210db9299fSJens Axboe * @buf: Virtual address for IO 1220db9299fSJens Axboe * @buflen: IO length 1230db9299fSJens Axboe * 1240db9299fSJens Axboe **/ 1250db9299fSJens Axboe void sg_init_one(struct scatterlist *sg, const void *buf, unsigned int buflen) 1260db9299fSJens Axboe { 1270db9299fSJens Axboe sg_init_table(sg, 1); 1280db9299fSJens Axboe sg_set_buf(sg, buf, buflen); 1290db9299fSJens Axboe } 1300db9299fSJens Axboe EXPORT_SYMBOL(sg_init_one); 1310db9299fSJens Axboe 1320db9299fSJens Axboe /* 1330db9299fSJens Axboe * The default behaviour of sg_alloc_table() is to use these kmalloc/kfree 1340db9299fSJens Axboe * helpers. 1350db9299fSJens Axboe */ 1360db9299fSJens Axboe static struct scatterlist *sg_kmalloc(unsigned int nents, gfp_t gfp_mask) 1370db9299fSJens Axboe { 138b94de9bbSChris Wilson if (nents == SG_MAX_SINGLE_ALLOC) { 139b94de9bbSChris Wilson /* 140b94de9bbSChris Wilson * Kmemleak doesn't track page allocations as they are not 141b94de9bbSChris Wilson * commonly used (in a raw form) for kernel data structures. 142b94de9bbSChris Wilson * As we chain together a list of pages and then a normal 143b94de9bbSChris Wilson * kmalloc (tracked by kmemleak), in order to for that last 144b94de9bbSChris Wilson * allocation not to become decoupled (and thus a 145b94de9bbSChris Wilson * false-positive) we need to inform kmemleak of all the 146b94de9bbSChris Wilson * intermediate allocations. 147b94de9bbSChris Wilson */ 148b94de9bbSChris Wilson void *ptr = (void *) __get_free_page(gfp_mask); 149b94de9bbSChris Wilson kmemleak_alloc(ptr, PAGE_SIZE, 1, gfp_mask); 150b94de9bbSChris Wilson return ptr; 151b94de9bbSChris Wilson } else 1520db9299fSJens Axboe return kmalloc(nents * sizeof(struct scatterlist), gfp_mask); 1530db9299fSJens Axboe } 1540db9299fSJens Axboe 1550db9299fSJens Axboe static void sg_kfree(struct scatterlist *sg, unsigned int nents) 1560db9299fSJens Axboe { 157b94de9bbSChris Wilson if (nents == SG_MAX_SINGLE_ALLOC) { 158b94de9bbSChris Wilson kmemleak_free(sg); 1590db9299fSJens Axboe free_page((unsigned long) sg); 160b94de9bbSChris Wilson } else 1610db9299fSJens Axboe kfree(sg); 1620db9299fSJens Axboe } 1630db9299fSJens Axboe 1640db9299fSJens Axboe /** 1650db9299fSJens Axboe * __sg_free_table - Free a previously mapped sg table 1660db9299fSJens Axboe * @table: The sg table header to use 1677cedb1f1SJames Bottomley * @max_ents: The maximum number of entries per single scatterlist 1680db9299fSJens Axboe * @free_fn: Free function 1690db9299fSJens Axboe * 1700db9299fSJens Axboe * Description: 1717cedb1f1SJames Bottomley * Free an sg table previously allocated and setup with 1727cedb1f1SJames Bottomley * __sg_alloc_table(). The @max_ents value must be identical to 1737cedb1f1SJames Bottomley * that previously used with __sg_alloc_table(). 1740db9299fSJens Axboe * 1750db9299fSJens Axboe **/ 1767cedb1f1SJames Bottomley void __sg_free_table(struct sg_table *table, unsigned int max_ents, 1777cedb1f1SJames Bottomley sg_free_fn *free_fn) 1780db9299fSJens Axboe { 1790db9299fSJens Axboe struct scatterlist *sgl, *next; 1800db9299fSJens Axboe 1810db9299fSJens Axboe if (unlikely(!table->sgl)) 1820db9299fSJens Axboe return; 1830db9299fSJens Axboe 1840db9299fSJens Axboe sgl = table->sgl; 1850db9299fSJens Axboe while (table->orig_nents) { 1860db9299fSJens Axboe unsigned int alloc_size = table->orig_nents; 1870db9299fSJens Axboe unsigned int sg_size; 1880db9299fSJens Axboe 1890db9299fSJens Axboe /* 1907cedb1f1SJames Bottomley * If we have more than max_ents segments left, 1910db9299fSJens Axboe * then assign 'next' to the sg table after the current one. 1920db9299fSJens Axboe * sg_size is then one less than alloc size, since the last 1930db9299fSJens Axboe * element is the chain pointer. 1940db9299fSJens Axboe */ 1957cedb1f1SJames Bottomley if (alloc_size > max_ents) { 1967cedb1f1SJames Bottomley next = sg_chain_ptr(&sgl[max_ents - 1]); 1977cedb1f1SJames Bottomley alloc_size = max_ents; 1980db9299fSJens Axboe sg_size = alloc_size - 1; 1990db9299fSJens Axboe } else { 2000db9299fSJens Axboe sg_size = alloc_size; 2010db9299fSJens Axboe next = NULL; 2020db9299fSJens Axboe } 2030db9299fSJens Axboe 2040db9299fSJens Axboe table->orig_nents -= sg_size; 2050db9299fSJens Axboe free_fn(sgl, alloc_size); 2060db9299fSJens Axboe sgl = next; 2070db9299fSJens Axboe } 2080db9299fSJens Axboe 2090db9299fSJens Axboe table->sgl = NULL; 2100db9299fSJens Axboe } 2110db9299fSJens Axboe EXPORT_SYMBOL(__sg_free_table); 2120db9299fSJens Axboe 2130db9299fSJens Axboe /** 2140db9299fSJens Axboe * sg_free_table - Free a previously allocated sg table 2150db9299fSJens Axboe * @table: The mapped sg table header 2160db9299fSJens Axboe * 2170db9299fSJens Axboe **/ 2180db9299fSJens Axboe void sg_free_table(struct sg_table *table) 2190db9299fSJens Axboe { 2207cedb1f1SJames Bottomley __sg_free_table(table, SG_MAX_SINGLE_ALLOC, sg_kfree); 2210db9299fSJens Axboe } 2220db9299fSJens Axboe EXPORT_SYMBOL(sg_free_table); 2230db9299fSJens Axboe 2240db9299fSJens Axboe /** 2250db9299fSJens Axboe * __sg_alloc_table - Allocate and initialize an sg table with given allocator 2260db9299fSJens Axboe * @table: The sg table header to use 2270db9299fSJens Axboe * @nents: Number of entries in sg list 2287cedb1f1SJames Bottomley * @max_ents: The maximum number of entries the allocator returns per call 2290db9299fSJens Axboe * @gfp_mask: GFP allocation mask 2300db9299fSJens Axboe * @alloc_fn: Allocator to use 2310db9299fSJens Axboe * 2327cedb1f1SJames Bottomley * Description: 2337cedb1f1SJames Bottomley * This function returns a @table @nents long. The allocator is 2347cedb1f1SJames Bottomley * defined to return scatterlist chunks of maximum size @max_ents. 2357cedb1f1SJames Bottomley * Thus if @nents is bigger than @max_ents, the scatterlists will be 2367cedb1f1SJames Bottomley * chained in units of @max_ents. 2377cedb1f1SJames Bottomley * 2380db9299fSJens Axboe * Notes: 2390db9299fSJens Axboe * If this function returns non-0 (eg failure), the caller must call 2400db9299fSJens Axboe * __sg_free_table() to cleanup any leftover allocations. 2410db9299fSJens Axboe * 2420db9299fSJens Axboe **/ 2437cedb1f1SJames Bottomley int __sg_alloc_table(struct sg_table *table, unsigned int nents, 2447cedb1f1SJames Bottomley unsigned int max_ents, gfp_t gfp_mask, 2450db9299fSJens Axboe sg_alloc_fn *alloc_fn) 2460db9299fSJens Axboe { 2470db9299fSJens Axboe struct scatterlist *sg, *prv; 2480db9299fSJens Axboe unsigned int left; 2490db9299fSJens Axboe 25027daabd9SDan Carpenter memset(table, 0, sizeof(*table)); 25127daabd9SDan Carpenter 25227daabd9SDan Carpenter if (nents == 0) 25327daabd9SDan Carpenter return -EINVAL; 2540db9299fSJens Axboe #ifndef ARCH_HAS_SG_CHAIN 2556fd59a83SNick Bowler if (WARN_ON_ONCE(nents > max_ents)) 2566fd59a83SNick Bowler return -EINVAL; 2570db9299fSJens Axboe #endif 2580db9299fSJens Axboe 2590db9299fSJens Axboe left = nents; 2600db9299fSJens Axboe prv = NULL; 2610db9299fSJens Axboe do { 2620db9299fSJens Axboe unsigned int sg_size, alloc_size = left; 2630db9299fSJens Axboe 2647cedb1f1SJames Bottomley if (alloc_size > max_ents) { 2657cedb1f1SJames Bottomley alloc_size = max_ents; 2660db9299fSJens Axboe sg_size = alloc_size - 1; 2670db9299fSJens Axboe } else 2680db9299fSJens Axboe sg_size = alloc_size; 2690db9299fSJens Axboe 2700db9299fSJens Axboe left -= sg_size; 2710db9299fSJens Axboe 2720db9299fSJens Axboe sg = alloc_fn(alloc_size, gfp_mask); 273edce6820SJeffrey Carlyle if (unlikely(!sg)) { 274edce6820SJeffrey Carlyle /* 275edce6820SJeffrey Carlyle * Adjust entry count to reflect that the last 276edce6820SJeffrey Carlyle * entry of the previous table won't be used for 277edce6820SJeffrey Carlyle * linkage. Without this, sg_kfree() may get 278edce6820SJeffrey Carlyle * confused. 279edce6820SJeffrey Carlyle */ 280edce6820SJeffrey Carlyle if (prv) 281edce6820SJeffrey Carlyle table->nents = ++table->orig_nents; 282edce6820SJeffrey Carlyle 2830db9299fSJens Axboe return -ENOMEM; 284edce6820SJeffrey Carlyle } 2850db9299fSJens Axboe 2860db9299fSJens Axboe sg_init_table(sg, alloc_size); 2870db9299fSJens Axboe table->nents = table->orig_nents += sg_size; 2880db9299fSJens Axboe 2890db9299fSJens Axboe /* 2900db9299fSJens Axboe * If this is the first mapping, assign the sg table header. 2910db9299fSJens Axboe * If this is not the first mapping, chain previous part. 2920db9299fSJens Axboe */ 2930db9299fSJens Axboe if (prv) 2947cedb1f1SJames Bottomley sg_chain(prv, max_ents, sg); 2950db9299fSJens Axboe else 2960db9299fSJens Axboe table->sgl = sg; 2970db9299fSJens Axboe 2980db9299fSJens Axboe /* 2990db9299fSJens Axboe * If no more entries after this one, mark the end 3000db9299fSJens Axboe */ 3010db9299fSJens Axboe if (!left) 3020db9299fSJens Axboe sg_mark_end(&sg[sg_size - 1]); 3030db9299fSJens Axboe 3040db9299fSJens Axboe prv = sg; 3050db9299fSJens Axboe } while (left); 3060db9299fSJens Axboe 3070db9299fSJens Axboe return 0; 3080db9299fSJens Axboe } 3090db9299fSJens Axboe EXPORT_SYMBOL(__sg_alloc_table); 3100db9299fSJens Axboe 3110db9299fSJens Axboe /** 3120db9299fSJens Axboe * sg_alloc_table - Allocate and initialize an sg table 3130db9299fSJens Axboe * @table: The sg table header to use 3140db9299fSJens Axboe * @nents: Number of entries in sg list 3150db9299fSJens Axboe * @gfp_mask: GFP allocation mask 3160db9299fSJens Axboe * 3170db9299fSJens Axboe * Description: 3180db9299fSJens Axboe * Allocate and initialize an sg table. If @nents@ is larger than 3190db9299fSJens Axboe * SG_MAX_SINGLE_ALLOC a chained sg table will be setup. 3200db9299fSJens Axboe * 3210db9299fSJens Axboe **/ 3220db9299fSJens Axboe int sg_alloc_table(struct sg_table *table, unsigned int nents, gfp_t gfp_mask) 3230db9299fSJens Axboe { 3240db9299fSJens Axboe int ret; 3250db9299fSJens Axboe 3267cedb1f1SJames Bottomley ret = __sg_alloc_table(table, nents, SG_MAX_SINGLE_ALLOC, 3277cedb1f1SJames Bottomley gfp_mask, sg_kmalloc); 3280db9299fSJens Axboe if (unlikely(ret)) 3297cedb1f1SJames Bottomley __sg_free_table(table, SG_MAX_SINGLE_ALLOC, sg_kfree); 3300db9299fSJens Axboe 3310db9299fSJens Axboe return ret; 3320db9299fSJens Axboe } 3330db9299fSJens Axboe EXPORT_SYMBOL(sg_alloc_table); 334b1adaf65SFUJITA Tomonori 335b1adaf65SFUJITA Tomonori /** 336efc42bc9STomasz Stanislawski * sg_alloc_table_from_pages - Allocate and initialize an sg table from 337efc42bc9STomasz Stanislawski * an array of pages 338efc42bc9STomasz Stanislawski * @sgt: The sg table header to use 339efc42bc9STomasz Stanislawski * @pages: Pointer to an array of page pointers 340efc42bc9STomasz Stanislawski * @n_pages: Number of pages in the pages array 341efc42bc9STomasz Stanislawski * @offset: Offset from start of the first page to the start of a buffer 342efc42bc9STomasz Stanislawski * @size: Number of valid bytes in the buffer (after offset) 343efc42bc9STomasz Stanislawski * @gfp_mask: GFP allocation mask 344efc42bc9STomasz Stanislawski * 345efc42bc9STomasz Stanislawski * Description: 346efc42bc9STomasz Stanislawski * Allocate and initialize an sg table from a list of pages. Contiguous 347efc42bc9STomasz Stanislawski * ranges of the pages are squashed into a single scatterlist node. A user 348efc42bc9STomasz Stanislawski * may provide an offset at a start and a size of valid data in a buffer 349efc42bc9STomasz Stanislawski * specified by the page array. The returned sg table is released by 350efc42bc9STomasz Stanislawski * sg_free_table. 351efc42bc9STomasz Stanislawski * 352efc42bc9STomasz Stanislawski * Returns: 353efc42bc9STomasz Stanislawski * 0 on success, negative error on failure 354efc42bc9STomasz Stanislawski */ 355efc42bc9STomasz Stanislawski int sg_alloc_table_from_pages(struct sg_table *sgt, 356efc42bc9STomasz Stanislawski struct page **pages, unsigned int n_pages, 357efc42bc9STomasz Stanislawski unsigned long offset, unsigned long size, 358efc42bc9STomasz Stanislawski gfp_t gfp_mask) 359efc42bc9STomasz Stanislawski { 360efc42bc9STomasz Stanislawski unsigned int chunks; 361efc42bc9STomasz Stanislawski unsigned int i; 362efc42bc9STomasz Stanislawski unsigned int cur_page; 363efc42bc9STomasz Stanislawski int ret; 364efc42bc9STomasz Stanislawski struct scatterlist *s; 365efc42bc9STomasz Stanislawski 366efc42bc9STomasz Stanislawski /* compute number of contiguous chunks */ 367efc42bc9STomasz Stanislawski chunks = 1; 368efc42bc9STomasz Stanislawski for (i = 1; i < n_pages; ++i) 369efc42bc9STomasz Stanislawski if (page_to_pfn(pages[i]) != page_to_pfn(pages[i - 1]) + 1) 370efc42bc9STomasz Stanislawski ++chunks; 371efc42bc9STomasz Stanislawski 372efc42bc9STomasz Stanislawski ret = sg_alloc_table(sgt, chunks, gfp_mask); 373efc42bc9STomasz Stanislawski if (unlikely(ret)) 374efc42bc9STomasz Stanislawski return ret; 375efc42bc9STomasz Stanislawski 376efc42bc9STomasz Stanislawski /* merging chunks and putting them into the scatterlist */ 377efc42bc9STomasz Stanislawski cur_page = 0; 378efc42bc9STomasz Stanislawski for_each_sg(sgt->sgl, s, sgt->orig_nents, i) { 379efc42bc9STomasz Stanislawski unsigned long chunk_size; 380efc42bc9STomasz Stanislawski unsigned int j; 381efc42bc9STomasz Stanislawski 382efc42bc9STomasz Stanislawski /* look for the end of the current chunk */ 383efc42bc9STomasz Stanislawski for (j = cur_page + 1; j < n_pages; ++j) 384efc42bc9STomasz Stanislawski if (page_to_pfn(pages[j]) != 385efc42bc9STomasz Stanislawski page_to_pfn(pages[j - 1]) + 1) 386efc42bc9STomasz Stanislawski break; 387efc42bc9STomasz Stanislawski 388efc42bc9STomasz Stanislawski chunk_size = ((j - cur_page) << PAGE_SHIFT) - offset; 389efc42bc9STomasz Stanislawski sg_set_page(s, pages[cur_page], min(size, chunk_size), offset); 390efc42bc9STomasz Stanislawski size -= chunk_size; 391efc42bc9STomasz Stanislawski offset = 0; 392efc42bc9STomasz Stanislawski cur_page = j; 393efc42bc9STomasz Stanislawski } 394efc42bc9STomasz Stanislawski 395efc42bc9STomasz Stanislawski return 0; 396efc42bc9STomasz Stanislawski } 397efc42bc9STomasz Stanislawski EXPORT_SYMBOL(sg_alloc_table_from_pages); 398efc42bc9STomasz Stanislawski 399a321e91bSImre Deak void __sg_page_iter_start(struct sg_page_iter *piter, 400a321e91bSImre Deak struct scatterlist *sglist, unsigned int nents, 401a321e91bSImre Deak unsigned long pgoffset) 402a321e91bSImre Deak { 403a321e91bSImre Deak piter->__pg_advance = 0; 404a321e91bSImre Deak piter->__nents = nents; 405a321e91bSImre Deak 406a321e91bSImre Deak piter->sg = sglist; 407a321e91bSImre Deak piter->sg_pgoffset = pgoffset; 408a321e91bSImre Deak } 409a321e91bSImre Deak EXPORT_SYMBOL(__sg_page_iter_start); 410a321e91bSImre Deak 411a321e91bSImre Deak static int sg_page_count(struct scatterlist *sg) 412a321e91bSImre Deak { 413a321e91bSImre Deak return PAGE_ALIGN(sg->offset + sg->length) >> PAGE_SHIFT; 414a321e91bSImre Deak } 415a321e91bSImre Deak 416a321e91bSImre Deak bool __sg_page_iter_next(struct sg_page_iter *piter) 417a321e91bSImre Deak { 418a321e91bSImre Deak if (!piter->__nents || !piter->sg) 419a321e91bSImre Deak return false; 420a321e91bSImre Deak 421a321e91bSImre Deak piter->sg_pgoffset += piter->__pg_advance; 422a321e91bSImre Deak piter->__pg_advance = 1; 423a321e91bSImre Deak 424a321e91bSImre Deak while (piter->sg_pgoffset >= sg_page_count(piter->sg)) { 425a321e91bSImre Deak piter->sg_pgoffset -= sg_page_count(piter->sg); 426a321e91bSImre Deak piter->sg = sg_next(piter->sg); 427a321e91bSImre Deak if (!--piter->__nents || !piter->sg) 428a321e91bSImre Deak return false; 429a321e91bSImre Deak } 430a321e91bSImre Deak 431a321e91bSImre Deak return true; 432a321e91bSImre Deak } 433a321e91bSImre Deak EXPORT_SYMBOL(__sg_page_iter_next); 434a321e91bSImre Deak 435efc42bc9STomasz Stanislawski /** 436137d3edbSTejun Heo * sg_miter_start - start mapping iteration over a sg list 437137d3edbSTejun Heo * @miter: sg mapping iter to be started 438137d3edbSTejun Heo * @sgl: sg list to iterate over 439137d3edbSTejun Heo * @nents: number of sg entries 440137d3edbSTejun Heo * 441137d3edbSTejun Heo * Description: 442137d3edbSTejun Heo * Starts mapping iterator @miter. 443137d3edbSTejun Heo * 444137d3edbSTejun Heo * Context: 445137d3edbSTejun Heo * Don't care. 446137d3edbSTejun Heo */ 447137d3edbSTejun Heo void sg_miter_start(struct sg_mapping_iter *miter, struct scatterlist *sgl, 448137d3edbSTejun Heo unsigned int nents, unsigned int flags) 449137d3edbSTejun Heo { 450137d3edbSTejun Heo memset(miter, 0, sizeof(struct sg_mapping_iter)); 451137d3edbSTejun Heo 4524225fc85SImre Deak __sg_page_iter_start(&miter->piter, sgl, nents, 0); 4536de7e356SSebastian Andrzej Siewior WARN_ON(!(flags & (SG_MITER_TO_SG | SG_MITER_FROM_SG))); 454137d3edbSTejun Heo miter->__flags = flags; 455137d3edbSTejun Heo } 456137d3edbSTejun Heo EXPORT_SYMBOL(sg_miter_start); 457137d3edbSTejun Heo 45811052004SAkinobu Mita static bool sg_miter_get_next_page(struct sg_mapping_iter *miter) 45911052004SAkinobu Mita { 46011052004SAkinobu Mita if (!miter->__remaining) { 46111052004SAkinobu Mita struct scatterlist *sg; 46211052004SAkinobu Mita unsigned long pgoffset; 46311052004SAkinobu Mita 46411052004SAkinobu Mita if (!__sg_page_iter_next(&miter->piter)) 46511052004SAkinobu Mita return false; 46611052004SAkinobu Mita 46711052004SAkinobu Mita sg = miter->piter.sg; 46811052004SAkinobu Mita pgoffset = miter->piter.sg_pgoffset; 46911052004SAkinobu Mita 47011052004SAkinobu Mita miter->__offset = pgoffset ? 0 : sg->offset; 47111052004SAkinobu Mita miter->__remaining = sg->offset + sg->length - 47211052004SAkinobu Mita (pgoffset << PAGE_SHIFT) - miter->__offset; 47311052004SAkinobu Mita miter->__remaining = min_t(unsigned long, miter->__remaining, 47411052004SAkinobu Mita PAGE_SIZE - miter->__offset); 47511052004SAkinobu Mita } 47611052004SAkinobu Mita 47711052004SAkinobu Mita return true; 47811052004SAkinobu Mita } 47911052004SAkinobu Mita 480137d3edbSTejun Heo /** 481df642ceaSAkinobu Mita * sg_miter_skip - reposition mapping iterator 482df642ceaSAkinobu Mita * @miter: sg mapping iter to be skipped 483df642ceaSAkinobu Mita * @offset: number of bytes to plus the current location 484df642ceaSAkinobu Mita * 485df642ceaSAkinobu Mita * Description: 486df642ceaSAkinobu Mita * Sets the offset of @miter to its current location plus @offset bytes. 487df642ceaSAkinobu Mita * If mapping iterator @miter has been proceeded by sg_miter_next(), this 488df642ceaSAkinobu Mita * stops @miter. 489df642ceaSAkinobu Mita * 490df642ceaSAkinobu Mita * Context: 491df642ceaSAkinobu Mita * Don't care if @miter is stopped, or not proceeded yet. 492df642ceaSAkinobu Mita * Otherwise, preemption disabled if the SG_MITER_ATOMIC is set. 493df642ceaSAkinobu Mita * 494df642ceaSAkinobu Mita * Returns: 495df642ceaSAkinobu Mita * true if @miter contains the valid mapping. false if end of sg 496df642ceaSAkinobu Mita * list is reached. 497df642ceaSAkinobu Mita */ 4980d6077f8SMing Lei bool sg_miter_skip(struct sg_mapping_iter *miter, off_t offset) 499df642ceaSAkinobu Mita { 500df642ceaSAkinobu Mita sg_miter_stop(miter); 501df642ceaSAkinobu Mita 502df642ceaSAkinobu Mita while (offset) { 503df642ceaSAkinobu Mita off_t consumed; 504df642ceaSAkinobu Mita 505df642ceaSAkinobu Mita if (!sg_miter_get_next_page(miter)) 506df642ceaSAkinobu Mita return false; 507df642ceaSAkinobu Mita 508df642ceaSAkinobu Mita consumed = min_t(off_t, offset, miter->__remaining); 509df642ceaSAkinobu Mita miter->__offset += consumed; 510df642ceaSAkinobu Mita miter->__remaining -= consumed; 511df642ceaSAkinobu Mita offset -= consumed; 512df642ceaSAkinobu Mita } 513df642ceaSAkinobu Mita 514df642ceaSAkinobu Mita return true; 515df642ceaSAkinobu Mita } 5160d6077f8SMing Lei EXPORT_SYMBOL(sg_miter_skip); 517df642ceaSAkinobu Mita 518df642ceaSAkinobu Mita /** 519137d3edbSTejun Heo * sg_miter_next - proceed mapping iterator to the next mapping 520137d3edbSTejun Heo * @miter: sg mapping iter to proceed 521137d3edbSTejun Heo * 522137d3edbSTejun Heo * Description: 5238290e2d2STejun Heo * Proceeds @miter to the next mapping. @miter should have been started 5248290e2d2STejun Heo * using sg_miter_start(). On successful return, @miter->page, 5258290e2d2STejun Heo * @miter->addr and @miter->length point to the current mapping. 526137d3edbSTejun Heo * 527137d3edbSTejun Heo * Context: 5288290e2d2STejun Heo * Preemption disabled if SG_MITER_ATOMIC. Preemption must stay disabled 5298290e2d2STejun Heo * till @miter is stopped. May sleep if !SG_MITER_ATOMIC. 530137d3edbSTejun Heo * 531137d3edbSTejun Heo * Returns: 532137d3edbSTejun Heo * true if @miter contains the next mapping. false if end of sg 533137d3edbSTejun Heo * list is reached. 534137d3edbSTejun Heo */ 535137d3edbSTejun Heo bool sg_miter_next(struct sg_mapping_iter *miter) 536137d3edbSTejun Heo { 537137d3edbSTejun Heo sg_miter_stop(miter); 538137d3edbSTejun Heo 5394225fc85SImre Deak /* 5404225fc85SImre Deak * Get to the next page if necessary. 5414225fc85SImre Deak * __remaining, __offset is adjusted by sg_miter_stop 5424225fc85SImre Deak */ 54311052004SAkinobu Mita if (!sg_miter_get_next_page(miter)) 54423c560a9STejun Heo return false; 5454225fc85SImre Deak 5462db76d7cSImre Deak miter->page = sg_page_iter_page(&miter->piter); 5474225fc85SImre Deak miter->consumed = miter->length = miter->__remaining; 548137d3edbSTejun Heo 549137d3edbSTejun Heo if (miter->__flags & SG_MITER_ATOMIC) 5504225fc85SImre Deak miter->addr = kmap_atomic(miter->page) + miter->__offset; 551137d3edbSTejun Heo else 5524225fc85SImre Deak miter->addr = kmap(miter->page) + miter->__offset; 553137d3edbSTejun Heo 554137d3edbSTejun Heo return true; 555137d3edbSTejun Heo } 556137d3edbSTejun Heo EXPORT_SYMBOL(sg_miter_next); 557137d3edbSTejun Heo 558137d3edbSTejun Heo /** 559137d3edbSTejun Heo * sg_miter_stop - stop mapping iteration 560137d3edbSTejun Heo * @miter: sg mapping iter to be stopped 561137d3edbSTejun Heo * 562137d3edbSTejun Heo * Description: 563137d3edbSTejun Heo * Stops mapping iterator @miter. @miter should have been started 564137d3edbSTejun Heo * started using sg_miter_start(). A stopped iteration can be 565137d3edbSTejun Heo * resumed by calling sg_miter_next() on it. This is useful when 566137d3edbSTejun Heo * resources (kmap) need to be released during iteration. 567137d3edbSTejun Heo * 568137d3edbSTejun Heo * Context: 5698290e2d2STejun Heo * Preemption disabled if the SG_MITER_ATOMIC is set. Don't care 5708290e2d2STejun Heo * otherwise. 571137d3edbSTejun Heo */ 572137d3edbSTejun Heo void sg_miter_stop(struct sg_mapping_iter *miter) 573137d3edbSTejun Heo { 574137d3edbSTejun Heo WARN_ON(miter->consumed > miter->length); 575137d3edbSTejun Heo 576137d3edbSTejun Heo /* drop resources from the last iteration */ 577137d3edbSTejun Heo if (miter->addr) { 578137d3edbSTejun Heo miter->__offset += miter->consumed; 5794225fc85SImre Deak miter->__remaining -= miter->consumed; 580137d3edbSTejun Heo 5813d77b50cSMing Lei if ((miter->__flags & SG_MITER_TO_SG) && 5823d77b50cSMing Lei !PageSlab(miter->page)) 5836de7e356SSebastian Andrzej Siewior flush_kernel_dcache_page(miter->page); 5846de7e356SSebastian Andrzej Siewior 585137d3edbSTejun Heo if (miter->__flags & SG_MITER_ATOMIC) { 5868290e2d2STejun Heo WARN_ON_ONCE(preemptible()); 587c3eede8eSCong Wang kunmap_atomic(miter->addr); 588137d3edbSTejun Heo } else 589f652c521SArjan van de Ven kunmap(miter->page); 590137d3edbSTejun Heo 591137d3edbSTejun Heo miter->page = NULL; 592137d3edbSTejun Heo miter->addr = NULL; 593137d3edbSTejun Heo miter->length = 0; 594137d3edbSTejun Heo miter->consumed = 0; 595137d3edbSTejun Heo } 596137d3edbSTejun Heo } 597137d3edbSTejun Heo EXPORT_SYMBOL(sg_miter_stop); 598137d3edbSTejun Heo 599137d3edbSTejun Heo /** 600b1adaf65SFUJITA Tomonori * sg_copy_buffer - Copy data between a linear buffer and an SG list 601b1adaf65SFUJITA Tomonori * @sgl: The SG list 602b1adaf65SFUJITA Tomonori * @nents: Number of SG entries 603b1adaf65SFUJITA Tomonori * @buf: Where to copy from 604b1adaf65SFUJITA Tomonori * @buflen: The number of bytes to copy 605df642ceaSAkinobu Mita * @skip: Number of bytes to skip before copying 606df642ceaSAkinobu Mita * @to_buffer: transfer direction (true == from an sg list to a 607df642ceaSAkinobu Mita * buffer, false == from a buffer to an sg list 608b1adaf65SFUJITA Tomonori * 609b1adaf65SFUJITA Tomonori * Returns the number of copied bytes. 610b1adaf65SFUJITA Tomonori * 611b1adaf65SFUJITA Tomonori **/ 612b1adaf65SFUJITA Tomonori static size_t sg_copy_buffer(struct scatterlist *sgl, unsigned int nents, 613df642ceaSAkinobu Mita void *buf, size_t buflen, off_t skip, 614df642ceaSAkinobu Mita bool to_buffer) 615b1adaf65SFUJITA Tomonori { 616137d3edbSTejun Heo unsigned int offset = 0; 617137d3edbSTejun Heo struct sg_mapping_iter miter; 61850bed2e2SFUJITA Tomonori unsigned long flags; 6196de7e356SSebastian Andrzej Siewior unsigned int sg_flags = SG_MITER_ATOMIC; 620b1adaf65SFUJITA Tomonori 6216de7e356SSebastian Andrzej Siewior if (to_buffer) 6226de7e356SSebastian Andrzej Siewior sg_flags |= SG_MITER_FROM_SG; 6236de7e356SSebastian Andrzej Siewior else 6246de7e356SSebastian Andrzej Siewior sg_flags |= SG_MITER_TO_SG; 6256de7e356SSebastian Andrzej Siewior 6266de7e356SSebastian Andrzej Siewior sg_miter_start(&miter, sgl, nents, sg_flags); 627b1adaf65SFUJITA Tomonori 628df642ceaSAkinobu Mita if (!sg_miter_skip(&miter, skip)) 629df642ceaSAkinobu Mita return false; 630df642ceaSAkinobu Mita 63150bed2e2SFUJITA Tomonori local_irq_save(flags); 63250bed2e2SFUJITA Tomonori 633137d3edbSTejun Heo while (sg_miter_next(&miter) && offset < buflen) { 634137d3edbSTejun Heo unsigned int len; 635b1adaf65SFUJITA Tomonori 636137d3edbSTejun Heo len = min(miter.length, buflen - offset); 637b1adaf65SFUJITA Tomonori 638b1adaf65SFUJITA Tomonori if (to_buffer) 639137d3edbSTejun Heo memcpy(buf + offset, miter.addr, len); 6406de7e356SSebastian Andrzej Siewior else 641137d3edbSTejun Heo memcpy(miter.addr, buf + offset, len); 642b1adaf65SFUJITA Tomonori 643137d3edbSTejun Heo offset += len; 644b1adaf65SFUJITA Tomonori } 645b1adaf65SFUJITA Tomonori 646137d3edbSTejun Heo sg_miter_stop(&miter); 647b1adaf65SFUJITA Tomonori 64850bed2e2SFUJITA Tomonori local_irq_restore(flags); 649137d3edbSTejun Heo return offset; 650b1adaf65SFUJITA Tomonori } 651b1adaf65SFUJITA Tomonori 652b1adaf65SFUJITA Tomonori /** 653b1adaf65SFUJITA Tomonori * sg_copy_from_buffer - Copy from a linear buffer to an SG list 654b1adaf65SFUJITA Tomonori * @sgl: The SG list 655b1adaf65SFUJITA Tomonori * @nents: Number of SG entries 656b1adaf65SFUJITA Tomonori * @buf: Where to copy from 657b1adaf65SFUJITA Tomonori * @buflen: The number of bytes to copy 658b1adaf65SFUJITA Tomonori * 659b1adaf65SFUJITA Tomonori * Returns the number of copied bytes. 660b1adaf65SFUJITA Tomonori * 661b1adaf65SFUJITA Tomonori **/ 662b1adaf65SFUJITA Tomonori size_t sg_copy_from_buffer(struct scatterlist *sgl, unsigned int nents, 663b1adaf65SFUJITA Tomonori void *buf, size_t buflen) 664b1adaf65SFUJITA Tomonori { 665df642ceaSAkinobu Mita return sg_copy_buffer(sgl, nents, buf, buflen, 0, false); 666b1adaf65SFUJITA Tomonori } 667b1adaf65SFUJITA Tomonori EXPORT_SYMBOL(sg_copy_from_buffer); 668b1adaf65SFUJITA Tomonori 669b1adaf65SFUJITA Tomonori /** 670b1adaf65SFUJITA Tomonori * sg_copy_to_buffer - Copy from an SG list to a linear buffer 671b1adaf65SFUJITA Tomonori * @sgl: The SG list 672b1adaf65SFUJITA Tomonori * @nents: Number of SG entries 673b1adaf65SFUJITA Tomonori * @buf: Where to copy to 674b1adaf65SFUJITA Tomonori * @buflen: The number of bytes to copy 675b1adaf65SFUJITA Tomonori * 676b1adaf65SFUJITA Tomonori * Returns the number of copied bytes. 677b1adaf65SFUJITA Tomonori * 678b1adaf65SFUJITA Tomonori **/ 679b1adaf65SFUJITA Tomonori size_t sg_copy_to_buffer(struct scatterlist *sgl, unsigned int nents, 680b1adaf65SFUJITA Tomonori void *buf, size_t buflen) 681b1adaf65SFUJITA Tomonori { 682df642ceaSAkinobu Mita return sg_copy_buffer(sgl, nents, buf, buflen, 0, true); 683b1adaf65SFUJITA Tomonori } 684b1adaf65SFUJITA Tomonori EXPORT_SYMBOL(sg_copy_to_buffer); 685df642ceaSAkinobu Mita 686df642ceaSAkinobu Mita /** 687df642ceaSAkinobu Mita * sg_pcopy_from_buffer - Copy from a linear buffer to an SG list 688df642ceaSAkinobu Mita * @sgl: The SG list 689df642ceaSAkinobu Mita * @nents: Number of SG entries 690df642ceaSAkinobu Mita * @buf: Where to copy from 691df642ceaSAkinobu Mita * @skip: Number of bytes to skip before copying 692df642ceaSAkinobu Mita * @buflen: The number of bytes to copy 693df642ceaSAkinobu Mita * 694df642ceaSAkinobu Mita * Returns the number of copied bytes. 695df642ceaSAkinobu Mita * 696df642ceaSAkinobu Mita **/ 697df642ceaSAkinobu Mita size_t sg_pcopy_from_buffer(struct scatterlist *sgl, unsigned int nents, 698df642ceaSAkinobu Mita void *buf, size_t buflen, off_t skip) 699df642ceaSAkinobu Mita { 700df642ceaSAkinobu Mita return sg_copy_buffer(sgl, nents, buf, buflen, skip, false); 701df642ceaSAkinobu Mita } 702df642ceaSAkinobu Mita EXPORT_SYMBOL(sg_pcopy_from_buffer); 703df642ceaSAkinobu Mita 704df642ceaSAkinobu Mita /** 705df642ceaSAkinobu Mita * sg_pcopy_to_buffer - Copy from an SG list to a linear buffer 706df642ceaSAkinobu Mita * @sgl: The SG list 707df642ceaSAkinobu Mita * @nents: Number of SG entries 708df642ceaSAkinobu Mita * @buf: Where to copy to 709df642ceaSAkinobu Mita * @skip: Number of bytes to skip before copying 710df642ceaSAkinobu Mita * @buflen: The number of bytes to copy 711df642ceaSAkinobu Mita * 712df642ceaSAkinobu Mita * Returns the number of copied bytes. 713df642ceaSAkinobu Mita * 714df642ceaSAkinobu Mita **/ 715df642ceaSAkinobu Mita size_t sg_pcopy_to_buffer(struct scatterlist *sgl, unsigned int nents, 716df642ceaSAkinobu Mita void *buf, size_t buflen, off_t skip) 717df642ceaSAkinobu Mita { 718df642ceaSAkinobu Mita return sg_copy_buffer(sgl, nents, buf, buflen, skip, true); 719df642ceaSAkinobu Mita } 720df642ceaSAkinobu Mita EXPORT_SYMBOL(sg_pcopy_to_buffer); 721