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 59cfaed10dSTom Lendacky /** 60cfaed10dSTom Lendacky * sg_nents_for_len - return total count of entries in scatterlist 61cfaed10dSTom Lendacky * needed to satisfy the supplied length 62cfaed10dSTom Lendacky * @sg: The scatterlist 63cfaed10dSTom Lendacky * @len: The total required length 64cfaed10dSTom Lendacky * 65cfaed10dSTom Lendacky * Description: 66cfaed10dSTom Lendacky * Determines the number of entries in sg that are required to meet 67cfaed10dSTom Lendacky * the supplied length, taking into acount chaining as well 68cfaed10dSTom Lendacky * 69cfaed10dSTom Lendacky * Returns: 70cfaed10dSTom Lendacky * the number of sg entries needed, negative error on failure 71cfaed10dSTom Lendacky * 72cfaed10dSTom Lendacky **/ 73cfaed10dSTom Lendacky int sg_nents_for_len(struct scatterlist *sg, u64 len) 74cfaed10dSTom Lendacky { 75cfaed10dSTom Lendacky int nents; 76cfaed10dSTom Lendacky u64 total; 77cfaed10dSTom Lendacky 78cfaed10dSTom Lendacky if (!len) 79cfaed10dSTom Lendacky return 0; 80cfaed10dSTom Lendacky 81cfaed10dSTom Lendacky for (nents = 0, total = 0; sg; sg = sg_next(sg)) { 82cfaed10dSTom Lendacky nents++; 83cfaed10dSTom Lendacky total += sg->length; 84cfaed10dSTom Lendacky if (total >= len) 85cfaed10dSTom Lendacky return nents; 86cfaed10dSTom Lendacky } 87cfaed10dSTom Lendacky 88cfaed10dSTom Lendacky return -EINVAL; 89cfaed10dSTom Lendacky } 90cfaed10dSTom Lendacky EXPORT_SYMBOL(sg_nents_for_len); 912e484610SMaxim Levitsky 922e484610SMaxim Levitsky /** 930db9299fSJens Axboe * sg_last - return the last scatterlist entry in a list 940db9299fSJens Axboe * @sgl: First entry in the scatterlist 950db9299fSJens Axboe * @nents: Number of entries in the scatterlist 960db9299fSJens Axboe * 970db9299fSJens Axboe * Description: 980db9299fSJens Axboe * Should only be used casually, it (currently) scans the entire list 990db9299fSJens Axboe * to get the last entry. 1000db9299fSJens Axboe * 1010db9299fSJens Axboe * Note that the @sgl@ pointer passed in need not be the first one, 1020db9299fSJens Axboe * the important bit is that @nents@ denotes the number of entries that 1030db9299fSJens Axboe * exist from @sgl@. 1040db9299fSJens Axboe * 1050db9299fSJens Axboe **/ 1060db9299fSJens Axboe struct scatterlist *sg_last(struct scatterlist *sgl, unsigned int nents) 1070db9299fSJens Axboe { 108308c09f1SLaura Abbott #ifndef CONFIG_ARCH_HAS_SG_CHAIN 1090db9299fSJens Axboe struct scatterlist *ret = &sgl[nents - 1]; 1100db9299fSJens Axboe #else 1110db9299fSJens Axboe struct scatterlist *sg, *ret = NULL; 1120db9299fSJens Axboe unsigned int i; 1130db9299fSJens Axboe 1140db9299fSJens Axboe for_each_sg(sgl, sg, nents, i) 1150db9299fSJens Axboe ret = sg; 1160db9299fSJens Axboe 1170db9299fSJens Axboe #endif 1180db9299fSJens Axboe #ifdef CONFIG_DEBUG_SG 1190db9299fSJens Axboe BUG_ON(sgl[0].sg_magic != SG_MAGIC); 1200db9299fSJens Axboe BUG_ON(!sg_is_last(ret)); 1210db9299fSJens Axboe #endif 1220db9299fSJens Axboe return ret; 1230db9299fSJens Axboe } 1240db9299fSJens Axboe EXPORT_SYMBOL(sg_last); 1250db9299fSJens Axboe 1260db9299fSJens Axboe /** 1270db9299fSJens Axboe * sg_init_table - Initialize SG table 1280db9299fSJens Axboe * @sgl: The SG table 1290db9299fSJens Axboe * @nents: Number of entries in table 1300db9299fSJens Axboe * 1310db9299fSJens Axboe * Notes: 1320db9299fSJens Axboe * If this is part of a chained sg table, sg_mark_end() should be 1330db9299fSJens Axboe * used only on the last table part. 1340db9299fSJens Axboe * 1350db9299fSJens Axboe **/ 1360db9299fSJens Axboe void sg_init_table(struct scatterlist *sgl, unsigned int nents) 1370db9299fSJens Axboe { 1380db9299fSJens Axboe memset(sgl, 0, sizeof(*sgl) * nents); 1390db9299fSJens Axboe #ifdef CONFIG_DEBUG_SG 1400db9299fSJens Axboe { 1410db9299fSJens Axboe unsigned int i; 1420db9299fSJens Axboe for (i = 0; i < nents; i++) 1430db9299fSJens Axboe sgl[i].sg_magic = SG_MAGIC; 1440db9299fSJens Axboe } 1450db9299fSJens Axboe #endif 1460db9299fSJens Axboe sg_mark_end(&sgl[nents - 1]); 1470db9299fSJens Axboe } 1480db9299fSJens Axboe EXPORT_SYMBOL(sg_init_table); 1490db9299fSJens Axboe 1500db9299fSJens Axboe /** 1510db9299fSJens Axboe * sg_init_one - Initialize a single entry sg list 1520db9299fSJens Axboe * @sg: SG entry 1530db9299fSJens Axboe * @buf: Virtual address for IO 1540db9299fSJens Axboe * @buflen: IO length 1550db9299fSJens Axboe * 1560db9299fSJens Axboe **/ 1570db9299fSJens Axboe void sg_init_one(struct scatterlist *sg, const void *buf, unsigned int buflen) 1580db9299fSJens Axboe { 1590db9299fSJens Axboe sg_init_table(sg, 1); 1600db9299fSJens Axboe sg_set_buf(sg, buf, buflen); 1610db9299fSJens Axboe } 1620db9299fSJens Axboe EXPORT_SYMBOL(sg_init_one); 1630db9299fSJens Axboe 1640db9299fSJens Axboe /* 1650db9299fSJens Axboe * The default behaviour of sg_alloc_table() is to use these kmalloc/kfree 1660db9299fSJens Axboe * helpers. 1670db9299fSJens Axboe */ 1680db9299fSJens Axboe static struct scatterlist *sg_kmalloc(unsigned int nents, gfp_t gfp_mask) 1690db9299fSJens Axboe { 170b94de9bbSChris Wilson if (nents == SG_MAX_SINGLE_ALLOC) { 171b94de9bbSChris Wilson /* 172b94de9bbSChris Wilson * Kmemleak doesn't track page allocations as they are not 173b94de9bbSChris Wilson * commonly used (in a raw form) for kernel data structures. 174b94de9bbSChris Wilson * As we chain together a list of pages and then a normal 175b94de9bbSChris Wilson * kmalloc (tracked by kmemleak), in order to for that last 176b94de9bbSChris Wilson * allocation not to become decoupled (and thus a 177b94de9bbSChris Wilson * false-positive) we need to inform kmemleak of all the 178b94de9bbSChris Wilson * intermediate allocations. 179b94de9bbSChris Wilson */ 180b94de9bbSChris Wilson void *ptr = (void *) __get_free_page(gfp_mask); 181b94de9bbSChris Wilson kmemleak_alloc(ptr, PAGE_SIZE, 1, gfp_mask); 182b94de9bbSChris Wilson return ptr; 183b94de9bbSChris Wilson } else 1840db9299fSJens Axboe return kmalloc(nents * sizeof(struct scatterlist), gfp_mask); 1850db9299fSJens Axboe } 1860db9299fSJens Axboe 1870db9299fSJens Axboe static void sg_kfree(struct scatterlist *sg, unsigned int nents) 1880db9299fSJens Axboe { 189b94de9bbSChris Wilson if (nents == SG_MAX_SINGLE_ALLOC) { 190b94de9bbSChris Wilson kmemleak_free(sg); 1910db9299fSJens Axboe free_page((unsigned long) sg); 192b94de9bbSChris Wilson } else 1930db9299fSJens Axboe kfree(sg); 1940db9299fSJens Axboe } 1950db9299fSJens Axboe 1960db9299fSJens Axboe /** 1970db9299fSJens Axboe * __sg_free_table - Free a previously mapped sg table 1980db9299fSJens Axboe * @table: The sg table header to use 1997cedb1f1SJames Bottomley * @max_ents: The maximum number of entries per single scatterlist 200c53c6d6aSChristoph Hellwig * @skip_first_chunk: don't free the (preallocated) first scatterlist chunk 2010db9299fSJens Axboe * @free_fn: Free function 2020db9299fSJens Axboe * 2030db9299fSJens Axboe * Description: 2047cedb1f1SJames Bottomley * Free an sg table previously allocated and setup with 2057cedb1f1SJames Bottomley * __sg_alloc_table(). The @max_ents value must be identical to 2067cedb1f1SJames Bottomley * that previously used with __sg_alloc_table(). 2070db9299fSJens Axboe * 2080db9299fSJens Axboe **/ 2097cedb1f1SJames Bottomley void __sg_free_table(struct sg_table *table, unsigned int max_ents, 210c53c6d6aSChristoph Hellwig bool skip_first_chunk, sg_free_fn *free_fn) 2110db9299fSJens Axboe { 2120db9299fSJens Axboe struct scatterlist *sgl, *next; 2130db9299fSJens Axboe 2140db9299fSJens Axboe if (unlikely(!table->sgl)) 2150db9299fSJens Axboe return; 2160db9299fSJens Axboe 2170db9299fSJens Axboe sgl = table->sgl; 2180db9299fSJens Axboe while (table->orig_nents) { 2190db9299fSJens Axboe unsigned int alloc_size = table->orig_nents; 2200db9299fSJens Axboe unsigned int sg_size; 2210db9299fSJens Axboe 2220db9299fSJens Axboe /* 2237cedb1f1SJames Bottomley * If we have more than max_ents segments left, 2240db9299fSJens Axboe * then assign 'next' to the sg table after the current one. 2250db9299fSJens Axboe * sg_size is then one less than alloc size, since the last 2260db9299fSJens Axboe * element is the chain pointer. 2270db9299fSJens Axboe */ 2287cedb1f1SJames Bottomley if (alloc_size > max_ents) { 2297cedb1f1SJames Bottomley next = sg_chain_ptr(&sgl[max_ents - 1]); 2307cedb1f1SJames Bottomley alloc_size = max_ents; 2310db9299fSJens Axboe sg_size = alloc_size - 1; 2320db9299fSJens Axboe } else { 2330db9299fSJens Axboe sg_size = alloc_size; 2340db9299fSJens Axboe next = NULL; 2350db9299fSJens Axboe } 2360db9299fSJens Axboe 2370db9299fSJens Axboe table->orig_nents -= sg_size; 238c21e59d8STony Battersby if (skip_first_chunk) 239c53c6d6aSChristoph Hellwig skip_first_chunk = false; 240c21e59d8STony Battersby else 241c21e59d8STony Battersby free_fn(sgl, alloc_size); 2420db9299fSJens Axboe sgl = next; 2430db9299fSJens Axboe } 2440db9299fSJens Axboe 2450db9299fSJens Axboe table->sgl = NULL; 2460db9299fSJens Axboe } 2470db9299fSJens Axboe EXPORT_SYMBOL(__sg_free_table); 2480db9299fSJens Axboe 2490db9299fSJens Axboe /** 2500db9299fSJens Axboe * sg_free_table - Free a previously allocated sg table 2510db9299fSJens Axboe * @table: The mapped sg table header 2520db9299fSJens Axboe * 2530db9299fSJens Axboe **/ 2540db9299fSJens Axboe void sg_free_table(struct sg_table *table) 2550db9299fSJens Axboe { 256c53c6d6aSChristoph Hellwig __sg_free_table(table, SG_MAX_SINGLE_ALLOC, false, sg_kfree); 2570db9299fSJens Axboe } 2580db9299fSJens Axboe EXPORT_SYMBOL(sg_free_table); 2590db9299fSJens Axboe 2600db9299fSJens Axboe /** 2610db9299fSJens Axboe * __sg_alloc_table - Allocate and initialize an sg table with given allocator 2620db9299fSJens Axboe * @table: The sg table header to use 2630db9299fSJens Axboe * @nents: Number of entries in sg list 2647cedb1f1SJames Bottomley * @max_ents: The maximum number of entries the allocator returns per call 2650db9299fSJens Axboe * @gfp_mask: GFP allocation mask 2660db9299fSJens Axboe * @alloc_fn: Allocator to use 2670db9299fSJens Axboe * 2687cedb1f1SJames Bottomley * Description: 2697cedb1f1SJames Bottomley * This function returns a @table @nents long. The allocator is 2707cedb1f1SJames Bottomley * defined to return scatterlist chunks of maximum size @max_ents. 2717cedb1f1SJames Bottomley * Thus if @nents is bigger than @max_ents, the scatterlists will be 2727cedb1f1SJames Bottomley * chained in units of @max_ents. 2737cedb1f1SJames Bottomley * 2740db9299fSJens Axboe * Notes: 2750db9299fSJens Axboe * If this function returns non-0 (eg failure), the caller must call 2760db9299fSJens Axboe * __sg_free_table() to cleanup any leftover allocations. 2770db9299fSJens Axboe * 2780db9299fSJens Axboe **/ 2797cedb1f1SJames Bottomley int __sg_alloc_table(struct sg_table *table, unsigned int nents, 280c53c6d6aSChristoph Hellwig unsigned int max_ents, struct scatterlist *first_chunk, 281c53c6d6aSChristoph Hellwig gfp_t gfp_mask, sg_alloc_fn *alloc_fn) 2820db9299fSJens Axboe { 2830db9299fSJens Axboe struct scatterlist *sg, *prv; 2840db9299fSJens Axboe unsigned int left; 2850db9299fSJens Axboe 28627daabd9SDan Carpenter memset(table, 0, sizeof(*table)); 28727daabd9SDan Carpenter 28827daabd9SDan Carpenter if (nents == 0) 28927daabd9SDan Carpenter return -EINVAL; 290308c09f1SLaura Abbott #ifndef CONFIG_ARCH_HAS_SG_CHAIN 2916fd59a83SNick Bowler if (WARN_ON_ONCE(nents > max_ents)) 2926fd59a83SNick Bowler return -EINVAL; 2930db9299fSJens Axboe #endif 2940db9299fSJens Axboe 2950db9299fSJens Axboe left = nents; 2960db9299fSJens Axboe prv = NULL; 2970db9299fSJens Axboe do { 2980db9299fSJens Axboe unsigned int sg_size, alloc_size = left; 2990db9299fSJens Axboe 3007cedb1f1SJames Bottomley if (alloc_size > max_ents) { 3017cedb1f1SJames Bottomley alloc_size = max_ents; 3020db9299fSJens Axboe sg_size = alloc_size - 1; 3030db9299fSJens Axboe } else 3040db9299fSJens Axboe sg_size = alloc_size; 3050db9299fSJens Axboe 3060db9299fSJens Axboe left -= sg_size; 3070db9299fSJens Axboe 308c53c6d6aSChristoph Hellwig if (first_chunk) { 309c53c6d6aSChristoph Hellwig sg = first_chunk; 310c53c6d6aSChristoph Hellwig first_chunk = NULL; 311c53c6d6aSChristoph Hellwig } else { 3120db9299fSJens Axboe sg = alloc_fn(alloc_size, gfp_mask); 313c53c6d6aSChristoph Hellwig } 314edce6820SJeffrey Carlyle if (unlikely(!sg)) { 315edce6820SJeffrey Carlyle /* 316edce6820SJeffrey Carlyle * Adjust entry count to reflect that the last 317edce6820SJeffrey Carlyle * entry of the previous table won't be used for 318edce6820SJeffrey Carlyle * linkage. Without this, sg_kfree() may get 319edce6820SJeffrey Carlyle * confused. 320edce6820SJeffrey Carlyle */ 321edce6820SJeffrey Carlyle if (prv) 322edce6820SJeffrey Carlyle table->nents = ++table->orig_nents; 323edce6820SJeffrey Carlyle 3240db9299fSJens Axboe return -ENOMEM; 325edce6820SJeffrey Carlyle } 3260db9299fSJens Axboe 3270db9299fSJens Axboe sg_init_table(sg, alloc_size); 3280db9299fSJens Axboe table->nents = table->orig_nents += sg_size; 3290db9299fSJens Axboe 3300db9299fSJens Axboe /* 3310db9299fSJens Axboe * If this is the first mapping, assign the sg table header. 3320db9299fSJens Axboe * If this is not the first mapping, chain previous part. 3330db9299fSJens Axboe */ 3340db9299fSJens Axboe if (prv) 3357cedb1f1SJames Bottomley sg_chain(prv, max_ents, sg); 3360db9299fSJens Axboe else 3370db9299fSJens Axboe table->sgl = sg; 3380db9299fSJens Axboe 3390db9299fSJens Axboe /* 3400db9299fSJens Axboe * If no more entries after this one, mark the end 3410db9299fSJens Axboe */ 3420db9299fSJens Axboe if (!left) 3430db9299fSJens Axboe sg_mark_end(&sg[sg_size - 1]); 3440db9299fSJens Axboe 3450db9299fSJens Axboe prv = sg; 3460db9299fSJens Axboe } while (left); 3470db9299fSJens Axboe 3480db9299fSJens Axboe return 0; 3490db9299fSJens Axboe } 3500db9299fSJens Axboe EXPORT_SYMBOL(__sg_alloc_table); 3510db9299fSJens Axboe 3520db9299fSJens Axboe /** 3530db9299fSJens Axboe * sg_alloc_table - Allocate and initialize an sg table 3540db9299fSJens Axboe * @table: The sg table header to use 3550db9299fSJens Axboe * @nents: Number of entries in sg list 3560db9299fSJens Axboe * @gfp_mask: GFP allocation mask 3570db9299fSJens Axboe * 3580db9299fSJens Axboe * Description: 3590db9299fSJens Axboe * Allocate and initialize an sg table. If @nents@ is larger than 3600db9299fSJens Axboe * SG_MAX_SINGLE_ALLOC a chained sg table will be setup. 3610db9299fSJens Axboe * 3620db9299fSJens Axboe **/ 3630db9299fSJens Axboe int sg_alloc_table(struct sg_table *table, unsigned int nents, gfp_t gfp_mask) 3640db9299fSJens Axboe { 3650db9299fSJens Axboe int ret; 3660db9299fSJens Axboe 3677cedb1f1SJames Bottomley ret = __sg_alloc_table(table, nents, SG_MAX_SINGLE_ALLOC, 368c53c6d6aSChristoph Hellwig NULL, gfp_mask, sg_kmalloc); 3690db9299fSJens Axboe if (unlikely(ret)) 370c53c6d6aSChristoph Hellwig __sg_free_table(table, SG_MAX_SINGLE_ALLOC, false, sg_kfree); 3710db9299fSJens Axboe 3720db9299fSJens Axboe return ret; 3730db9299fSJens Axboe } 3740db9299fSJens Axboe EXPORT_SYMBOL(sg_alloc_table); 375b1adaf65SFUJITA Tomonori 376b1adaf65SFUJITA Tomonori /** 377efc42bc9STomasz Stanislawski * sg_alloc_table_from_pages - Allocate and initialize an sg table from 378efc42bc9STomasz Stanislawski * an array of pages 379efc42bc9STomasz Stanislawski * @sgt: The sg table header to use 380efc42bc9STomasz Stanislawski * @pages: Pointer to an array of page pointers 381efc42bc9STomasz Stanislawski * @n_pages: Number of pages in the pages array 382efc42bc9STomasz Stanislawski * @offset: Offset from start of the first page to the start of a buffer 383efc42bc9STomasz Stanislawski * @size: Number of valid bytes in the buffer (after offset) 384efc42bc9STomasz Stanislawski * @gfp_mask: GFP allocation mask 385efc42bc9STomasz Stanislawski * 386efc42bc9STomasz Stanislawski * Description: 387efc42bc9STomasz Stanislawski * Allocate and initialize an sg table from a list of pages. Contiguous 388efc42bc9STomasz Stanislawski * ranges of the pages are squashed into a single scatterlist node. A user 389efc42bc9STomasz Stanislawski * may provide an offset at a start and a size of valid data in a buffer 390efc42bc9STomasz Stanislawski * specified by the page array. The returned sg table is released by 391efc42bc9STomasz Stanislawski * sg_free_table. 392efc42bc9STomasz Stanislawski * 393efc42bc9STomasz Stanislawski * Returns: 394efc42bc9STomasz Stanislawski * 0 on success, negative error on failure 395efc42bc9STomasz Stanislawski */ 396efc42bc9STomasz Stanislawski int sg_alloc_table_from_pages(struct sg_table *sgt, 397efc42bc9STomasz Stanislawski struct page **pages, unsigned int n_pages, 398efc42bc9STomasz Stanislawski unsigned long offset, unsigned long size, 399efc42bc9STomasz Stanislawski gfp_t gfp_mask) 400efc42bc9STomasz Stanislawski { 401efc42bc9STomasz Stanislawski unsigned int chunks; 402efc42bc9STomasz Stanislawski unsigned int i; 403efc42bc9STomasz Stanislawski unsigned int cur_page; 404efc42bc9STomasz Stanislawski int ret; 405efc42bc9STomasz Stanislawski struct scatterlist *s; 406efc42bc9STomasz Stanislawski 407efc42bc9STomasz Stanislawski /* compute number of contiguous chunks */ 408efc42bc9STomasz Stanislawski chunks = 1; 409efc42bc9STomasz Stanislawski for (i = 1; i < n_pages; ++i) 410efc42bc9STomasz Stanislawski if (page_to_pfn(pages[i]) != page_to_pfn(pages[i - 1]) + 1) 411efc42bc9STomasz Stanislawski ++chunks; 412efc42bc9STomasz Stanislawski 413efc42bc9STomasz Stanislawski ret = sg_alloc_table(sgt, chunks, gfp_mask); 414efc42bc9STomasz Stanislawski if (unlikely(ret)) 415efc42bc9STomasz Stanislawski return ret; 416efc42bc9STomasz Stanislawski 417efc42bc9STomasz Stanislawski /* merging chunks and putting them into the scatterlist */ 418efc42bc9STomasz Stanislawski cur_page = 0; 419efc42bc9STomasz Stanislawski for_each_sg(sgt->sgl, s, sgt->orig_nents, i) { 420efc42bc9STomasz Stanislawski unsigned long chunk_size; 421efc42bc9STomasz Stanislawski unsigned int j; 422efc42bc9STomasz Stanislawski 423efc42bc9STomasz Stanislawski /* look for the end of the current chunk */ 424efc42bc9STomasz Stanislawski for (j = cur_page + 1; j < n_pages; ++j) 425efc42bc9STomasz Stanislawski if (page_to_pfn(pages[j]) != 426efc42bc9STomasz Stanislawski page_to_pfn(pages[j - 1]) + 1) 427efc42bc9STomasz Stanislawski break; 428efc42bc9STomasz Stanislawski 429efc42bc9STomasz Stanislawski chunk_size = ((j - cur_page) << PAGE_SHIFT) - offset; 430efc42bc9STomasz Stanislawski sg_set_page(s, pages[cur_page], min(size, chunk_size), offset); 431efc42bc9STomasz Stanislawski size -= chunk_size; 432efc42bc9STomasz Stanislawski offset = 0; 433efc42bc9STomasz Stanislawski cur_page = j; 434efc42bc9STomasz Stanislawski } 435efc42bc9STomasz Stanislawski 436efc42bc9STomasz Stanislawski return 0; 437efc42bc9STomasz Stanislawski } 438efc42bc9STomasz Stanislawski EXPORT_SYMBOL(sg_alloc_table_from_pages); 439efc42bc9STomasz Stanislawski 440a321e91bSImre Deak void __sg_page_iter_start(struct sg_page_iter *piter, 441a321e91bSImre Deak struct scatterlist *sglist, unsigned int nents, 442a321e91bSImre Deak unsigned long pgoffset) 443a321e91bSImre Deak { 444a321e91bSImre Deak piter->__pg_advance = 0; 445a321e91bSImre Deak piter->__nents = nents; 446a321e91bSImre Deak 447a321e91bSImre Deak piter->sg = sglist; 448a321e91bSImre Deak piter->sg_pgoffset = pgoffset; 449a321e91bSImre Deak } 450a321e91bSImre Deak EXPORT_SYMBOL(__sg_page_iter_start); 451a321e91bSImre Deak 452a321e91bSImre Deak static int sg_page_count(struct scatterlist *sg) 453a321e91bSImre Deak { 454a321e91bSImre Deak return PAGE_ALIGN(sg->offset + sg->length) >> PAGE_SHIFT; 455a321e91bSImre Deak } 456a321e91bSImre Deak 457a321e91bSImre Deak bool __sg_page_iter_next(struct sg_page_iter *piter) 458a321e91bSImre Deak { 459a321e91bSImre Deak if (!piter->__nents || !piter->sg) 460a321e91bSImre Deak return false; 461a321e91bSImre Deak 462a321e91bSImre Deak piter->sg_pgoffset += piter->__pg_advance; 463a321e91bSImre Deak piter->__pg_advance = 1; 464a321e91bSImre Deak 465a321e91bSImre Deak while (piter->sg_pgoffset >= sg_page_count(piter->sg)) { 466a321e91bSImre Deak piter->sg_pgoffset -= sg_page_count(piter->sg); 467a321e91bSImre Deak piter->sg = sg_next(piter->sg); 468a321e91bSImre Deak if (!--piter->__nents || !piter->sg) 469a321e91bSImre Deak return false; 470a321e91bSImre Deak } 471a321e91bSImre Deak 472a321e91bSImre Deak return true; 473a321e91bSImre Deak } 474a321e91bSImre Deak EXPORT_SYMBOL(__sg_page_iter_next); 475a321e91bSImre Deak 476efc42bc9STomasz Stanislawski /** 477137d3edbSTejun Heo * sg_miter_start - start mapping iteration over a sg list 478137d3edbSTejun Heo * @miter: sg mapping iter to be started 479137d3edbSTejun Heo * @sgl: sg list to iterate over 480137d3edbSTejun Heo * @nents: number of sg entries 481137d3edbSTejun Heo * 482137d3edbSTejun Heo * Description: 483137d3edbSTejun Heo * Starts mapping iterator @miter. 484137d3edbSTejun Heo * 485137d3edbSTejun Heo * Context: 486137d3edbSTejun Heo * Don't care. 487137d3edbSTejun Heo */ 488137d3edbSTejun Heo void sg_miter_start(struct sg_mapping_iter *miter, struct scatterlist *sgl, 489137d3edbSTejun Heo unsigned int nents, unsigned int flags) 490137d3edbSTejun Heo { 491137d3edbSTejun Heo memset(miter, 0, sizeof(struct sg_mapping_iter)); 492137d3edbSTejun Heo 4934225fc85SImre Deak __sg_page_iter_start(&miter->piter, sgl, nents, 0); 4946de7e356SSebastian Andrzej Siewior WARN_ON(!(flags & (SG_MITER_TO_SG | SG_MITER_FROM_SG))); 495137d3edbSTejun Heo miter->__flags = flags; 496137d3edbSTejun Heo } 497137d3edbSTejun Heo EXPORT_SYMBOL(sg_miter_start); 498137d3edbSTejun Heo 49911052004SAkinobu Mita static bool sg_miter_get_next_page(struct sg_mapping_iter *miter) 50011052004SAkinobu Mita { 50111052004SAkinobu Mita if (!miter->__remaining) { 50211052004SAkinobu Mita struct scatterlist *sg; 50311052004SAkinobu Mita unsigned long pgoffset; 50411052004SAkinobu Mita 50511052004SAkinobu Mita if (!__sg_page_iter_next(&miter->piter)) 50611052004SAkinobu Mita return false; 50711052004SAkinobu Mita 50811052004SAkinobu Mita sg = miter->piter.sg; 50911052004SAkinobu Mita pgoffset = miter->piter.sg_pgoffset; 51011052004SAkinobu Mita 51111052004SAkinobu Mita miter->__offset = pgoffset ? 0 : sg->offset; 51211052004SAkinobu Mita miter->__remaining = sg->offset + sg->length - 51311052004SAkinobu Mita (pgoffset << PAGE_SHIFT) - miter->__offset; 51411052004SAkinobu Mita miter->__remaining = min_t(unsigned long, miter->__remaining, 51511052004SAkinobu Mita PAGE_SIZE - miter->__offset); 51611052004SAkinobu Mita } 51711052004SAkinobu Mita 51811052004SAkinobu Mita return true; 51911052004SAkinobu Mita } 52011052004SAkinobu Mita 521137d3edbSTejun Heo /** 522df642ceaSAkinobu Mita * sg_miter_skip - reposition mapping iterator 523df642ceaSAkinobu Mita * @miter: sg mapping iter to be skipped 524df642ceaSAkinobu Mita * @offset: number of bytes to plus the current location 525df642ceaSAkinobu Mita * 526df642ceaSAkinobu Mita * Description: 527df642ceaSAkinobu Mita * Sets the offset of @miter to its current location plus @offset bytes. 528df642ceaSAkinobu Mita * If mapping iterator @miter has been proceeded by sg_miter_next(), this 529df642ceaSAkinobu Mita * stops @miter. 530df642ceaSAkinobu Mita * 531df642ceaSAkinobu Mita * Context: 532df642ceaSAkinobu Mita * Don't care if @miter is stopped, or not proceeded yet. 533df642ceaSAkinobu Mita * Otherwise, preemption disabled if the SG_MITER_ATOMIC is set. 534df642ceaSAkinobu Mita * 535df642ceaSAkinobu Mita * Returns: 536df642ceaSAkinobu Mita * true if @miter contains the valid mapping. false if end of sg 537df642ceaSAkinobu Mita * list is reached. 538df642ceaSAkinobu Mita */ 5390d6077f8SMing Lei bool sg_miter_skip(struct sg_mapping_iter *miter, off_t offset) 540df642ceaSAkinobu Mita { 541df642ceaSAkinobu Mita sg_miter_stop(miter); 542df642ceaSAkinobu Mita 543df642ceaSAkinobu Mita while (offset) { 544df642ceaSAkinobu Mita off_t consumed; 545df642ceaSAkinobu Mita 546df642ceaSAkinobu Mita if (!sg_miter_get_next_page(miter)) 547df642ceaSAkinobu Mita return false; 548df642ceaSAkinobu Mita 549df642ceaSAkinobu Mita consumed = min_t(off_t, offset, miter->__remaining); 550df642ceaSAkinobu Mita miter->__offset += consumed; 551df642ceaSAkinobu Mita miter->__remaining -= consumed; 552df642ceaSAkinobu Mita offset -= consumed; 553df642ceaSAkinobu Mita } 554df642ceaSAkinobu Mita 555df642ceaSAkinobu Mita return true; 556df642ceaSAkinobu Mita } 5570d6077f8SMing Lei EXPORT_SYMBOL(sg_miter_skip); 558df642ceaSAkinobu Mita 559df642ceaSAkinobu Mita /** 560137d3edbSTejun Heo * sg_miter_next - proceed mapping iterator to the next mapping 561137d3edbSTejun Heo * @miter: sg mapping iter to proceed 562137d3edbSTejun Heo * 563137d3edbSTejun Heo * Description: 5648290e2d2STejun Heo * Proceeds @miter to the next mapping. @miter should have been started 5658290e2d2STejun Heo * using sg_miter_start(). On successful return, @miter->page, 5668290e2d2STejun Heo * @miter->addr and @miter->length point to the current mapping. 567137d3edbSTejun Heo * 568137d3edbSTejun Heo * Context: 5698290e2d2STejun Heo * Preemption disabled if SG_MITER_ATOMIC. Preemption must stay disabled 5708290e2d2STejun Heo * till @miter is stopped. May sleep if !SG_MITER_ATOMIC. 571137d3edbSTejun Heo * 572137d3edbSTejun Heo * Returns: 573137d3edbSTejun Heo * true if @miter contains the next mapping. false if end of sg 574137d3edbSTejun Heo * list is reached. 575137d3edbSTejun Heo */ 576137d3edbSTejun Heo bool sg_miter_next(struct sg_mapping_iter *miter) 577137d3edbSTejun Heo { 578137d3edbSTejun Heo sg_miter_stop(miter); 579137d3edbSTejun Heo 5804225fc85SImre Deak /* 5814225fc85SImre Deak * Get to the next page if necessary. 5824225fc85SImre Deak * __remaining, __offset is adjusted by sg_miter_stop 5834225fc85SImre Deak */ 58411052004SAkinobu Mita if (!sg_miter_get_next_page(miter)) 58523c560a9STejun Heo return false; 5864225fc85SImre Deak 5872db76d7cSImre Deak miter->page = sg_page_iter_page(&miter->piter); 5884225fc85SImre Deak miter->consumed = miter->length = miter->__remaining; 589137d3edbSTejun Heo 590137d3edbSTejun Heo if (miter->__flags & SG_MITER_ATOMIC) 5914225fc85SImre Deak miter->addr = kmap_atomic(miter->page) + miter->__offset; 592137d3edbSTejun Heo else 5934225fc85SImre Deak miter->addr = kmap(miter->page) + miter->__offset; 594137d3edbSTejun Heo 595137d3edbSTejun Heo return true; 596137d3edbSTejun Heo } 597137d3edbSTejun Heo EXPORT_SYMBOL(sg_miter_next); 598137d3edbSTejun Heo 599137d3edbSTejun Heo /** 600137d3edbSTejun Heo * sg_miter_stop - stop mapping iteration 601137d3edbSTejun Heo * @miter: sg mapping iter to be stopped 602137d3edbSTejun Heo * 603137d3edbSTejun Heo * Description: 604137d3edbSTejun Heo * Stops mapping iterator @miter. @miter should have been started 605137d3edbSTejun Heo * started using sg_miter_start(). A stopped iteration can be 606137d3edbSTejun Heo * resumed by calling sg_miter_next() on it. This is useful when 607137d3edbSTejun Heo * resources (kmap) need to be released during iteration. 608137d3edbSTejun Heo * 609137d3edbSTejun Heo * Context: 6108290e2d2STejun Heo * Preemption disabled if the SG_MITER_ATOMIC is set. Don't care 6118290e2d2STejun Heo * otherwise. 612137d3edbSTejun Heo */ 613137d3edbSTejun Heo void sg_miter_stop(struct sg_mapping_iter *miter) 614137d3edbSTejun Heo { 615137d3edbSTejun Heo WARN_ON(miter->consumed > miter->length); 616137d3edbSTejun Heo 617137d3edbSTejun Heo /* drop resources from the last iteration */ 618137d3edbSTejun Heo if (miter->addr) { 619137d3edbSTejun Heo miter->__offset += miter->consumed; 6204225fc85SImre Deak miter->__remaining -= miter->consumed; 621137d3edbSTejun Heo 6223d77b50cSMing Lei if ((miter->__flags & SG_MITER_TO_SG) && 6233d77b50cSMing Lei !PageSlab(miter->page)) 6246de7e356SSebastian Andrzej Siewior flush_kernel_dcache_page(miter->page); 6256de7e356SSebastian Andrzej Siewior 626137d3edbSTejun Heo if (miter->__flags & SG_MITER_ATOMIC) { 6278290e2d2STejun Heo WARN_ON_ONCE(preemptible()); 628c3eede8eSCong Wang kunmap_atomic(miter->addr); 629137d3edbSTejun Heo } else 630f652c521SArjan van de Ven kunmap(miter->page); 631137d3edbSTejun Heo 632137d3edbSTejun Heo miter->page = NULL; 633137d3edbSTejun Heo miter->addr = NULL; 634137d3edbSTejun Heo miter->length = 0; 635137d3edbSTejun Heo miter->consumed = 0; 636137d3edbSTejun Heo } 637137d3edbSTejun Heo } 638137d3edbSTejun Heo EXPORT_SYMBOL(sg_miter_stop); 639137d3edbSTejun Heo 640137d3edbSTejun Heo /** 641b1adaf65SFUJITA Tomonori * sg_copy_buffer - Copy data between a linear buffer and an SG list 642b1adaf65SFUJITA Tomonori * @sgl: The SG list 643b1adaf65SFUJITA Tomonori * @nents: Number of SG entries 644b1adaf65SFUJITA Tomonori * @buf: Where to copy from 645b1adaf65SFUJITA Tomonori * @buflen: The number of bytes to copy 646df642ceaSAkinobu Mita * @skip: Number of bytes to skip before copying 647df642ceaSAkinobu Mita * @to_buffer: transfer direction (true == from an sg list to a 648df642ceaSAkinobu Mita * buffer, false == from a buffer to an sg list 649b1adaf65SFUJITA Tomonori * 650b1adaf65SFUJITA Tomonori * Returns the number of copied bytes. 651b1adaf65SFUJITA Tomonori * 652b1adaf65SFUJITA Tomonori **/ 653386ecb12SDave Gordon size_t sg_copy_buffer(struct scatterlist *sgl, unsigned int nents, void *buf, 654386ecb12SDave Gordon size_t buflen, off_t skip, bool to_buffer) 655b1adaf65SFUJITA Tomonori { 656137d3edbSTejun Heo unsigned int offset = 0; 657137d3edbSTejun Heo struct sg_mapping_iter miter; 65850bed2e2SFUJITA Tomonori unsigned long flags; 6596de7e356SSebastian Andrzej Siewior unsigned int sg_flags = SG_MITER_ATOMIC; 660b1adaf65SFUJITA Tomonori 6616de7e356SSebastian Andrzej Siewior if (to_buffer) 6626de7e356SSebastian Andrzej Siewior sg_flags |= SG_MITER_FROM_SG; 6636de7e356SSebastian Andrzej Siewior else 6646de7e356SSebastian Andrzej Siewior sg_flags |= SG_MITER_TO_SG; 6656de7e356SSebastian Andrzej Siewior 6666de7e356SSebastian Andrzej Siewior sg_miter_start(&miter, sgl, nents, sg_flags); 667b1adaf65SFUJITA Tomonori 668df642ceaSAkinobu Mita if (!sg_miter_skip(&miter, skip)) 669df642ceaSAkinobu Mita return false; 670df642ceaSAkinobu Mita 67150bed2e2SFUJITA Tomonori local_irq_save(flags); 67250bed2e2SFUJITA Tomonori 673137d3edbSTejun Heo while (sg_miter_next(&miter) && offset < buflen) { 674137d3edbSTejun Heo unsigned int len; 675b1adaf65SFUJITA Tomonori 676137d3edbSTejun Heo len = min(miter.length, buflen - offset); 677b1adaf65SFUJITA Tomonori 678b1adaf65SFUJITA Tomonori if (to_buffer) 679137d3edbSTejun Heo memcpy(buf + offset, miter.addr, len); 6806de7e356SSebastian Andrzej Siewior else 681137d3edbSTejun Heo memcpy(miter.addr, buf + offset, len); 682b1adaf65SFUJITA Tomonori 683137d3edbSTejun Heo offset += len; 684b1adaf65SFUJITA Tomonori } 685b1adaf65SFUJITA Tomonori 686137d3edbSTejun Heo sg_miter_stop(&miter); 687b1adaf65SFUJITA Tomonori 68850bed2e2SFUJITA Tomonori local_irq_restore(flags); 689137d3edbSTejun Heo return offset; 690b1adaf65SFUJITA Tomonori } 691386ecb12SDave Gordon EXPORT_SYMBOL(sg_copy_buffer); 692b1adaf65SFUJITA Tomonori 693b1adaf65SFUJITA Tomonori /** 694b1adaf65SFUJITA Tomonori * sg_copy_from_buffer - Copy from a linear buffer to an SG list 695b1adaf65SFUJITA Tomonori * @sgl: The SG list 696b1adaf65SFUJITA Tomonori * @nents: Number of SG entries 697b1adaf65SFUJITA Tomonori * @buf: Where to copy from 698b1adaf65SFUJITA Tomonori * @buflen: The number of bytes to copy 699b1adaf65SFUJITA Tomonori * 700b1adaf65SFUJITA Tomonori * Returns the number of copied bytes. 701b1adaf65SFUJITA Tomonori * 702b1adaf65SFUJITA Tomonori **/ 703b1adaf65SFUJITA Tomonori size_t sg_copy_from_buffer(struct scatterlist *sgl, unsigned int nents, 7042a1bf8f9SDave Gordon const void *buf, size_t buflen) 705b1adaf65SFUJITA Tomonori { 7062a1bf8f9SDave Gordon return sg_copy_buffer(sgl, nents, (void *)buf, buflen, 0, false); 707b1adaf65SFUJITA Tomonori } 708b1adaf65SFUJITA Tomonori EXPORT_SYMBOL(sg_copy_from_buffer); 709b1adaf65SFUJITA Tomonori 710b1adaf65SFUJITA Tomonori /** 711b1adaf65SFUJITA Tomonori * sg_copy_to_buffer - Copy from an SG list to a linear buffer 712b1adaf65SFUJITA Tomonori * @sgl: The SG list 713b1adaf65SFUJITA Tomonori * @nents: Number of SG entries 714b1adaf65SFUJITA Tomonori * @buf: Where to copy to 715b1adaf65SFUJITA Tomonori * @buflen: The number of bytes to copy 716b1adaf65SFUJITA Tomonori * 717b1adaf65SFUJITA Tomonori * Returns the number of copied bytes. 718b1adaf65SFUJITA Tomonori * 719b1adaf65SFUJITA Tomonori **/ 720b1adaf65SFUJITA Tomonori size_t sg_copy_to_buffer(struct scatterlist *sgl, unsigned int nents, 721b1adaf65SFUJITA Tomonori void *buf, size_t buflen) 722b1adaf65SFUJITA Tomonori { 723df642ceaSAkinobu Mita return sg_copy_buffer(sgl, nents, buf, buflen, 0, true); 724b1adaf65SFUJITA Tomonori } 725b1adaf65SFUJITA Tomonori EXPORT_SYMBOL(sg_copy_to_buffer); 726df642ceaSAkinobu Mita 727df642ceaSAkinobu Mita /** 728df642ceaSAkinobu Mita * sg_pcopy_from_buffer - Copy from a linear buffer to an SG list 729df642ceaSAkinobu Mita * @sgl: The SG list 730df642ceaSAkinobu Mita * @nents: Number of SG entries 731df642ceaSAkinobu Mita * @buf: Where to copy from 732df642ceaSAkinobu Mita * @buflen: The number of bytes to copy 7334dc7daf8SDave Gordon * @skip: Number of bytes to skip before copying 734df642ceaSAkinobu Mita * 735df642ceaSAkinobu Mita * Returns the number of copied bytes. 736df642ceaSAkinobu Mita * 737df642ceaSAkinobu Mita **/ 738df642ceaSAkinobu Mita size_t sg_pcopy_from_buffer(struct scatterlist *sgl, unsigned int nents, 7392a1bf8f9SDave Gordon const void *buf, size_t buflen, off_t skip) 740df642ceaSAkinobu Mita { 7412a1bf8f9SDave Gordon return sg_copy_buffer(sgl, nents, (void *)buf, buflen, skip, false); 742df642ceaSAkinobu Mita } 743df642ceaSAkinobu Mita EXPORT_SYMBOL(sg_pcopy_from_buffer); 744df642ceaSAkinobu Mita 745df642ceaSAkinobu Mita /** 746df642ceaSAkinobu Mita * sg_pcopy_to_buffer - Copy from an SG list to a linear buffer 747df642ceaSAkinobu Mita * @sgl: The SG list 748df642ceaSAkinobu Mita * @nents: Number of SG entries 749df642ceaSAkinobu Mita * @buf: Where to copy to 750df642ceaSAkinobu Mita * @buflen: The number of bytes to copy 7514dc7daf8SDave Gordon * @skip: Number of bytes to skip before copying 752df642ceaSAkinobu Mita * 753df642ceaSAkinobu Mita * Returns the number of copied bytes. 754df642ceaSAkinobu Mita * 755df642ceaSAkinobu Mita **/ 756df642ceaSAkinobu Mita size_t sg_pcopy_to_buffer(struct scatterlist *sgl, unsigned int nents, 757df642ceaSAkinobu Mita void *buf, size_t buflen, off_t skip) 758df642ceaSAkinobu Mita { 759df642ceaSAkinobu Mita return sg_copy_buffer(sgl, nents, buf, buflen, skip, true); 760df642ceaSAkinobu Mita } 761df642ceaSAkinobu Mita EXPORT_SYMBOL(sg_pcopy_to_buffer); 762