xref: /openbmc/linux/lib/scatterlist.c (revision c698e482)
140b0b3f8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
20db9299fSJens Axboe /*
30db9299fSJens Axboe  * Copyright (C) 2007 Jens Axboe <jens.axboe@oracle.com>
40db9299fSJens Axboe  *
50db9299fSJens Axboe  * Scatterlist handling helpers.
60db9299fSJens Axboe  */
78bc3bcc9SPaul Gortmaker #include <linux/export.h>
85a0e3ad6STejun Heo #include <linux/slab.h>
90db9299fSJens Axboe #include <linux/scatterlist.h>
10b1adaf65SFUJITA Tomonori #include <linux/highmem.h>
11b94de9bbSChris Wilson #include <linux/kmemleak.h>
12f5f82cd1SDavid Howells #include <linux/bvec.h>
13f5f82cd1SDavid Howells #include <linux/uio.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  **/
sg_next(struct scatterlist * sg)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:
439dbbc3b9SZhen Lei  * Allows to know how many entries are in sg, taking into account
442e484610SMaxim Levitsky  * chaining as well
452e484610SMaxim Levitsky  *
462e484610SMaxim Levitsky  **/
sg_nents(struct scatterlist * sg)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
649dbbc3b9SZhen Lei  * the supplied length, taking into account chaining as well
65cfaed10dSTom Lendacky  *
66cfaed10dSTom Lendacky  * Returns:
67cfaed10dSTom Lendacky  *   the number of sg entries needed, negative error on failure
68cfaed10dSTom Lendacky  *
69cfaed10dSTom Lendacky  **/
sg_nents_for_len(struct scatterlist * sg,u64 len)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  **/
sg_last(struct scatterlist * sgl,unsigned int nents)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  **/
sg_init_table(struct scatterlist * sgl,unsigned int nents)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  **/
sg_init_one(struct scatterlist * sg,const void * buf,unsigned int buflen)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  */
sg_kmalloc(unsigned int nents,gfp_t gfp_mask)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 
sg_kfree(struct scatterlist * sg,unsigned int nents)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
1844635873cSMing Lei  * @nents_first_chunk: Number of entries int the (preallocated) first
1854635873cSMing Lei  * 	scatterlist chunk, 0 means no such preallocated first chunk
1860db9299fSJens Axboe  * @free_fn:	Free function
1873e302dbcSMaor Gottlieb  * @num_ents:	Number of entries in the table
1880db9299fSJens Axboe  *
1890db9299fSJens Axboe  *  Description:
1907cedb1f1SJames Bottomley  *    Free an sg table previously allocated and setup with
1917cedb1f1SJames Bottomley  *    __sg_alloc_table().  The @max_ents value must be identical to
1927cedb1f1SJames Bottomley  *    that previously used with __sg_alloc_table().
1930db9299fSJens Axboe  *
1940db9299fSJens Axboe  **/
__sg_free_table(struct sg_table * table,unsigned int max_ents,unsigned int nents_first_chunk,sg_free_fn * free_fn,unsigned int num_ents)1957cedb1f1SJames Bottomley void __sg_free_table(struct sg_table *table, unsigned int max_ents,
1963e302dbcSMaor Gottlieb 		     unsigned int nents_first_chunk, sg_free_fn *free_fn,
1973e302dbcSMaor Gottlieb 		     unsigned int num_ents)
1980db9299fSJens Axboe {
1990db9299fSJens Axboe 	struct scatterlist *sgl, *next;
2004635873cSMing Lei 	unsigned curr_max_ents = nents_first_chunk ?: max_ents;
2010db9299fSJens Axboe 
2020db9299fSJens Axboe 	if (unlikely(!table->sgl))
2030db9299fSJens Axboe 		return;
2040db9299fSJens Axboe 
2050db9299fSJens Axboe 	sgl = table->sgl;
2063e302dbcSMaor Gottlieb 	while (num_ents) {
2073e302dbcSMaor Gottlieb 		unsigned int alloc_size = num_ents;
2080db9299fSJens Axboe 		unsigned int sg_size;
2090db9299fSJens Axboe 
2100db9299fSJens Axboe 		/*
2117cedb1f1SJames Bottomley 		 * If we have more than max_ents segments left,
2120db9299fSJens Axboe 		 * then assign 'next' to the sg table after the current one.
2130db9299fSJens Axboe 		 * sg_size is then one less than alloc size, since the last
2140db9299fSJens Axboe 		 * element is the chain pointer.
2150db9299fSJens Axboe 		 */
2164635873cSMing Lei 		if (alloc_size > curr_max_ents) {
2174635873cSMing Lei 			next = sg_chain_ptr(&sgl[curr_max_ents - 1]);
2184635873cSMing Lei 			alloc_size = curr_max_ents;
2190db9299fSJens Axboe 			sg_size = alloc_size - 1;
2200db9299fSJens Axboe 		} else {
2210db9299fSJens Axboe 			sg_size = alloc_size;
2220db9299fSJens Axboe 			next = NULL;
2230db9299fSJens Axboe 		}
2240db9299fSJens Axboe 
2253e302dbcSMaor Gottlieb 		num_ents -= sg_size;
2264635873cSMing Lei 		if (nents_first_chunk)
2274635873cSMing Lei 			nents_first_chunk = 0;
228c21e59d8STony Battersby 		else
229c21e59d8STony Battersby 			free_fn(sgl, alloc_size);
2300db9299fSJens Axboe 		sgl = next;
2314635873cSMing Lei 		curr_max_ents = max_ents;
2320db9299fSJens Axboe 	}
2330db9299fSJens Axboe 
2340db9299fSJens Axboe 	table->sgl = NULL;
2350db9299fSJens Axboe }
2360db9299fSJens Axboe EXPORT_SYMBOL(__sg_free_table);
2370db9299fSJens Axboe 
2380db9299fSJens Axboe /**
2393e302dbcSMaor Gottlieb  * sg_free_append_table - Free a previously allocated append sg table.
2403e302dbcSMaor Gottlieb  * @table:	 The mapped sg append table header
2413e302dbcSMaor Gottlieb  *
2423e302dbcSMaor Gottlieb  **/
sg_free_append_table(struct sg_append_table * table)2433e302dbcSMaor Gottlieb void sg_free_append_table(struct sg_append_table *table)
2443e302dbcSMaor Gottlieb {
2456d529ea8Swuchi 	__sg_free_table(&table->sgt, SG_MAX_SINGLE_ALLOC, 0, sg_kfree,
2463e302dbcSMaor Gottlieb 			table->total_nents);
2473e302dbcSMaor Gottlieb }
2483e302dbcSMaor Gottlieb EXPORT_SYMBOL(sg_free_append_table);
2493e302dbcSMaor Gottlieb 
2503e302dbcSMaor Gottlieb 
2513e302dbcSMaor Gottlieb /**
2520db9299fSJens Axboe  * sg_free_table - Free a previously allocated sg table
2530db9299fSJens Axboe  * @table:	The mapped sg table header
2540db9299fSJens Axboe  *
2550db9299fSJens Axboe  **/
sg_free_table(struct sg_table * table)2560db9299fSJens Axboe void sg_free_table(struct sg_table *table)
2570db9299fSJens Axboe {
2586d529ea8Swuchi 	__sg_free_table(table, SG_MAX_SINGLE_ALLOC, 0, sg_kfree,
2593e302dbcSMaor Gottlieb 			table->orig_nents);
2600db9299fSJens Axboe }
2610db9299fSJens Axboe EXPORT_SYMBOL(sg_free_table);
2620db9299fSJens Axboe 
2630db9299fSJens Axboe /**
2640db9299fSJens Axboe  * __sg_alloc_table - Allocate and initialize an sg table with given allocator
2650db9299fSJens Axboe  * @table:	The sg table header to use
2660db9299fSJens Axboe  * @nents:	Number of entries in sg list
2677cedb1f1SJames Bottomley  * @max_ents:	The maximum number of entries the allocator returns per call
268c80da1fbSRandy Dunlap  * @first_chunk: first SGL if preallocated (may be %NULL)
269c80da1fbSRandy Dunlap  * @nents_first_chunk: Number of entries in the (preallocated) first
2704635873cSMing Lei  * 	scatterlist chunk, 0 means no such preallocated chunk provided by user
2710db9299fSJens Axboe  * @gfp_mask:	GFP allocation mask
2720db9299fSJens Axboe  * @alloc_fn:	Allocator to use
2730db9299fSJens Axboe  *
2747cedb1f1SJames Bottomley  * Description:
2757cedb1f1SJames Bottomley  *   This function returns a @table @nents long. The allocator is
2767cedb1f1SJames Bottomley  *   defined to return scatterlist chunks of maximum size @max_ents.
2777cedb1f1SJames Bottomley  *   Thus if @nents is bigger than @max_ents, the scatterlists will be
2787cedb1f1SJames Bottomley  *   chained in units of @max_ents.
2797cedb1f1SJames Bottomley  *
2800db9299fSJens Axboe  * Notes:
2810db9299fSJens Axboe  *   If this function returns non-0 (eg failure), the caller must call
2820db9299fSJens Axboe  *   __sg_free_table() to cleanup any leftover allocations.
2830db9299fSJens Axboe  *
2840db9299fSJens Axboe  **/
__sg_alloc_table(struct sg_table * table,unsigned int nents,unsigned int max_ents,struct scatterlist * first_chunk,unsigned int nents_first_chunk,gfp_t gfp_mask,sg_alloc_fn * alloc_fn)2857cedb1f1SJames Bottomley int __sg_alloc_table(struct sg_table *table, unsigned int nents,
286c53c6d6aSChristoph Hellwig 		     unsigned int max_ents, struct scatterlist *first_chunk,
2874635873cSMing Lei 		     unsigned int nents_first_chunk, gfp_t gfp_mask,
2884635873cSMing Lei 		     sg_alloc_fn *alloc_fn)
2890db9299fSJens Axboe {
2900db9299fSJens Axboe 	struct scatterlist *sg, *prv;
2910db9299fSJens Axboe 	unsigned int left;
2924635873cSMing Lei 	unsigned curr_max_ents = nents_first_chunk ?: max_ents;
2934635873cSMing Lei 	unsigned prv_max_ents;
2940db9299fSJens Axboe 
29527daabd9SDan Carpenter 	memset(table, 0, sizeof(*table));
29627daabd9SDan Carpenter 
29727daabd9SDan Carpenter 	if (nents == 0)
29827daabd9SDan Carpenter 		return -EINVAL;
2997c703e54SChristoph Hellwig #ifdef CONFIG_ARCH_NO_SG_CHAIN
3006fd59a83SNick Bowler 	if (WARN_ON_ONCE(nents > max_ents))
3016fd59a83SNick Bowler 		return -EINVAL;
3020db9299fSJens Axboe #endif
3030db9299fSJens Axboe 
3040db9299fSJens Axboe 	left = nents;
3050db9299fSJens Axboe 	prv = NULL;
3060db9299fSJens Axboe 	do {
3070db9299fSJens Axboe 		unsigned int sg_size, alloc_size = left;
3080db9299fSJens Axboe 
3094635873cSMing Lei 		if (alloc_size > curr_max_ents) {
3104635873cSMing Lei 			alloc_size = curr_max_ents;
3110db9299fSJens Axboe 			sg_size = alloc_size - 1;
3120db9299fSJens Axboe 		} else
3130db9299fSJens Axboe 			sg_size = alloc_size;
3140db9299fSJens Axboe 
3150db9299fSJens Axboe 		left -= sg_size;
3160db9299fSJens Axboe 
317c53c6d6aSChristoph Hellwig 		if (first_chunk) {
318c53c6d6aSChristoph Hellwig 			sg = first_chunk;
319c53c6d6aSChristoph Hellwig 			first_chunk = NULL;
320c53c6d6aSChristoph Hellwig 		} else {
3210db9299fSJens Axboe 			sg = alloc_fn(alloc_size, gfp_mask);
322c53c6d6aSChristoph Hellwig 		}
323edce6820SJeffrey Carlyle 		if (unlikely(!sg)) {
324edce6820SJeffrey Carlyle 			/*
325edce6820SJeffrey Carlyle 			 * Adjust entry count to reflect that the last
326edce6820SJeffrey Carlyle 			 * entry of the previous table won't be used for
327edce6820SJeffrey Carlyle 			 * linkage.  Without this, sg_kfree() may get
328edce6820SJeffrey Carlyle 			 * confused.
329edce6820SJeffrey Carlyle 			 */
330edce6820SJeffrey Carlyle 			if (prv)
331edce6820SJeffrey Carlyle 				table->nents = ++table->orig_nents;
332edce6820SJeffrey Carlyle 
3330db9299fSJens Axboe 			return -ENOMEM;
334edce6820SJeffrey Carlyle 		}
3350db9299fSJens Axboe 
3360db9299fSJens Axboe 		sg_init_table(sg, alloc_size);
3370db9299fSJens Axboe 		table->nents = table->orig_nents += sg_size;
3380db9299fSJens Axboe 
3390db9299fSJens Axboe 		/*
3400db9299fSJens Axboe 		 * If this is the first mapping, assign the sg table header.
3410db9299fSJens Axboe 		 * If this is not the first mapping, chain previous part.
3420db9299fSJens Axboe 		 */
3430db9299fSJens Axboe 		if (prv)
3444635873cSMing Lei 			sg_chain(prv, prv_max_ents, sg);
3450db9299fSJens Axboe 		else
3460db9299fSJens Axboe 			table->sgl = sg;
3470db9299fSJens Axboe 
3480db9299fSJens Axboe 		/*
3490db9299fSJens Axboe 		 * If no more entries after this one, mark the end
3500db9299fSJens Axboe 		 */
3510db9299fSJens Axboe 		if (!left)
3520db9299fSJens Axboe 			sg_mark_end(&sg[sg_size - 1]);
3530db9299fSJens Axboe 
3540db9299fSJens Axboe 		prv = sg;
3554635873cSMing Lei 		prv_max_ents = curr_max_ents;
3564635873cSMing Lei 		curr_max_ents = max_ents;
3570db9299fSJens Axboe 	} while (left);
3580db9299fSJens Axboe 
3590db9299fSJens Axboe 	return 0;
3600db9299fSJens Axboe }
3610db9299fSJens Axboe EXPORT_SYMBOL(__sg_alloc_table);
3620db9299fSJens Axboe 
3630db9299fSJens Axboe /**
3640db9299fSJens Axboe  * sg_alloc_table - Allocate and initialize an sg table
3650db9299fSJens Axboe  * @table:	The sg table header to use
3660db9299fSJens Axboe  * @nents:	Number of entries in sg list
3670db9299fSJens Axboe  * @gfp_mask:	GFP allocation mask
3680db9299fSJens Axboe  *
3690db9299fSJens Axboe  *  Description:
3700db9299fSJens Axboe  *    Allocate and initialize an sg table. If @nents@ is larger than
3710db9299fSJens Axboe  *    SG_MAX_SINGLE_ALLOC a chained sg table will be setup.
3720db9299fSJens Axboe  *
3730db9299fSJens Axboe  **/
sg_alloc_table(struct sg_table * table,unsigned int nents,gfp_t gfp_mask)3740db9299fSJens Axboe int sg_alloc_table(struct sg_table *table, unsigned int nents, gfp_t gfp_mask)
3750db9299fSJens Axboe {
3760db9299fSJens Axboe 	int ret;
3770db9299fSJens Axboe 
3787cedb1f1SJames Bottomley 	ret = __sg_alloc_table(table, nents, SG_MAX_SINGLE_ALLOC,
3794635873cSMing Lei 			       NULL, 0, gfp_mask, sg_kmalloc);
3800db9299fSJens Axboe 	if (unlikely(ret))
3813e302dbcSMaor Gottlieb 		sg_free_table(table);
3820db9299fSJens Axboe 	return ret;
3830db9299fSJens Axboe }
3840db9299fSJens Axboe EXPORT_SYMBOL(sg_alloc_table);
385b1adaf65SFUJITA Tomonori 
get_next_sg(struct sg_append_table * table,struct scatterlist * cur,unsigned long needed_sges,gfp_t gfp_mask)3863e302dbcSMaor Gottlieb static struct scatterlist *get_next_sg(struct sg_append_table *table,
38707da1223SMaor Gottlieb 				       struct scatterlist *cur,
38807da1223SMaor Gottlieb 				       unsigned long needed_sges,
38907da1223SMaor Gottlieb 				       gfp_t gfp_mask)
39007da1223SMaor Gottlieb {
39107da1223SMaor Gottlieb 	struct scatterlist *new_sg, *next_sg;
39207da1223SMaor Gottlieb 	unsigned int alloc_size;
39307da1223SMaor Gottlieb 
39407da1223SMaor Gottlieb 	if (cur) {
39507da1223SMaor Gottlieb 		next_sg = sg_next(cur);
39607da1223SMaor Gottlieb 		/* Check if last entry should be keeped for chainning */
39707da1223SMaor Gottlieb 		if (!sg_is_last(next_sg) || needed_sges == 1)
39807da1223SMaor Gottlieb 			return next_sg;
39907da1223SMaor Gottlieb 	}
40007da1223SMaor Gottlieb 
40107da1223SMaor Gottlieb 	alloc_size = min_t(unsigned long, needed_sges, SG_MAX_SINGLE_ALLOC);
40207da1223SMaor Gottlieb 	new_sg = sg_kmalloc(alloc_size, gfp_mask);
40307da1223SMaor Gottlieb 	if (!new_sg)
40407da1223SMaor Gottlieb 		return ERR_PTR(-ENOMEM);
40507da1223SMaor Gottlieb 	sg_init_table(new_sg, alloc_size);
40607da1223SMaor Gottlieb 	if (cur) {
4073e302dbcSMaor Gottlieb 		table->total_nents += alloc_size - 1;
40807da1223SMaor Gottlieb 		__sg_chain(next_sg, new_sg);
40907da1223SMaor Gottlieb 	} else {
4103e302dbcSMaor Gottlieb 		table->sgt.sgl = new_sg;
4113e302dbcSMaor Gottlieb 		table->total_nents = alloc_size;
41207da1223SMaor Gottlieb 	}
41307da1223SMaor Gottlieb 	return new_sg;
41407da1223SMaor Gottlieb }
41507da1223SMaor Gottlieb 
pages_are_mergeable(struct page * a,struct page * b)4161567b49dSLogan Gunthorpe static bool pages_are_mergeable(struct page *a, struct page *b)
4171567b49dSLogan Gunthorpe {
4181567b49dSLogan Gunthorpe 	if (page_to_pfn(a) != page_to_pfn(b) + 1)
4191567b49dSLogan Gunthorpe 		return false;
4201567b49dSLogan Gunthorpe 	if (!zone_device_pages_have_same_pgmap(a, b))
4211567b49dSLogan Gunthorpe 		return false;
4221567b49dSLogan Gunthorpe 	return true;
4231567b49dSLogan Gunthorpe }
4241567b49dSLogan Gunthorpe 
425b1adaf65SFUJITA Tomonori /**
4263e302dbcSMaor Gottlieb  * sg_alloc_append_table_from_pages - Allocate and initialize an append sg
4273e302dbcSMaor Gottlieb  *                                    table from an array of pages
4283e302dbcSMaor Gottlieb  * @sgt_append:  The sg append table to use
429efc42bc9STomasz Stanislawski  * @pages:       Pointer to an array of page pointers
430efc42bc9STomasz Stanislawski  * @n_pages:     Number of pages in the pages array
431efc42bc9STomasz Stanislawski  * @offset:      Offset from start of the first page to the start of a buffer
432efc42bc9STomasz Stanislawski  * @size:        Number of valid bytes in the buffer (after offset)
4339a40401cSJason Gunthorpe  * @max_segment: Maximum size of a scatterlist element in bytes
43407da1223SMaor Gottlieb  * @left_pages:  Left pages caller have to set after this call
435efc42bc9STomasz Stanislawski  * @gfp_mask:	 GFP allocation mask
436efc42bc9STomasz Stanislawski  *
437efc42bc9STomasz Stanislawski  * Description:
4383e302dbcSMaor Gottlieb  *    In the first call it allocate and initialize an sg table from a list of
4393e302dbcSMaor Gottlieb  *    pages, else reuse the scatterlist from sgt_append. Contiguous ranges of
4403e302dbcSMaor Gottlieb  *    the pages are squashed into a single scatterlist entry up to the maximum
4413e302dbcSMaor Gottlieb  *    size specified in @max_segment.  A user may provide an offset at a start
4423e302dbcSMaor Gottlieb  *    and a size of valid data in a buffer specified by the page array. The
4433e302dbcSMaor Gottlieb  *    returned sg table is released by sg_free_append_table
444efc42bc9STomasz Stanislawski  *
445efc42bc9STomasz Stanislawski  * Returns:
4463e302dbcSMaor Gottlieb  *   0 on success, negative error on failure
44707da1223SMaor Gottlieb  *
44807da1223SMaor Gottlieb  * Notes:
44907da1223SMaor Gottlieb  *   If this function returns non-0 (eg failure), the caller must call
4503e302dbcSMaor Gottlieb  *   sg_free_append_table() to cleanup any leftover allocations.
4513e302dbcSMaor Gottlieb  *
4523e302dbcSMaor Gottlieb  *   In the fist call, sgt_append must by initialized.
453efc42bc9STomasz Stanislawski  */
sg_alloc_append_table_from_pages(struct sg_append_table * sgt_append,struct page ** pages,unsigned int n_pages,unsigned int offset,unsigned long size,unsigned int max_segment,unsigned int left_pages,gfp_t gfp_mask)4543e302dbcSMaor Gottlieb int sg_alloc_append_table_from_pages(struct sg_append_table *sgt_append,
45507da1223SMaor Gottlieb 		struct page **pages, unsigned int n_pages, unsigned int offset,
45689d8589cSTvrtko Ursulin 		unsigned long size, unsigned int max_segment,
4573e302dbcSMaor Gottlieb 		unsigned int left_pages, gfp_t gfp_mask)
458efc42bc9STomasz Stanislawski {
45907da1223SMaor Gottlieb 	unsigned int chunks, cur_page, seg_len, i, prv_len = 0;
46007da1223SMaor Gottlieb 	unsigned int added_nents = 0;
4613e302dbcSMaor Gottlieb 	struct scatterlist *s = sgt_append->prv;
4621567b49dSLogan Gunthorpe 	struct page *last_pg;
463efc42bc9STomasz Stanislawski 
4649a40401cSJason Gunthorpe 	/*
4659a40401cSJason Gunthorpe 	 * The algorithm below requires max_segment to be aligned to PAGE_SIZE
4669a40401cSJason Gunthorpe 	 * otherwise it can overshoot.
4679a40401cSJason Gunthorpe 	 */
4689a40401cSJason Gunthorpe 	max_segment = ALIGN_DOWN(max_segment, PAGE_SIZE);
4699a40401cSJason Gunthorpe 	if (WARN_ON(max_segment < PAGE_SIZE))
4703e302dbcSMaor Gottlieb 		return -EINVAL;
47107da1223SMaor Gottlieb 
4723e302dbcSMaor Gottlieb 	if (IS_ENABLED(CONFIG_ARCH_NO_SG_CHAIN) && sgt_append->prv)
4733e302dbcSMaor Gottlieb 		return -EOPNOTSUPP;
47407da1223SMaor Gottlieb 
4753e302dbcSMaor Gottlieb 	if (sgt_append->prv) {
4760f097f08SYishai Hadas 		unsigned long next_pfn = (page_to_phys(sg_page(sgt_append->prv)) +
4770f097f08SYishai Hadas 			sgt_append->prv->offset + sgt_append->prv->length) / PAGE_SIZE;
4780f097f08SYishai Hadas 
47907da1223SMaor Gottlieb 		if (WARN_ON(offset))
4803e302dbcSMaor Gottlieb 			return -EINVAL;
48107da1223SMaor Gottlieb 
48207da1223SMaor Gottlieb 		/* Merge contiguous pages into the last SG */
4833e302dbcSMaor Gottlieb 		prv_len = sgt_append->prv->length;
4840f097f08SYishai Hadas 		if (page_to_pfn(pages[0]) == next_pfn) {
4850f097f08SYishai Hadas 			last_pg = pfn_to_page(next_pfn - 1);
486e95d50d7SYishai Hadas 			while (n_pages && pages_are_mergeable(pages[0], last_pg)) {
4873e302dbcSMaor Gottlieb 				if (sgt_append->prv->length + PAGE_SIZE > max_segment)
48807da1223SMaor Gottlieb 					break;
4893e302dbcSMaor Gottlieb 				sgt_append->prv->length += PAGE_SIZE;
4901567b49dSLogan Gunthorpe 				last_pg = pages[0];
49107da1223SMaor Gottlieb 				pages++;
49207da1223SMaor Gottlieb 				n_pages--;
49307da1223SMaor Gottlieb 			}
49407da1223SMaor Gottlieb 			if (!n_pages)
49507da1223SMaor Gottlieb 				goto out;
49607da1223SMaor Gottlieb 		}
4970f097f08SYishai Hadas 	}
49889d8589cSTvrtko Ursulin 
499efc42bc9STomasz Stanislawski 	/* compute number of contiguous chunks */
500efc42bc9STomasz Stanislawski 	chunks = 1;
501c125906bSTvrtko Ursulin 	seg_len = 0;
502c125906bSTvrtko Ursulin 	for (i = 1; i < n_pages; i++) {
503c125906bSTvrtko Ursulin 		seg_len += PAGE_SIZE;
504c125906bSTvrtko Ursulin 		if (seg_len >= max_segment ||
5051567b49dSLogan Gunthorpe 		    !pages_are_mergeable(pages[i], pages[i - 1])) {
506c125906bSTvrtko Ursulin 			chunks++;
507c125906bSTvrtko Ursulin 			seg_len = 0;
508c125906bSTvrtko Ursulin 		}
509c125906bSTvrtko Ursulin 	}
510efc42bc9STomasz Stanislawski 
511efc42bc9STomasz Stanislawski 	/* merging chunks and putting them into the scatterlist */
512efc42bc9STomasz Stanislawski 	cur_page = 0;
51307da1223SMaor Gottlieb 	for (i = 0; i < chunks; i++) {
514c125906bSTvrtko Ursulin 		unsigned int j, chunk_size;
515efc42bc9STomasz Stanislawski 
516efc42bc9STomasz Stanislawski 		/* look for the end of the current chunk */
517c125906bSTvrtko Ursulin 		seg_len = 0;
518c125906bSTvrtko Ursulin 		for (j = cur_page + 1; j < n_pages; j++) {
519c125906bSTvrtko Ursulin 			seg_len += PAGE_SIZE;
520c125906bSTvrtko Ursulin 			if (seg_len >= max_segment ||
5211567b49dSLogan Gunthorpe 			    !pages_are_mergeable(pages[j], pages[j - 1]))
522efc42bc9STomasz Stanislawski 				break;
523c125906bSTvrtko Ursulin 		}
524efc42bc9STomasz Stanislawski 
52507da1223SMaor Gottlieb 		/* Pass how many chunks might be left */
5263e302dbcSMaor Gottlieb 		s = get_next_sg(sgt_append, s, chunks - i + left_pages,
5273e302dbcSMaor Gottlieb 				gfp_mask);
52807da1223SMaor Gottlieb 		if (IS_ERR(s)) {
52907da1223SMaor Gottlieb 			/*
53007da1223SMaor Gottlieb 			 * Adjust entry length to be as before function was
53107da1223SMaor Gottlieb 			 * called.
53207da1223SMaor Gottlieb 			 */
5333e302dbcSMaor Gottlieb 			if (sgt_append->prv)
5343e302dbcSMaor Gottlieb 				sgt_append->prv->length = prv_len;
5353e302dbcSMaor Gottlieb 			return PTR_ERR(s);
53607da1223SMaor Gottlieb 		}
537efc42bc9STomasz Stanislawski 		chunk_size = ((j - cur_page) << PAGE_SHIFT) - offset;
538c125906bSTvrtko Ursulin 		sg_set_page(s, pages[cur_page],
539c125906bSTvrtko Ursulin 			    min_t(unsigned long, size, chunk_size), offset);
54007da1223SMaor Gottlieb 		added_nents++;
541efc42bc9STomasz Stanislawski 		size -= chunk_size;
542efc42bc9STomasz Stanislawski 		offset = 0;
543efc42bc9STomasz Stanislawski 		cur_page = j;
544efc42bc9STomasz Stanislawski 	}
5453e302dbcSMaor Gottlieb 	sgt_append->sgt.nents += added_nents;
5463e302dbcSMaor Gottlieb 	sgt_append->sgt.orig_nents = sgt_append->sgt.nents;
5473e302dbcSMaor Gottlieb 	sgt_append->prv = s;
54807da1223SMaor Gottlieb out:
54907da1223SMaor Gottlieb 	if (!left_pages)
55007da1223SMaor Gottlieb 		sg_mark_end(s);
5513e302dbcSMaor Gottlieb 	return 0;
552efc42bc9STomasz Stanislawski }
55390e7a6deSMaor Gottlieb EXPORT_SYMBOL(sg_alloc_append_table_from_pages);
55489d8589cSTvrtko Ursulin 
55589d8589cSTvrtko Ursulin /**
55690e7a6deSMaor Gottlieb  * sg_alloc_table_from_pages_segment - Allocate and initialize an sg table from
55790e7a6deSMaor Gottlieb  *                                     an array of pages and given maximum
55890e7a6deSMaor Gottlieb  *                                     segment.
55989d8589cSTvrtko Ursulin  * @sgt:	 The sg table header to use
56089d8589cSTvrtko Ursulin  * @pages:	 Pointer to an array of page pointers
56189d8589cSTvrtko Ursulin  * @n_pages:	 Number of pages in the pages array
56289d8589cSTvrtko Ursulin  * @offset:      Offset from start of the first page to the start of a buffer
56389d8589cSTvrtko Ursulin  * @size:        Number of valid bytes in the buffer (after offset)
56490e7a6deSMaor Gottlieb  * @max_segment: Maximum size of a scatterlist element in bytes
56589d8589cSTvrtko Ursulin  * @gfp_mask:	 GFP allocation mask
56689d8589cSTvrtko Ursulin  *
56789d8589cSTvrtko Ursulin  *  Description:
56889d8589cSTvrtko Ursulin  *    Allocate and initialize an sg table from a list of pages. Contiguous
56990e7a6deSMaor Gottlieb  *    ranges of the pages are squashed into a single scatterlist node up to the
57090e7a6deSMaor Gottlieb  *    maximum size specified in @max_segment. A user may provide an offset at a
57190e7a6deSMaor Gottlieb  *    start and a size of valid data in a buffer specified by the page array.
57290e7a6deSMaor Gottlieb  *
57390e7a6deSMaor Gottlieb  *    The returned sg table is released by sg_free_table.
57489d8589cSTvrtko Ursulin  *
57589d8589cSTvrtko Ursulin  *  Returns:
57689d8589cSTvrtko Ursulin  *   0 on success, negative error on failure
57789d8589cSTvrtko Ursulin  */
sg_alloc_table_from_pages_segment(struct sg_table * sgt,struct page ** pages,unsigned int n_pages,unsigned int offset,unsigned long size,unsigned int max_segment,gfp_t gfp_mask)57890e7a6deSMaor Gottlieb int sg_alloc_table_from_pages_segment(struct sg_table *sgt, struct page **pages,
57989d8589cSTvrtko Ursulin 				unsigned int n_pages, unsigned int offset,
58090e7a6deSMaor Gottlieb 				unsigned long size, unsigned int max_segment,
58190e7a6deSMaor Gottlieb 				gfp_t gfp_mask)
58289d8589cSTvrtko Ursulin {
5833e302dbcSMaor Gottlieb 	struct sg_append_table append = {};
5843e302dbcSMaor Gottlieb 	int err;
5853e302dbcSMaor Gottlieb 
5863e302dbcSMaor Gottlieb 	err = sg_alloc_append_table_from_pages(&append, pages, n_pages, offset,
5873e302dbcSMaor Gottlieb 					       size, max_segment, 0, gfp_mask);
5883e302dbcSMaor Gottlieb 	if (err) {
5893e302dbcSMaor Gottlieb 		sg_free_append_table(&append);
5903e302dbcSMaor Gottlieb 		return err;
5913e302dbcSMaor Gottlieb 	}
5923e302dbcSMaor Gottlieb 	memcpy(sgt, &append.sgt, sizeof(*sgt));
5933e302dbcSMaor Gottlieb 	WARN_ON(append.total_nents != sgt->orig_nents);
5943e302dbcSMaor Gottlieb 	return 0;
59589d8589cSTvrtko Ursulin }
59690e7a6deSMaor Gottlieb EXPORT_SYMBOL(sg_alloc_table_from_pages_segment);
597efc42bc9STomasz Stanislawski 
598e80a0af4SBart Van Assche #ifdef CONFIG_SGL_ALLOC
599e80a0af4SBart Van Assche 
600e80a0af4SBart Van Assche /**
601e80a0af4SBart Van Assche  * sgl_alloc_order - allocate a scatterlist and its pages
602e80a0af4SBart Van Assche  * @length: Length in bytes of the scatterlist. Must be at least one
603e80a0af4SBart Van Assche  * @order: Second argument for alloc_pages()
604e80a0af4SBart Van Assche  * @chainable: Whether or not to allocate an extra element in the scatterlist
605e80a0af4SBart Van Assche  *	for scatterlist chaining purposes
606e80a0af4SBart Van Assche  * @gfp: Memory allocation flags
607e80a0af4SBart Van Assche  * @nent_p: [out] Number of entries in the scatterlist that have pages
608e80a0af4SBart Van Assche  *
609e80a0af4SBart Van Assche  * Returns: A pointer to an initialized scatterlist or %NULL upon failure.
610e80a0af4SBart Van Assche  */
sgl_alloc_order(unsigned long long length,unsigned int order,bool chainable,gfp_t gfp,unsigned int * nent_p)611e80a0af4SBart Van Assche struct scatterlist *sgl_alloc_order(unsigned long long length,
612e80a0af4SBart Van Assche 				    unsigned int order, bool chainable,
613e80a0af4SBart Van Assche 				    gfp_t gfp, unsigned int *nent_p)
614e80a0af4SBart Van Assche {
615e80a0af4SBart Van Assche 	struct scatterlist *sgl, *sg;
616e80a0af4SBart Van Assche 	struct page *page;
617e80a0af4SBart Van Assche 	unsigned int nent, nalloc;
618e80a0af4SBart Van Assche 	u32 elem_len;
619e80a0af4SBart Van Assche 
620e80a0af4SBart Van Assche 	nent = round_up(length, PAGE_SIZE << order) >> (PAGE_SHIFT + order);
621e80a0af4SBart Van Assche 	/* Check for integer overflow */
622e80a0af4SBart Van Assche 	if (length > (nent << (PAGE_SHIFT + order)))
623e80a0af4SBart Van Assche 		return NULL;
624e80a0af4SBart Van Assche 	nalloc = nent;
625e80a0af4SBart Van Assche 	if (chainable) {
626e80a0af4SBart Van Assche 		/* Check for integer overflow */
627e80a0af4SBart Van Assche 		if (nalloc + 1 < nalloc)
628e80a0af4SBart Van Assche 			return NULL;
629e80a0af4SBart Van Assche 		nalloc++;
630e80a0af4SBart Van Assche 	}
631e80a0af4SBart Van Assche 	sgl = kmalloc_array(nalloc, sizeof(struct scatterlist),
6326ed9b92eSChristophe JAILLET 			    gfp & ~GFP_DMA);
633e80a0af4SBart Van Assche 	if (!sgl)
634e80a0af4SBart Van Assche 		return NULL;
635e80a0af4SBart Van Assche 
6368c7a8d1cSBart Van Assche 	sg_init_table(sgl, nalloc);
637e80a0af4SBart Van Assche 	sg = sgl;
638e80a0af4SBart Van Assche 	while (length) {
639e80a0af4SBart Van Assche 		elem_len = min_t(u64, length, PAGE_SIZE << order);
640e80a0af4SBart Van Assche 		page = alloc_pages(gfp, order);
641e80a0af4SBart Van Assche 		if (!page) {
642b2a182a4SDouglas Gilbert 			sgl_free_order(sgl, order);
643e80a0af4SBart Van Assche 			return NULL;
644e80a0af4SBart Van Assche 		}
645e80a0af4SBart Van Assche 
646e80a0af4SBart Van Assche 		sg_set_page(sg, page, elem_len, 0);
647e80a0af4SBart Van Assche 		length -= elem_len;
648e80a0af4SBart Van Assche 		sg = sg_next(sg);
649e80a0af4SBart Van Assche 	}
6508c7a8d1cSBart Van Assche 	WARN_ONCE(length, "length = %lld\n", length);
651e80a0af4SBart Van Assche 	if (nent_p)
652e80a0af4SBart Van Assche 		*nent_p = nent;
653e80a0af4SBart Van Assche 	return sgl;
654e80a0af4SBart Van Assche }
655e80a0af4SBart Van Assche EXPORT_SYMBOL(sgl_alloc_order);
656e80a0af4SBart Van Assche 
657e80a0af4SBart Van Assche /**
658e80a0af4SBart Van Assche  * sgl_alloc - allocate a scatterlist and its pages
659e80a0af4SBart Van Assche  * @length: Length in bytes of the scatterlist
660e80a0af4SBart Van Assche  * @gfp: Memory allocation flags
661e80a0af4SBart Van Assche  * @nent_p: [out] Number of entries in the scatterlist
662e80a0af4SBart Van Assche  *
663e80a0af4SBart Van Assche  * Returns: A pointer to an initialized scatterlist or %NULL upon failure.
664e80a0af4SBart Van Assche  */
sgl_alloc(unsigned long long length,gfp_t gfp,unsigned int * nent_p)665e80a0af4SBart Van Assche struct scatterlist *sgl_alloc(unsigned long long length, gfp_t gfp,
666e80a0af4SBart Van Assche 			      unsigned int *nent_p)
667e80a0af4SBart Van Assche {
668e80a0af4SBart Van Assche 	return sgl_alloc_order(length, 0, false, gfp, nent_p);
669e80a0af4SBart Van Assche }
670e80a0af4SBart Van Assche EXPORT_SYMBOL(sgl_alloc);
671e80a0af4SBart Van Assche 
672e80a0af4SBart Van Assche /**
6738c7a8d1cSBart Van Assche  * sgl_free_n_order - free a scatterlist and its pages
6748c7a8d1cSBart Van Assche  * @sgl: Scatterlist with one or more elements
6758c7a8d1cSBart Van Assche  * @nents: Maximum number of elements to free
6768c7a8d1cSBart Van Assche  * @order: Second argument for __free_pages()
6778c7a8d1cSBart Van Assche  *
6788c7a8d1cSBart Van Assche  * Notes:
6798c7a8d1cSBart Van Assche  * - If several scatterlists have been chained and each chain element is
6808c7a8d1cSBart Van Assche  *   freed separately then it's essential to set nents correctly to avoid that a
6818c7a8d1cSBart Van Assche  *   page would get freed twice.
6828c7a8d1cSBart Van Assche  * - All pages in a chained scatterlist can be freed at once by setting @nents
6838c7a8d1cSBart Van Assche  *   to a high number.
6848c7a8d1cSBart Van Assche  */
sgl_free_n_order(struct scatterlist * sgl,int nents,int order)6858c7a8d1cSBart Van Assche void sgl_free_n_order(struct scatterlist *sgl, int nents, int order)
6868c7a8d1cSBart Van Assche {
6878c7a8d1cSBart Van Assche 	struct scatterlist *sg;
6888c7a8d1cSBart Van Assche 	struct page *page;
6898c7a8d1cSBart Van Assche 	int i;
6908c7a8d1cSBart Van Assche 
6918c7a8d1cSBart Van Assche 	for_each_sg(sgl, sg, nents, i) {
6928c7a8d1cSBart Van Assche 		if (!sg)
6938c7a8d1cSBart Van Assche 			break;
6948c7a8d1cSBart Van Assche 		page = sg_page(sg);
6958c7a8d1cSBart Van Assche 		if (page)
6968c7a8d1cSBart Van Assche 			__free_pages(page, order);
6978c7a8d1cSBart Van Assche 	}
6988c7a8d1cSBart Van Assche 	kfree(sgl);
6998c7a8d1cSBart Van Assche }
7008c7a8d1cSBart Van Assche EXPORT_SYMBOL(sgl_free_n_order);
7018c7a8d1cSBart Van Assche 
7028c7a8d1cSBart Van Assche /**
703e80a0af4SBart Van Assche  * sgl_free_order - free a scatterlist and its pages
704e80a0af4SBart Van Assche  * @sgl: Scatterlist with one or more elements
705e80a0af4SBart Van Assche  * @order: Second argument for __free_pages()
706e80a0af4SBart Van Assche  */
sgl_free_order(struct scatterlist * sgl,int order)707e80a0af4SBart Van Assche void sgl_free_order(struct scatterlist *sgl, int order)
708e80a0af4SBart Van Assche {
7098c7a8d1cSBart Van Assche 	sgl_free_n_order(sgl, INT_MAX, order);
710e80a0af4SBart Van Assche }
711e80a0af4SBart Van Assche EXPORT_SYMBOL(sgl_free_order);
712e80a0af4SBart Van Assche 
713e80a0af4SBart Van Assche /**
714e80a0af4SBart Van Assche  * sgl_free - free a scatterlist and its pages
715e80a0af4SBart Van Assche  * @sgl: Scatterlist with one or more elements
716e80a0af4SBart Van Assche  */
sgl_free(struct scatterlist * sgl)717e80a0af4SBart Van Assche void sgl_free(struct scatterlist *sgl)
718e80a0af4SBart Van Assche {
719e80a0af4SBart Van Assche 	sgl_free_order(sgl, 0);
720e80a0af4SBart Van Assche }
721e80a0af4SBart Van Assche EXPORT_SYMBOL(sgl_free);
722e80a0af4SBart Van Assche 
723e80a0af4SBart Van Assche #endif /* CONFIG_SGL_ALLOC */
724e80a0af4SBart Van Assche 
__sg_page_iter_start(struct sg_page_iter * piter,struct scatterlist * sglist,unsigned int nents,unsigned long pgoffset)725a321e91bSImre Deak void __sg_page_iter_start(struct sg_page_iter *piter,
726a321e91bSImre Deak 			  struct scatterlist *sglist, unsigned int nents,
727a321e91bSImre Deak 			  unsigned long pgoffset)
728a321e91bSImre Deak {
729a321e91bSImre Deak 	piter->__pg_advance = 0;
730a321e91bSImre Deak 	piter->__nents = nents;
731a321e91bSImre Deak 
732a321e91bSImre Deak 	piter->sg = sglist;
733a321e91bSImre Deak 	piter->sg_pgoffset = pgoffset;
734a321e91bSImre Deak }
735a321e91bSImre Deak EXPORT_SYMBOL(__sg_page_iter_start);
736a321e91bSImre Deak 
sg_page_count(struct scatterlist * sg)737a321e91bSImre Deak static int sg_page_count(struct scatterlist *sg)
738a321e91bSImre Deak {
739a321e91bSImre Deak 	return PAGE_ALIGN(sg->offset + sg->length) >> PAGE_SHIFT;
740a321e91bSImre Deak }
741a321e91bSImre Deak 
__sg_page_iter_next(struct sg_page_iter * piter)742a321e91bSImre Deak bool __sg_page_iter_next(struct sg_page_iter *piter)
743a321e91bSImre Deak {
744a321e91bSImre Deak 	if (!piter->__nents || !piter->sg)
745a321e91bSImre Deak 		return false;
746a321e91bSImre Deak 
747a321e91bSImre Deak 	piter->sg_pgoffset += piter->__pg_advance;
748a321e91bSImre Deak 	piter->__pg_advance = 1;
749a321e91bSImre Deak 
750a321e91bSImre Deak 	while (piter->sg_pgoffset >= sg_page_count(piter->sg)) {
751a321e91bSImre Deak 		piter->sg_pgoffset -= sg_page_count(piter->sg);
752a321e91bSImre Deak 		piter->sg = sg_next(piter->sg);
753a321e91bSImre Deak 		if (!--piter->__nents || !piter->sg)
754a321e91bSImre Deak 			return false;
755a321e91bSImre Deak 	}
756a321e91bSImre Deak 
757a321e91bSImre Deak 	return true;
758a321e91bSImre Deak }
759a321e91bSImre Deak EXPORT_SYMBOL(__sg_page_iter_next);
760a321e91bSImre Deak 
sg_dma_page_count(struct scatterlist * sg)761d901b276SJason Gunthorpe static int sg_dma_page_count(struct scatterlist *sg)
762d901b276SJason Gunthorpe {
763d901b276SJason Gunthorpe 	return PAGE_ALIGN(sg->offset + sg_dma_len(sg)) >> PAGE_SHIFT;
764d901b276SJason Gunthorpe }
765d901b276SJason Gunthorpe 
__sg_page_iter_dma_next(struct sg_dma_page_iter * dma_iter)766d901b276SJason Gunthorpe bool __sg_page_iter_dma_next(struct sg_dma_page_iter *dma_iter)
767d901b276SJason Gunthorpe {
768d901b276SJason Gunthorpe 	struct sg_page_iter *piter = &dma_iter->base;
769d901b276SJason Gunthorpe 
770d901b276SJason Gunthorpe 	if (!piter->__nents || !piter->sg)
771d901b276SJason Gunthorpe 		return false;
772d901b276SJason Gunthorpe 
773d901b276SJason Gunthorpe 	piter->sg_pgoffset += piter->__pg_advance;
774d901b276SJason Gunthorpe 	piter->__pg_advance = 1;
775d901b276SJason Gunthorpe 
776d901b276SJason Gunthorpe 	while (piter->sg_pgoffset >= sg_dma_page_count(piter->sg)) {
777d901b276SJason Gunthorpe 		piter->sg_pgoffset -= sg_dma_page_count(piter->sg);
778d901b276SJason Gunthorpe 		piter->sg = sg_next(piter->sg);
779d901b276SJason Gunthorpe 		if (!--piter->__nents || !piter->sg)
780d901b276SJason Gunthorpe 			return false;
781d901b276SJason Gunthorpe 	}
782d901b276SJason Gunthorpe 
783d901b276SJason Gunthorpe 	return true;
784d901b276SJason Gunthorpe }
785d901b276SJason Gunthorpe EXPORT_SYMBOL(__sg_page_iter_dma_next);
786d901b276SJason Gunthorpe 
787efc42bc9STomasz Stanislawski /**
788137d3edbSTejun Heo  * sg_miter_start - start mapping iteration over a sg list
789137d3edbSTejun Heo  * @miter: sg mapping iter to be started
790137d3edbSTejun Heo  * @sgl: sg list to iterate over
791137d3edbSTejun Heo  * @nents: number of sg entries
792c80da1fbSRandy Dunlap  * @flags: sg iterator flags
793137d3edbSTejun Heo  *
794137d3edbSTejun Heo  * Description:
795137d3edbSTejun Heo  *   Starts mapping iterator @miter.
796137d3edbSTejun Heo  *
797137d3edbSTejun Heo  * Context:
798137d3edbSTejun Heo  *   Don't care.
799137d3edbSTejun Heo  */
sg_miter_start(struct sg_mapping_iter * miter,struct scatterlist * sgl,unsigned int nents,unsigned int flags)800137d3edbSTejun Heo void sg_miter_start(struct sg_mapping_iter *miter, struct scatterlist *sgl,
801137d3edbSTejun Heo 		    unsigned int nents, unsigned int flags)
802137d3edbSTejun Heo {
803137d3edbSTejun Heo 	memset(miter, 0, sizeof(struct sg_mapping_iter));
804137d3edbSTejun Heo 
8054225fc85SImre Deak 	__sg_page_iter_start(&miter->piter, sgl, nents, 0);
8066de7e356SSebastian Andrzej Siewior 	WARN_ON(!(flags & (SG_MITER_TO_SG | SG_MITER_FROM_SG)));
807137d3edbSTejun Heo 	miter->__flags = flags;
808137d3edbSTejun Heo }
809137d3edbSTejun Heo EXPORT_SYMBOL(sg_miter_start);
810137d3edbSTejun Heo 
sg_miter_get_next_page(struct sg_mapping_iter * miter)81111052004SAkinobu Mita static bool sg_miter_get_next_page(struct sg_mapping_iter *miter)
81211052004SAkinobu Mita {
81311052004SAkinobu Mita 	if (!miter->__remaining) {
81411052004SAkinobu Mita 		struct scatterlist *sg;
81511052004SAkinobu Mita 
81611052004SAkinobu Mita 		if (!__sg_page_iter_next(&miter->piter))
81711052004SAkinobu Mita 			return false;
81811052004SAkinobu Mita 
81911052004SAkinobu Mita 		sg = miter->piter.sg;
82011052004SAkinobu Mita 
821aeb87246SChristophe Leroy 		miter->__offset = miter->piter.sg_pgoffset ? 0 : sg->offset;
822aeb87246SChristophe Leroy 		miter->piter.sg_pgoffset += miter->__offset >> PAGE_SHIFT;
823aeb87246SChristophe Leroy 		miter->__offset &= PAGE_SIZE - 1;
82411052004SAkinobu Mita 		miter->__remaining = sg->offset + sg->length -
825aeb87246SChristophe Leroy 				     (miter->piter.sg_pgoffset << PAGE_SHIFT) -
826aeb87246SChristophe Leroy 				     miter->__offset;
82711052004SAkinobu Mita 		miter->__remaining = min_t(unsigned long, miter->__remaining,
82811052004SAkinobu Mita 					   PAGE_SIZE - miter->__offset);
82911052004SAkinobu Mita 	}
83011052004SAkinobu Mita 
83111052004SAkinobu Mita 	return true;
83211052004SAkinobu Mita }
83311052004SAkinobu Mita 
834137d3edbSTejun Heo /**
835df642ceaSAkinobu Mita  * sg_miter_skip - reposition mapping iterator
836df642ceaSAkinobu Mita  * @miter: sg mapping iter to be skipped
837df642ceaSAkinobu Mita  * @offset: number of bytes to plus the current location
838df642ceaSAkinobu Mita  *
839df642ceaSAkinobu Mita  * Description:
840df642ceaSAkinobu Mita  *   Sets the offset of @miter to its current location plus @offset bytes.
841df642ceaSAkinobu Mita  *   If mapping iterator @miter has been proceeded by sg_miter_next(), this
842df642ceaSAkinobu Mita  *   stops @miter.
843df642ceaSAkinobu Mita  *
844df642ceaSAkinobu Mita  * Context:
845723aca20SThomas Gleixner  *   Don't care.
846df642ceaSAkinobu Mita  *
847df642ceaSAkinobu Mita  * Returns:
848df642ceaSAkinobu Mita  *   true if @miter contains the valid mapping.  false if end of sg
849df642ceaSAkinobu Mita  *   list is reached.
850df642ceaSAkinobu Mita  */
sg_miter_skip(struct sg_mapping_iter * miter,off_t offset)8510d6077f8SMing Lei bool sg_miter_skip(struct sg_mapping_iter *miter, off_t offset)
852df642ceaSAkinobu Mita {
853df642ceaSAkinobu Mita 	sg_miter_stop(miter);
854df642ceaSAkinobu Mita 
855df642ceaSAkinobu Mita 	while (offset) {
856df642ceaSAkinobu Mita 		off_t consumed;
857df642ceaSAkinobu Mita 
858df642ceaSAkinobu Mita 		if (!sg_miter_get_next_page(miter))
859df642ceaSAkinobu Mita 			return false;
860df642ceaSAkinobu Mita 
861df642ceaSAkinobu Mita 		consumed = min_t(off_t, offset, miter->__remaining);
862df642ceaSAkinobu Mita 		miter->__offset += consumed;
863df642ceaSAkinobu Mita 		miter->__remaining -= consumed;
864df642ceaSAkinobu Mita 		offset -= consumed;
865df642ceaSAkinobu Mita 	}
866df642ceaSAkinobu Mita 
867df642ceaSAkinobu Mita 	return true;
868df642ceaSAkinobu Mita }
8690d6077f8SMing Lei EXPORT_SYMBOL(sg_miter_skip);
870df642ceaSAkinobu Mita 
871df642ceaSAkinobu Mita /**
872137d3edbSTejun Heo  * sg_miter_next - proceed mapping iterator to the next mapping
873137d3edbSTejun Heo  * @miter: sg mapping iter to proceed
874137d3edbSTejun Heo  *
875137d3edbSTejun Heo  * Description:
8768290e2d2STejun Heo  *   Proceeds @miter to the next mapping.  @miter should have been started
8778290e2d2STejun Heo  *   using sg_miter_start().  On successful return, @miter->page,
8788290e2d2STejun Heo  *   @miter->addr and @miter->length point to the current mapping.
879137d3edbSTejun Heo  *
880137d3edbSTejun Heo  * Context:
881723aca20SThomas Gleixner  *   May sleep if !SG_MITER_ATOMIC.
882137d3edbSTejun Heo  *
883137d3edbSTejun Heo  * Returns:
884137d3edbSTejun Heo  *   true if @miter contains the next mapping.  false if end of sg
885137d3edbSTejun Heo  *   list is reached.
886137d3edbSTejun Heo  */
sg_miter_next(struct sg_mapping_iter * miter)887137d3edbSTejun Heo bool sg_miter_next(struct sg_mapping_iter *miter)
888137d3edbSTejun Heo {
889137d3edbSTejun Heo 	sg_miter_stop(miter);
890137d3edbSTejun Heo 
8914225fc85SImre Deak 	/*
8924225fc85SImre Deak 	 * Get to the next page if necessary.
8934225fc85SImre Deak 	 * __remaining, __offset is adjusted by sg_miter_stop
8944225fc85SImre Deak 	 */
89511052004SAkinobu Mita 	if (!sg_miter_get_next_page(miter))
89623c560a9STejun Heo 		return false;
8974225fc85SImre Deak 
8982db76d7cSImre Deak 	miter->page = sg_page_iter_page(&miter->piter);
8994225fc85SImre Deak 	miter->consumed = miter->length = miter->__remaining;
900137d3edbSTejun Heo 
901137d3edbSTejun Heo 	if (miter->__flags & SG_MITER_ATOMIC)
9024225fc85SImre Deak 		miter->addr = kmap_atomic(miter->page) + miter->__offset;
903137d3edbSTejun Heo 	else
9044225fc85SImre Deak 		miter->addr = kmap(miter->page) + miter->__offset;
905137d3edbSTejun Heo 
906137d3edbSTejun Heo 	return true;
907137d3edbSTejun Heo }
908137d3edbSTejun Heo EXPORT_SYMBOL(sg_miter_next);
909137d3edbSTejun Heo 
910137d3edbSTejun Heo /**
911137d3edbSTejun Heo  * sg_miter_stop - stop mapping iteration
912137d3edbSTejun Heo  * @miter: sg mapping iter to be stopped
913137d3edbSTejun Heo  *
914137d3edbSTejun Heo  * Description:
915137d3edbSTejun Heo  *   Stops mapping iterator @miter.  @miter should have been started
9164ba6a2b2SMasahiro Yamada  *   using sg_miter_start().  A stopped iteration can be resumed by
9174ba6a2b2SMasahiro Yamada  *   calling sg_miter_next() on it.  This is useful when resources (kmap)
9184ba6a2b2SMasahiro Yamada  *   need to be released during iteration.
919137d3edbSTejun Heo  *
920137d3edbSTejun Heo  * Context:
921723aca20SThomas Gleixner  *   Don't care otherwise.
922137d3edbSTejun Heo  */
sg_miter_stop(struct sg_mapping_iter * miter)923137d3edbSTejun Heo void sg_miter_stop(struct sg_mapping_iter *miter)
924137d3edbSTejun Heo {
925137d3edbSTejun Heo 	WARN_ON(miter->consumed > miter->length);
926137d3edbSTejun Heo 
927137d3edbSTejun Heo 	/* drop resources from the last iteration */
928137d3edbSTejun Heo 	if (miter->addr) {
929137d3edbSTejun Heo 		miter->__offset += miter->consumed;
9304225fc85SImre Deak 		miter->__remaining -= miter->consumed;
931137d3edbSTejun Heo 
9320e84f5dbSChristoph Hellwig 		if (miter->__flags & SG_MITER_TO_SG)
9330e84f5dbSChristoph Hellwig 			flush_dcache_page(miter->page);
9346de7e356SSebastian Andrzej Siewior 
935137d3edbSTejun Heo 		if (miter->__flags & SG_MITER_ATOMIC) {
936723aca20SThomas Gleixner 			WARN_ON_ONCE(!pagefault_disabled());
937c3eede8eSCong Wang 			kunmap_atomic(miter->addr);
938137d3edbSTejun Heo 		} else
939f652c521SArjan van de Ven 			kunmap(miter->page);
940137d3edbSTejun Heo 
941137d3edbSTejun Heo 		miter->page = NULL;
942137d3edbSTejun Heo 		miter->addr = NULL;
943137d3edbSTejun Heo 		miter->length = 0;
944137d3edbSTejun Heo 		miter->consumed = 0;
945137d3edbSTejun Heo 	}
946137d3edbSTejun Heo }
947137d3edbSTejun Heo EXPORT_SYMBOL(sg_miter_stop);
948137d3edbSTejun Heo 
949137d3edbSTejun Heo /**
950b1adaf65SFUJITA Tomonori  * sg_copy_buffer - Copy data between a linear buffer and an SG list
951b1adaf65SFUJITA Tomonori  * @sgl:		 The SG list
952b1adaf65SFUJITA Tomonori  * @nents:		 Number of SG entries
953b1adaf65SFUJITA Tomonori  * @buf:		 Where to copy from
954b1adaf65SFUJITA Tomonori  * @buflen:		 The number of bytes to copy
955df642ceaSAkinobu Mita  * @skip:		 Number of bytes to skip before copying
956df642ceaSAkinobu Mita  * @to_buffer:		 transfer direction (true == from an sg list to a
9576e853185SGeert Uytterhoeven  *			 buffer, false == from a buffer to an sg list)
958b1adaf65SFUJITA Tomonori  *
959b1adaf65SFUJITA Tomonori  * Returns the number of copied bytes.
960b1adaf65SFUJITA Tomonori  *
961b1adaf65SFUJITA Tomonori  **/
sg_copy_buffer(struct scatterlist * sgl,unsigned int nents,void * buf,size_t buflen,off_t skip,bool to_buffer)962386ecb12SDave Gordon size_t sg_copy_buffer(struct scatterlist *sgl, unsigned int nents, void *buf,
963386ecb12SDave Gordon 		      size_t buflen, off_t skip, bool to_buffer)
964b1adaf65SFUJITA Tomonori {
965137d3edbSTejun Heo 	unsigned int offset = 0;
966137d3edbSTejun Heo 	struct sg_mapping_iter miter;
9676de7e356SSebastian Andrzej Siewior 	unsigned int sg_flags = SG_MITER_ATOMIC;
968b1adaf65SFUJITA Tomonori 
9696de7e356SSebastian Andrzej Siewior 	if (to_buffer)
9706de7e356SSebastian Andrzej Siewior 		sg_flags |= SG_MITER_FROM_SG;
9716de7e356SSebastian Andrzej Siewior 	else
9726de7e356SSebastian Andrzej Siewior 		sg_flags |= SG_MITER_TO_SG;
9736de7e356SSebastian Andrzej Siewior 
9746de7e356SSebastian Andrzej Siewior 	sg_miter_start(&miter, sgl, nents, sg_flags);
975b1adaf65SFUJITA Tomonori 
976df642ceaSAkinobu Mita 	if (!sg_miter_skip(&miter, skip))
9771f41be7dSDavid Disseldorp 		return 0;
978df642ceaSAkinobu Mita 
9791d5210efSGilad Ben-Yossef 	while ((offset < buflen) && sg_miter_next(&miter)) {
980137d3edbSTejun Heo 		unsigned int len;
981b1adaf65SFUJITA Tomonori 
982137d3edbSTejun Heo 		len = min(miter.length, buflen - offset);
983b1adaf65SFUJITA Tomonori 
984b1adaf65SFUJITA Tomonori 		if (to_buffer)
985137d3edbSTejun Heo 			memcpy(buf + offset, miter.addr, len);
9866de7e356SSebastian Andrzej Siewior 		else
987137d3edbSTejun Heo 			memcpy(miter.addr, buf + offset, len);
988b1adaf65SFUJITA Tomonori 
989137d3edbSTejun Heo 		offset += len;
990b1adaf65SFUJITA Tomonori 	}
991b1adaf65SFUJITA Tomonori 
992137d3edbSTejun Heo 	sg_miter_stop(&miter);
993b1adaf65SFUJITA Tomonori 
994137d3edbSTejun Heo 	return offset;
995b1adaf65SFUJITA Tomonori }
996386ecb12SDave Gordon EXPORT_SYMBOL(sg_copy_buffer);
997b1adaf65SFUJITA Tomonori 
998b1adaf65SFUJITA Tomonori /**
999b1adaf65SFUJITA Tomonori  * sg_copy_from_buffer - Copy from a linear buffer to an SG list
1000b1adaf65SFUJITA Tomonori  * @sgl:		 The SG list
1001b1adaf65SFUJITA Tomonori  * @nents:		 Number of SG entries
1002b1adaf65SFUJITA Tomonori  * @buf:		 Where to copy from
1003b1adaf65SFUJITA Tomonori  * @buflen:		 The number of bytes to copy
1004b1adaf65SFUJITA Tomonori  *
1005b1adaf65SFUJITA Tomonori  * Returns the number of copied bytes.
1006b1adaf65SFUJITA Tomonori  *
1007b1adaf65SFUJITA Tomonori  **/
sg_copy_from_buffer(struct scatterlist * sgl,unsigned int nents,const void * buf,size_t buflen)1008b1adaf65SFUJITA Tomonori size_t sg_copy_from_buffer(struct scatterlist *sgl, unsigned int nents,
10092a1bf8f9SDave Gordon 			   const void *buf, size_t buflen)
1010b1adaf65SFUJITA Tomonori {
10112a1bf8f9SDave Gordon 	return sg_copy_buffer(sgl, nents, (void *)buf, buflen, 0, false);
1012b1adaf65SFUJITA Tomonori }
1013b1adaf65SFUJITA Tomonori EXPORT_SYMBOL(sg_copy_from_buffer);
1014b1adaf65SFUJITA Tomonori 
1015b1adaf65SFUJITA Tomonori /**
1016b1adaf65SFUJITA Tomonori  * sg_copy_to_buffer - Copy from an SG list to a linear buffer
1017b1adaf65SFUJITA Tomonori  * @sgl:		 The SG list
1018b1adaf65SFUJITA Tomonori  * @nents:		 Number of SG entries
1019b1adaf65SFUJITA Tomonori  * @buf:		 Where to copy to
1020b1adaf65SFUJITA Tomonori  * @buflen:		 The number of bytes to copy
1021b1adaf65SFUJITA Tomonori  *
1022b1adaf65SFUJITA Tomonori  * Returns the number of copied bytes.
1023b1adaf65SFUJITA Tomonori  *
1024b1adaf65SFUJITA Tomonori  **/
sg_copy_to_buffer(struct scatterlist * sgl,unsigned int nents,void * buf,size_t buflen)1025b1adaf65SFUJITA Tomonori size_t sg_copy_to_buffer(struct scatterlist *sgl, unsigned int nents,
1026b1adaf65SFUJITA Tomonori 			 void *buf, size_t buflen)
1027b1adaf65SFUJITA Tomonori {
1028df642ceaSAkinobu Mita 	return sg_copy_buffer(sgl, nents, buf, buflen, 0, true);
1029b1adaf65SFUJITA Tomonori }
1030b1adaf65SFUJITA Tomonori EXPORT_SYMBOL(sg_copy_to_buffer);
1031df642ceaSAkinobu Mita 
1032df642ceaSAkinobu Mita /**
1033df642ceaSAkinobu Mita  * sg_pcopy_from_buffer - Copy from a linear buffer to an SG list
1034df642ceaSAkinobu Mita  * @sgl:		 The SG list
1035df642ceaSAkinobu Mita  * @nents:		 Number of SG entries
1036df642ceaSAkinobu Mita  * @buf:		 Where to copy from
1037df642ceaSAkinobu Mita  * @buflen:		 The number of bytes to copy
10384dc7daf8SDave Gordon  * @skip:		 Number of bytes to skip before copying
1039df642ceaSAkinobu Mita  *
1040df642ceaSAkinobu Mita  * Returns the number of copied bytes.
1041df642ceaSAkinobu Mita  *
1042df642ceaSAkinobu Mita  **/
sg_pcopy_from_buffer(struct scatterlist * sgl,unsigned int nents,const void * buf,size_t buflen,off_t skip)1043df642ceaSAkinobu Mita size_t sg_pcopy_from_buffer(struct scatterlist *sgl, unsigned int nents,
10442a1bf8f9SDave Gordon 			    const void *buf, size_t buflen, off_t skip)
1045df642ceaSAkinobu Mita {
10462a1bf8f9SDave Gordon 	return sg_copy_buffer(sgl, nents, (void *)buf, buflen, skip, false);
1047df642ceaSAkinobu Mita }
1048df642ceaSAkinobu Mita EXPORT_SYMBOL(sg_pcopy_from_buffer);
1049df642ceaSAkinobu Mita 
1050df642ceaSAkinobu Mita /**
1051df642ceaSAkinobu Mita  * sg_pcopy_to_buffer - Copy from an SG list to a linear buffer
1052df642ceaSAkinobu Mita  * @sgl:		 The SG list
1053df642ceaSAkinobu Mita  * @nents:		 Number of SG entries
1054df642ceaSAkinobu Mita  * @buf:		 Where to copy to
1055df642ceaSAkinobu Mita  * @buflen:		 The number of bytes to copy
10564dc7daf8SDave Gordon  * @skip:		 Number of bytes to skip before copying
1057df642ceaSAkinobu Mita  *
1058df642ceaSAkinobu Mita  * Returns the number of copied bytes.
1059df642ceaSAkinobu Mita  *
1060df642ceaSAkinobu Mita  **/
sg_pcopy_to_buffer(struct scatterlist * sgl,unsigned int nents,void * buf,size_t buflen,off_t skip)1061df642ceaSAkinobu Mita size_t sg_pcopy_to_buffer(struct scatterlist *sgl, unsigned int nents,
1062df642ceaSAkinobu Mita 			  void *buf, size_t buflen, off_t skip)
1063df642ceaSAkinobu Mita {
1064df642ceaSAkinobu Mita 	return sg_copy_buffer(sgl, nents, buf, buflen, skip, true);
1065df642ceaSAkinobu Mita }
1066df642ceaSAkinobu Mita EXPORT_SYMBOL(sg_pcopy_to_buffer);
10670945e569SJohannes Thumshirn 
10680945e569SJohannes Thumshirn /**
10690945e569SJohannes Thumshirn  * sg_zero_buffer - Zero-out a part of a SG list
10700945e569SJohannes Thumshirn  * @sgl:		 The SG list
10710945e569SJohannes Thumshirn  * @nents:		 Number of SG entries
10720945e569SJohannes Thumshirn  * @buflen:		 The number of bytes to zero out
10730945e569SJohannes Thumshirn  * @skip:		 Number of bytes to skip before zeroing
10740945e569SJohannes Thumshirn  *
10750945e569SJohannes Thumshirn  * Returns the number of bytes zeroed.
10760945e569SJohannes Thumshirn  **/
sg_zero_buffer(struct scatterlist * sgl,unsigned int nents,size_t buflen,off_t skip)10770945e569SJohannes Thumshirn size_t sg_zero_buffer(struct scatterlist *sgl, unsigned int nents,
10780945e569SJohannes Thumshirn 		       size_t buflen, off_t skip)
10790945e569SJohannes Thumshirn {
10800945e569SJohannes Thumshirn 	unsigned int offset = 0;
10810945e569SJohannes Thumshirn 	struct sg_mapping_iter miter;
10820945e569SJohannes Thumshirn 	unsigned int sg_flags = SG_MITER_ATOMIC | SG_MITER_TO_SG;
10830945e569SJohannes Thumshirn 
10840945e569SJohannes Thumshirn 	sg_miter_start(&miter, sgl, nents, sg_flags);
10850945e569SJohannes Thumshirn 
10860945e569SJohannes Thumshirn 	if (!sg_miter_skip(&miter, skip))
10870945e569SJohannes Thumshirn 		return false;
10880945e569SJohannes Thumshirn 
10890945e569SJohannes Thumshirn 	while (offset < buflen && sg_miter_next(&miter)) {
10900945e569SJohannes Thumshirn 		unsigned int len;
10910945e569SJohannes Thumshirn 
10920945e569SJohannes Thumshirn 		len = min(miter.length, buflen - offset);
10930945e569SJohannes Thumshirn 		memset(miter.addr, 0, len);
10940945e569SJohannes Thumshirn 
10950945e569SJohannes Thumshirn 		offset += len;
10960945e569SJohannes Thumshirn 	}
10970945e569SJohannes Thumshirn 
10980945e569SJohannes Thumshirn 	sg_miter_stop(&miter);
10990945e569SJohannes Thumshirn 	return offset;
11000945e569SJohannes Thumshirn }
11010945e569SJohannes Thumshirn EXPORT_SYMBOL(sg_zero_buffer);
1102f5f82cd1SDavid Howells 
1103f5f82cd1SDavid Howells /*
1104f5f82cd1SDavid Howells  * Extract and pin a list of up to sg_max pages from UBUF- or IOVEC-class
1105f5f82cd1SDavid Howells  * iterators, and add them to the scatterlist.
1106f5f82cd1SDavid Howells  */
extract_user_to_sg(struct iov_iter * iter,ssize_t maxsize,struct sg_table * sgtable,unsigned int sg_max,iov_iter_extraction_t extraction_flags)1107f5f82cd1SDavid Howells static ssize_t extract_user_to_sg(struct iov_iter *iter,
1108f5f82cd1SDavid Howells 				  ssize_t maxsize,
1109f5f82cd1SDavid Howells 				  struct sg_table *sgtable,
1110f5f82cd1SDavid Howells 				  unsigned int sg_max,
1111f5f82cd1SDavid Howells 				  iov_iter_extraction_t extraction_flags)
1112f5f82cd1SDavid Howells {
1113f5f82cd1SDavid Howells 	struct scatterlist *sg = sgtable->sgl + sgtable->nents;
1114f5f82cd1SDavid Howells 	struct page **pages;
1115f5f82cd1SDavid Howells 	unsigned int npages;
1116f5f82cd1SDavid Howells 	ssize_t ret = 0, res;
1117f5f82cd1SDavid Howells 	size_t len, off;
1118f5f82cd1SDavid Howells 
1119f5f82cd1SDavid Howells 	/* We decant the page list into the tail of the scatterlist */
1120f5f82cd1SDavid Howells 	pages = (void *)sgtable->sgl +
1121f5f82cd1SDavid Howells 		array_size(sg_max, sizeof(struct scatterlist));
1122f5f82cd1SDavid Howells 	pages -= sg_max;
1123f5f82cd1SDavid Howells 
1124f5f82cd1SDavid Howells 	do {
1125f5f82cd1SDavid Howells 		res = iov_iter_extract_pages(iter, &pages, maxsize, sg_max,
1126f5f82cd1SDavid Howells 					     extraction_flags, &off);
1127c698e482SDavid Howells 		if (res <= 0)
1128f5f82cd1SDavid Howells 			goto failed;
1129f5f82cd1SDavid Howells 
1130f5f82cd1SDavid Howells 		len = res;
1131f5f82cd1SDavid Howells 		maxsize -= len;
1132f5f82cd1SDavid Howells 		ret += len;
1133f5f82cd1SDavid Howells 		npages = DIV_ROUND_UP(off + len, PAGE_SIZE);
1134f5f82cd1SDavid Howells 		sg_max -= npages;
1135f5f82cd1SDavid Howells 
1136f5f82cd1SDavid Howells 		for (; npages > 0; npages--) {
1137f5f82cd1SDavid Howells 			struct page *page = *pages;
1138f5f82cd1SDavid Howells 			size_t seg = min_t(size_t, PAGE_SIZE - off, len);
1139f5f82cd1SDavid Howells 
1140f5f82cd1SDavid Howells 			*pages++ = NULL;
1141f5f82cd1SDavid Howells 			sg_set_page(sg, page, seg, off);
1142f5f82cd1SDavid Howells 			sgtable->nents++;
1143f5f82cd1SDavid Howells 			sg++;
1144f5f82cd1SDavid Howells 			len -= seg;
1145f5f82cd1SDavid Howells 			off = 0;
1146f5f82cd1SDavid Howells 		}
1147f5f82cd1SDavid Howells 	} while (maxsize > 0 && sg_max > 0);
1148f5f82cd1SDavid Howells 
1149f5f82cd1SDavid Howells 	return ret;
1150f5f82cd1SDavid Howells 
1151f5f82cd1SDavid Howells failed:
1152f5f82cd1SDavid Howells 	while (sgtable->nents > sgtable->orig_nents)
1153f443fd5aSDavid Howells 		unpin_user_page(sg_page(&sgtable->sgl[--sgtable->nents]));
1154f5f82cd1SDavid Howells 	return res;
1155f5f82cd1SDavid Howells }
1156f5f82cd1SDavid Howells 
1157f5f82cd1SDavid Howells /*
1158f5f82cd1SDavid Howells  * Extract up to sg_max pages from a BVEC-type iterator and add them to the
1159f5f82cd1SDavid Howells  * scatterlist.  The pages are not pinned.
1160f5f82cd1SDavid Howells  */
extract_bvec_to_sg(struct iov_iter * iter,ssize_t maxsize,struct sg_table * sgtable,unsigned int sg_max,iov_iter_extraction_t extraction_flags)1161f5f82cd1SDavid Howells static ssize_t extract_bvec_to_sg(struct iov_iter *iter,
1162f5f82cd1SDavid Howells 				  ssize_t maxsize,
1163f5f82cd1SDavid Howells 				  struct sg_table *sgtable,
1164f5f82cd1SDavid Howells 				  unsigned int sg_max,
1165f5f82cd1SDavid Howells 				  iov_iter_extraction_t extraction_flags)
1166f5f82cd1SDavid Howells {
1167f5f82cd1SDavid Howells 	const struct bio_vec *bv = iter->bvec;
1168f5f82cd1SDavid Howells 	struct scatterlist *sg = sgtable->sgl + sgtable->nents;
1169f5f82cd1SDavid Howells 	unsigned long start = iter->iov_offset;
1170f5f82cd1SDavid Howells 	unsigned int i;
1171f5f82cd1SDavid Howells 	ssize_t ret = 0;
1172f5f82cd1SDavid Howells 
1173f5f82cd1SDavid Howells 	for (i = 0; i < iter->nr_segs; i++) {
1174f5f82cd1SDavid Howells 		size_t off, len;
1175f5f82cd1SDavid Howells 
1176f5f82cd1SDavid Howells 		len = bv[i].bv_len;
1177f5f82cd1SDavid Howells 		if (start >= len) {
1178f5f82cd1SDavid Howells 			start -= len;
1179f5f82cd1SDavid Howells 			continue;
1180f5f82cd1SDavid Howells 		}
1181f5f82cd1SDavid Howells 
1182f5f82cd1SDavid Howells 		len = min_t(size_t, maxsize, len - start);
1183f5f82cd1SDavid Howells 		off = bv[i].bv_offset + start;
1184f5f82cd1SDavid Howells 
1185f5f82cd1SDavid Howells 		sg_set_page(sg, bv[i].bv_page, len, off);
1186f5f82cd1SDavid Howells 		sgtable->nents++;
1187f5f82cd1SDavid Howells 		sg++;
1188f5f82cd1SDavid Howells 		sg_max--;
1189f5f82cd1SDavid Howells 
1190f5f82cd1SDavid Howells 		ret += len;
1191f5f82cd1SDavid Howells 		maxsize -= len;
1192f5f82cd1SDavid Howells 		if (maxsize <= 0 || sg_max == 0)
1193f5f82cd1SDavid Howells 			break;
1194f5f82cd1SDavid Howells 		start = 0;
1195f5f82cd1SDavid Howells 	}
1196f5f82cd1SDavid Howells 
1197f5f82cd1SDavid Howells 	if (ret > 0)
1198f5f82cd1SDavid Howells 		iov_iter_advance(iter, ret);
1199f5f82cd1SDavid Howells 	return ret;
1200f5f82cd1SDavid Howells }
1201f5f82cd1SDavid Howells 
1202f5f82cd1SDavid Howells /*
1203f5f82cd1SDavid Howells  * Extract up to sg_max pages from a KVEC-type iterator and add them to the
1204f5f82cd1SDavid Howells  * scatterlist.  This can deal with vmalloc'd buffers as well as kmalloc'd or
1205f5f82cd1SDavid Howells  * static buffers.  The pages are not pinned.
1206f5f82cd1SDavid Howells  */
extract_kvec_to_sg(struct iov_iter * iter,ssize_t maxsize,struct sg_table * sgtable,unsigned int sg_max,iov_iter_extraction_t extraction_flags)1207f5f82cd1SDavid Howells static ssize_t extract_kvec_to_sg(struct iov_iter *iter,
1208f5f82cd1SDavid Howells 				  ssize_t maxsize,
1209f5f82cd1SDavid Howells 				  struct sg_table *sgtable,
1210f5f82cd1SDavid Howells 				  unsigned int sg_max,
1211f5f82cd1SDavid Howells 				  iov_iter_extraction_t extraction_flags)
1212f5f82cd1SDavid Howells {
1213f5f82cd1SDavid Howells 	const struct kvec *kv = iter->kvec;
1214f5f82cd1SDavid Howells 	struct scatterlist *sg = sgtable->sgl + sgtable->nents;
1215f5f82cd1SDavid Howells 	unsigned long start = iter->iov_offset;
1216f5f82cd1SDavid Howells 	unsigned int i;
1217f5f82cd1SDavid Howells 	ssize_t ret = 0;
1218f5f82cd1SDavid Howells 
1219f5f82cd1SDavid Howells 	for (i = 0; i < iter->nr_segs; i++) {
1220f5f82cd1SDavid Howells 		struct page *page;
1221f5f82cd1SDavid Howells 		unsigned long kaddr;
1222f5f82cd1SDavid Howells 		size_t off, len, seg;
1223f5f82cd1SDavid Howells 
1224f5f82cd1SDavid Howells 		len = kv[i].iov_len;
1225f5f82cd1SDavid Howells 		if (start >= len) {
1226f5f82cd1SDavid Howells 			start -= len;
1227f5f82cd1SDavid Howells 			continue;
1228f5f82cd1SDavid Howells 		}
1229f5f82cd1SDavid Howells 
1230f5f82cd1SDavid Howells 		kaddr = (unsigned long)kv[i].iov_base + start;
1231f5f82cd1SDavid Howells 		off = kaddr & ~PAGE_MASK;
1232f5f82cd1SDavid Howells 		len = min_t(size_t, maxsize, len - start);
1233f5f82cd1SDavid Howells 		kaddr &= PAGE_MASK;
1234f5f82cd1SDavid Howells 
1235f5f82cd1SDavid Howells 		maxsize -= len;
1236f5f82cd1SDavid Howells 		ret += len;
1237f5f82cd1SDavid Howells 		do {
1238f5f82cd1SDavid Howells 			seg = min_t(size_t, len, PAGE_SIZE - off);
1239f5f82cd1SDavid Howells 			if (is_vmalloc_or_module_addr((void *)kaddr))
1240f5f82cd1SDavid Howells 				page = vmalloc_to_page((void *)kaddr);
1241f5f82cd1SDavid Howells 			else
12427b82e904SLinus Torvalds 				page = virt_to_page((void *)kaddr);
1243f5f82cd1SDavid Howells 
1244f5f82cd1SDavid Howells 			sg_set_page(sg, page, len, off);
1245f5f82cd1SDavid Howells 			sgtable->nents++;
1246f5f82cd1SDavid Howells 			sg++;
1247f5f82cd1SDavid Howells 			sg_max--;
1248f5f82cd1SDavid Howells 
1249f5f82cd1SDavid Howells 			len -= seg;
1250f5f82cd1SDavid Howells 			kaddr += PAGE_SIZE;
1251f5f82cd1SDavid Howells 			off = 0;
1252f5f82cd1SDavid Howells 		} while (len > 0 && sg_max > 0);
1253f5f82cd1SDavid Howells 
1254f5f82cd1SDavid Howells 		if (maxsize <= 0 || sg_max == 0)
1255f5f82cd1SDavid Howells 			break;
1256f5f82cd1SDavid Howells 		start = 0;
1257f5f82cd1SDavid Howells 	}
1258f5f82cd1SDavid Howells 
1259f5f82cd1SDavid Howells 	if (ret > 0)
1260f5f82cd1SDavid Howells 		iov_iter_advance(iter, ret);
1261f5f82cd1SDavid Howells 	return ret;
1262f5f82cd1SDavid Howells }
1263f5f82cd1SDavid Howells 
1264f5f82cd1SDavid Howells /*
1265f5f82cd1SDavid Howells  * Extract up to sg_max folios from an XARRAY-type iterator and add them to
1266f5f82cd1SDavid Howells  * the scatterlist.  The pages are not pinned.
1267f5f82cd1SDavid Howells  */
extract_xarray_to_sg(struct iov_iter * iter,ssize_t maxsize,struct sg_table * sgtable,unsigned int sg_max,iov_iter_extraction_t extraction_flags)1268f5f82cd1SDavid Howells static ssize_t extract_xarray_to_sg(struct iov_iter *iter,
1269f5f82cd1SDavid Howells 				    ssize_t maxsize,
1270f5f82cd1SDavid Howells 				    struct sg_table *sgtable,
1271f5f82cd1SDavid Howells 				    unsigned int sg_max,
1272f5f82cd1SDavid Howells 				    iov_iter_extraction_t extraction_flags)
1273f5f82cd1SDavid Howells {
1274f5f82cd1SDavid Howells 	struct scatterlist *sg = sgtable->sgl + sgtable->nents;
1275f5f82cd1SDavid Howells 	struct xarray *xa = iter->xarray;
1276f5f82cd1SDavid Howells 	struct folio *folio;
1277f5f82cd1SDavid Howells 	loff_t start = iter->xarray_start + iter->iov_offset;
1278f5f82cd1SDavid Howells 	pgoff_t index = start / PAGE_SIZE;
1279f5f82cd1SDavid Howells 	ssize_t ret = 0;
1280f5f82cd1SDavid Howells 	size_t offset, len;
1281f5f82cd1SDavid Howells 	XA_STATE(xas, xa, index);
1282f5f82cd1SDavid Howells 
1283f5f82cd1SDavid Howells 	rcu_read_lock();
1284f5f82cd1SDavid Howells 
1285f5f82cd1SDavid Howells 	xas_for_each(&xas, folio, ULONG_MAX) {
1286f5f82cd1SDavid Howells 		if (xas_retry(&xas, folio))
1287f5f82cd1SDavid Howells 			continue;
1288f5f82cd1SDavid Howells 		if (WARN_ON(xa_is_value(folio)))
1289f5f82cd1SDavid Howells 			break;
1290f5f82cd1SDavid Howells 		if (WARN_ON(folio_test_hugetlb(folio)))
1291f5f82cd1SDavid Howells 			break;
1292f5f82cd1SDavid Howells 
1293f5f82cd1SDavid Howells 		offset = offset_in_folio(folio, start);
1294f5f82cd1SDavid Howells 		len = min_t(size_t, maxsize, folio_size(folio) - offset);
1295f5f82cd1SDavid Howells 
1296f5f82cd1SDavid Howells 		sg_set_page(sg, folio_page(folio, 0), len, offset);
1297f5f82cd1SDavid Howells 		sgtable->nents++;
1298f5f82cd1SDavid Howells 		sg++;
1299f5f82cd1SDavid Howells 		sg_max--;
1300f5f82cd1SDavid Howells 
1301f5f82cd1SDavid Howells 		maxsize -= len;
1302f5f82cd1SDavid Howells 		ret += len;
1303f5f82cd1SDavid Howells 		if (maxsize <= 0 || sg_max == 0)
1304f5f82cd1SDavid Howells 			break;
1305f5f82cd1SDavid Howells 	}
1306f5f82cd1SDavid Howells 
1307f5f82cd1SDavid Howells 	rcu_read_unlock();
1308f5f82cd1SDavid Howells 	if (ret > 0)
1309f5f82cd1SDavid Howells 		iov_iter_advance(iter, ret);
1310f5f82cd1SDavid Howells 	return ret;
1311f5f82cd1SDavid Howells }
1312f5f82cd1SDavid Howells 
1313f5f82cd1SDavid Howells /**
1314f5f82cd1SDavid Howells  * extract_iter_to_sg - Extract pages from an iterator and add to an sglist
1315f5f82cd1SDavid Howells  * @iter: The iterator to extract from
1316f5f82cd1SDavid Howells  * @maxsize: The amount of iterator to copy
1317f5f82cd1SDavid Howells  * @sgtable: The scatterlist table to fill in
1318f5f82cd1SDavid Howells  * @sg_max: Maximum number of elements in @sgtable that may be filled
1319f5f82cd1SDavid Howells  * @extraction_flags: Flags to qualify the request
1320f5f82cd1SDavid Howells  *
1321f5f82cd1SDavid Howells  * Extract the page fragments from the given amount of the source iterator and
1322f5f82cd1SDavid Howells  * add them to a scatterlist that refers to all of those bits, to a maximum
1323f5f82cd1SDavid Howells  * addition of @sg_max elements.
1324f5f82cd1SDavid Howells  *
1325f5f82cd1SDavid Howells  * The pages referred to by UBUF- and IOVEC-type iterators are extracted and
1326f5f82cd1SDavid Howells  * pinned; BVEC-, KVEC- and XARRAY-type are extracted but aren't pinned; PIPE-
1327f5f82cd1SDavid Howells  * and DISCARD-type are not supported.
1328f5f82cd1SDavid Howells  *
1329f5f82cd1SDavid Howells  * No end mark is placed on the scatterlist; that's left to the caller.
1330f5f82cd1SDavid Howells  *
1331f5f82cd1SDavid Howells  * @extraction_flags can have ITER_ALLOW_P2PDMA set to request peer-to-peer DMA
1332f5f82cd1SDavid Howells  * be allowed on the pages extracted.
1333f5f82cd1SDavid Howells  *
1334f5f82cd1SDavid Howells  * If successful, @sgtable->nents is updated to include the number of elements
1335f5f82cd1SDavid Howells  * added and the number of bytes added is returned.  @sgtable->orig_nents is
1336f5f82cd1SDavid Howells  * left unaltered.
1337f5f82cd1SDavid Howells  *
1338f5f82cd1SDavid Howells  * The iov_iter_extract_mode() function should be used to query how cleanup
1339f5f82cd1SDavid Howells  * should be performed.
1340f5f82cd1SDavid Howells  */
extract_iter_to_sg(struct iov_iter * iter,size_t maxsize,struct sg_table * sgtable,unsigned int sg_max,iov_iter_extraction_t extraction_flags)1341f5f82cd1SDavid Howells ssize_t extract_iter_to_sg(struct iov_iter *iter, size_t maxsize,
1342f5f82cd1SDavid Howells 			   struct sg_table *sgtable, unsigned int sg_max,
1343f5f82cd1SDavid Howells 			   iov_iter_extraction_t extraction_flags)
1344f5f82cd1SDavid Howells {
1345f5f82cd1SDavid Howells 	if (maxsize == 0)
1346f5f82cd1SDavid Howells 		return 0;
1347f5f82cd1SDavid Howells 
1348f5f82cd1SDavid Howells 	switch (iov_iter_type(iter)) {
1349f5f82cd1SDavid Howells 	case ITER_UBUF:
1350f5f82cd1SDavid Howells 	case ITER_IOVEC:
1351f5f82cd1SDavid Howells 		return extract_user_to_sg(iter, maxsize, sgtable, sg_max,
1352f5f82cd1SDavid Howells 					  extraction_flags);
1353f5f82cd1SDavid Howells 	case ITER_BVEC:
1354f5f82cd1SDavid Howells 		return extract_bvec_to_sg(iter, maxsize, sgtable, sg_max,
1355f5f82cd1SDavid Howells 					  extraction_flags);
1356f5f82cd1SDavid Howells 	case ITER_KVEC:
1357f5f82cd1SDavid Howells 		return extract_kvec_to_sg(iter, maxsize, sgtable, sg_max,
1358f5f82cd1SDavid Howells 					  extraction_flags);
1359f5f82cd1SDavid Howells 	case ITER_XARRAY:
1360f5f82cd1SDavid Howells 		return extract_xarray_to_sg(iter, maxsize, sgtable, sg_max,
1361f5f82cd1SDavid Howells 					    extraction_flags);
1362f5f82cd1SDavid Howells 	default:
1363f5f82cd1SDavid Howells 		pr_err("%s(%u) unsupported\n", __func__, iov_iter_type(iter));
1364f5f82cd1SDavid Howells 		WARN_ON_ONCE(1);
1365f5f82cd1SDavid Howells 		return -EIO;
1366f5f82cd1SDavid Howells 	}
1367f5f82cd1SDavid Howells }
1368f5f82cd1SDavid Howells EXPORT_SYMBOL_GPL(extract_iter_to_sg);
1369