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 if (sg_is_last(sg)) 280db9299fSJens Axboe return NULL; 290db9299fSJens Axboe 300db9299fSJens Axboe sg++; 310db9299fSJens Axboe if (unlikely(sg_is_chain(sg))) 320db9299fSJens Axboe sg = sg_chain_ptr(sg); 330db9299fSJens Axboe 340db9299fSJens Axboe return sg; 350db9299fSJens Axboe } 360db9299fSJens Axboe EXPORT_SYMBOL(sg_next); 370db9299fSJens Axboe 380db9299fSJens Axboe /** 392e484610SMaxim Levitsky * sg_nents - return total count of entries in scatterlist 402e484610SMaxim Levitsky * @sg: The scatterlist 412e484610SMaxim Levitsky * 422e484610SMaxim Levitsky * Description: 432e484610SMaxim Levitsky * Allows to know how many entries are in sg, taking into acount 442e484610SMaxim Levitsky * chaining as well 452e484610SMaxim Levitsky * 462e484610SMaxim Levitsky **/ 472e484610SMaxim Levitsky int sg_nents(struct scatterlist *sg) 482e484610SMaxim Levitsky { 49232f1b51SMaxim Levitsky int nents; 50232f1b51SMaxim Levitsky for (nents = 0; sg; sg = sg_next(sg)) 512e484610SMaxim Levitsky nents++; 522e484610SMaxim Levitsky return nents; 532e484610SMaxim Levitsky } 542e484610SMaxim Levitsky EXPORT_SYMBOL(sg_nents); 552e484610SMaxim Levitsky 56cfaed10dSTom Lendacky /** 57cfaed10dSTom Lendacky * sg_nents_for_len - return total count of entries in scatterlist 58cfaed10dSTom Lendacky * needed to satisfy the supplied length 59cfaed10dSTom Lendacky * @sg: The scatterlist 60cfaed10dSTom Lendacky * @len: The total required length 61cfaed10dSTom Lendacky * 62cfaed10dSTom Lendacky * Description: 63cfaed10dSTom Lendacky * Determines the number of entries in sg that are required to meet 64cfaed10dSTom Lendacky * the supplied length, taking into acount chaining as well 65cfaed10dSTom Lendacky * 66cfaed10dSTom Lendacky * Returns: 67cfaed10dSTom Lendacky * the number of sg entries needed, negative error on failure 68cfaed10dSTom Lendacky * 69cfaed10dSTom Lendacky **/ 70cfaed10dSTom Lendacky int sg_nents_for_len(struct scatterlist *sg, u64 len) 71cfaed10dSTom Lendacky { 72cfaed10dSTom Lendacky int nents; 73cfaed10dSTom Lendacky u64 total; 74cfaed10dSTom Lendacky 75cfaed10dSTom Lendacky if (!len) 76cfaed10dSTom Lendacky return 0; 77cfaed10dSTom Lendacky 78cfaed10dSTom Lendacky for (nents = 0, total = 0; sg; sg = sg_next(sg)) { 79cfaed10dSTom Lendacky nents++; 80cfaed10dSTom Lendacky total += sg->length; 81cfaed10dSTom Lendacky if (total >= len) 82cfaed10dSTom Lendacky return nents; 83cfaed10dSTom Lendacky } 84cfaed10dSTom Lendacky 85cfaed10dSTom Lendacky return -EINVAL; 86cfaed10dSTom Lendacky } 87cfaed10dSTom Lendacky EXPORT_SYMBOL(sg_nents_for_len); 882e484610SMaxim Levitsky 892e484610SMaxim Levitsky /** 900db9299fSJens Axboe * sg_last - return the last scatterlist entry in a list 910db9299fSJens Axboe * @sgl: First entry in the scatterlist 920db9299fSJens Axboe * @nents: Number of entries in the scatterlist 930db9299fSJens Axboe * 940db9299fSJens Axboe * Description: 950db9299fSJens Axboe * Should only be used casually, it (currently) scans the entire list 960db9299fSJens Axboe * to get the last entry. 970db9299fSJens Axboe * 980db9299fSJens Axboe * Note that the @sgl@ pointer passed in need not be the first one, 990db9299fSJens Axboe * the important bit is that @nents@ denotes the number of entries that 1000db9299fSJens Axboe * exist from @sgl@. 1010db9299fSJens Axboe * 1020db9299fSJens Axboe **/ 1030db9299fSJens Axboe struct scatterlist *sg_last(struct scatterlist *sgl, unsigned int nents) 1040db9299fSJens Axboe { 1050db9299fSJens Axboe struct scatterlist *sg, *ret = NULL; 1060db9299fSJens Axboe unsigned int i; 1070db9299fSJens Axboe 1080db9299fSJens Axboe for_each_sg(sgl, sg, nents, i) 1090db9299fSJens Axboe ret = sg; 1100db9299fSJens Axboe 1110db9299fSJens Axboe BUG_ON(!sg_is_last(ret)); 1120db9299fSJens Axboe return ret; 1130db9299fSJens Axboe } 1140db9299fSJens Axboe EXPORT_SYMBOL(sg_last); 1150db9299fSJens Axboe 1160db9299fSJens Axboe /** 1170db9299fSJens Axboe * sg_init_table - Initialize SG table 1180db9299fSJens Axboe * @sgl: The SG table 1190db9299fSJens Axboe * @nents: Number of entries in table 1200db9299fSJens Axboe * 1210db9299fSJens Axboe * Notes: 1220db9299fSJens Axboe * If this is part of a chained sg table, sg_mark_end() should be 1230db9299fSJens Axboe * used only on the last table part. 1240db9299fSJens Axboe * 1250db9299fSJens Axboe **/ 1260db9299fSJens Axboe void sg_init_table(struct scatterlist *sgl, unsigned int nents) 1270db9299fSJens Axboe { 1280db9299fSJens Axboe memset(sgl, 0, sizeof(*sgl) * nents); 129f3851786SPrashant Bhole sg_init_marker(sgl, nents); 1300db9299fSJens Axboe } 1310db9299fSJens Axboe EXPORT_SYMBOL(sg_init_table); 1320db9299fSJens Axboe 1330db9299fSJens Axboe /** 1340db9299fSJens Axboe * sg_init_one - Initialize a single entry sg list 1350db9299fSJens Axboe * @sg: SG entry 1360db9299fSJens Axboe * @buf: Virtual address for IO 1370db9299fSJens Axboe * @buflen: IO length 1380db9299fSJens Axboe * 1390db9299fSJens Axboe **/ 1400db9299fSJens Axboe void sg_init_one(struct scatterlist *sg, const void *buf, unsigned int buflen) 1410db9299fSJens Axboe { 1420db9299fSJens Axboe sg_init_table(sg, 1); 1430db9299fSJens Axboe sg_set_buf(sg, buf, buflen); 1440db9299fSJens Axboe } 1450db9299fSJens Axboe EXPORT_SYMBOL(sg_init_one); 1460db9299fSJens Axboe 1470db9299fSJens Axboe /* 1480db9299fSJens Axboe * The default behaviour of sg_alloc_table() is to use these kmalloc/kfree 1490db9299fSJens Axboe * helpers. 1500db9299fSJens Axboe */ 1510db9299fSJens Axboe static struct scatterlist *sg_kmalloc(unsigned int nents, gfp_t gfp_mask) 1520db9299fSJens Axboe { 153b94de9bbSChris Wilson if (nents == SG_MAX_SINGLE_ALLOC) { 154b94de9bbSChris Wilson /* 155b94de9bbSChris Wilson * Kmemleak doesn't track page allocations as they are not 156b94de9bbSChris Wilson * commonly used (in a raw form) for kernel data structures. 157b94de9bbSChris Wilson * As we chain together a list of pages and then a normal 158b94de9bbSChris Wilson * kmalloc (tracked by kmemleak), in order to for that last 159b94de9bbSChris Wilson * allocation not to become decoupled (and thus a 160b94de9bbSChris Wilson * false-positive) we need to inform kmemleak of all the 161b94de9bbSChris Wilson * intermediate allocations. 162b94de9bbSChris Wilson */ 163b94de9bbSChris Wilson void *ptr = (void *) __get_free_page(gfp_mask); 164b94de9bbSChris Wilson kmemleak_alloc(ptr, PAGE_SIZE, 1, gfp_mask); 165b94de9bbSChris Wilson return ptr; 166b94de9bbSChris Wilson } else 1676da2ec56SKees Cook return kmalloc_array(nents, sizeof(struct scatterlist), 1686da2ec56SKees Cook gfp_mask); 1690db9299fSJens Axboe } 1700db9299fSJens Axboe 1710db9299fSJens Axboe static void sg_kfree(struct scatterlist *sg, unsigned int nents) 1720db9299fSJens Axboe { 173b94de9bbSChris Wilson if (nents == SG_MAX_SINGLE_ALLOC) { 174b94de9bbSChris Wilson kmemleak_free(sg); 1750db9299fSJens Axboe free_page((unsigned long) sg); 176b94de9bbSChris Wilson } else 1770db9299fSJens Axboe kfree(sg); 1780db9299fSJens Axboe } 1790db9299fSJens Axboe 1800db9299fSJens Axboe /** 1810db9299fSJens Axboe * __sg_free_table - Free a previously mapped sg table 1820db9299fSJens Axboe * @table: The sg table header to use 1837cedb1f1SJames Bottomley * @max_ents: The maximum number of entries per single scatterlist 184c53c6d6aSChristoph Hellwig * @skip_first_chunk: don't free the (preallocated) first scatterlist chunk 1850db9299fSJens Axboe * @free_fn: Free function 1860db9299fSJens Axboe * 1870db9299fSJens Axboe * Description: 1887cedb1f1SJames Bottomley * Free an sg table previously allocated and setup with 1897cedb1f1SJames Bottomley * __sg_alloc_table(). The @max_ents value must be identical to 1907cedb1f1SJames Bottomley * that previously used with __sg_alloc_table(). 1910db9299fSJens Axboe * 1920db9299fSJens Axboe **/ 1937cedb1f1SJames Bottomley void __sg_free_table(struct sg_table *table, unsigned int max_ents, 194c53c6d6aSChristoph Hellwig bool skip_first_chunk, sg_free_fn *free_fn) 1950db9299fSJens Axboe { 1960db9299fSJens Axboe struct scatterlist *sgl, *next; 1970db9299fSJens Axboe 1980db9299fSJens Axboe if (unlikely(!table->sgl)) 1990db9299fSJens Axboe return; 2000db9299fSJens Axboe 2010db9299fSJens Axboe sgl = table->sgl; 2020db9299fSJens Axboe while (table->orig_nents) { 2030db9299fSJens Axboe unsigned int alloc_size = table->orig_nents; 2040db9299fSJens Axboe unsigned int sg_size; 2050db9299fSJens Axboe 2060db9299fSJens Axboe /* 2077cedb1f1SJames Bottomley * If we have more than max_ents segments left, 2080db9299fSJens Axboe * then assign 'next' to the sg table after the current one. 2090db9299fSJens Axboe * sg_size is then one less than alloc size, since the last 2100db9299fSJens Axboe * element is the chain pointer. 2110db9299fSJens Axboe */ 2127cedb1f1SJames Bottomley if (alloc_size > max_ents) { 2137cedb1f1SJames Bottomley next = sg_chain_ptr(&sgl[max_ents - 1]); 2147cedb1f1SJames Bottomley alloc_size = max_ents; 2150db9299fSJens Axboe sg_size = alloc_size - 1; 2160db9299fSJens Axboe } else { 2170db9299fSJens Axboe sg_size = alloc_size; 2180db9299fSJens Axboe next = NULL; 2190db9299fSJens Axboe } 2200db9299fSJens Axboe 2210db9299fSJens Axboe table->orig_nents -= sg_size; 222c21e59d8STony Battersby if (skip_first_chunk) 223c53c6d6aSChristoph Hellwig skip_first_chunk = false; 224c21e59d8STony Battersby else 225c21e59d8STony Battersby free_fn(sgl, alloc_size); 2260db9299fSJens Axboe sgl = next; 2270db9299fSJens Axboe } 2280db9299fSJens Axboe 2290db9299fSJens Axboe table->sgl = NULL; 2300db9299fSJens Axboe } 2310db9299fSJens Axboe EXPORT_SYMBOL(__sg_free_table); 2320db9299fSJens Axboe 2330db9299fSJens Axboe /** 2340db9299fSJens Axboe * sg_free_table - Free a previously allocated sg table 2350db9299fSJens Axboe * @table: The mapped sg table header 2360db9299fSJens Axboe * 2370db9299fSJens Axboe **/ 2380db9299fSJens Axboe void sg_free_table(struct sg_table *table) 2390db9299fSJens Axboe { 240c53c6d6aSChristoph Hellwig __sg_free_table(table, SG_MAX_SINGLE_ALLOC, false, sg_kfree); 2410db9299fSJens Axboe } 2420db9299fSJens Axboe EXPORT_SYMBOL(sg_free_table); 2430db9299fSJens Axboe 2440db9299fSJens Axboe /** 2450db9299fSJens Axboe * __sg_alloc_table - Allocate and initialize an sg table with given allocator 2460db9299fSJens Axboe * @table: The sg table header to use 2470db9299fSJens Axboe * @nents: Number of entries in sg list 2487cedb1f1SJames Bottomley * @max_ents: The maximum number of entries the allocator returns per call 2490db9299fSJens Axboe * @gfp_mask: GFP allocation mask 2500db9299fSJens Axboe * @alloc_fn: Allocator to use 2510db9299fSJens Axboe * 2527cedb1f1SJames Bottomley * Description: 2537cedb1f1SJames Bottomley * This function returns a @table @nents long. The allocator is 2547cedb1f1SJames Bottomley * defined to return scatterlist chunks of maximum size @max_ents. 2557cedb1f1SJames Bottomley * Thus if @nents is bigger than @max_ents, the scatterlists will be 2567cedb1f1SJames Bottomley * chained in units of @max_ents. 2577cedb1f1SJames Bottomley * 2580db9299fSJens Axboe * Notes: 2590db9299fSJens Axboe * If this function returns non-0 (eg failure), the caller must call 2600db9299fSJens Axboe * __sg_free_table() to cleanup any leftover allocations. 2610db9299fSJens Axboe * 2620db9299fSJens Axboe **/ 2637cedb1f1SJames Bottomley int __sg_alloc_table(struct sg_table *table, unsigned int nents, 264c53c6d6aSChristoph Hellwig unsigned int max_ents, struct scatterlist *first_chunk, 265c53c6d6aSChristoph Hellwig gfp_t gfp_mask, sg_alloc_fn *alloc_fn) 2660db9299fSJens Axboe { 2670db9299fSJens Axboe struct scatterlist *sg, *prv; 2680db9299fSJens Axboe unsigned int left; 2690db9299fSJens Axboe 27027daabd9SDan Carpenter memset(table, 0, sizeof(*table)); 27127daabd9SDan Carpenter 27227daabd9SDan Carpenter if (nents == 0) 27327daabd9SDan Carpenter return -EINVAL; 2747c703e54SChristoph Hellwig #ifdef CONFIG_ARCH_NO_SG_CHAIN 2756fd59a83SNick Bowler if (WARN_ON_ONCE(nents > max_ents)) 2766fd59a83SNick Bowler return -EINVAL; 2770db9299fSJens Axboe #endif 2780db9299fSJens Axboe 2790db9299fSJens Axboe left = nents; 2800db9299fSJens Axboe prv = NULL; 2810db9299fSJens Axboe do { 2820db9299fSJens Axboe unsigned int sg_size, alloc_size = left; 2830db9299fSJens Axboe 2847cedb1f1SJames Bottomley if (alloc_size > max_ents) { 2857cedb1f1SJames Bottomley alloc_size = max_ents; 2860db9299fSJens Axboe sg_size = alloc_size - 1; 2870db9299fSJens Axboe } else 2880db9299fSJens Axboe sg_size = alloc_size; 2890db9299fSJens Axboe 2900db9299fSJens Axboe left -= sg_size; 2910db9299fSJens Axboe 292c53c6d6aSChristoph Hellwig if (first_chunk) { 293c53c6d6aSChristoph Hellwig sg = first_chunk; 294c53c6d6aSChristoph Hellwig first_chunk = NULL; 295c53c6d6aSChristoph Hellwig } else { 2960db9299fSJens Axboe sg = alloc_fn(alloc_size, gfp_mask); 297c53c6d6aSChristoph Hellwig } 298edce6820SJeffrey Carlyle if (unlikely(!sg)) { 299edce6820SJeffrey Carlyle /* 300edce6820SJeffrey Carlyle * Adjust entry count to reflect that the last 301edce6820SJeffrey Carlyle * entry of the previous table won't be used for 302edce6820SJeffrey Carlyle * linkage. Without this, sg_kfree() may get 303edce6820SJeffrey Carlyle * confused. 304edce6820SJeffrey Carlyle */ 305edce6820SJeffrey Carlyle if (prv) 306edce6820SJeffrey Carlyle table->nents = ++table->orig_nents; 307edce6820SJeffrey Carlyle 3080db9299fSJens Axboe return -ENOMEM; 309edce6820SJeffrey Carlyle } 3100db9299fSJens Axboe 3110db9299fSJens Axboe sg_init_table(sg, alloc_size); 3120db9299fSJens Axboe table->nents = table->orig_nents += sg_size; 3130db9299fSJens Axboe 3140db9299fSJens Axboe /* 3150db9299fSJens Axboe * If this is the first mapping, assign the sg table header. 3160db9299fSJens Axboe * If this is not the first mapping, chain previous part. 3170db9299fSJens Axboe */ 3180db9299fSJens Axboe if (prv) 3197cedb1f1SJames Bottomley sg_chain(prv, max_ents, sg); 3200db9299fSJens Axboe else 3210db9299fSJens Axboe table->sgl = sg; 3220db9299fSJens Axboe 3230db9299fSJens Axboe /* 3240db9299fSJens Axboe * If no more entries after this one, mark the end 3250db9299fSJens Axboe */ 3260db9299fSJens Axboe if (!left) 3270db9299fSJens Axboe sg_mark_end(&sg[sg_size - 1]); 3280db9299fSJens Axboe 3290db9299fSJens Axboe prv = sg; 3300db9299fSJens Axboe } while (left); 3310db9299fSJens Axboe 3320db9299fSJens Axboe return 0; 3330db9299fSJens Axboe } 3340db9299fSJens Axboe EXPORT_SYMBOL(__sg_alloc_table); 3350db9299fSJens Axboe 3360db9299fSJens Axboe /** 3370db9299fSJens Axboe * sg_alloc_table - Allocate and initialize an sg table 3380db9299fSJens Axboe * @table: The sg table header to use 3390db9299fSJens Axboe * @nents: Number of entries in sg list 3400db9299fSJens Axboe * @gfp_mask: GFP allocation mask 3410db9299fSJens Axboe * 3420db9299fSJens Axboe * Description: 3430db9299fSJens Axboe * Allocate and initialize an sg table. If @nents@ is larger than 3440db9299fSJens Axboe * SG_MAX_SINGLE_ALLOC a chained sg table will be setup. 3450db9299fSJens Axboe * 3460db9299fSJens Axboe **/ 3470db9299fSJens Axboe int sg_alloc_table(struct sg_table *table, unsigned int nents, gfp_t gfp_mask) 3480db9299fSJens Axboe { 3490db9299fSJens Axboe int ret; 3500db9299fSJens Axboe 3517cedb1f1SJames Bottomley ret = __sg_alloc_table(table, nents, SG_MAX_SINGLE_ALLOC, 352c53c6d6aSChristoph Hellwig NULL, gfp_mask, sg_kmalloc); 3530db9299fSJens Axboe if (unlikely(ret)) 354c53c6d6aSChristoph Hellwig __sg_free_table(table, SG_MAX_SINGLE_ALLOC, false, sg_kfree); 3550db9299fSJens Axboe 3560db9299fSJens Axboe return ret; 3570db9299fSJens Axboe } 3580db9299fSJens Axboe EXPORT_SYMBOL(sg_alloc_table); 359b1adaf65SFUJITA Tomonori 360b1adaf65SFUJITA Tomonori /** 36189d8589cSTvrtko Ursulin * __sg_alloc_table_from_pages - Allocate and initialize an sg table from 362efc42bc9STomasz Stanislawski * an array of pages 363efc42bc9STomasz Stanislawski * @sgt: The sg table header to use 364efc42bc9STomasz Stanislawski * @pages: Pointer to an array of page pointers 365efc42bc9STomasz Stanislawski * @n_pages: Number of pages in the pages array 366efc42bc9STomasz Stanislawski * @offset: Offset from start of the first page to the start of a buffer 367efc42bc9STomasz Stanislawski * @size: Number of valid bytes in the buffer (after offset) 36889d8589cSTvrtko Ursulin * @max_segment: Maximum size of a scatterlist node in bytes (page aligned) 369efc42bc9STomasz Stanislawski * @gfp_mask: GFP allocation mask 370efc42bc9STomasz Stanislawski * 371efc42bc9STomasz Stanislawski * Description: 372efc42bc9STomasz Stanislawski * Allocate and initialize an sg table from a list of pages. Contiguous 37389d8589cSTvrtko Ursulin * ranges of the pages are squashed into a single scatterlist node up to the 37489d8589cSTvrtko Ursulin * maximum size specified in @max_segment. An user may provide an offset at a 37589d8589cSTvrtko Ursulin * start and a size of valid data in a buffer specified by the page array. 37689d8589cSTvrtko Ursulin * The returned sg table is released by sg_free_table. 377efc42bc9STomasz Stanislawski * 378efc42bc9STomasz Stanislawski * Returns: 379efc42bc9STomasz Stanislawski * 0 on success, negative error on failure 380efc42bc9STomasz Stanislawski */ 38189d8589cSTvrtko Ursulin int __sg_alloc_table_from_pages(struct sg_table *sgt, struct page **pages, 38289d8589cSTvrtko Ursulin unsigned int n_pages, unsigned int offset, 38389d8589cSTvrtko Ursulin unsigned long size, unsigned int max_segment, 384efc42bc9STomasz Stanislawski gfp_t gfp_mask) 385efc42bc9STomasz Stanislawski { 386c125906bSTvrtko Ursulin unsigned int chunks, cur_page, seg_len, i; 387efc42bc9STomasz Stanislawski int ret; 388efc42bc9STomasz Stanislawski struct scatterlist *s; 389efc42bc9STomasz Stanislawski 39089d8589cSTvrtko Ursulin if (WARN_ON(!max_segment || offset_in_page(max_segment))) 39189d8589cSTvrtko Ursulin return -EINVAL; 39289d8589cSTvrtko Ursulin 393efc42bc9STomasz Stanislawski /* compute number of contiguous chunks */ 394efc42bc9STomasz Stanislawski chunks = 1; 395c125906bSTvrtko Ursulin seg_len = 0; 396c125906bSTvrtko Ursulin for (i = 1; i < n_pages; i++) { 397c125906bSTvrtko Ursulin seg_len += PAGE_SIZE; 398c125906bSTvrtko Ursulin if (seg_len >= max_segment || 399c125906bSTvrtko Ursulin page_to_pfn(pages[i]) != page_to_pfn(pages[i - 1]) + 1) { 400c125906bSTvrtko Ursulin chunks++; 401c125906bSTvrtko Ursulin seg_len = 0; 402c125906bSTvrtko Ursulin } 403c125906bSTvrtko Ursulin } 404efc42bc9STomasz Stanislawski 405efc42bc9STomasz Stanislawski ret = sg_alloc_table(sgt, chunks, gfp_mask); 406efc42bc9STomasz Stanislawski if (unlikely(ret)) 407efc42bc9STomasz Stanislawski return ret; 408efc42bc9STomasz Stanislawski 409efc42bc9STomasz Stanislawski /* merging chunks and putting them into the scatterlist */ 410efc42bc9STomasz Stanislawski cur_page = 0; 411efc42bc9STomasz Stanislawski for_each_sg(sgt->sgl, s, sgt->orig_nents, i) { 412c125906bSTvrtko Ursulin unsigned int j, chunk_size; 413efc42bc9STomasz Stanislawski 414efc42bc9STomasz Stanislawski /* look for the end of the current chunk */ 415c125906bSTvrtko Ursulin seg_len = 0; 416c125906bSTvrtko Ursulin for (j = cur_page + 1; j < n_pages; j++) { 417c125906bSTvrtko Ursulin seg_len += PAGE_SIZE; 418c125906bSTvrtko Ursulin if (seg_len >= max_segment || 419c125906bSTvrtko Ursulin page_to_pfn(pages[j]) != 420efc42bc9STomasz Stanislawski page_to_pfn(pages[j - 1]) + 1) 421efc42bc9STomasz Stanislawski break; 422c125906bSTvrtko Ursulin } 423efc42bc9STomasz Stanislawski 424efc42bc9STomasz Stanislawski chunk_size = ((j - cur_page) << PAGE_SHIFT) - offset; 425c125906bSTvrtko Ursulin sg_set_page(s, pages[cur_page], 426c125906bSTvrtko Ursulin min_t(unsigned long, size, chunk_size), offset); 427efc42bc9STomasz Stanislawski size -= chunk_size; 428efc42bc9STomasz Stanislawski offset = 0; 429efc42bc9STomasz Stanislawski cur_page = j; 430efc42bc9STomasz Stanislawski } 431efc42bc9STomasz Stanislawski 432efc42bc9STomasz Stanislawski return 0; 433efc42bc9STomasz Stanislawski } 43489d8589cSTvrtko Ursulin EXPORT_SYMBOL(__sg_alloc_table_from_pages); 43589d8589cSTvrtko Ursulin 43689d8589cSTvrtko Ursulin /** 43789d8589cSTvrtko Ursulin * sg_alloc_table_from_pages - Allocate and initialize an sg table from 43889d8589cSTvrtko Ursulin * an array of pages 43989d8589cSTvrtko Ursulin * @sgt: The sg table header to use 44089d8589cSTvrtko Ursulin * @pages: Pointer to an array of page pointers 44189d8589cSTvrtko Ursulin * @n_pages: Number of pages in the pages array 44289d8589cSTvrtko Ursulin * @offset: Offset from start of the first page to the start of a buffer 44389d8589cSTvrtko Ursulin * @size: Number of valid bytes in the buffer (after offset) 44489d8589cSTvrtko Ursulin * @gfp_mask: GFP allocation mask 44589d8589cSTvrtko Ursulin * 44689d8589cSTvrtko Ursulin * Description: 44789d8589cSTvrtko Ursulin * Allocate and initialize an sg table from a list of pages. Contiguous 44889d8589cSTvrtko Ursulin * ranges of the pages are squashed into a single scatterlist node. A user 44989d8589cSTvrtko Ursulin * may provide an offset at a start and a size of valid data in a buffer 45089d8589cSTvrtko Ursulin * specified by the page array. The returned sg table is released by 45189d8589cSTvrtko Ursulin * sg_free_table. 45289d8589cSTvrtko Ursulin * 45389d8589cSTvrtko Ursulin * Returns: 45489d8589cSTvrtko Ursulin * 0 on success, negative error on failure 45589d8589cSTvrtko Ursulin */ 45689d8589cSTvrtko Ursulin int sg_alloc_table_from_pages(struct sg_table *sgt, struct page **pages, 45789d8589cSTvrtko Ursulin unsigned int n_pages, unsigned int offset, 45889d8589cSTvrtko Ursulin unsigned long size, gfp_t gfp_mask) 45989d8589cSTvrtko Ursulin { 46089d8589cSTvrtko Ursulin return __sg_alloc_table_from_pages(sgt, pages, n_pages, offset, size, 46189d8589cSTvrtko Ursulin SCATTERLIST_MAX_SEGMENT, gfp_mask); 46289d8589cSTvrtko Ursulin } 463efc42bc9STomasz Stanislawski EXPORT_SYMBOL(sg_alloc_table_from_pages); 464efc42bc9STomasz Stanislawski 465e80a0af4SBart Van Assche #ifdef CONFIG_SGL_ALLOC 466e80a0af4SBart Van Assche 467e80a0af4SBart Van Assche /** 468e80a0af4SBart Van Assche * sgl_alloc_order - allocate a scatterlist and its pages 469e80a0af4SBart Van Assche * @length: Length in bytes of the scatterlist. Must be at least one 470e80a0af4SBart Van Assche * @order: Second argument for alloc_pages() 471e80a0af4SBart Van Assche * @chainable: Whether or not to allocate an extra element in the scatterlist 472e80a0af4SBart Van Assche * for scatterlist chaining purposes 473e80a0af4SBart Van Assche * @gfp: Memory allocation flags 474e80a0af4SBart Van Assche * @nent_p: [out] Number of entries in the scatterlist that have pages 475e80a0af4SBart Van Assche * 476e80a0af4SBart Van Assche * Returns: A pointer to an initialized scatterlist or %NULL upon failure. 477e80a0af4SBart Van Assche */ 478e80a0af4SBart Van Assche struct scatterlist *sgl_alloc_order(unsigned long long length, 479e80a0af4SBart Van Assche unsigned int order, bool chainable, 480e80a0af4SBart Van Assche gfp_t gfp, unsigned int *nent_p) 481e80a0af4SBart Van Assche { 482e80a0af4SBart Van Assche struct scatterlist *sgl, *sg; 483e80a0af4SBart Van Assche struct page *page; 484e80a0af4SBart Van Assche unsigned int nent, nalloc; 485e80a0af4SBart Van Assche u32 elem_len; 486e80a0af4SBart Van Assche 487e80a0af4SBart Van Assche nent = round_up(length, PAGE_SIZE << order) >> (PAGE_SHIFT + order); 488e80a0af4SBart Van Assche /* Check for integer overflow */ 489e80a0af4SBart Van Assche if (length > (nent << (PAGE_SHIFT + order))) 490e80a0af4SBart Van Assche return NULL; 491e80a0af4SBart Van Assche nalloc = nent; 492e80a0af4SBart Van Assche if (chainable) { 493e80a0af4SBart Van Assche /* Check for integer overflow */ 494e80a0af4SBart Van Assche if (nalloc + 1 < nalloc) 495e80a0af4SBart Van Assche return NULL; 496e80a0af4SBart Van Assche nalloc++; 497e80a0af4SBart Van Assche } 498e80a0af4SBart Van Assche sgl = kmalloc_array(nalloc, sizeof(struct scatterlist), 499e80a0af4SBart Van Assche (gfp & ~GFP_DMA) | __GFP_ZERO); 500e80a0af4SBart Van Assche if (!sgl) 501e80a0af4SBart Van Assche return NULL; 502e80a0af4SBart Van Assche 5038c7a8d1cSBart Van Assche sg_init_table(sgl, nalloc); 504e80a0af4SBart Van Assche sg = sgl; 505e80a0af4SBart Van Assche while (length) { 506e80a0af4SBart Van Assche elem_len = min_t(u64, length, PAGE_SIZE << order); 507e80a0af4SBart Van Assche page = alloc_pages(gfp, order); 508e80a0af4SBart Van Assche if (!page) { 509e80a0af4SBart Van Assche sgl_free(sgl); 510e80a0af4SBart Van Assche return NULL; 511e80a0af4SBart Van Assche } 512e80a0af4SBart Van Assche 513e80a0af4SBart Van Assche sg_set_page(sg, page, elem_len, 0); 514e80a0af4SBart Van Assche length -= elem_len; 515e80a0af4SBart Van Assche sg = sg_next(sg); 516e80a0af4SBart Van Assche } 5178c7a8d1cSBart Van Assche WARN_ONCE(length, "length = %lld\n", length); 518e80a0af4SBart Van Assche if (nent_p) 519e80a0af4SBart Van Assche *nent_p = nent; 520e80a0af4SBart Van Assche return sgl; 521e80a0af4SBart Van Assche } 522e80a0af4SBart Van Assche EXPORT_SYMBOL(sgl_alloc_order); 523e80a0af4SBart Van Assche 524e80a0af4SBart Van Assche /** 525e80a0af4SBart Van Assche * sgl_alloc - allocate a scatterlist and its pages 526e80a0af4SBart Van Assche * @length: Length in bytes of the scatterlist 527e80a0af4SBart Van Assche * @gfp: Memory allocation flags 528e80a0af4SBart Van Assche * @nent_p: [out] Number of entries in the scatterlist 529e80a0af4SBart Van Assche * 530e80a0af4SBart Van Assche * Returns: A pointer to an initialized scatterlist or %NULL upon failure. 531e80a0af4SBart Van Assche */ 532e80a0af4SBart Van Assche struct scatterlist *sgl_alloc(unsigned long long length, gfp_t gfp, 533e80a0af4SBart Van Assche unsigned int *nent_p) 534e80a0af4SBart Van Assche { 535e80a0af4SBart Van Assche return sgl_alloc_order(length, 0, false, gfp, nent_p); 536e80a0af4SBart Van Assche } 537e80a0af4SBart Van Assche EXPORT_SYMBOL(sgl_alloc); 538e80a0af4SBart Van Assche 539e80a0af4SBart Van Assche /** 5408c7a8d1cSBart Van Assche * sgl_free_n_order - free a scatterlist and its pages 5418c7a8d1cSBart Van Assche * @sgl: Scatterlist with one or more elements 5428c7a8d1cSBart Van Assche * @nents: Maximum number of elements to free 5438c7a8d1cSBart Van Assche * @order: Second argument for __free_pages() 5448c7a8d1cSBart Van Assche * 5458c7a8d1cSBart Van Assche * Notes: 5468c7a8d1cSBart Van Assche * - If several scatterlists have been chained and each chain element is 5478c7a8d1cSBart Van Assche * freed separately then it's essential to set nents correctly to avoid that a 5488c7a8d1cSBart Van Assche * page would get freed twice. 5498c7a8d1cSBart Van Assche * - All pages in a chained scatterlist can be freed at once by setting @nents 5508c7a8d1cSBart Van Assche * to a high number. 5518c7a8d1cSBart Van Assche */ 5528c7a8d1cSBart Van Assche void sgl_free_n_order(struct scatterlist *sgl, int nents, int order) 5538c7a8d1cSBart Van Assche { 5548c7a8d1cSBart Van Assche struct scatterlist *sg; 5558c7a8d1cSBart Van Assche struct page *page; 5568c7a8d1cSBart Van Assche int i; 5578c7a8d1cSBart Van Assche 5588c7a8d1cSBart Van Assche for_each_sg(sgl, sg, nents, i) { 5598c7a8d1cSBart Van Assche if (!sg) 5608c7a8d1cSBart Van Assche break; 5618c7a8d1cSBart Van Assche page = sg_page(sg); 5628c7a8d1cSBart Van Assche if (page) 5638c7a8d1cSBart Van Assche __free_pages(page, order); 5648c7a8d1cSBart Van Assche } 5658c7a8d1cSBart Van Assche kfree(sgl); 5668c7a8d1cSBart Van Assche } 5678c7a8d1cSBart Van Assche EXPORT_SYMBOL(sgl_free_n_order); 5688c7a8d1cSBart Van Assche 5698c7a8d1cSBart Van Assche /** 570e80a0af4SBart Van Assche * sgl_free_order - free a scatterlist and its pages 571e80a0af4SBart Van Assche * @sgl: Scatterlist with one or more elements 572e80a0af4SBart Van Assche * @order: Second argument for __free_pages() 573e80a0af4SBart Van Assche */ 574e80a0af4SBart Van Assche void sgl_free_order(struct scatterlist *sgl, int order) 575e80a0af4SBart Van Assche { 5768c7a8d1cSBart Van Assche sgl_free_n_order(sgl, INT_MAX, order); 577e80a0af4SBart Van Assche } 578e80a0af4SBart Van Assche EXPORT_SYMBOL(sgl_free_order); 579e80a0af4SBart Van Assche 580e80a0af4SBart Van Assche /** 581e80a0af4SBart Van Assche * sgl_free - free a scatterlist and its pages 582e80a0af4SBart Van Assche * @sgl: Scatterlist with one or more elements 583e80a0af4SBart Van Assche */ 584e80a0af4SBart Van Assche void sgl_free(struct scatterlist *sgl) 585e80a0af4SBart Van Assche { 586e80a0af4SBart Van Assche sgl_free_order(sgl, 0); 587e80a0af4SBart Van Assche } 588e80a0af4SBart Van Assche EXPORT_SYMBOL(sgl_free); 589e80a0af4SBart Van Assche 590e80a0af4SBart Van Assche #endif /* CONFIG_SGL_ALLOC */ 591e80a0af4SBart Van Assche 592a321e91bSImre Deak void __sg_page_iter_start(struct sg_page_iter *piter, 593a321e91bSImre Deak struct scatterlist *sglist, unsigned int nents, 594a321e91bSImre Deak unsigned long pgoffset) 595a321e91bSImre Deak { 596a321e91bSImre Deak piter->__pg_advance = 0; 597a321e91bSImre Deak piter->__nents = nents; 598a321e91bSImre Deak 599a321e91bSImre Deak piter->sg = sglist; 600a321e91bSImre Deak piter->sg_pgoffset = pgoffset; 601a321e91bSImre Deak } 602a321e91bSImre Deak EXPORT_SYMBOL(__sg_page_iter_start); 603a321e91bSImre Deak 604a321e91bSImre Deak static int sg_page_count(struct scatterlist *sg) 605a321e91bSImre Deak { 606a321e91bSImre Deak return PAGE_ALIGN(sg->offset + sg->length) >> PAGE_SHIFT; 607a321e91bSImre Deak } 608a321e91bSImre Deak 609a321e91bSImre Deak bool __sg_page_iter_next(struct sg_page_iter *piter) 610a321e91bSImre Deak { 611a321e91bSImre Deak if (!piter->__nents || !piter->sg) 612a321e91bSImre Deak return false; 613a321e91bSImre Deak 614a321e91bSImre Deak piter->sg_pgoffset += piter->__pg_advance; 615a321e91bSImre Deak piter->__pg_advance = 1; 616a321e91bSImre Deak 617a321e91bSImre Deak while (piter->sg_pgoffset >= sg_page_count(piter->sg)) { 618a321e91bSImre Deak piter->sg_pgoffset -= sg_page_count(piter->sg); 619a321e91bSImre Deak piter->sg = sg_next(piter->sg); 620a321e91bSImre Deak if (!--piter->__nents || !piter->sg) 621a321e91bSImre Deak return false; 622a321e91bSImre Deak } 623a321e91bSImre Deak 624a321e91bSImre Deak return true; 625a321e91bSImre Deak } 626a321e91bSImre Deak EXPORT_SYMBOL(__sg_page_iter_next); 627a321e91bSImre Deak 628efc42bc9STomasz Stanislawski /** 629137d3edbSTejun Heo * sg_miter_start - start mapping iteration over a sg list 630137d3edbSTejun Heo * @miter: sg mapping iter to be started 631137d3edbSTejun Heo * @sgl: sg list to iterate over 632137d3edbSTejun Heo * @nents: number of sg entries 633137d3edbSTejun Heo * 634137d3edbSTejun Heo * Description: 635137d3edbSTejun Heo * Starts mapping iterator @miter. 636137d3edbSTejun Heo * 637137d3edbSTejun Heo * Context: 638137d3edbSTejun Heo * Don't care. 639137d3edbSTejun Heo */ 640137d3edbSTejun Heo void sg_miter_start(struct sg_mapping_iter *miter, struct scatterlist *sgl, 641137d3edbSTejun Heo unsigned int nents, unsigned int flags) 642137d3edbSTejun Heo { 643137d3edbSTejun Heo memset(miter, 0, sizeof(struct sg_mapping_iter)); 644137d3edbSTejun Heo 6454225fc85SImre Deak __sg_page_iter_start(&miter->piter, sgl, nents, 0); 6466de7e356SSebastian Andrzej Siewior WARN_ON(!(flags & (SG_MITER_TO_SG | SG_MITER_FROM_SG))); 647137d3edbSTejun Heo miter->__flags = flags; 648137d3edbSTejun Heo } 649137d3edbSTejun Heo EXPORT_SYMBOL(sg_miter_start); 650137d3edbSTejun Heo 65111052004SAkinobu Mita static bool sg_miter_get_next_page(struct sg_mapping_iter *miter) 65211052004SAkinobu Mita { 65311052004SAkinobu Mita if (!miter->__remaining) { 65411052004SAkinobu Mita struct scatterlist *sg; 65511052004SAkinobu Mita unsigned long pgoffset; 65611052004SAkinobu Mita 65711052004SAkinobu Mita if (!__sg_page_iter_next(&miter->piter)) 65811052004SAkinobu Mita return false; 65911052004SAkinobu Mita 66011052004SAkinobu Mita sg = miter->piter.sg; 66111052004SAkinobu Mita pgoffset = miter->piter.sg_pgoffset; 66211052004SAkinobu Mita 66311052004SAkinobu Mita miter->__offset = pgoffset ? 0 : sg->offset; 66411052004SAkinobu Mita miter->__remaining = sg->offset + sg->length - 66511052004SAkinobu Mita (pgoffset << PAGE_SHIFT) - miter->__offset; 66611052004SAkinobu Mita miter->__remaining = min_t(unsigned long, miter->__remaining, 66711052004SAkinobu Mita PAGE_SIZE - miter->__offset); 66811052004SAkinobu Mita } 66911052004SAkinobu Mita 67011052004SAkinobu Mita return true; 67111052004SAkinobu Mita } 67211052004SAkinobu Mita 673137d3edbSTejun Heo /** 674df642ceaSAkinobu Mita * sg_miter_skip - reposition mapping iterator 675df642ceaSAkinobu Mita * @miter: sg mapping iter to be skipped 676df642ceaSAkinobu Mita * @offset: number of bytes to plus the current location 677df642ceaSAkinobu Mita * 678df642ceaSAkinobu Mita * Description: 679df642ceaSAkinobu Mita * Sets the offset of @miter to its current location plus @offset bytes. 680df642ceaSAkinobu Mita * If mapping iterator @miter has been proceeded by sg_miter_next(), this 681df642ceaSAkinobu Mita * stops @miter. 682df642ceaSAkinobu Mita * 683df642ceaSAkinobu Mita * Context: 684df642ceaSAkinobu Mita * Don't care if @miter is stopped, or not proceeded yet. 685df642ceaSAkinobu Mita * Otherwise, preemption disabled if the SG_MITER_ATOMIC is set. 686df642ceaSAkinobu Mita * 687df642ceaSAkinobu Mita * Returns: 688df642ceaSAkinobu Mita * true if @miter contains the valid mapping. false if end of sg 689df642ceaSAkinobu Mita * list is reached. 690df642ceaSAkinobu Mita */ 6910d6077f8SMing Lei bool sg_miter_skip(struct sg_mapping_iter *miter, off_t offset) 692df642ceaSAkinobu Mita { 693df642ceaSAkinobu Mita sg_miter_stop(miter); 694df642ceaSAkinobu Mita 695df642ceaSAkinobu Mita while (offset) { 696df642ceaSAkinobu Mita off_t consumed; 697df642ceaSAkinobu Mita 698df642ceaSAkinobu Mita if (!sg_miter_get_next_page(miter)) 699df642ceaSAkinobu Mita return false; 700df642ceaSAkinobu Mita 701df642ceaSAkinobu Mita consumed = min_t(off_t, offset, miter->__remaining); 702df642ceaSAkinobu Mita miter->__offset += consumed; 703df642ceaSAkinobu Mita miter->__remaining -= consumed; 704df642ceaSAkinobu Mita offset -= consumed; 705df642ceaSAkinobu Mita } 706df642ceaSAkinobu Mita 707df642ceaSAkinobu Mita return true; 708df642ceaSAkinobu Mita } 7090d6077f8SMing Lei EXPORT_SYMBOL(sg_miter_skip); 710df642ceaSAkinobu Mita 711df642ceaSAkinobu Mita /** 712137d3edbSTejun Heo * sg_miter_next - proceed mapping iterator to the next mapping 713137d3edbSTejun Heo * @miter: sg mapping iter to proceed 714137d3edbSTejun Heo * 715137d3edbSTejun Heo * Description: 7168290e2d2STejun Heo * Proceeds @miter to the next mapping. @miter should have been started 7178290e2d2STejun Heo * using sg_miter_start(). On successful return, @miter->page, 7188290e2d2STejun Heo * @miter->addr and @miter->length point to the current mapping. 719137d3edbSTejun Heo * 720137d3edbSTejun Heo * Context: 7218290e2d2STejun Heo * Preemption disabled if SG_MITER_ATOMIC. Preemption must stay disabled 7228290e2d2STejun Heo * till @miter is stopped. May sleep if !SG_MITER_ATOMIC. 723137d3edbSTejun Heo * 724137d3edbSTejun Heo * Returns: 725137d3edbSTejun Heo * true if @miter contains the next mapping. false if end of sg 726137d3edbSTejun Heo * list is reached. 727137d3edbSTejun Heo */ 728137d3edbSTejun Heo bool sg_miter_next(struct sg_mapping_iter *miter) 729137d3edbSTejun Heo { 730137d3edbSTejun Heo sg_miter_stop(miter); 731137d3edbSTejun Heo 7324225fc85SImre Deak /* 7334225fc85SImre Deak * Get to the next page if necessary. 7344225fc85SImre Deak * __remaining, __offset is adjusted by sg_miter_stop 7354225fc85SImre Deak */ 73611052004SAkinobu Mita if (!sg_miter_get_next_page(miter)) 73723c560a9STejun Heo return false; 7384225fc85SImre Deak 7392db76d7cSImre Deak miter->page = sg_page_iter_page(&miter->piter); 7404225fc85SImre Deak miter->consumed = miter->length = miter->__remaining; 741137d3edbSTejun Heo 742137d3edbSTejun Heo if (miter->__flags & SG_MITER_ATOMIC) 7434225fc85SImre Deak miter->addr = kmap_atomic(miter->page) + miter->__offset; 744137d3edbSTejun Heo else 7454225fc85SImre Deak miter->addr = kmap(miter->page) + miter->__offset; 746137d3edbSTejun Heo 747137d3edbSTejun Heo return true; 748137d3edbSTejun Heo } 749137d3edbSTejun Heo EXPORT_SYMBOL(sg_miter_next); 750137d3edbSTejun Heo 751137d3edbSTejun Heo /** 752137d3edbSTejun Heo * sg_miter_stop - stop mapping iteration 753137d3edbSTejun Heo * @miter: sg mapping iter to be stopped 754137d3edbSTejun Heo * 755137d3edbSTejun Heo * Description: 756137d3edbSTejun Heo * Stops mapping iterator @miter. @miter should have been started 7574ba6a2b2SMasahiro Yamada * using sg_miter_start(). A stopped iteration can be resumed by 7584ba6a2b2SMasahiro Yamada * calling sg_miter_next() on it. This is useful when resources (kmap) 7594ba6a2b2SMasahiro Yamada * need to be released during iteration. 760137d3edbSTejun Heo * 761137d3edbSTejun Heo * Context: 7628290e2d2STejun Heo * Preemption disabled if the SG_MITER_ATOMIC is set. Don't care 7638290e2d2STejun Heo * otherwise. 764137d3edbSTejun Heo */ 765137d3edbSTejun Heo void sg_miter_stop(struct sg_mapping_iter *miter) 766137d3edbSTejun Heo { 767137d3edbSTejun Heo WARN_ON(miter->consumed > miter->length); 768137d3edbSTejun Heo 769137d3edbSTejun Heo /* drop resources from the last iteration */ 770137d3edbSTejun Heo if (miter->addr) { 771137d3edbSTejun Heo miter->__offset += miter->consumed; 7724225fc85SImre Deak miter->__remaining -= miter->consumed; 773137d3edbSTejun Heo 7743d77b50cSMing Lei if ((miter->__flags & SG_MITER_TO_SG) && 7753d77b50cSMing Lei !PageSlab(miter->page)) 7766de7e356SSebastian Andrzej Siewior flush_kernel_dcache_page(miter->page); 7776de7e356SSebastian Andrzej Siewior 778137d3edbSTejun Heo if (miter->__flags & SG_MITER_ATOMIC) { 7798290e2d2STejun Heo WARN_ON_ONCE(preemptible()); 780c3eede8eSCong Wang kunmap_atomic(miter->addr); 781137d3edbSTejun Heo } else 782f652c521SArjan van de Ven kunmap(miter->page); 783137d3edbSTejun Heo 784137d3edbSTejun Heo miter->page = NULL; 785137d3edbSTejun Heo miter->addr = NULL; 786137d3edbSTejun Heo miter->length = 0; 787137d3edbSTejun Heo miter->consumed = 0; 788137d3edbSTejun Heo } 789137d3edbSTejun Heo } 790137d3edbSTejun Heo EXPORT_SYMBOL(sg_miter_stop); 791137d3edbSTejun Heo 792137d3edbSTejun Heo /** 793b1adaf65SFUJITA Tomonori * sg_copy_buffer - Copy data between a linear buffer and an SG list 794b1adaf65SFUJITA Tomonori * @sgl: The SG list 795b1adaf65SFUJITA Tomonori * @nents: Number of SG entries 796b1adaf65SFUJITA Tomonori * @buf: Where to copy from 797b1adaf65SFUJITA Tomonori * @buflen: The number of bytes to copy 798df642ceaSAkinobu Mita * @skip: Number of bytes to skip before copying 799df642ceaSAkinobu Mita * @to_buffer: transfer direction (true == from an sg list to a 800df642ceaSAkinobu Mita * buffer, false == from a buffer to an sg list 801b1adaf65SFUJITA Tomonori * 802b1adaf65SFUJITA Tomonori * Returns the number of copied bytes. 803b1adaf65SFUJITA Tomonori * 804b1adaf65SFUJITA Tomonori **/ 805386ecb12SDave Gordon size_t sg_copy_buffer(struct scatterlist *sgl, unsigned int nents, void *buf, 806386ecb12SDave Gordon size_t buflen, off_t skip, bool to_buffer) 807b1adaf65SFUJITA Tomonori { 808137d3edbSTejun Heo unsigned int offset = 0; 809137d3edbSTejun Heo struct sg_mapping_iter miter; 8106de7e356SSebastian Andrzej Siewior unsigned int sg_flags = SG_MITER_ATOMIC; 811b1adaf65SFUJITA Tomonori 8126de7e356SSebastian Andrzej Siewior if (to_buffer) 8136de7e356SSebastian Andrzej Siewior sg_flags |= SG_MITER_FROM_SG; 8146de7e356SSebastian Andrzej Siewior else 8156de7e356SSebastian Andrzej Siewior sg_flags |= SG_MITER_TO_SG; 8166de7e356SSebastian Andrzej Siewior 8176de7e356SSebastian Andrzej Siewior sg_miter_start(&miter, sgl, nents, sg_flags); 818b1adaf65SFUJITA Tomonori 819df642ceaSAkinobu Mita if (!sg_miter_skip(&miter, skip)) 820df642ceaSAkinobu Mita return false; 821df642ceaSAkinobu Mita 8221d5210efSGilad Ben-Yossef while ((offset < buflen) && sg_miter_next(&miter)) { 823137d3edbSTejun Heo unsigned int len; 824b1adaf65SFUJITA Tomonori 825137d3edbSTejun Heo len = min(miter.length, buflen - offset); 826b1adaf65SFUJITA Tomonori 827b1adaf65SFUJITA Tomonori if (to_buffer) 828137d3edbSTejun Heo memcpy(buf + offset, miter.addr, len); 8296de7e356SSebastian Andrzej Siewior else 830137d3edbSTejun Heo memcpy(miter.addr, buf + offset, len); 831b1adaf65SFUJITA Tomonori 832137d3edbSTejun Heo offset += len; 833b1adaf65SFUJITA Tomonori } 834b1adaf65SFUJITA Tomonori 835137d3edbSTejun Heo sg_miter_stop(&miter); 836b1adaf65SFUJITA Tomonori 837137d3edbSTejun Heo return offset; 838b1adaf65SFUJITA Tomonori } 839386ecb12SDave Gordon EXPORT_SYMBOL(sg_copy_buffer); 840b1adaf65SFUJITA Tomonori 841b1adaf65SFUJITA Tomonori /** 842b1adaf65SFUJITA Tomonori * sg_copy_from_buffer - Copy from a linear buffer to an SG list 843b1adaf65SFUJITA Tomonori * @sgl: The SG list 844b1adaf65SFUJITA Tomonori * @nents: Number of SG entries 845b1adaf65SFUJITA Tomonori * @buf: Where to copy from 846b1adaf65SFUJITA Tomonori * @buflen: The number of bytes to copy 847b1adaf65SFUJITA Tomonori * 848b1adaf65SFUJITA Tomonori * Returns the number of copied bytes. 849b1adaf65SFUJITA Tomonori * 850b1adaf65SFUJITA Tomonori **/ 851b1adaf65SFUJITA Tomonori size_t sg_copy_from_buffer(struct scatterlist *sgl, unsigned int nents, 8522a1bf8f9SDave Gordon const void *buf, size_t buflen) 853b1adaf65SFUJITA Tomonori { 8542a1bf8f9SDave Gordon return sg_copy_buffer(sgl, nents, (void *)buf, buflen, 0, false); 855b1adaf65SFUJITA Tomonori } 856b1adaf65SFUJITA Tomonori EXPORT_SYMBOL(sg_copy_from_buffer); 857b1adaf65SFUJITA Tomonori 858b1adaf65SFUJITA Tomonori /** 859b1adaf65SFUJITA Tomonori * sg_copy_to_buffer - Copy from an SG list to a linear buffer 860b1adaf65SFUJITA Tomonori * @sgl: The SG list 861b1adaf65SFUJITA Tomonori * @nents: Number of SG entries 862b1adaf65SFUJITA Tomonori * @buf: Where to copy to 863b1adaf65SFUJITA Tomonori * @buflen: The number of bytes to copy 864b1adaf65SFUJITA Tomonori * 865b1adaf65SFUJITA Tomonori * Returns the number of copied bytes. 866b1adaf65SFUJITA Tomonori * 867b1adaf65SFUJITA Tomonori **/ 868b1adaf65SFUJITA Tomonori size_t sg_copy_to_buffer(struct scatterlist *sgl, unsigned int nents, 869b1adaf65SFUJITA Tomonori void *buf, size_t buflen) 870b1adaf65SFUJITA Tomonori { 871df642ceaSAkinobu Mita return sg_copy_buffer(sgl, nents, buf, buflen, 0, true); 872b1adaf65SFUJITA Tomonori } 873b1adaf65SFUJITA Tomonori EXPORT_SYMBOL(sg_copy_to_buffer); 874df642ceaSAkinobu Mita 875df642ceaSAkinobu Mita /** 876df642ceaSAkinobu Mita * sg_pcopy_from_buffer - Copy from a linear buffer to an SG list 877df642ceaSAkinobu Mita * @sgl: The SG list 878df642ceaSAkinobu Mita * @nents: Number of SG entries 879df642ceaSAkinobu Mita * @buf: Where to copy from 880df642ceaSAkinobu Mita * @buflen: The number of bytes to copy 8814dc7daf8SDave Gordon * @skip: Number of bytes to skip before copying 882df642ceaSAkinobu Mita * 883df642ceaSAkinobu Mita * Returns the number of copied bytes. 884df642ceaSAkinobu Mita * 885df642ceaSAkinobu Mita **/ 886df642ceaSAkinobu Mita size_t sg_pcopy_from_buffer(struct scatterlist *sgl, unsigned int nents, 8872a1bf8f9SDave Gordon const void *buf, size_t buflen, off_t skip) 888df642ceaSAkinobu Mita { 8892a1bf8f9SDave Gordon return sg_copy_buffer(sgl, nents, (void *)buf, buflen, skip, false); 890df642ceaSAkinobu Mita } 891df642ceaSAkinobu Mita EXPORT_SYMBOL(sg_pcopy_from_buffer); 892df642ceaSAkinobu Mita 893df642ceaSAkinobu Mita /** 894df642ceaSAkinobu Mita * sg_pcopy_to_buffer - Copy from an SG list to a linear buffer 895df642ceaSAkinobu Mita * @sgl: The SG list 896df642ceaSAkinobu Mita * @nents: Number of SG entries 897df642ceaSAkinobu Mita * @buf: Where to copy to 898df642ceaSAkinobu Mita * @buflen: The number of bytes to copy 8994dc7daf8SDave Gordon * @skip: Number of bytes to skip before copying 900df642ceaSAkinobu Mita * 901df642ceaSAkinobu Mita * Returns the number of copied bytes. 902df642ceaSAkinobu Mita * 903df642ceaSAkinobu Mita **/ 904df642ceaSAkinobu Mita size_t sg_pcopy_to_buffer(struct scatterlist *sgl, unsigned int nents, 905df642ceaSAkinobu Mita void *buf, size_t buflen, off_t skip) 906df642ceaSAkinobu Mita { 907df642ceaSAkinobu Mita return sg_copy_buffer(sgl, nents, buf, buflen, skip, true); 908df642ceaSAkinobu Mita } 909df642ceaSAkinobu Mita EXPORT_SYMBOL(sg_pcopy_to_buffer); 9100945e569SJohannes Thumshirn 9110945e569SJohannes Thumshirn /** 9120945e569SJohannes Thumshirn * sg_zero_buffer - Zero-out a part of a SG list 9130945e569SJohannes Thumshirn * @sgl: The SG list 9140945e569SJohannes Thumshirn * @nents: Number of SG entries 9150945e569SJohannes Thumshirn * @buflen: The number of bytes to zero out 9160945e569SJohannes Thumshirn * @skip: Number of bytes to skip before zeroing 9170945e569SJohannes Thumshirn * 9180945e569SJohannes Thumshirn * Returns the number of bytes zeroed. 9190945e569SJohannes Thumshirn **/ 9200945e569SJohannes Thumshirn size_t sg_zero_buffer(struct scatterlist *sgl, unsigned int nents, 9210945e569SJohannes Thumshirn size_t buflen, off_t skip) 9220945e569SJohannes Thumshirn { 9230945e569SJohannes Thumshirn unsigned int offset = 0; 9240945e569SJohannes Thumshirn struct sg_mapping_iter miter; 9250945e569SJohannes Thumshirn unsigned int sg_flags = SG_MITER_ATOMIC | SG_MITER_TO_SG; 9260945e569SJohannes Thumshirn 9270945e569SJohannes Thumshirn sg_miter_start(&miter, sgl, nents, sg_flags); 9280945e569SJohannes Thumshirn 9290945e569SJohannes Thumshirn if (!sg_miter_skip(&miter, skip)) 9300945e569SJohannes Thumshirn return false; 9310945e569SJohannes Thumshirn 9320945e569SJohannes Thumshirn while (offset < buflen && sg_miter_next(&miter)) { 9330945e569SJohannes Thumshirn unsigned int len; 9340945e569SJohannes Thumshirn 9350945e569SJohannes Thumshirn len = min(miter.length, buflen - offset); 9360945e569SJohannes Thumshirn memset(miter.addr, 0, len); 9370945e569SJohannes Thumshirn 9380945e569SJohannes Thumshirn offset += len; 9390945e569SJohannes Thumshirn } 9400945e569SJohannes Thumshirn 9410945e569SJohannes Thumshirn sg_miter_stop(&miter); 9420945e569SJohannes Thumshirn return offset; 9430945e569SJohannes Thumshirn } 9440945e569SJohannes Thumshirn EXPORT_SYMBOL(sg_zero_buffer); 945