xref: /openbmc/linux/lib/scatterlist.c (revision 0f097f08)
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>
120db9299fSJens Axboe 
130db9299fSJens Axboe /**
140db9299fSJens Axboe  * sg_next - return the next scatterlist entry in a list
150db9299fSJens Axboe  * @sg:		The current sg entry
160db9299fSJens Axboe  *
170db9299fSJens Axboe  * Description:
180db9299fSJens Axboe  *   Usually the next entry will be @sg@ + 1, but if this sg element is part
190db9299fSJens Axboe  *   of a chained scatterlist, it could jump to the start of a new
200db9299fSJens Axboe  *   scatterlist array.
210db9299fSJens Axboe  *
220db9299fSJens Axboe  **/
230db9299fSJens Axboe struct scatterlist *sg_next(struct scatterlist *sg)
240db9299fSJens Axboe {
250db9299fSJens Axboe 	if (sg_is_last(sg))
260db9299fSJens Axboe 		return NULL;
270db9299fSJens Axboe 
280db9299fSJens Axboe 	sg++;
290db9299fSJens Axboe 	if (unlikely(sg_is_chain(sg)))
300db9299fSJens Axboe 		sg = sg_chain_ptr(sg);
310db9299fSJens Axboe 
320db9299fSJens Axboe 	return sg;
330db9299fSJens Axboe }
340db9299fSJens Axboe EXPORT_SYMBOL(sg_next);
350db9299fSJens Axboe 
360db9299fSJens Axboe /**
372e484610SMaxim Levitsky  * sg_nents - return total count of entries in scatterlist
382e484610SMaxim Levitsky  * @sg:		The scatterlist
392e484610SMaxim Levitsky  *
402e484610SMaxim Levitsky  * Description:
419dbbc3b9SZhen Lei  * Allows to know how many entries are in sg, taking into account
422e484610SMaxim Levitsky  * chaining as well
432e484610SMaxim Levitsky  *
442e484610SMaxim Levitsky  **/
452e484610SMaxim Levitsky int sg_nents(struct scatterlist *sg)
462e484610SMaxim Levitsky {
47232f1b51SMaxim Levitsky 	int nents;
48232f1b51SMaxim Levitsky 	for (nents = 0; sg; sg = sg_next(sg))
492e484610SMaxim Levitsky 		nents++;
502e484610SMaxim Levitsky 	return nents;
512e484610SMaxim Levitsky }
522e484610SMaxim Levitsky EXPORT_SYMBOL(sg_nents);
532e484610SMaxim Levitsky 
54cfaed10dSTom Lendacky /**
55cfaed10dSTom Lendacky  * sg_nents_for_len - return total count of entries in scatterlist
56cfaed10dSTom Lendacky  *                    needed to satisfy the supplied length
57cfaed10dSTom Lendacky  * @sg:		The scatterlist
58cfaed10dSTom Lendacky  * @len:	The total required length
59cfaed10dSTom Lendacky  *
60cfaed10dSTom Lendacky  * Description:
61cfaed10dSTom Lendacky  * Determines the number of entries in sg that are required to meet
629dbbc3b9SZhen Lei  * the supplied length, taking into account chaining as well
63cfaed10dSTom Lendacky  *
64cfaed10dSTom Lendacky  * Returns:
65cfaed10dSTom Lendacky  *   the number of sg entries needed, negative error on failure
66cfaed10dSTom Lendacky  *
67cfaed10dSTom Lendacky  **/
68cfaed10dSTom Lendacky int sg_nents_for_len(struct scatterlist *sg, u64 len)
69cfaed10dSTom Lendacky {
70cfaed10dSTom Lendacky 	int nents;
71cfaed10dSTom Lendacky 	u64 total;
72cfaed10dSTom Lendacky 
73cfaed10dSTom Lendacky 	if (!len)
74cfaed10dSTom Lendacky 		return 0;
75cfaed10dSTom Lendacky 
76cfaed10dSTom Lendacky 	for (nents = 0, total = 0; sg; sg = sg_next(sg)) {
77cfaed10dSTom Lendacky 		nents++;
78cfaed10dSTom Lendacky 		total += sg->length;
79cfaed10dSTom Lendacky 		if (total >= len)
80cfaed10dSTom Lendacky 			return nents;
81cfaed10dSTom Lendacky 	}
82cfaed10dSTom Lendacky 
83cfaed10dSTom Lendacky 	return -EINVAL;
84cfaed10dSTom Lendacky }
85cfaed10dSTom Lendacky EXPORT_SYMBOL(sg_nents_for_len);
862e484610SMaxim Levitsky 
872e484610SMaxim Levitsky /**
880db9299fSJens Axboe  * sg_last - return the last scatterlist entry in a list
890db9299fSJens Axboe  * @sgl:	First entry in the scatterlist
900db9299fSJens Axboe  * @nents:	Number of entries in the scatterlist
910db9299fSJens Axboe  *
920db9299fSJens Axboe  * Description:
930db9299fSJens Axboe  *   Should only be used casually, it (currently) scans the entire list
940db9299fSJens Axboe  *   to get the last entry.
950db9299fSJens Axboe  *
960db9299fSJens Axboe  *   Note that the @sgl@ pointer passed in need not be the first one,
970db9299fSJens Axboe  *   the important bit is that @nents@ denotes the number of entries that
980db9299fSJens Axboe  *   exist from @sgl@.
990db9299fSJens Axboe  *
1000db9299fSJens Axboe  **/
1010db9299fSJens Axboe struct scatterlist *sg_last(struct scatterlist *sgl, unsigned int nents)
1020db9299fSJens Axboe {
1030db9299fSJens Axboe 	struct scatterlist *sg, *ret = NULL;
1040db9299fSJens Axboe 	unsigned int i;
1050db9299fSJens Axboe 
1060db9299fSJens Axboe 	for_each_sg(sgl, sg, nents, i)
1070db9299fSJens Axboe 		ret = sg;
1080db9299fSJens Axboe 
1090db9299fSJens Axboe 	BUG_ON(!sg_is_last(ret));
1100db9299fSJens Axboe 	return ret;
1110db9299fSJens Axboe }
1120db9299fSJens Axboe EXPORT_SYMBOL(sg_last);
1130db9299fSJens Axboe 
1140db9299fSJens Axboe /**
1150db9299fSJens Axboe  * sg_init_table - Initialize SG table
1160db9299fSJens Axboe  * @sgl:	   The SG table
1170db9299fSJens Axboe  * @nents:	   Number of entries in table
1180db9299fSJens Axboe  *
1190db9299fSJens Axboe  * Notes:
1200db9299fSJens Axboe  *   If this is part of a chained sg table, sg_mark_end() should be
1210db9299fSJens Axboe  *   used only on the last table part.
1220db9299fSJens Axboe  *
1230db9299fSJens Axboe  **/
1240db9299fSJens Axboe void sg_init_table(struct scatterlist *sgl, unsigned int nents)
1250db9299fSJens Axboe {
1260db9299fSJens Axboe 	memset(sgl, 0, sizeof(*sgl) * nents);
127f3851786SPrashant Bhole 	sg_init_marker(sgl, nents);
1280db9299fSJens Axboe }
1290db9299fSJens Axboe EXPORT_SYMBOL(sg_init_table);
1300db9299fSJens Axboe 
1310db9299fSJens Axboe /**
1320db9299fSJens Axboe  * sg_init_one - Initialize a single entry sg list
1330db9299fSJens Axboe  * @sg:		 SG entry
1340db9299fSJens Axboe  * @buf:	 Virtual address for IO
1350db9299fSJens Axboe  * @buflen:	 IO length
1360db9299fSJens Axboe  *
1370db9299fSJens Axboe  **/
1380db9299fSJens Axboe void sg_init_one(struct scatterlist *sg, const void *buf, unsigned int buflen)
1390db9299fSJens Axboe {
1400db9299fSJens Axboe 	sg_init_table(sg, 1);
1410db9299fSJens Axboe 	sg_set_buf(sg, buf, buflen);
1420db9299fSJens Axboe }
1430db9299fSJens Axboe EXPORT_SYMBOL(sg_init_one);
1440db9299fSJens Axboe 
1450db9299fSJens Axboe /*
1460db9299fSJens Axboe  * The default behaviour of sg_alloc_table() is to use these kmalloc/kfree
1470db9299fSJens Axboe  * helpers.
1480db9299fSJens Axboe  */
1490db9299fSJens Axboe static struct scatterlist *sg_kmalloc(unsigned int nents, gfp_t gfp_mask)
1500db9299fSJens Axboe {
151b94de9bbSChris Wilson 	if (nents == SG_MAX_SINGLE_ALLOC) {
152b94de9bbSChris Wilson 		/*
153b94de9bbSChris Wilson 		 * Kmemleak doesn't track page allocations as they are not
154b94de9bbSChris Wilson 		 * commonly used (in a raw form) for kernel data structures.
155b94de9bbSChris Wilson 		 * As we chain together a list of pages and then a normal
156b94de9bbSChris Wilson 		 * kmalloc (tracked by kmemleak), in order to for that last
157b94de9bbSChris Wilson 		 * allocation not to become decoupled (and thus a
158b94de9bbSChris Wilson 		 * false-positive) we need to inform kmemleak of all the
159b94de9bbSChris Wilson 		 * intermediate allocations.
160b94de9bbSChris Wilson 		 */
161b94de9bbSChris Wilson 		void *ptr = (void *) __get_free_page(gfp_mask);
162b94de9bbSChris Wilson 		kmemleak_alloc(ptr, PAGE_SIZE, 1, gfp_mask);
163b94de9bbSChris Wilson 		return ptr;
164b94de9bbSChris Wilson 	} else
1656da2ec56SKees Cook 		return kmalloc_array(nents, sizeof(struct scatterlist),
1666da2ec56SKees Cook 				     gfp_mask);
1670db9299fSJens Axboe }
1680db9299fSJens Axboe 
1690db9299fSJens Axboe static void sg_kfree(struct scatterlist *sg, unsigned int nents)
1700db9299fSJens Axboe {
171b94de9bbSChris Wilson 	if (nents == SG_MAX_SINGLE_ALLOC) {
172b94de9bbSChris Wilson 		kmemleak_free(sg);
1730db9299fSJens Axboe 		free_page((unsigned long) sg);
174b94de9bbSChris Wilson 	} else
1750db9299fSJens Axboe 		kfree(sg);
1760db9299fSJens Axboe }
1770db9299fSJens Axboe 
1780db9299fSJens Axboe /**
1790db9299fSJens Axboe  * __sg_free_table - Free a previously mapped sg table
1800db9299fSJens Axboe  * @table:	The sg table header to use
1817cedb1f1SJames Bottomley  * @max_ents:	The maximum number of entries per single scatterlist
1824635873cSMing Lei  * @nents_first_chunk: Number of entries int the (preallocated) first
1834635873cSMing Lei  * 	scatterlist chunk, 0 means no such preallocated first chunk
1840db9299fSJens Axboe  * @free_fn:	Free function
1853e302dbcSMaor Gottlieb  * @num_ents:	Number of entries in the table
1860db9299fSJens Axboe  *
1870db9299fSJens Axboe  *  Description:
1887cedb1f1SJames Bottomley  *    Free an sg table previously allocated and setup with
1897cedb1f1SJames Bottomley  *    __sg_alloc_table().  The @max_ents value must be identical to
1907cedb1f1SJames Bottomley  *    that previously used with __sg_alloc_table().
1910db9299fSJens Axboe  *
1920db9299fSJens Axboe  **/
1937cedb1f1SJames Bottomley void __sg_free_table(struct sg_table *table, unsigned int max_ents,
1943e302dbcSMaor Gottlieb 		     unsigned int nents_first_chunk, sg_free_fn *free_fn,
1953e302dbcSMaor Gottlieb 		     unsigned int num_ents)
1960db9299fSJens Axboe {
1970db9299fSJens Axboe 	struct scatterlist *sgl, *next;
1984635873cSMing Lei 	unsigned curr_max_ents = nents_first_chunk ?: max_ents;
1990db9299fSJens Axboe 
2000db9299fSJens Axboe 	if (unlikely(!table->sgl))
2010db9299fSJens Axboe 		return;
2020db9299fSJens Axboe 
2030db9299fSJens Axboe 	sgl = table->sgl;
2043e302dbcSMaor Gottlieb 	while (num_ents) {
2053e302dbcSMaor Gottlieb 		unsigned int alloc_size = num_ents;
2060db9299fSJens Axboe 		unsigned int sg_size;
2070db9299fSJens Axboe 
2080db9299fSJens Axboe 		/*
2097cedb1f1SJames Bottomley 		 * If we have more than max_ents segments left,
2100db9299fSJens Axboe 		 * then assign 'next' to the sg table after the current one.
2110db9299fSJens Axboe 		 * sg_size is then one less than alloc size, since the last
2120db9299fSJens Axboe 		 * element is the chain pointer.
2130db9299fSJens Axboe 		 */
2144635873cSMing Lei 		if (alloc_size > curr_max_ents) {
2154635873cSMing Lei 			next = sg_chain_ptr(&sgl[curr_max_ents - 1]);
2164635873cSMing Lei 			alloc_size = curr_max_ents;
2170db9299fSJens Axboe 			sg_size = alloc_size - 1;
2180db9299fSJens Axboe 		} else {
2190db9299fSJens Axboe 			sg_size = alloc_size;
2200db9299fSJens Axboe 			next = NULL;
2210db9299fSJens Axboe 		}
2220db9299fSJens Axboe 
2233e302dbcSMaor Gottlieb 		num_ents -= sg_size;
2244635873cSMing Lei 		if (nents_first_chunk)
2254635873cSMing Lei 			nents_first_chunk = 0;
226c21e59d8STony Battersby 		else
227c21e59d8STony Battersby 			free_fn(sgl, alloc_size);
2280db9299fSJens Axboe 		sgl = next;
2294635873cSMing Lei 		curr_max_ents = max_ents;
2300db9299fSJens Axboe 	}
2310db9299fSJens Axboe 
2320db9299fSJens Axboe 	table->sgl = NULL;
2330db9299fSJens Axboe }
2340db9299fSJens Axboe EXPORT_SYMBOL(__sg_free_table);
2350db9299fSJens Axboe 
2360db9299fSJens Axboe /**
2373e302dbcSMaor Gottlieb  * sg_free_append_table - Free a previously allocated append sg table.
2383e302dbcSMaor Gottlieb  * @table:	 The mapped sg append table header
2393e302dbcSMaor Gottlieb  *
2403e302dbcSMaor Gottlieb  **/
2413e302dbcSMaor Gottlieb void sg_free_append_table(struct sg_append_table *table)
2423e302dbcSMaor Gottlieb {
2436d529ea8Swuchi 	__sg_free_table(&table->sgt, SG_MAX_SINGLE_ALLOC, 0, sg_kfree,
2443e302dbcSMaor Gottlieb 			table->total_nents);
2453e302dbcSMaor Gottlieb }
2463e302dbcSMaor Gottlieb EXPORT_SYMBOL(sg_free_append_table);
2473e302dbcSMaor Gottlieb 
2483e302dbcSMaor Gottlieb 
2493e302dbcSMaor Gottlieb /**
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 {
2566d529ea8Swuchi 	__sg_free_table(table, SG_MAX_SINGLE_ALLOC, 0, sg_kfree,
2573e302dbcSMaor Gottlieb 			table->orig_nents);
2580db9299fSJens Axboe }
2590db9299fSJens Axboe EXPORT_SYMBOL(sg_free_table);
2600db9299fSJens Axboe 
2610db9299fSJens Axboe /**
2620db9299fSJens Axboe  * __sg_alloc_table - Allocate and initialize an sg table with given allocator
2630db9299fSJens Axboe  * @table:	The sg table header to use
2640db9299fSJens Axboe  * @nents:	Number of entries in sg list
2657cedb1f1SJames Bottomley  * @max_ents:	The maximum number of entries the allocator returns per call
2664635873cSMing Lei  * @nents_first_chunk: Number of entries int the (preallocated) first
2674635873cSMing Lei  * 	scatterlist chunk, 0 means no such preallocated chunk provided by user
2680db9299fSJens Axboe  * @gfp_mask:	GFP allocation mask
2690db9299fSJens Axboe  * @alloc_fn:	Allocator to use
2700db9299fSJens Axboe  *
2717cedb1f1SJames Bottomley  * Description:
2727cedb1f1SJames Bottomley  *   This function returns a @table @nents long. The allocator is
2737cedb1f1SJames Bottomley  *   defined to return scatterlist chunks of maximum size @max_ents.
2747cedb1f1SJames Bottomley  *   Thus if @nents is bigger than @max_ents, the scatterlists will be
2757cedb1f1SJames Bottomley  *   chained in units of @max_ents.
2767cedb1f1SJames Bottomley  *
2770db9299fSJens Axboe  * Notes:
2780db9299fSJens Axboe  *   If this function returns non-0 (eg failure), the caller must call
2790db9299fSJens Axboe  *   __sg_free_table() to cleanup any leftover allocations.
2800db9299fSJens Axboe  *
2810db9299fSJens Axboe  **/
2827cedb1f1SJames Bottomley int __sg_alloc_table(struct sg_table *table, unsigned int nents,
283c53c6d6aSChristoph Hellwig 		     unsigned int max_ents, struct scatterlist *first_chunk,
2844635873cSMing Lei 		     unsigned int nents_first_chunk, gfp_t gfp_mask,
2854635873cSMing Lei 		     sg_alloc_fn *alloc_fn)
2860db9299fSJens Axboe {
2870db9299fSJens Axboe 	struct scatterlist *sg, *prv;
2880db9299fSJens Axboe 	unsigned int left;
2894635873cSMing Lei 	unsigned curr_max_ents = nents_first_chunk ?: max_ents;
2904635873cSMing Lei 	unsigned prv_max_ents;
2910db9299fSJens Axboe 
29227daabd9SDan Carpenter 	memset(table, 0, sizeof(*table));
29327daabd9SDan Carpenter 
29427daabd9SDan Carpenter 	if (nents == 0)
29527daabd9SDan Carpenter 		return -EINVAL;
2967c703e54SChristoph Hellwig #ifdef CONFIG_ARCH_NO_SG_CHAIN
2976fd59a83SNick Bowler 	if (WARN_ON_ONCE(nents > max_ents))
2986fd59a83SNick Bowler 		return -EINVAL;
2990db9299fSJens Axboe #endif
3000db9299fSJens Axboe 
3010db9299fSJens Axboe 	left = nents;
3020db9299fSJens Axboe 	prv = NULL;
3030db9299fSJens Axboe 	do {
3040db9299fSJens Axboe 		unsigned int sg_size, alloc_size = left;
3050db9299fSJens Axboe 
3064635873cSMing Lei 		if (alloc_size > curr_max_ents) {
3074635873cSMing Lei 			alloc_size = curr_max_ents;
3080db9299fSJens Axboe 			sg_size = alloc_size - 1;
3090db9299fSJens Axboe 		} else
3100db9299fSJens Axboe 			sg_size = alloc_size;
3110db9299fSJens Axboe 
3120db9299fSJens Axboe 		left -= sg_size;
3130db9299fSJens Axboe 
314c53c6d6aSChristoph Hellwig 		if (first_chunk) {
315c53c6d6aSChristoph Hellwig 			sg = first_chunk;
316c53c6d6aSChristoph Hellwig 			first_chunk = NULL;
317c53c6d6aSChristoph Hellwig 		} else {
3180db9299fSJens Axboe 			sg = alloc_fn(alloc_size, gfp_mask);
319c53c6d6aSChristoph Hellwig 		}
320edce6820SJeffrey Carlyle 		if (unlikely(!sg)) {
321edce6820SJeffrey Carlyle 			/*
322edce6820SJeffrey Carlyle 			 * Adjust entry count to reflect that the last
323edce6820SJeffrey Carlyle 			 * entry of the previous table won't be used for
324edce6820SJeffrey Carlyle 			 * linkage.  Without this, sg_kfree() may get
325edce6820SJeffrey Carlyle 			 * confused.
326edce6820SJeffrey Carlyle 			 */
327edce6820SJeffrey Carlyle 			if (prv)
328edce6820SJeffrey Carlyle 				table->nents = ++table->orig_nents;
329edce6820SJeffrey Carlyle 
3300db9299fSJens Axboe 			return -ENOMEM;
331edce6820SJeffrey Carlyle 		}
3320db9299fSJens Axboe 
3330db9299fSJens Axboe 		sg_init_table(sg, alloc_size);
3340db9299fSJens Axboe 		table->nents = table->orig_nents += sg_size;
3350db9299fSJens Axboe 
3360db9299fSJens Axboe 		/*
3370db9299fSJens Axboe 		 * If this is the first mapping, assign the sg table header.
3380db9299fSJens Axboe 		 * If this is not the first mapping, chain previous part.
3390db9299fSJens Axboe 		 */
3400db9299fSJens Axboe 		if (prv)
3414635873cSMing Lei 			sg_chain(prv, prv_max_ents, sg);
3420db9299fSJens Axboe 		else
3430db9299fSJens Axboe 			table->sgl = sg;
3440db9299fSJens Axboe 
3450db9299fSJens Axboe 		/*
3460db9299fSJens Axboe 		 * If no more entries after this one, mark the end
3470db9299fSJens Axboe 		 */
3480db9299fSJens Axboe 		if (!left)
3490db9299fSJens Axboe 			sg_mark_end(&sg[sg_size - 1]);
3500db9299fSJens Axboe 
3510db9299fSJens Axboe 		prv = sg;
3524635873cSMing Lei 		prv_max_ents = curr_max_ents;
3534635873cSMing Lei 		curr_max_ents = max_ents;
3540db9299fSJens Axboe 	} while (left);
3550db9299fSJens Axboe 
3560db9299fSJens Axboe 	return 0;
3570db9299fSJens Axboe }
3580db9299fSJens Axboe EXPORT_SYMBOL(__sg_alloc_table);
3590db9299fSJens Axboe 
3600db9299fSJens Axboe /**
3610db9299fSJens Axboe  * sg_alloc_table - Allocate and initialize an sg table
3620db9299fSJens Axboe  * @table:	The sg table header to use
3630db9299fSJens Axboe  * @nents:	Number of entries in sg list
3640db9299fSJens Axboe  * @gfp_mask:	GFP allocation mask
3650db9299fSJens Axboe  *
3660db9299fSJens Axboe  *  Description:
3670db9299fSJens Axboe  *    Allocate and initialize an sg table. If @nents@ is larger than
3680db9299fSJens Axboe  *    SG_MAX_SINGLE_ALLOC a chained sg table will be setup.
3690db9299fSJens Axboe  *
3700db9299fSJens Axboe  **/
3710db9299fSJens Axboe int sg_alloc_table(struct sg_table *table, unsigned int nents, gfp_t gfp_mask)
3720db9299fSJens Axboe {
3730db9299fSJens Axboe 	int ret;
3740db9299fSJens Axboe 
3757cedb1f1SJames Bottomley 	ret = __sg_alloc_table(table, nents, SG_MAX_SINGLE_ALLOC,
3764635873cSMing Lei 			       NULL, 0, gfp_mask, sg_kmalloc);
3770db9299fSJens Axboe 	if (unlikely(ret))
3783e302dbcSMaor Gottlieb 		sg_free_table(table);
3790db9299fSJens Axboe 	return ret;
3800db9299fSJens Axboe }
3810db9299fSJens Axboe EXPORT_SYMBOL(sg_alloc_table);
382b1adaf65SFUJITA Tomonori 
3833e302dbcSMaor Gottlieb static struct scatterlist *get_next_sg(struct sg_append_table *table,
38407da1223SMaor Gottlieb 				       struct scatterlist *cur,
38507da1223SMaor Gottlieb 				       unsigned long needed_sges,
38607da1223SMaor Gottlieb 				       gfp_t gfp_mask)
38707da1223SMaor Gottlieb {
38807da1223SMaor Gottlieb 	struct scatterlist *new_sg, *next_sg;
38907da1223SMaor Gottlieb 	unsigned int alloc_size;
39007da1223SMaor Gottlieb 
39107da1223SMaor Gottlieb 	if (cur) {
39207da1223SMaor Gottlieb 		next_sg = sg_next(cur);
39307da1223SMaor Gottlieb 		/* Check if last entry should be keeped for chainning */
39407da1223SMaor Gottlieb 		if (!sg_is_last(next_sg) || needed_sges == 1)
39507da1223SMaor Gottlieb 			return next_sg;
39607da1223SMaor Gottlieb 	}
39707da1223SMaor Gottlieb 
39807da1223SMaor Gottlieb 	alloc_size = min_t(unsigned long, needed_sges, SG_MAX_SINGLE_ALLOC);
39907da1223SMaor Gottlieb 	new_sg = sg_kmalloc(alloc_size, gfp_mask);
40007da1223SMaor Gottlieb 	if (!new_sg)
40107da1223SMaor Gottlieb 		return ERR_PTR(-ENOMEM);
40207da1223SMaor Gottlieb 	sg_init_table(new_sg, alloc_size);
40307da1223SMaor Gottlieb 	if (cur) {
4043e302dbcSMaor Gottlieb 		table->total_nents += alloc_size - 1;
40507da1223SMaor Gottlieb 		__sg_chain(next_sg, new_sg);
40607da1223SMaor Gottlieb 	} else {
4073e302dbcSMaor Gottlieb 		table->sgt.sgl = new_sg;
4083e302dbcSMaor Gottlieb 		table->total_nents = alloc_size;
40907da1223SMaor Gottlieb 	}
41007da1223SMaor Gottlieb 	return new_sg;
41107da1223SMaor Gottlieb }
41207da1223SMaor Gottlieb 
4131567b49dSLogan Gunthorpe static bool pages_are_mergeable(struct page *a, struct page *b)
4141567b49dSLogan Gunthorpe {
4151567b49dSLogan Gunthorpe 	if (page_to_pfn(a) != page_to_pfn(b) + 1)
4161567b49dSLogan Gunthorpe 		return false;
4171567b49dSLogan Gunthorpe 	if (!zone_device_pages_have_same_pgmap(a, b))
4181567b49dSLogan Gunthorpe 		return false;
4191567b49dSLogan Gunthorpe 	return true;
4201567b49dSLogan Gunthorpe }
4211567b49dSLogan Gunthorpe 
422b1adaf65SFUJITA Tomonori /**
4233e302dbcSMaor Gottlieb  * sg_alloc_append_table_from_pages - Allocate and initialize an append sg
4243e302dbcSMaor Gottlieb  *                                    table from an array of pages
4253e302dbcSMaor Gottlieb  * @sgt_append:  The sg append table to use
426efc42bc9STomasz Stanislawski  * @pages:       Pointer to an array of page pointers
427efc42bc9STomasz Stanislawski  * @n_pages:     Number of pages in the pages array
428efc42bc9STomasz Stanislawski  * @offset:      Offset from start of the first page to the start of a buffer
429efc42bc9STomasz Stanislawski  * @size:        Number of valid bytes in the buffer (after offset)
4309a40401cSJason Gunthorpe  * @max_segment: Maximum size of a scatterlist element in bytes
43107da1223SMaor Gottlieb  * @left_pages:  Left pages caller have to set after this call
432efc42bc9STomasz Stanislawski  * @gfp_mask:	 GFP allocation mask
433efc42bc9STomasz Stanislawski  *
434efc42bc9STomasz Stanislawski  * Description:
4353e302dbcSMaor Gottlieb  *    In the first call it allocate and initialize an sg table from a list of
4363e302dbcSMaor Gottlieb  *    pages, else reuse the scatterlist from sgt_append. Contiguous ranges of
4373e302dbcSMaor Gottlieb  *    the pages are squashed into a single scatterlist entry up to the maximum
4383e302dbcSMaor Gottlieb  *    size specified in @max_segment.  A user may provide an offset at a start
4393e302dbcSMaor Gottlieb  *    and a size of valid data in a buffer specified by the page array. The
4403e302dbcSMaor Gottlieb  *    returned sg table is released by sg_free_append_table
441efc42bc9STomasz Stanislawski  *
442efc42bc9STomasz Stanislawski  * Returns:
4433e302dbcSMaor Gottlieb  *   0 on success, negative error on failure
44407da1223SMaor Gottlieb  *
44507da1223SMaor Gottlieb  * Notes:
44607da1223SMaor Gottlieb  *   If this function returns non-0 (eg failure), the caller must call
4473e302dbcSMaor Gottlieb  *   sg_free_append_table() to cleanup any leftover allocations.
4483e302dbcSMaor Gottlieb  *
4493e302dbcSMaor Gottlieb  *   In the fist call, sgt_append must by initialized.
450efc42bc9STomasz Stanislawski  */
4513e302dbcSMaor Gottlieb int sg_alloc_append_table_from_pages(struct sg_append_table *sgt_append,
45207da1223SMaor Gottlieb 		struct page **pages, unsigned int n_pages, unsigned int offset,
45389d8589cSTvrtko Ursulin 		unsigned long size, unsigned int max_segment,
4543e302dbcSMaor Gottlieb 		unsigned int left_pages, gfp_t gfp_mask)
455efc42bc9STomasz Stanislawski {
45607da1223SMaor Gottlieb 	unsigned int chunks, cur_page, seg_len, i, prv_len = 0;
45707da1223SMaor Gottlieb 	unsigned int added_nents = 0;
4583e302dbcSMaor Gottlieb 	struct scatterlist *s = sgt_append->prv;
4591567b49dSLogan Gunthorpe 	struct page *last_pg;
460efc42bc9STomasz Stanislawski 
4619a40401cSJason Gunthorpe 	/*
4629a40401cSJason Gunthorpe 	 * The algorithm below requires max_segment to be aligned to PAGE_SIZE
4639a40401cSJason Gunthorpe 	 * otherwise it can overshoot.
4649a40401cSJason Gunthorpe 	 */
4659a40401cSJason Gunthorpe 	max_segment = ALIGN_DOWN(max_segment, PAGE_SIZE);
4669a40401cSJason Gunthorpe 	if (WARN_ON(max_segment < PAGE_SIZE))
4673e302dbcSMaor Gottlieb 		return -EINVAL;
46807da1223SMaor Gottlieb 
4693e302dbcSMaor Gottlieb 	if (IS_ENABLED(CONFIG_ARCH_NO_SG_CHAIN) && sgt_append->prv)
4703e302dbcSMaor Gottlieb 		return -EOPNOTSUPP;
47107da1223SMaor Gottlieb 
4723e302dbcSMaor Gottlieb 	if (sgt_append->prv) {
473*0f097f08SYishai Hadas 		unsigned long next_pfn = (page_to_phys(sg_page(sgt_append->prv)) +
474*0f097f08SYishai Hadas 			sgt_append->prv->offset + sgt_append->prv->length) / PAGE_SIZE;
475*0f097f08SYishai Hadas 
47607da1223SMaor Gottlieb 		if (WARN_ON(offset))
4773e302dbcSMaor Gottlieb 			return -EINVAL;
47807da1223SMaor Gottlieb 
47907da1223SMaor Gottlieb 		/* Merge contiguous pages into the last SG */
4803e302dbcSMaor Gottlieb 		prv_len = sgt_append->prv->length;
481*0f097f08SYishai Hadas 		if (page_to_pfn(pages[0]) == next_pfn) {
482*0f097f08SYishai Hadas 			last_pg = pfn_to_page(next_pfn - 1);
483e95d50d7SYishai Hadas 			while (n_pages && pages_are_mergeable(pages[0], last_pg)) {
4843e302dbcSMaor Gottlieb 				if (sgt_append->prv->length + PAGE_SIZE > max_segment)
48507da1223SMaor Gottlieb 					break;
4863e302dbcSMaor Gottlieb 				sgt_append->prv->length += PAGE_SIZE;
4871567b49dSLogan Gunthorpe 				last_pg = pages[0];
48807da1223SMaor Gottlieb 				pages++;
48907da1223SMaor Gottlieb 				n_pages--;
49007da1223SMaor Gottlieb 			}
49107da1223SMaor Gottlieb 			if (!n_pages)
49207da1223SMaor Gottlieb 				goto out;
49307da1223SMaor Gottlieb 		}
494*0f097f08SYishai Hadas 	}
49589d8589cSTvrtko Ursulin 
496efc42bc9STomasz Stanislawski 	/* compute number of contiguous chunks */
497efc42bc9STomasz Stanislawski 	chunks = 1;
498c125906bSTvrtko Ursulin 	seg_len = 0;
499c125906bSTvrtko Ursulin 	for (i = 1; i < n_pages; i++) {
500c125906bSTvrtko Ursulin 		seg_len += PAGE_SIZE;
501c125906bSTvrtko Ursulin 		if (seg_len >= max_segment ||
5021567b49dSLogan Gunthorpe 		    !pages_are_mergeable(pages[i], pages[i - 1])) {
503c125906bSTvrtko Ursulin 			chunks++;
504c125906bSTvrtko Ursulin 			seg_len = 0;
505c125906bSTvrtko Ursulin 		}
506c125906bSTvrtko Ursulin 	}
507efc42bc9STomasz Stanislawski 
508efc42bc9STomasz Stanislawski 	/* merging chunks and putting them into the scatterlist */
509efc42bc9STomasz Stanislawski 	cur_page = 0;
51007da1223SMaor Gottlieb 	for (i = 0; i < chunks; i++) {
511c125906bSTvrtko Ursulin 		unsigned int j, chunk_size;
512efc42bc9STomasz Stanislawski 
513efc42bc9STomasz Stanislawski 		/* look for the end of the current chunk */
514c125906bSTvrtko Ursulin 		seg_len = 0;
515c125906bSTvrtko Ursulin 		for (j = cur_page + 1; j < n_pages; j++) {
516c125906bSTvrtko Ursulin 			seg_len += PAGE_SIZE;
517c125906bSTvrtko Ursulin 			if (seg_len >= max_segment ||
5181567b49dSLogan Gunthorpe 			    !pages_are_mergeable(pages[j], pages[j - 1]))
519efc42bc9STomasz Stanislawski 				break;
520c125906bSTvrtko Ursulin 		}
521efc42bc9STomasz Stanislawski 
52207da1223SMaor Gottlieb 		/* Pass how many chunks might be left */
5233e302dbcSMaor Gottlieb 		s = get_next_sg(sgt_append, s, chunks - i + left_pages,
5243e302dbcSMaor Gottlieb 				gfp_mask);
52507da1223SMaor Gottlieb 		if (IS_ERR(s)) {
52607da1223SMaor Gottlieb 			/*
52707da1223SMaor Gottlieb 			 * Adjust entry length to be as before function was
52807da1223SMaor Gottlieb 			 * called.
52907da1223SMaor Gottlieb 			 */
5303e302dbcSMaor Gottlieb 			if (sgt_append->prv)
5313e302dbcSMaor Gottlieb 				sgt_append->prv->length = prv_len;
5323e302dbcSMaor Gottlieb 			return PTR_ERR(s);
53307da1223SMaor Gottlieb 		}
534efc42bc9STomasz Stanislawski 		chunk_size = ((j - cur_page) << PAGE_SHIFT) - offset;
535c125906bSTvrtko Ursulin 		sg_set_page(s, pages[cur_page],
536c125906bSTvrtko Ursulin 			    min_t(unsigned long, size, chunk_size), offset);
53707da1223SMaor Gottlieb 		added_nents++;
538efc42bc9STomasz Stanislawski 		size -= chunk_size;
539efc42bc9STomasz Stanislawski 		offset = 0;
540efc42bc9STomasz Stanislawski 		cur_page = j;
541efc42bc9STomasz Stanislawski 	}
5423e302dbcSMaor Gottlieb 	sgt_append->sgt.nents += added_nents;
5433e302dbcSMaor Gottlieb 	sgt_append->sgt.orig_nents = sgt_append->sgt.nents;
5443e302dbcSMaor Gottlieb 	sgt_append->prv = s;
54507da1223SMaor Gottlieb out:
54607da1223SMaor Gottlieb 	if (!left_pages)
54707da1223SMaor Gottlieb 		sg_mark_end(s);
5483e302dbcSMaor Gottlieb 	return 0;
549efc42bc9STomasz Stanislawski }
55090e7a6deSMaor Gottlieb EXPORT_SYMBOL(sg_alloc_append_table_from_pages);
55189d8589cSTvrtko Ursulin 
55289d8589cSTvrtko Ursulin /**
55390e7a6deSMaor Gottlieb  * sg_alloc_table_from_pages_segment - Allocate and initialize an sg table from
55490e7a6deSMaor Gottlieb  *                                     an array of pages and given maximum
55590e7a6deSMaor Gottlieb  *                                     segment.
55689d8589cSTvrtko Ursulin  * @sgt:	 The sg table header to use
55789d8589cSTvrtko Ursulin  * @pages:	 Pointer to an array of page pointers
55889d8589cSTvrtko Ursulin  * @n_pages:	 Number of pages in the pages array
55989d8589cSTvrtko Ursulin  * @offset:      Offset from start of the first page to the start of a buffer
56089d8589cSTvrtko Ursulin  * @size:        Number of valid bytes in the buffer (after offset)
56190e7a6deSMaor Gottlieb  * @max_segment: Maximum size of a scatterlist element in bytes
56289d8589cSTvrtko Ursulin  * @gfp_mask:	 GFP allocation mask
56389d8589cSTvrtko Ursulin  *
56489d8589cSTvrtko Ursulin  *  Description:
56589d8589cSTvrtko Ursulin  *    Allocate and initialize an sg table from a list of pages. Contiguous
56690e7a6deSMaor Gottlieb  *    ranges of the pages are squashed into a single scatterlist node up to the
56790e7a6deSMaor Gottlieb  *    maximum size specified in @max_segment. A user may provide an offset at a
56890e7a6deSMaor Gottlieb  *    start and a size of valid data in a buffer specified by the page array.
56990e7a6deSMaor Gottlieb  *
57090e7a6deSMaor Gottlieb  *    The returned sg table is released by sg_free_table.
57189d8589cSTvrtko Ursulin  *
57289d8589cSTvrtko Ursulin  *  Returns:
57389d8589cSTvrtko Ursulin  *   0 on success, negative error on failure
57489d8589cSTvrtko Ursulin  */
57590e7a6deSMaor Gottlieb int sg_alloc_table_from_pages_segment(struct sg_table *sgt, struct page **pages,
57689d8589cSTvrtko Ursulin 				unsigned int n_pages, unsigned int offset,
57790e7a6deSMaor Gottlieb 				unsigned long size, unsigned int max_segment,
57890e7a6deSMaor Gottlieb 				gfp_t gfp_mask)
57989d8589cSTvrtko Ursulin {
5803e302dbcSMaor Gottlieb 	struct sg_append_table append = {};
5813e302dbcSMaor Gottlieb 	int err;
5823e302dbcSMaor Gottlieb 
5833e302dbcSMaor Gottlieb 	err = sg_alloc_append_table_from_pages(&append, pages, n_pages, offset,
5843e302dbcSMaor Gottlieb 					       size, max_segment, 0, gfp_mask);
5853e302dbcSMaor Gottlieb 	if (err) {
5863e302dbcSMaor Gottlieb 		sg_free_append_table(&append);
5873e302dbcSMaor Gottlieb 		return err;
5883e302dbcSMaor Gottlieb 	}
5893e302dbcSMaor Gottlieb 	memcpy(sgt, &append.sgt, sizeof(*sgt));
5903e302dbcSMaor Gottlieb 	WARN_ON(append.total_nents != sgt->orig_nents);
5913e302dbcSMaor Gottlieb 	return 0;
59289d8589cSTvrtko Ursulin }
59390e7a6deSMaor Gottlieb EXPORT_SYMBOL(sg_alloc_table_from_pages_segment);
594efc42bc9STomasz Stanislawski 
595e80a0af4SBart Van Assche #ifdef CONFIG_SGL_ALLOC
596e80a0af4SBart Van Assche 
597e80a0af4SBart Van Assche /**
598e80a0af4SBart Van Assche  * sgl_alloc_order - allocate a scatterlist and its pages
599e80a0af4SBart Van Assche  * @length: Length in bytes of the scatterlist. Must be at least one
600e80a0af4SBart Van Assche  * @order: Second argument for alloc_pages()
601e80a0af4SBart Van Assche  * @chainable: Whether or not to allocate an extra element in the scatterlist
602e80a0af4SBart Van Assche  *	for scatterlist chaining purposes
603e80a0af4SBart Van Assche  * @gfp: Memory allocation flags
604e80a0af4SBart Van Assche  * @nent_p: [out] Number of entries in the scatterlist that have pages
605e80a0af4SBart Van Assche  *
606e80a0af4SBart Van Assche  * Returns: A pointer to an initialized scatterlist or %NULL upon failure.
607e80a0af4SBart Van Assche  */
608e80a0af4SBart Van Assche struct scatterlist *sgl_alloc_order(unsigned long long length,
609e80a0af4SBart Van Assche 				    unsigned int order, bool chainable,
610e80a0af4SBart Van Assche 				    gfp_t gfp, unsigned int *nent_p)
611e80a0af4SBart Van Assche {
612e80a0af4SBart Van Assche 	struct scatterlist *sgl, *sg;
613e80a0af4SBart Van Assche 	struct page *page;
614e80a0af4SBart Van Assche 	unsigned int nent, nalloc;
615e80a0af4SBart Van Assche 	u32 elem_len;
616e80a0af4SBart Van Assche 
617e80a0af4SBart Van Assche 	nent = round_up(length, PAGE_SIZE << order) >> (PAGE_SHIFT + order);
618e80a0af4SBart Van Assche 	/* Check for integer overflow */
619e80a0af4SBart Van Assche 	if (length > (nent << (PAGE_SHIFT + order)))
620e80a0af4SBart Van Assche 		return NULL;
621e80a0af4SBart Van Assche 	nalloc = nent;
622e80a0af4SBart Van Assche 	if (chainable) {
623e80a0af4SBart Van Assche 		/* Check for integer overflow */
624e80a0af4SBart Van Assche 		if (nalloc + 1 < nalloc)
625e80a0af4SBart Van Assche 			return NULL;
626e80a0af4SBart Van Assche 		nalloc++;
627e80a0af4SBart Van Assche 	}
628e80a0af4SBart Van Assche 	sgl = kmalloc_array(nalloc, sizeof(struct scatterlist),
6296ed9b92eSChristophe JAILLET 			    gfp & ~GFP_DMA);
630e80a0af4SBart Van Assche 	if (!sgl)
631e80a0af4SBart Van Assche 		return NULL;
632e80a0af4SBart Van Assche 
6338c7a8d1cSBart Van Assche 	sg_init_table(sgl, nalloc);
634e80a0af4SBart Van Assche 	sg = sgl;
635e80a0af4SBart Van Assche 	while (length) {
636e80a0af4SBart Van Assche 		elem_len = min_t(u64, length, PAGE_SIZE << order);
637e80a0af4SBart Van Assche 		page = alloc_pages(gfp, order);
638e80a0af4SBart Van Assche 		if (!page) {
639b2a182a4SDouglas Gilbert 			sgl_free_order(sgl, order);
640e80a0af4SBart Van Assche 			return NULL;
641e80a0af4SBart Van Assche 		}
642e80a0af4SBart Van Assche 
643e80a0af4SBart Van Assche 		sg_set_page(sg, page, elem_len, 0);
644e80a0af4SBart Van Assche 		length -= elem_len;
645e80a0af4SBart Van Assche 		sg = sg_next(sg);
646e80a0af4SBart Van Assche 	}
6478c7a8d1cSBart Van Assche 	WARN_ONCE(length, "length = %lld\n", length);
648e80a0af4SBart Van Assche 	if (nent_p)
649e80a0af4SBart Van Assche 		*nent_p = nent;
650e80a0af4SBart Van Assche 	return sgl;
651e80a0af4SBart Van Assche }
652e80a0af4SBart Van Assche EXPORT_SYMBOL(sgl_alloc_order);
653e80a0af4SBart Van Assche 
654e80a0af4SBart Van Assche /**
655e80a0af4SBart Van Assche  * sgl_alloc - allocate a scatterlist and its pages
656e80a0af4SBart Van Assche  * @length: Length in bytes of the scatterlist
657e80a0af4SBart Van Assche  * @gfp: Memory allocation flags
658e80a0af4SBart Van Assche  * @nent_p: [out] Number of entries in the scatterlist
659e80a0af4SBart Van Assche  *
660e80a0af4SBart Van Assche  * Returns: A pointer to an initialized scatterlist or %NULL upon failure.
661e80a0af4SBart Van Assche  */
662e80a0af4SBart Van Assche struct scatterlist *sgl_alloc(unsigned long long length, gfp_t gfp,
663e80a0af4SBart Van Assche 			      unsigned int *nent_p)
664e80a0af4SBart Van Assche {
665e80a0af4SBart Van Assche 	return sgl_alloc_order(length, 0, false, gfp, nent_p);
666e80a0af4SBart Van Assche }
667e80a0af4SBart Van Assche EXPORT_SYMBOL(sgl_alloc);
668e80a0af4SBart Van Assche 
669e80a0af4SBart Van Assche /**
6708c7a8d1cSBart Van Assche  * sgl_free_n_order - free a scatterlist and its pages
6718c7a8d1cSBart Van Assche  * @sgl: Scatterlist with one or more elements
6728c7a8d1cSBart Van Assche  * @nents: Maximum number of elements to free
6738c7a8d1cSBart Van Assche  * @order: Second argument for __free_pages()
6748c7a8d1cSBart Van Assche  *
6758c7a8d1cSBart Van Assche  * Notes:
6768c7a8d1cSBart Van Assche  * - If several scatterlists have been chained and each chain element is
6778c7a8d1cSBart Van Assche  *   freed separately then it's essential to set nents correctly to avoid that a
6788c7a8d1cSBart Van Assche  *   page would get freed twice.
6798c7a8d1cSBart Van Assche  * - All pages in a chained scatterlist can be freed at once by setting @nents
6808c7a8d1cSBart Van Assche  *   to a high number.
6818c7a8d1cSBart Van Assche  */
6828c7a8d1cSBart Van Assche void sgl_free_n_order(struct scatterlist *sgl, int nents, int order)
6838c7a8d1cSBart Van Assche {
6848c7a8d1cSBart Van Assche 	struct scatterlist *sg;
6858c7a8d1cSBart Van Assche 	struct page *page;
6868c7a8d1cSBart Van Assche 	int i;
6878c7a8d1cSBart Van Assche 
6888c7a8d1cSBart Van Assche 	for_each_sg(sgl, sg, nents, i) {
6898c7a8d1cSBart Van Assche 		if (!sg)
6908c7a8d1cSBart Van Assche 			break;
6918c7a8d1cSBart Van Assche 		page = sg_page(sg);
6928c7a8d1cSBart Van Assche 		if (page)
6938c7a8d1cSBart Van Assche 			__free_pages(page, order);
6948c7a8d1cSBart Van Assche 	}
6958c7a8d1cSBart Van Assche 	kfree(sgl);
6968c7a8d1cSBart Van Assche }
6978c7a8d1cSBart Van Assche EXPORT_SYMBOL(sgl_free_n_order);
6988c7a8d1cSBart Van Assche 
6998c7a8d1cSBart Van Assche /**
700e80a0af4SBart Van Assche  * sgl_free_order - free a scatterlist and its pages
701e80a0af4SBart Van Assche  * @sgl: Scatterlist with one or more elements
702e80a0af4SBart Van Assche  * @order: Second argument for __free_pages()
703e80a0af4SBart Van Assche  */
704e80a0af4SBart Van Assche void sgl_free_order(struct scatterlist *sgl, int order)
705e80a0af4SBart Van Assche {
7068c7a8d1cSBart Van Assche 	sgl_free_n_order(sgl, INT_MAX, order);
707e80a0af4SBart Van Assche }
708e80a0af4SBart Van Assche EXPORT_SYMBOL(sgl_free_order);
709e80a0af4SBart Van Assche 
710e80a0af4SBart Van Assche /**
711e80a0af4SBart Van Assche  * sgl_free - free a scatterlist and its pages
712e80a0af4SBart Van Assche  * @sgl: Scatterlist with one or more elements
713e80a0af4SBart Van Assche  */
714e80a0af4SBart Van Assche void sgl_free(struct scatterlist *sgl)
715e80a0af4SBart Van Assche {
716e80a0af4SBart Van Assche 	sgl_free_order(sgl, 0);
717e80a0af4SBart Van Assche }
718e80a0af4SBart Van Assche EXPORT_SYMBOL(sgl_free);
719e80a0af4SBart Van Assche 
720e80a0af4SBart Van Assche #endif /* CONFIG_SGL_ALLOC */
721e80a0af4SBart Van Assche 
722a321e91bSImre Deak void __sg_page_iter_start(struct sg_page_iter *piter,
723a321e91bSImre Deak 			  struct scatterlist *sglist, unsigned int nents,
724a321e91bSImre Deak 			  unsigned long pgoffset)
725a321e91bSImre Deak {
726a321e91bSImre Deak 	piter->__pg_advance = 0;
727a321e91bSImre Deak 	piter->__nents = nents;
728a321e91bSImre Deak 
729a321e91bSImre Deak 	piter->sg = sglist;
730a321e91bSImre Deak 	piter->sg_pgoffset = pgoffset;
731a321e91bSImre Deak }
732a321e91bSImre Deak EXPORT_SYMBOL(__sg_page_iter_start);
733a321e91bSImre Deak 
734a321e91bSImre Deak static int sg_page_count(struct scatterlist *sg)
735a321e91bSImre Deak {
736a321e91bSImre Deak 	return PAGE_ALIGN(sg->offset + sg->length) >> PAGE_SHIFT;
737a321e91bSImre Deak }
738a321e91bSImre Deak 
739a321e91bSImre Deak bool __sg_page_iter_next(struct sg_page_iter *piter)
740a321e91bSImre Deak {
741a321e91bSImre Deak 	if (!piter->__nents || !piter->sg)
742a321e91bSImre Deak 		return false;
743a321e91bSImre Deak 
744a321e91bSImre Deak 	piter->sg_pgoffset += piter->__pg_advance;
745a321e91bSImre Deak 	piter->__pg_advance = 1;
746a321e91bSImre Deak 
747a321e91bSImre Deak 	while (piter->sg_pgoffset >= sg_page_count(piter->sg)) {
748a321e91bSImre Deak 		piter->sg_pgoffset -= sg_page_count(piter->sg);
749a321e91bSImre Deak 		piter->sg = sg_next(piter->sg);
750a321e91bSImre Deak 		if (!--piter->__nents || !piter->sg)
751a321e91bSImre Deak 			return false;
752a321e91bSImre Deak 	}
753a321e91bSImre Deak 
754a321e91bSImre Deak 	return true;
755a321e91bSImre Deak }
756a321e91bSImre Deak EXPORT_SYMBOL(__sg_page_iter_next);
757a321e91bSImre Deak 
758d901b276SJason Gunthorpe static int sg_dma_page_count(struct scatterlist *sg)
759d901b276SJason Gunthorpe {
760d901b276SJason Gunthorpe 	return PAGE_ALIGN(sg->offset + sg_dma_len(sg)) >> PAGE_SHIFT;
761d901b276SJason Gunthorpe }
762d901b276SJason Gunthorpe 
763d901b276SJason Gunthorpe bool __sg_page_iter_dma_next(struct sg_dma_page_iter *dma_iter)
764d901b276SJason Gunthorpe {
765d901b276SJason Gunthorpe 	struct sg_page_iter *piter = &dma_iter->base;
766d901b276SJason Gunthorpe 
767d901b276SJason Gunthorpe 	if (!piter->__nents || !piter->sg)
768d901b276SJason Gunthorpe 		return false;
769d901b276SJason Gunthorpe 
770d901b276SJason Gunthorpe 	piter->sg_pgoffset += piter->__pg_advance;
771d901b276SJason Gunthorpe 	piter->__pg_advance = 1;
772d901b276SJason Gunthorpe 
773d901b276SJason Gunthorpe 	while (piter->sg_pgoffset >= sg_dma_page_count(piter->sg)) {
774d901b276SJason Gunthorpe 		piter->sg_pgoffset -= sg_dma_page_count(piter->sg);
775d901b276SJason Gunthorpe 		piter->sg = sg_next(piter->sg);
776d901b276SJason Gunthorpe 		if (!--piter->__nents || !piter->sg)
777d901b276SJason Gunthorpe 			return false;
778d901b276SJason Gunthorpe 	}
779d901b276SJason Gunthorpe 
780d901b276SJason Gunthorpe 	return true;
781d901b276SJason Gunthorpe }
782d901b276SJason Gunthorpe EXPORT_SYMBOL(__sg_page_iter_dma_next);
783d901b276SJason Gunthorpe 
784efc42bc9STomasz Stanislawski /**
785137d3edbSTejun Heo  * sg_miter_start - start mapping iteration over a sg list
786137d3edbSTejun Heo  * @miter: sg mapping iter to be started
787137d3edbSTejun Heo  * @sgl: sg list to iterate over
788137d3edbSTejun Heo  * @nents: number of sg entries
789137d3edbSTejun Heo  *
790137d3edbSTejun Heo  * Description:
791137d3edbSTejun Heo  *   Starts mapping iterator @miter.
792137d3edbSTejun Heo  *
793137d3edbSTejun Heo  * Context:
794137d3edbSTejun Heo  *   Don't care.
795137d3edbSTejun Heo  */
796137d3edbSTejun Heo void sg_miter_start(struct sg_mapping_iter *miter, struct scatterlist *sgl,
797137d3edbSTejun Heo 		    unsigned int nents, unsigned int flags)
798137d3edbSTejun Heo {
799137d3edbSTejun Heo 	memset(miter, 0, sizeof(struct sg_mapping_iter));
800137d3edbSTejun Heo 
8014225fc85SImre Deak 	__sg_page_iter_start(&miter->piter, sgl, nents, 0);
8026de7e356SSebastian Andrzej Siewior 	WARN_ON(!(flags & (SG_MITER_TO_SG | SG_MITER_FROM_SG)));
803137d3edbSTejun Heo 	miter->__flags = flags;
804137d3edbSTejun Heo }
805137d3edbSTejun Heo EXPORT_SYMBOL(sg_miter_start);
806137d3edbSTejun Heo 
80711052004SAkinobu Mita static bool sg_miter_get_next_page(struct sg_mapping_iter *miter)
80811052004SAkinobu Mita {
80911052004SAkinobu Mita 	if (!miter->__remaining) {
81011052004SAkinobu Mita 		struct scatterlist *sg;
81111052004SAkinobu Mita 
81211052004SAkinobu Mita 		if (!__sg_page_iter_next(&miter->piter))
81311052004SAkinobu Mita 			return false;
81411052004SAkinobu Mita 
81511052004SAkinobu Mita 		sg = miter->piter.sg;
81611052004SAkinobu Mita 
817aeb87246SChristophe Leroy 		miter->__offset = miter->piter.sg_pgoffset ? 0 : sg->offset;
818aeb87246SChristophe Leroy 		miter->piter.sg_pgoffset += miter->__offset >> PAGE_SHIFT;
819aeb87246SChristophe Leroy 		miter->__offset &= PAGE_SIZE - 1;
82011052004SAkinobu Mita 		miter->__remaining = sg->offset + sg->length -
821aeb87246SChristophe Leroy 				     (miter->piter.sg_pgoffset << PAGE_SHIFT) -
822aeb87246SChristophe Leroy 				     miter->__offset;
82311052004SAkinobu Mita 		miter->__remaining = min_t(unsigned long, miter->__remaining,
82411052004SAkinobu Mita 					   PAGE_SIZE - miter->__offset);
82511052004SAkinobu Mita 	}
82611052004SAkinobu Mita 
82711052004SAkinobu Mita 	return true;
82811052004SAkinobu Mita }
82911052004SAkinobu Mita 
830137d3edbSTejun Heo /**
831df642ceaSAkinobu Mita  * sg_miter_skip - reposition mapping iterator
832df642ceaSAkinobu Mita  * @miter: sg mapping iter to be skipped
833df642ceaSAkinobu Mita  * @offset: number of bytes to plus the current location
834df642ceaSAkinobu Mita  *
835df642ceaSAkinobu Mita  * Description:
836df642ceaSAkinobu Mita  *   Sets the offset of @miter to its current location plus @offset bytes.
837df642ceaSAkinobu Mita  *   If mapping iterator @miter has been proceeded by sg_miter_next(), this
838df642ceaSAkinobu Mita  *   stops @miter.
839df642ceaSAkinobu Mita  *
840df642ceaSAkinobu Mita  * Context:
841723aca20SThomas Gleixner  *   Don't care.
842df642ceaSAkinobu Mita  *
843df642ceaSAkinobu Mita  * Returns:
844df642ceaSAkinobu Mita  *   true if @miter contains the valid mapping.  false if end of sg
845df642ceaSAkinobu Mita  *   list is reached.
846df642ceaSAkinobu Mita  */
8470d6077f8SMing Lei bool sg_miter_skip(struct sg_mapping_iter *miter, off_t offset)
848df642ceaSAkinobu Mita {
849df642ceaSAkinobu Mita 	sg_miter_stop(miter);
850df642ceaSAkinobu Mita 
851df642ceaSAkinobu Mita 	while (offset) {
852df642ceaSAkinobu Mita 		off_t consumed;
853df642ceaSAkinobu Mita 
854df642ceaSAkinobu Mita 		if (!sg_miter_get_next_page(miter))
855df642ceaSAkinobu Mita 			return false;
856df642ceaSAkinobu Mita 
857df642ceaSAkinobu Mita 		consumed = min_t(off_t, offset, miter->__remaining);
858df642ceaSAkinobu Mita 		miter->__offset += consumed;
859df642ceaSAkinobu Mita 		miter->__remaining -= consumed;
860df642ceaSAkinobu Mita 		offset -= consumed;
861df642ceaSAkinobu Mita 	}
862df642ceaSAkinobu Mita 
863df642ceaSAkinobu Mita 	return true;
864df642ceaSAkinobu Mita }
8650d6077f8SMing Lei EXPORT_SYMBOL(sg_miter_skip);
866df642ceaSAkinobu Mita 
867df642ceaSAkinobu Mita /**
868137d3edbSTejun Heo  * sg_miter_next - proceed mapping iterator to the next mapping
869137d3edbSTejun Heo  * @miter: sg mapping iter to proceed
870137d3edbSTejun Heo  *
871137d3edbSTejun Heo  * Description:
8728290e2d2STejun Heo  *   Proceeds @miter to the next mapping.  @miter should have been started
8738290e2d2STejun Heo  *   using sg_miter_start().  On successful return, @miter->page,
8748290e2d2STejun Heo  *   @miter->addr and @miter->length point to the current mapping.
875137d3edbSTejun Heo  *
876137d3edbSTejun Heo  * Context:
877723aca20SThomas Gleixner  *   May sleep if !SG_MITER_ATOMIC.
878137d3edbSTejun Heo  *
879137d3edbSTejun Heo  * Returns:
880137d3edbSTejun Heo  *   true if @miter contains the next mapping.  false if end of sg
881137d3edbSTejun Heo  *   list is reached.
882137d3edbSTejun Heo  */
883137d3edbSTejun Heo bool sg_miter_next(struct sg_mapping_iter *miter)
884137d3edbSTejun Heo {
885137d3edbSTejun Heo 	sg_miter_stop(miter);
886137d3edbSTejun Heo 
8874225fc85SImre Deak 	/*
8884225fc85SImre Deak 	 * Get to the next page if necessary.
8894225fc85SImre Deak 	 * __remaining, __offset is adjusted by sg_miter_stop
8904225fc85SImre Deak 	 */
89111052004SAkinobu Mita 	if (!sg_miter_get_next_page(miter))
89223c560a9STejun Heo 		return false;
8934225fc85SImre Deak 
8942db76d7cSImre Deak 	miter->page = sg_page_iter_page(&miter->piter);
8954225fc85SImre Deak 	miter->consumed = miter->length = miter->__remaining;
896137d3edbSTejun Heo 
897137d3edbSTejun Heo 	if (miter->__flags & SG_MITER_ATOMIC)
8984225fc85SImre Deak 		miter->addr = kmap_atomic(miter->page) + miter->__offset;
899137d3edbSTejun Heo 	else
9004225fc85SImre Deak 		miter->addr = kmap(miter->page) + miter->__offset;
901137d3edbSTejun Heo 
902137d3edbSTejun Heo 	return true;
903137d3edbSTejun Heo }
904137d3edbSTejun Heo EXPORT_SYMBOL(sg_miter_next);
905137d3edbSTejun Heo 
906137d3edbSTejun Heo /**
907137d3edbSTejun Heo  * sg_miter_stop - stop mapping iteration
908137d3edbSTejun Heo  * @miter: sg mapping iter to be stopped
909137d3edbSTejun Heo  *
910137d3edbSTejun Heo  * Description:
911137d3edbSTejun Heo  *   Stops mapping iterator @miter.  @miter should have been started
9124ba6a2b2SMasahiro Yamada  *   using sg_miter_start().  A stopped iteration can be resumed by
9134ba6a2b2SMasahiro Yamada  *   calling sg_miter_next() on it.  This is useful when resources (kmap)
9144ba6a2b2SMasahiro Yamada  *   need to be released during iteration.
915137d3edbSTejun Heo  *
916137d3edbSTejun Heo  * Context:
917723aca20SThomas Gleixner  *   Don't care otherwise.
918137d3edbSTejun Heo  */
919137d3edbSTejun Heo void sg_miter_stop(struct sg_mapping_iter *miter)
920137d3edbSTejun Heo {
921137d3edbSTejun Heo 	WARN_ON(miter->consumed > miter->length);
922137d3edbSTejun Heo 
923137d3edbSTejun Heo 	/* drop resources from the last iteration */
924137d3edbSTejun Heo 	if (miter->addr) {
925137d3edbSTejun Heo 		miter->__offset += miter->consumed;
9264225fc85SImre Deak 		miter->__remaining -= miter->consumed;
927137d3edbSTejun Heo 
9280e84f5dbSChristoph Hellwig 		if (miter->__flags & SG_MITER_TO_SG)
9290e84f5dbSChristoph Hellwig 			flush_dcache_page(miter->page);
9306de7e356SSebastian Andrzej Siewior 
931137d3edbSTejun Heo 		if (miter->__flags & SG_MITER_ATOMIC) {
932723aca20SThomas Gleixner 			WARN_ON_ONCE(!pagefault_disabled());
933c3eede8eSCong Wang 			kunmap_atomic(miter->addr);
934137d3edbSTejun Heo 		} else
935f652c521SArjan van de Ven 			kunmap(miter->page);
936137d3edbSTejun Heo 
937137d3edbSTejun Heo 		miter->page = NULL;
938137d3edbSTejun Heo 		miter->addr = NULL;
939137d3edbSTejun Heo 		miter->length = 0;
940137d3edbSTejun Heo 		miter->consumed = 0;
941137d3edbSTejun Heo 	}
942137d3edbSTejun Heo }
943137d3edbSTejun Heo EXPORT_SYMBOL(sg_miter_stop);
944137d3edbSTejun Heo 
945137d3edbSTejun Heo /**
946b1adaf65SFUJITA Tomonori  * sg_copy_buffer - Copy data between a linear buffer and an SG list
947b1adaf65SFUJITA Tomonori  * @sgl:		 The SG list
948b1adaf65SFUJITA Tomonori  * @nents:		 Number of SG entries
949b1adaf65SFUJITA Tomonori  * @buf:		 Where to copy from
950b1adaf65SFUJITA Tomonori  * @buflen:		 The number of bytes to copy
951df642ceaSAkinobu Mita  * @skip:		 Number of bytes to skip before copying
952df642ceaSAkinobu Mita  * @to_buffer:		 transfer direction (true == from an sg list to a
9536e853185SGeert Uytterhoeven  *			 buffer, false == from a buffer to an sg list)
954b1adaf65SFUJITA Tomonori  *
955b1adaf65SFUJITA Tomonori  * Returns the number of copied bytes.
956b1adaf65SFUJITA Tomonori  *
957b1adaf65SFUJITA Tomonori  **/
958386ecb12SDave Gordon size_t sg_copy_buffer(struct scatterlist *sgl, unsigned int nents, void *buf,
959386ecb12SDave Gordon 		      size_t buflen, off_t skip, bool to_buffer)
960b1adaf65SFUJITA Tomonori {
961137d3edbSTejun Heo 	unsigned int offset = 0;
962137d3edbSTejun Heo 	struct sg_mapping_iter miter;
9636de7e356SSebastian Andrzej Siewior 	unsigned int sg_flags = SG_MITER_ATOMIC;
964b1adaf65SFUJITA Tomonori 
9656de7e356SSebastian Andrzej Siewior 	if (to_buffer)
9666de7e356SSebastian Andrzej Siewior 		sg_flags |= SG_MITER_FROM_SG;
9676de7e356SSebastian Andrzej Siewior 	else
9686de7e356SSebastian Andrzej Siewior 		sg_flags |= SG_MITER_TO_SG;
9696de7e356SSebastian Andrzej Siewior 
9706de7e356SSebastian Andrzej Siewior 	sg_miter_start(&miter, sgl, nents, sg_flags);
971b1adaf65SFUJITA Tomonori 
972df642ceaSAkinobu Mita 	if (!sg_miter_skip(&miter, skip))
9731f41be7dSDavid Disseldorp 		return 0;
974df642ceaSAkinobu Mita 
9751d5210efSGilad Ben-Yossef 	while ((offset < buflen) && sg_miter_next(&miter)) {
976137d3edbSTejun Heo 		unsigned int len;
977b1adaf65SFUJITA Tomonori 
978137d3edbSTejun Heo 		len = min(miter.length, buflen - offset);
979b1adaf65SFUJITA Tomonori 
980b1adaf65SFUJITA Tomonori 		if (to_buffer)
981137d3edbSTejun Heo 			memcpy(buf + offset, miter.addr, len);
9826de7e356SSebastian Andrzej Siewior 		else
983137d3edbSTejun Heo 			memcpy(miter.addr, buf + offset, len);
984b1adaf65SFUJITA Tomonori 
985137d3edbSTejun Heo 		offset += len;
986b1adaf65SFUJITA Tomonori 	}
987b1adaf65SFUJITA Tomonori 
988137d3edbSTejun Heo 	sg_miter_stop(&miter);
989b1adaf65SFUJITA Tomonori 
990137d3edbSTejun Heo 	return offset;
991b1adaf65SFUJITA Tomonori }
992386ecb12SDave Gordon EXPORT_SYMBOL(sg_copy_buffer);
993b1adaf65SFUJITA Tomonori 
994b1adaf65SFUJITA Tomonori /**
995b1adaf65SFUJITA Tomonori  * sg_copy_from_buffer - Copy from a linear buffer to an SG list
996b1adaf65SFUJITA Tomonori  * @sgl:		 The SG list
997b1adaf65SFUJITA Tomonori  * @nents:		 Number of SG entries
998b1adaf65SFUJITA Tomonori  * @buf:		 Where to copy from
999b1adaf65SFUJITA Tomonori  * @buflen:		 The number of bytes to copy
1000b1adaf65SFUJITA Tomonori  *
1001b1adaf65SFUJITA Tomonori  * Returns the number of copied bytes.
1002b1adaf65SFUJITA Tomonori  *
1003b1adaf65SFUJITA Tomonori  **/
1004b1adaf65SFUJITA Tomonori size_t sg_copy_from_buffer(struct scatterlist *sgl, unsigned int nents,
10052a1bf8f9SDave Gordon 			   const void *buf, size_t buflen)
1006b1adaf65SFUJITA Tomonori {
10072a1bf8f9SDave Gordon 	return sg_copy_buffer(sgl, nents, (void *)buf, buflen, 0, false);
1008b1adaf65SFUJITA Tomonori }
1009b1adaf65SFUJITA Tomonori EXPORT_SYMBOL(sg_copy_from_buffer);
1010b1adaf65SFUJITA Tomonori 
1011b1adaf65SFUJITA Tomonori /**
1012b1adaf65SFUJITA Tomonori  * sg_copy_to_buffer - Copy from an SG list to a linear buffer
1013b1adaf65SFUJITA Tomonori  * @sgl:		 The SG list
1014b1adaf65SFUJITA Tomonori  * @nents:		 Number of SG entries
1015b1adaf65SFUJITA Tomonori  * @buf:		 Where to copy to
1016b1adaf65SFUJITA Tomonori  * @buflen:		 The number of bytes to copy
1017b1adaf65SFUJITA Tomonori  *
1018b1adaf65SFUJITA Tomonori  * Returns the number of copied bytes.
1019b1adaf65SFUJITA Tomonori  *
1020b1adaf65SFUJITA Tomonori  **/
1021b1adaf65SFUJITA Tomonori size_t sg_copy_to_buffer(struct scatterlist *sgl, unsigned int nents,
1022b1adaf65SFUJITA Tomonori 			 void *buf, size_t buflen)
1023b1adaf65SFUJITA Tomonori {
1024df642ceaSAkinobu Mita 	return sg_copy_buffer(sgl, nents, buf, buflen, 0, true);
1025b1adaf65SFUJITA Tomonori }
1026b1adaf65SFUJITA Tomonori EXPORT_SYMBOL(sg_copy_to_buffer);
1027df642ceaSAkinobu Mita 
1028df642ceaSAkinobu Mita /**
1029df642ceaSAkinobu Mita  * sg_pcopy_from_buffer - Copy from a linear buffer to an SG list
1030df642ceaSAkinobu Mita  * @sgl:		 The SG list
1031df642ceaSAkinobu Mita  * @nents:		 Number of SG entries
1032df642ceaSAkinobu Mita  * @buf:		 Where to copy from
1033df642ceaSAkinobu Mita  * @buflen:		 The number of bytes to copy
10344dc7daf8SDave Gordon  * @skip:		 Number of bytes to skip before copying
1035df642ceaSAkinobu Mita  *
1036df642ceaSAkinobu Mita  * Returns the number of copied bytes.
1037df642ceaSAkinobu Mita  *
1038df642ceaSAkinobu Mita  **/
1039df642ceaSAkinobu Mita size_t sg_pcopy_from_buffer(struct scatterlist *sgl, unsigned int nents,
10402a1bf8f9SDave Gordon 			    const void *buf, size_t buflen, off_t skip)
1041df642ceaSAkinobu Mita {
10422a1bf8f9SDave Gordon 	return sg_copy_buffer(sgl, nents, (void *)buf, buflen, skip, false);
1043df642ceaSAkinobu Mita }
1044df642ceaSAkinobu Mita EXPORT_SYMBOL(sg_pcopy_from_buffer);
1045df642ceaSAkinobu Mita 
1046df642ceaSAkinobu Mita /**
1047df642ceaSAkinobu Mita  * sg_pcopy_to_buffer - Copy from an SG list to a linear buffer
1048df642ceaSAkinobu Mita  * @sgl:		 The SG list
1049df642ceaSAkinobu Mita  * @nents:		 Number of SG entries
1050df642ceaSAkinobu Mita  * @buf:		 Where to copy to
1051df642ceaSAkinobu Mita  * @buflen:		 The number of bytes to copy
10524dc7daf8SDave Gordon  * @skip:		 Number of bytes to skip before copying
1053df642ceaSAkinobu Mita  *
1054df642ceaSAkinobu Mita  * Returns the number of copied bytes.
1055df642ceaSAkinobu Mita  *
1056df642ceaSAkinobu Mita  **/
1057df642ceaSAkinobu Mita size_t sg_pcopy_to_buffer(struct scatterlist *sgl, unsigned int nents,
1058df642ceaSAkinobu Mita 			  void *buf, size_t buflen, off_t skip)
1059df642ceaSAkinobu Mita {
1060df642ceaSAkinobu Mita 	return sg_copy_buffer(sgl, nents, buf, buflen, skip, true);
1061df642ceaSAkinobu Mita }
1062df642ceaSAkinobu Mita EXPORT_SYMBOL(sg_pcopy_to_buffer);
10630945e569SJohannes Thumshirn 
10640945e569SJohannes Thumshirn /**
10650945e569SJohannes Thumshirn  * sg_zero_buffer - Zero-out a part of a SG list
10660945e569SJohannes Thumshirn  * @sgl:		 The SG list
10670945e569SJohannes Thumshirn  * @nents:		 Number of SG entries
10680945e569SJohannes Thumshirn  * @buflen:		 The number of bytes to zero out
10690945e569SJohannes Thumshirn  * @skip:		 Number of bytes to skip before zeroing
10700945e569SJohannes Thumshirn  *
10710945e569SJohannes Thumshirn  * Returns the number of bytes zeroed.
10720945e569SJohannes Thumshirn  **/
10730945e569SJohannes Thumshirn size_t sg_zero_buffer(struct scatterlist *sgl, unsigned int nents,
10740945e569SJohannes Thumshirn 		       size_t buflen, off_t skip)
10750945e569SJohannes Thumshirn {
10760945e569SJohannes Thumshirn 	unsigned int offset = 0;
10770945e569SJohannes Thumshirn 	struct sg_mapping_iter miter;
10780945e569SJohannes Thumshirn 	unsigned int sg_flags = SG_MITER_ATOMIC | SG_MITER_TO_SG;
10790945e569SJohannes Thumshirn 
10800945e569SJohannes Thumshirn 	sg_miter_start(&miter, sgl, nents, sg_flags);
10810945e569SJohannes Thumshirn 
10820945e569SJohannes Thumshirn 	if (!sg_miter_skip(&miter, skip))
10830945e569SJohannes Thumshirn 		return false;
10840945e569SJohannes Thumshirn 
10850945e569SJohannes Thumshirn 	while (offset < buflen && sg_miter_next(&miter)) {
10860945e569SJohannes Thumshirn 		unsigned int len;
10870945e569SJohannes Thumshirn 
10880945e569SJohannes Thumshirn 		len = min(miter.length, buflen - offset);
10890945e569SJohannes Thumshirn 		memset(miter.addr, 0, len);
10900945e569SJohannes Thumshirn 
10910945e569SJohannes Thumshirn 		offset += len;
10920945e569SJohannes Thumshirn 	}
10930945e569SJohannes Thumshirn 
10940945e569SJohannes Thumshirn 	sg_miter_stop(&miter);
10950945e569SJohannes Thumshirn 	return offset;
10960945e569SJohannes Thumshirn }
10970945e569SJohannes Thumshirn EXPORT_SYMBOL(sg_zero_buffer);
1098