xref: /OK3568_Linux_fs/kernel/lib/scatterlist.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (C) 2007 Jens Axboe <jens.axboe@oracle.com>
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Scatterlist handling helpers.
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun #include <linux/export.h>
8*4882a593Smuzhiyun #include <linux/slab.h>
9*4882a593Smuzhiyun #include <linux/scatterlist.h>
10*4882a593Smuzhiyun #include <linux/highmem.h>
11*4882a593Smuzhiyun #include <linux/kmemleak.h>
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun /**
14*4882a593Smuzhiyun  * sg_next - return the next scatterlist entry in a list
15*4882a593Smuzhiyun  * @sg:		The current sg entry
16*4882a593Smuzhiyun  *
17*4882a593Smuzhiyun  * Description:
18*4882a593Smuzhiyun  *   Usually the next entry will be @sg@ + 1, but if this sg element is part
19*4882a593Smuzhiyun  *   of a chained scatterlist, it could jump to the start of a new
20*4882a593Smuzhiyun  *   scatterlist array.
21*4882a593Smuzhiyun  *
22*4882a593Smuzhiyun  **/
sg_next(struct scatterlist * sg)23*4882a593Smuzhiyun struct scatterlist *sg_next(struct scatterlist *sg)
24*4882a593Smuzhiyun {
25*4882a593Smuzhiyun 	if (sg_is_last(sg))
26*4882a593Smuzhiyun 		return NULL;
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun 	sg++;
29*4882a593Smuzhiyun 	if (unlikely(sg_is_chain(sg)))
30*4882a593Smuzhiyun 		sg = sg_chain_ptr(sg);
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun 	return sg;
33*4882a593Smuzhiyun }
34*4882a593Smuzhiyun EXPORT_SYMBOL(sg_next);
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun /**
37*4882a593Smuzhiyun  * sg_nents - return total count of entries in scatterlist
38*4882a593Smuzhiyun  * @sg:		The scatterlist
39*4882a593Smuzhiyun  *
40*4882a593Smuzhiyun  * Description:
41*4882a593Smuzhiyun  * Allows to know how many entries are in sg, taking into acount
42*4882a593Smuzhiyun  * chaining as well
43*4882a593Smuzhiyun  *
44*4882a593Smuzhiyun  **/
sg_nents(struct scatterlist * sg)45*4882a593Smuzhiyun int sg_nents(struct scatterlist *sg)
46*4882a593Smuzhiyun {
47*4882a593Smuzhiyun 	int nents;
48*4882a593Smuzhiyun 	for (nents = 0; sg; sg = sg_next(sg))
49*4882a593Smuzhiyun 		nents++;
50*4882a593Smuzhiyun 	return nents;
51*4882a593Smuzhiyun }
52*4882a593Smuzhiyun EXPORT_SYMBOL(sg_nents);
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun /**
55*4882a593Smuzhiyun  * sg_nents_for_len - return total count of entries in scatterlist
56*4882a593Smuzhiyun  *                    needed to satisfy the supplied length
57*4882a593Smuzhiyun  * @sg:		The scatterlist
58*4882a593Smuzhiyun  * @len:	The total required length
59*4882a593Smuzhiyun  *
60*4882a593Smuzhiyun  * Description:
61*4882a593Smuzhiyun  * Determines the number of entries in sg that are required to meet
62*4882a593Smuzhiyun  * the supplied length, taking into acount chaining as well
63*4882a593Smuzhiyun  *
64*4882a593Smuzhiyun  * Returns:
65*4882a593Smuzhiyun  *   the number of sg entries needed, negative error on failure
66*4882a593Smuzhiyun  *
67*4882a593Smuzhiyun  **/
sg_nents_for_len(struct scatterlist * sg,u64 len)68*4882a593Smuzhiyun int sg_nents_for_len(struct scatterlist *sg, u64 len)
69*4882a593Smuzhiyun {
70*4882a593Smuzhiyun 	int nents;
71*4882a593Smuzhiyun 	u64 total;
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun 	if (!len)
74*4882a593Smuzhiyun 		return 0;
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun 	for (nents = 0, total = 0; sg; sg = sg_next(sg)) {
77*4882a593Smuzhiyun 		nents++;
78*4882a593Smuzhiyun 		total += sg->length;
79*4882a593Smuzhiyun 		if (total >= len)
80*4882a593Smuzhiyun 			return nents;
81*4882a593Smuzhiyun 	}
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun 	return -EINVAL;
84*4882a593Smuzhiyun }
85*4882a593Smuzhiyun EXPORT_SYMBOL(sg_nents_for_len);
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun /**
88*4882a593Smuzhiyun  * sg_last - return the last scatterlist entry in a list
89*4882a593Smuzhiyun  * @sgl:	First entry in the scatterlist
90*4882a593Smuzhiyun  * @nents:	Number of entries in the scatterlist
91*4882a593Smuzhiyun  *
92*4882a593Smuzhiyun  * Description:
93*4882a593Smuzhiyun  *   Should only be used casually, it (currently) scans the entire list
94*4882a593Smuzhiyun  *   to get the last entry.
95*4882a593Smuzhiyun  *
96*4882a593Smuzhiyun  *   Note that the @sgl@ pointer passed in need not be the first one,
97*4882a593Smuzhiyun  *   the important bit is that @nents@ denotes the number of entries that
98*4882a593Smuzhiyun  *   exist from @sgl@.
99*4882a593Smuzhiyun  *
100*4882a593Smuzhiyun  **/
sg_last(struct scatterlist * sgl,unsigned int nents)101*4882a593Smuzhiyun struct scatterlist *sg_last(struct scatterlist *sgl, unsigned int nents)
102*4882a593Smuzhiyun {
103*4882a593Smuzhiyun 	struct scatterlist *sg, *ret = NULL;
104*4882a593Smuzhiyun 	unsigned int i;
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun 	for_each_sg(sgl, sg, nents, i)
107*4882a593Smuzhiyun 		ret = sg;
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun 	BUG_ON(!sg_is_last(ret));
110*4882a593Smuzhiyun 	return ret;
111*4882a593Smuzhiyun }
112*4882a593Smuzhiyun EXPORT_SYMBOL(sg_last);
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun /**
115*4882a593Smuzhiyun  * sg_init_table - Initialize SG table
116*4882a593Smuzhiyun  * @sgl:	   The SG table
117*4882a593Smuzhiyun  * @nents:	   Number of entries in table
118*4882a593Smuzhiyun  *
119*4882a593Smuzhiyun  * Notes:
120*4882a593Smuzhiyun  *   If this is part of a chained sg table, sg_mark_end() should be
121*4882a593Smuzhiyun  *   used only on the last table part.
122*4882a593Smuzhiyun  *
123*4882a593Smuzhiyun  **/
sg_init_table(struct scatterlist * sgl,unsigned int nents)124*4882a593Smuzhiyun void sg_init_table(struct scatterlist *sgl, unsigned int nents)
125*4882a593Smuzhiyun {
126*4882a593Smuzhiyun 	memset(sgl, 0, sizeof(*sgl) * nents);
127*4882a593Smuzhiyun 	sg_init_marker(sgl, nents);
128*4882a593Smuzhiyun }
129*4882a593Smuzhiyun EXPORT_SYMBOL(sg_init_table);
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun /**
132*4882a593Smuzhiyun  * sg_init_one - Initialize a single entry sg list
133*4882a593Smuzhiyun  * @sg:		 SG entry
134*4882a593Smuzhiyun  * @buf:	 Virtual address for IO
135*4882a593Smuzhiyun  * @buflen:	 IO length
136*4882a593Smuzhiyun  *
137*4882a593Smuzhiyun  **/
sg_init_one(struct scatterlist * sg,const void * buf,unsigned int buflen)138*4882a593Smuzhiyun void sg_init_one(struct scatterlist *sg, const void *buf, unsigned int buflen)
139*4882a593Smuzhiyun {
140*4882a593Smuzhiyun 	sg_init_table(sg, 1);
141*4882a593Smuzhiyun 	sg_set_buf(sg, buf, buflen);
142*4882a593Smuzhiyun }
143*4882a593Smuzhiyun EXPORT_SYMBOL(sg_init_one);
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun /*
146*4882a593Smuzhiyun  * The default behaviour of sg_alloc_table() is to use these kmalloc/kfree
147*4882a593Smuzhiyun  * helpers.
148*4882a593Smuzhiyun  */
sg_kmalloc(unsigned int nents,gfp_t gfp_mask)149*4882a593Smuzhiyun static struct scatterlist *sg_kmalloc(unsigned int nents, gfp_t gfp_mask)
150*4882a593Smuzhiyun {
151*4882a593Smuzhiyun 	if (nents == SG_MAX_SINGLE_ALLOC) {
152*4882a593Smuzhiyun 		/*
153*4882a593Smuzhiyun 		 * Kmemleak doesn't track page allocations as they are not
154*4882a593Smuzhiyun 		 * commonly used (in a raw form) for kernel data structures.
155*4882a593Smuzhiyun 		 * As we chain together a list of pages and then a normal
156*4882a593Smuzhiyun 		 * kmalloc (tracked by kmemleak), in order to for that last
157*4882a593Smuzhiyun 		 * allocation not to become decoupled (and thus a
158*4882a593Smuzhiyun 		 * false-positive) we need to inform kmemleak of all the
159*4882a593Smuzhiyun 		 * intermediate allocations.
160*4882a593Smuzhiyun 		 */
161*4882a593Smuzhiyun 		void *ptr = (void *) __get_free_page(gfp_mask);
162*4882a593Smuzhiyun 		kmemleak_alloc(ptr, PAGE_SIZE, 1, gfp_mask);
163*4882a593Smuzhiyun 		return ptr;
164*4882a593Smuzhiyun 	} else
165*4882a593Smuzhiyun 		return kmalloc_array(nents, sizeof(struct scatterlist),
166*4882a593Smuzhiyun 				     gfp_mask);
167*4882a593Smuzhiyun }
168*4882a593Smuzhiyun 
sg_kfree(struct scatterlist * sg,unsigned int nents)169*4882a593Smuzhiyun static void sg_kfree(struct scatterlist *sg, unsigned int nents)
170*4882a593Smuzhiyun {
171*4882a593Smuzhiyun 	if (nents == SG_MAX_SINGLE_ALLOC) {
172*4882a593Smuzhiyun 		kmemleak_free(sg);
173*4882a593Smuzhiyun 		free_page((unsigned long) sg);
174*4882a593Smuzhiyun 	} else
175*4882a593Smuzhiyun 		kfree(sg);
176*4882a593Smuzhiyun }
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun /**
179*4882a593Smuzhiyun  * __sg_free_table - Free a previously mapped sg table
180*4882a593Smuzhiyun  * @table:	The sg table header to use
181*4882a593Smuzhiyun  * @max_ents:	The maximum number of entries per single scatterlist
182*4882a593Smuzhiyun  * @nents_first_chunk: Number of entries int the (preallocated) first
183*4882a593Smuzhiyun  * 	scatterlist chunk, 0 means no such preallocated first chunk
184*4882a593Smuzhiyun  * @free_fn:	Free function
185*4882a593Smuzhiyun  *
186*4882a593Smuzhiyun  *  Description:
187*4882a593Smuzhiyun  *    Free an sg table previously allocated and setup with
188*4882a593Smuzhiyun  *    __sg_alloc_table().  The @max_ents value must be identical to
189*4882a593Smuzhiyun  *    that previously used with __sg_alloc_table().
190*4882a593Smuzhiyun  *
191*4882a593Smuzhiyun  **/
__sg_free_table(struct sg_table * table,unsigned int max_ents,unsigned int nents_first_chunk,sg_free_fn * free_fn)192*4882a593Smuzhiyun void __sg_free_table(struct sg_table *table, unsigned int max_ents,
193*4882a593Smuzhiyun 		     unsigned int nents_first_chunk, sg_free_fn *free_fn)
194*4882a593Smuzhiyun {
195*4882a593Smuzhiyun 	struct scatterlist *sgl, *next;
196*4882a593Smuzhiyun 	unsigned curr_max_ents = nents_first_chunk ?: max_ents;
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun 	if (unlikely(!table->sgl))
199*4882a593Smuzhiyun 		return;
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun 	sgl = table->sgl;
202*4882a593Smuzhiyun 	while (table->orig_nents) {
203*4882a593Smuzhiyun 		unsigned int alloc_size = table->orig_nents;
204*4882a593Smuzhiyun 		unsigned int sg_size;
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun 		/*
207*4882a593Smuzhiyun 		 * If we have more than max_ents segments left,
208*4882a593Smuzhiyun 		 * then assign 'next' to the sg table after the current one.
209*4882a593Smuzhiyun 		 * sg_size is then one less than alloc size, since the last
210*4882a593Smuzhiyun 		 * element is the chain pointer.
211*4882a593Smuzhiyun 		 */
212*4882a593Smuzhiyun 		if (alloc_size > curr_max_ents) {
213*4882a593Smuzhiyun 			next = sg_chain_ptr(&sgl[curr_max_ents - 1]);
214*4882a593Smuzhiyun 			alloc_size = curr_max_ents;
215*4882a593Smuzhiyun 			sg_size = alloc_size - 1;
216*4882a593Smuzhiyun 		} else {
217*4882a593Smuzhiyun 			sg_size = alloc_size;
218*4882a593Smuzhiyun 			next = NULL;
219*4882a593Smuzhiyun 		}
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun 		table->orig_nents -= sg_size;
222*4882a593Smuzhiyun 		if (nents_first_chunk)
223*4882a593Smuzhiyun 			nents_first_chunk = 0;
224*4882a593Smuzhiyun 		else
225*4882a593Smuzhiyun 			free_fn(sgl, alloc_size);
226*4882a593Smuzhiyun 		sgl = next;
227*4882a593Smuzhiyun 		curr_max_ents = max_ents;
228*4882a593Smuzhiyun 	}
229*4882a593Smuzhiyun 
230*4882a593Smuzhiyun 	table->sgl = NULL;
231*4882a593Smuzhiyun }
232*4882a593Smuzhiyun EXPORT_SYMBOL(__sg_free_table);
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun /**
235*4882a593Smuzhiyun  * sg_free_table - Free a previously allocated sg table
236*4882a593Smuzhiyun  * @table:	The mapped sg table header
237*4882a593Smuzhiyun  *
238*4882a593Smuzhiyun  **/
sg_free_table(struct sg_table * table)239*4882a593Smuzhiyun void sg_free_table(struct sg_table *table)
240*4882a593Smuzhiyun {
241*4882a593Smuzhiyun 	__sg_free_table(table, SG_MAX_SINGLE_ALLOC, false, sg_kfree);
242*4882a593Smuzhiyun }
243*4882a593Smuzhiyun EXPORT_SYMBOL(sg_free_table);
244*4882a593Smuzhiyun 
245*4882a593Smuzhiyun /**
246*4882a593Smuzhiyun  * __sg_alloc_table - Allocate and initialize an sg table with given allocator
247*4882a593Smuzhiyun  * @table:	The sg table header to use
248*4882a593Smuzhiyun  * @nents:	Number of entries in sg list
249*4882a593Smuzhiyun  * @max_ents:	The maximum number of entries the allocator returns per call
250*4882a593Smuzhiyun  * @nents_first_chunk: Number of entries int the (preallocated) first
251*4882a593Smuzhiyun  * 	scatterlist chunk, 0 means no such preallocated chunk provided by user
252*4882a593Smuzhiyun  * @gfp_mask:	GFP allocation mask
253*4882a593Smuzhiyun  * @alloc_fn:	Allocator to use
254*4882a593Smuzhiyun  *
255*4882a593Smuzhiyun  * Description:
256*4882a593Smuzhiyun  *   This function returns a @table @nents long. The allocator is
257*4882a593Smuzhiyun  *   defined to return scatterlist chunks of maximum size @max_ents.
258*4882a593Smuzhiyun  *   Thus if @nents is bigger than @max_ents, the scatterlists will be
259*4882a593Smuzhiyun  *   chained in units of @max_ents.
260*4882a593Smuzhiyun  *
261*4882a593Smuzhiyun  * Notes:
262*4882a593Smuzhiyun  *   If this function returns non-0 (eg failure), the caller must call
263*4882a593Smuzhiyun  *   __sg_free_table() to cleanup any leftover allocations.
264*4882a593Smuzhiyun  *
265*4882a593Smuzhiyun  **/
__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)266*4882a593Smuzhiyun int __sg_alloc_table(struct sg_table *table, unsigned int nents,
267*4882a593Smuzhiyun 		     unsigned int max_ents, struct scatterlist *first_chunk,
268*4882a593Smuzhiyun 		     unsigned int nents_first_chunk, gfp_t gfp_mask,
269*4882a593Smuzhiyun 		     sg_alloc_fn *alloc_fn)
270*4882a593Smuzhiyun {
271*4882a593Smuzhiyun 	struct scatterlist *sg, *prv;
272*4882a593Smuzhiyun 	unsigned int left;
273*4882a593Smuzhiyun 	unsigned curr_max_ents = nents_first_chunk ?: max_ents;
274*4882a593Smuzhiyun 	unsigned prv_max_ents;
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun 	memset(table, 0, sizeof(*table));
277*4882a593Smuzhiyun 
278*4882a593Smuzhiyun 	if (nents == 0)
279*4882a593Smuzhiyun 		return -EINVAL;
280*4882a593Smuzhiyun #ifdef CONFIG_ARCH_NO_SG_CHAIN
281*4882a593Smuzhiyun 	if (WARN_ON_ONCE(nents > max_ents))
282*4882a593Smuzhiyun 		return -EINVAL;
283*4882a593Smuzhiyun #endif
284*4882a593Smuzhiyun 
285*4882a593Smuzhiyun 	left = nents;
286*4882a593Smuzhiyun 	prv = NULL;
287*4882a593Smuzhiyun 	do {
288*4882a593Smuzhiyun 		unsigned int sg_size, alloc_size = left;
289*4882a593Smuzhiyun 
290*4882a593Smuzhiyun 		if (alloc_size > curr_max_ents) {
291*4882a593Smuzhiyun 			alloc_size = curr_max_ents;
292*4882a593Smuzhiyun 			sg_size = alloc_size - 1;
293*4882a593Smuzhiyun 		} else
294*4882a593Smuzhiyun 			sg_size = alloc_size;
295*4882a593Smuzhiyun 
296*4882a593Smuzhiyun 		left -= sg_size;
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun 		if (first_chunk) {
299*4882a593Smuzhiyun 			sg = first_chunk;
300*4882a593Smuzhiyun 			first_chunk = NULL;
301*4882a593Smuzhiyun 		} else {
302*4882a593Smuzhiyun 			sg = alloc_fn(alloc_size, gfp_mask);
303*4882a593Smuzhiyun 		}
304*4882a593Smuzhiyun 		if (unlikely(!sg)) {
305*4882a593Smuzhiyun 			/*
306*4882a593Smuzhiyun 			 * Adjust entry count to reflect that the last
307*4882a593Smuzhiyun 			 * entry of the previous table won't be used for
308*4882a593Smuzhiyun 			 * linkage.  Without this, sg_kfree() may get
309*4882a593Smuzhiyun 			 * confused.
310*4882a593Smuzhiyun 			 */
311*4882a593Smuzhiyun 			if (prv)
312*4882a593Smuzhiyun 				table->nents = ++table->orig_nents;
313*4882a593Smuzhiyun 
314*4882a593Smuzhiyun 			return -ENOMEM;
315*4882a593Smuzhiyun 		}
316*4882a593Smuzhiyun 
317*4882a593Smuzhiyun 		sg_init_table(sg, alloc_size);
318*4882a593Smuzhiyun 		table->nents = table->orig_nents += sg_size;
319*4882a593Smuzhiyun 
320*4882a593Smuzhiyun 		/*
321*4882a593Smuzhiyun 		 * If this is the first mapping, assign the sg table header.
322*4882a593Smuzhiyun 		 * If this is not the first mapping, chain previous part.
323*4882a593Smuzhiyun 		 */
324*4882a593Smuzhiyun 		if (prv)
325*4882a593Smuzhiyun 			sg_chain(prv, prv_max_ents, sg);
326*4882a593Smuzhiyun 		else
327*4882a593Smuzhiyun 			table->sgl = sg;
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun 		/*
330*4882a593Smuzhiyun 		 * If no more entries after this one, mark the end
331*4882a593Smuzhiyun 		 */
332*4882a593Smuzhiyun 		if (!left)
333*4882a593Smuzhiyun 			sg_mark_end(&sg[sg_size - 1]);
334*4882a593Smuzhiyun 
335*4882a593Smuzhiyun 		prv = sg;
336*4882a593Smuzhiyun 		prv_max_ents = curr_max_ents;
337*4882a593Smuzhiyun 		curr_max_ents = max_ents;
338*4882a593Smuzhiyun 	} while (left);
339*4882a593Smuzhiyun 
340*4882a593Smuzhiyun 	return 0;
341*4882a593Smuzhiyun }
342*4882a593Smuzhiyun EXPORT_SYMBOL(__sg_alloc_table);
343*4882a593Smuzhiyun 
344*4882a593Smuzhiyun /**
345*4882a593Smuzhiyun  * sg_alloc_table - Allocate and initialize an sg table
346*4882a593Smuzhiyun  * @table:	The sg table header to use
347*4882a593Smuzhiyun  * @nents:	Number of entries in sg list
348*4882a593Smuzhiyun  * @gfp_mask:	GFP allocation mask
349*4882a593Smuzhiyun  *
350*4882a593Smuzhiyun  *  Description:
351*4882a593Smuzhiyun  *    Allocate and initialize an sg table. If @nents@ is larger than
352*4882a593Smuzhiyun  *    SG_MAX_SINGLE_ALLOC a chained sg table will be setup.
353*4882a593Smuzhiyun  *
354*4882a593Smuzhiyun  **/
sg_alloc_table(struct sg_table * table,unsigned int nents,gfp_t gfp_mask)355*4882a593Smuzhiyun int sg_alloc_table(struct sg_table *table, unsigned int nents, gfp_t gfp_mask)
356*4882a593Smuzhiyun {
357*4882a593Smuzhiyun 	int ret;
358*4882a593Smuzhiyun 
359*4882a593Smuzhiyun 	ret = __sg_alloc_table(table, nents, SG_MAX_SINGLE_ALLOC,
360*4882a593Smuzhiyun 			       NULL, 0, gfp_mask, sg_kmalloc);
361*4882a593Smuzhiyun 	if (unlikely(ret))
362*4882a593Smuzhiyun 		__sg_free_table(table, SG_MAX_SINGLE_ALLOC, 0, sg_kfree);
363*4882a593Smuzhiyun 
364*4882a593Smuzhiyun 	return ret;
365*4882a593Smuzhiyun }
366*4882a593Smuzhiyun EXPORT_SYMBOL(sg_alloc_table);
367*4882a593Smuzhiyun 
get_next_sg(struct sg_table * table,struct scatterlist * cur,unsigned long needed_sges,gfp_t gfp_mask)368*4882a593Smuzhiyun static struct scatterlist *get_next_sg(struct sg_table *table,
369*4882a593Smuzhiyun 				       struct scatterlist *cur,
370*4882a593Smuzhiyun 				       unsigned long needed_sges,
371*4882a593Smuzhiyun 				       gfp_t gfp_mask)
372*4882a593Smuzhiyun {
373*4882a593Smuzhiyun 	struct scatterlist *new_sg, *next_sg;
374*4882a593Smuzhiyun 	unsigned int alloc_size;
375*4882a593Smuzhiyun 
376*4882a593Smuzhiyun 	if (cur) {
377*4882a593Smuzhiyun 		next_sg = sg_next(cur);
378*4882a593Smuzhiyun 		/* Check if last entry should be keeped for chainning */
379*4882a593Smuzhiyun 		if (!sg_is_last(next_sg) || needed_sges == 1)
380*4882a593Smuzhiyun 			return next_sg;
381*4882a593Smuzhiyun 	}
382*4882a593Smuzhiyun 
383*4882a593Smuzhiyun 	alloc_size = min_t(unsigned long, needed_sges, SG_MAX_SINGLE_ALLOC);
384*4882a593Smuzhiyun 	new_sg = sg_kmalloc(alloc_size, gfp_mask);
385*4882a593Smuzhiyun 	if (!new_sg)
386*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
387*4882a593Smuzhiyun 	sg_init_table(new_sg, alloc_size);
388*4882a593Smuzhiyun 	if (cur) {
389*4882a593Smuzhiyun 		__sg_chain(next_sg, new_sg);
390*4882a593Smuzhiyun 		table->orig_nents += alloc_size - 1;
391*4882a593Smuzhiyun 	} else {
392*4882a593Smuzhiyun 		table->sgl = new_sg;
393*4882a593Smuzhiyun 		table->orig_nents = alloc_size;
394*4882a593Smuzhiyun 		table->nents = 0;
395*4882a593Smuzhiyun 	}
396*4882a593Smuzhiyun 	return new_sg;
397*4882a593Smuzhiyun }
398*4882a593Smuzhiyun 
399*4882a593Smuzhiyun /**
400*4882a593Smuzhiyun  * __sg_alloc_table_from_pages - Allocate and initialize an sg table from
401*4882a593Smuzhiyun  *			         an array of pages
402*4882a593Smuzhiyun  * @sgt:	 The sg table header to use
403*4882a593Smuzhiyun  * @pages:	 Pointer to an array of page pointers
404*4882a593Smuzhiyun  * @n_pages:	 Number of pages in the pages array
405*4882a593Smuzhiyun  * @offset:      Offset from start of the first page to the start of a buffer
406*4882a593Smuzhiyun  * @size:        Number of valid bytes in the buffer (after offset)
407*4882a593Smuzhiyun  * @max_segment: Maximum size of a scatterlist element in bytes
408*4882a593Smuzhiyun  * @prv:	 Last populated sge in sgt
409*4882a593Smuzhiyun  * @left_pages:  Left pages caller have to set after this call
410*4882a593Smuzhiyun  * @gfp_mask:	 GFP allocation mask
411*4882a593Smuzhiyun  *
412*4882a593Smuzhiyun  * Description:
413*4882a593Smuzhiyun  *    If @prv is NULL, allocate and initialize an sg table from a list of pages,
414*4882a593Smuzhiyun  *    else reuse the scatterlist passed in at @prv.
415*4882a593Smuzhiyun  *    Contiguous ranges of the pages are squashed into a single scatterlist
416*4882a593Smuzhiyun  *    entry up to the maximum size specified in @max_segment.  A user may
417*4882a593Smuzhiyun  *    provide an offset at a start and a size of valid data in a buffer
418*4882a593Smuzhiyun  *    specified by the page array.
419*4882a593Smuzhiyun  *
420*4882a593Smuzhiyun  * Returns:
421*4882a593Smuzhiyun  *   Last SGE in sgt on success, PTR_ERR on otherwise.
422*4882a593Smuzhiyun  *   The allocation in @sgt must be released by sg_free_table.
423*4882a593Smuzhiyun  *
424*4882a593Smuzhiyun  * Notes:
425*4882a593Smuzhiyun  *   If this function returns non-0 (eg failure), the caller must call
426*4882a593Smuzhiyun  *   sg_free_table() to cleanup any leftover allocations.
427*4882a593Smuzhiyun  */
__sg_alloc_table_from_pages(struct sg_table * sgt,struct page ** pages,unsigned int n_pages,unsigned int offset,unsigned long size,unsigned int max_segment,struct scatterlist * prv,unsigned int left_pages,gfp_t gfp_mask)428*4882a593Smuzhiyun struct scatterlist *__sg_alloc_table_from_pages(struct sg_table *sgt,
429*4882a593Smuzhiyun 		struct page **pages, unsigned int n_pages, unsigned int offset,
430*4882a593Smuzhiyun 		unsigned long size, unsigned int max_segment,
431*4882a593Smuzhiyun 		struct scatterlist *prv, unsigned int left_pages,
432*4882a593Smuzhiyun 		gfp_t gfp_mask)
433*4882a593Smuzhiyun {
434*4882a593Smuzhiyun 	unsigned int chunks, cur_page, seg_len, i, prv_len = 0;
435*4882a593Smuzhiyun 	unsigned int added_nents = 0;
436*4882a593Smuzhiyun 	struct scatterlist *s = prv;
437*4882a593Smuzhiyun 
438*4882a593Smuzhiyun 	/*
439*4882a593Smuzhiyun 	 * The algorithm below requires max_segment to be aligned to PAGE_SIZE
440*4882a593Smuzhiyun 	 * otherwise it can overshoot.
441*4882a593Smuzhiyun 	 */
442*4882a593Smuzhiyun 	max_segment = ALIGN_DOWN(max_segment, PAGE_SIZE);
443*4882a593Smuzhiyun 	if (WARN_ON(max_segment < PAGE_SIZE))
444*4882a593Smuzhiyun 		return ERR_PTR(-EINVAL);
445*4882a593Smuzhiyun 
446*4882a593Smuzhiyun 	if (IS_ENABLED(CONFIG_ARCH_NO_SG_CHAIN) && prv)
447*4882a593Smuzhiyun 		return ERR_PTR(-EOPNOTSUPP);
448*4882a593Smuzhiyun 
449*4882a593Smuzhiyun 	if (prv) {
450*4882a593Smuzhiyun 		unsigned long paddr = (page_to_pfn(sg_page(prv)) * PAGE_SIZE +
451*4882a593Smuzhiyun 				       prv->offset + prv->length) /
452*4882a593Smuzhiyun 				      PAGE_SIZE;
453*4882a593Smuzhiyun 
454*4882a593Smuzhiyun 		if (WARN_ON(offset))
455*4882a593Smuzhiyun 			return ERR_PTR(-EINVAL);
456*4882a593Smuzhiyun 
457*4882a593Smuzhiyun 		/* Merge contiguous pages into the last SG */
458*4882a593Smuzhiyun 		prv_len = prv->length;
459*4882a593Smuzhiyun 		while (n_pages && page_to_pfn(pages[0]) == paddr) {
460*4882a593Smuzhiyun 			if (prv->length + PAGE_SIZE > max_segment)
461*4882a593Smuzhiyun 				break;
462*4882a593Smuzhiyun 			prv->length += PAGE_SIZE;
463*4882a593Smuzhiyun 			paddr++;
464*4882a593Smuzhiyun 			pages++;
465*4882a593Smuzhiyun 			n_pages--;
466*4882a593Smuzhiyun 		}
467*4882a593Smuzhiyun 		if (!n_pages)
468*4882a593Smuzhiyun 			goto out;
469*4882a593Smuzhiyun 	}
470*4882a593Smuzhiyun 
471*4882a593Smuzhiyun 	/* compute number of contiguous chunks */
472*4882a593Smuzhiyun 	chunks = 1;
473*4882a593Smuzhiyun 	seg_len = 0;
474*4882a593Smuzhiyun 	for (i = 1; i < n_pages; i++) {
475*4882a593Smuzhiyun 		seg_len += PAGE_SIZE;
476*4882a593Smuzhiyun 		if (seg_len >= max_segment ||
477*4882a593Smuzhiyun 		    page_to_pfn(pages[i]) != page_to_pfn(pages[i - 1]) + 1) {
478*4882a593Smuzhiyun 			chunks++;
479*4882a593Smuzhiyun 			seg_len = 0;
480*4882a593Smuzhiyun 		}
481*4882a593Smuzhiyun 	}
482*4882a593Smuzhiyun 
483*4882a593Smuzhiyun 	/* merging chunks and putting them into the scatterlist */
484*4882a593Smuzhiyun 	cur_page = 0;
485*4882a593Smuzhiyun 	for (i = 0; i < chunks; i++) {
486*4882a593Smuzhiyun 		unsigned int j, chunk_size;
487*4882a593Smuzhiyun 
488*4882a593Smuzhiyun 		/* look for the end of the current chunk */
489*4882a593Smuzhiyun 		seg_len = 0;
490*4882a593Smuzhiyun 		for (j = cur_page + 1; j < n_pages; j++) {
491*4882a593Smuzhiyun 			seg_len += PAGE_SIZE;
492*4882a593Smuzhiyun 			if (seg_len >= max_segment ||
493*4882a593Smuzhiyun 			    page_to_pfn(pages[j]) !=
494*4882a593Smuzhiyun 			    page_to_pfn(pages[j - 1]) + 1)
495*4882a593Smuzhiyun 				break;
496*4882a593Smuzhiyun 		}
497*4882a593Smuzhiyun 
498*4882a593Smuzhiyun 		/* Pass how many chunks might be left */
499*4882a593Smuzhiyun 		s = get_next_sg(sgt, s, chunks - i + left_pages, gfp_mask);
500*4882a593Smuzhiyun 		if (IS_ERR(s)) {
501*4882a593Smuzhiyun 			/*
502*4882a593Smuzhiyun 			 * Adjust entry length to be as before function was
503*4882a593Smuzhiyun 			 * called.
504*4882a593Smuzhiyun 			 */
505*4882a593Smuzhiyun 			if (prv)
506*4882a593Smuzhiyun 				prv->length = prv_len;
507*4882a593Smuzhiyun 			return s;
508*4882a593Smuzhiyun 		}
509*4882a593Smuzhiyun 		chunk_size = ((j - cur_page) << PAGE_SHIFT) - offset;
510*4882a593Smuzhiyun 		sg_set_page(s, pages[cur_page],
511*4882a593Smuzhiyun 			    min_t(unsigned long, size, chunk_size), offset);
512*4882a593Smuzhiyun 		added_nents++;
513*4882a593Smuzhiyun 		size -= chunk_size;
514*4882a593Smuzhiyun 		offset = 0;
515*4882a593Smuzhiyun 		cur_page = j;
516*4882a593Smuzhiyun 	}
517*4882a593Smuzhiyun 	sgt->nents += added_nents;
518*4882a593Smuzhiyun out:
519*4882a593Smuzhiyun 	if (!left_pages)
520*4882a593Smuzhiyun 		sg_mark_end(s);
521*4882a593Smuzhiyun 	return s;
522*4882a593Smuzhiyun }
523*4882a593Smuzhiyun EXPORT_SYMBOL(__sg_alloc_table_from_pages);
524*4882a593Smuzhiyun 
525*4882a593Smuzhiyun /**
526*4882a593Smuzhiyun  * sg_alloc_table_from_pages - Allocate and initialize an sg table from
527*4882a593Smuzhiyun  *			       an array of pages
528*4882a593Smuzhiyun  * @sgt:	 The sg table header to use
529*4882a593Smuzhiyun  * @pages:	 Pointer to an array of page pointers
530*4882a593Smuzhiyun  * @n_pages:	 Number of pages in the pages array
531*4882a593Smuzhiyun  * @offset:      Offset from start of the first page to the start of a buffer
532*4882a593Smuzhiyun  * @size:        Number of valid bytes in the buffer (after offset)
533*4882a593Smuzhiyun  * @gfp_mask:	 GFP allocation mask
534*4882a593Smuzhiyun  *
535*4882a593Smuzhiyun  *  Description:
536*4882a593Smuzhiyun  *    Allocate and initialize an sg table from a list of pages. Contiguous
537*4882a593Smuzhiyun  *    ranges of the pages are squashed into a single scatterlist node. A user
538*4882a593Smuzhiyun  *    may provide an offset at a start and a size of valid data in a buffer
539*4882a593Smuzhiyun  *    specified by the page array. The returned sg table is released by
540*4882a593Smuzhiyun  *    sg_free_table.
541*4882a593Smuzhiyun  *
542*4882a593Smuzhiyun  * Returns:
543*4882a593Smuzhiyun  *   0 on success, negative error on failure
544*4882a593Smuzhiyun  */
sg_alloc_table_from_pages(struct sg_table * sgt,struct page ** pages,unsigned int n_pages,unsigned int offset,unsigned long size,gfp_t gfp_mask)545*4882a593Smuzhiyun int sg_alloc_table_from_pages(struct sg_table *sgt, struct page **pages,
546*4882a593Smuzhiyun 			      unsigned int n_pages, unsigned int offset,
547*4882a593Smuzhiyun 			      unsigned long size, gfp_t gfp_mask)
548*4882a593Smuzhiyun {
549*4882a593Smuzhiyun 	return PTR_ERR_OR_ZERO(__sg_alloc_table_from_pages(sgt, pages, n_pages,
550*4882a593Smuzhiyun 			offset, size, UINT_MAX, NULL, 0, gfp_mask));
551*4882a593Smuzhiyun }
552*4882a593Smuzhiyun EXPORT_SYMBOL(sg_alloc_table_from_pages);
553*4882a593Smuzhiyun 
554*4882a593Smuzhiyun #ifdef CONFIG_SGL_ALLOC
555*4882a593Smuzhiyun 
556*4882a593Smuzhiyun /**
557*4882a593Smuzhiyun  * sgl_alloc_order - allocate a scatterlist and its pages
558*4882a593Smuzhiyun  * @length: Length in bytes of the scatterlist. Must be at least one
559*4882a593Smuzhiyun  * @order: Second argument for alloc_pages()
560*4882a593Smuzhiyun  * @chainable: Whether or not to allocate an extra element in the scatterlist
561*4882a593Smuzhiyun  *	for scatterlist chaining purposes
562*4882a593Smuzhiyun  * @gfp: Memory allocation flags
563*4882a593Smuzhiyun  * @nent_p: [out] Number of entries in the scatterlist that have pages
564*4882a593Smuzhiyun  *
565*4882a593Smuzhiyun  * Returns: A pointer to an initialized scatterlist or %NULL upon failure.
566*4882a593Smuzhiyun  */
sgl_alloc_order(unsigned long long length,unsigned int order,bool chainable,gfp_t gfp,unsigned int * nent_p)567*4882a593Smuzhiyun struct scatterlist *sgl_alloc_order(unsigned long long length,
568*4882a593Smuzhiyun 				    unsigned int order, bool chainable,
569*4882a593Smuzhiyun 				    gfp_t gfp, unsigned int *nent_p)
570*4882a593Smuzhiyun {
571*4882a593Smuzhiyun 	struct scatterlist *sgl, *sg;
572*4882a593Smuzhiyun 	struct page *page;
573*4882a593Smuzhiyun 	unsigned int nent, nalloc;
574*4882a593Smuzhiyun 	u32 elem_len;
575*4882a593Smuzhiyun 
576*4882a593Smuzhiyun 	nent = round_up(length, PAGE_SIZE << order) >> (PAGE_SHIFT + order);
577*4882a593Smuzhiyun 	/* Check for integer overflow */
578*4882a593Smuzhiyun 	if (length > (nent << (PAGE_SHIFT + order)))
579*4882a593Smuzhiyun 		return NULL;
580*4882a593Smuzhiyun 	nalloc = nent;
581*4882a593Smuzhiyun 	if (chainable) {
582*4882a593Smuzhiyun 		/* Check for integer overflow */
583*4882a593Smuzhiyun 		if (nalloc + 1 < nalloc)
584*4882a593Smuzhiyun 			return NULL;
585*4882a593Smuzhiyun 		nalloc++;
586*4882a593Smuzhiyun 	}
587*4882a593Smuzhiyun 	sgl = kmalloc_array(nalloc, sizeof(struct scatterlist),
588*4882a593Smuzhiyun 			    gfp & ~GFP_DMA);
589*4882a593Smuzhiyun 	if (!sgl)
590*4882a593Smuzhiyun 		return NULL;
591*4882a593Smuzhiyun 
592*4882a593Smuzhiyun 	sg_init_table(sgl, nalloc);
593*4882a593Smuzhiyun 	sg = sgl;
594*4882a593Smuzhiyun 	while (length) {
595*4882a593Smuzhiyun 		elem_len = min_t(u64, length, PAGE_SIZE << order);
596*4882a593Smuzhiyun 		page = alloc_pages(gfp, order);
597*4882a593Smuzhiyun 		if (!page) {
598*4882a593Smuzhiyun 			sgl_free_order(sgl, order);
599*4882a593Smuzhiyun 			return NULL;
600*4882a593Smuzhiyun 		}
601*4882a593Smuzhiyun 
602*4882a593Smuzhiyun 		sg_set_page(sg, page, elem_len, 0);
603*4882a593Smuzhiyun 		length -= elem_len;
604*4882a593Smuzhiyun 		sg = sg_next(sg);
605*4882a593Smuzhiyun 	}
606*4882a593Smuzhiyun 	WARN_ONCE(length, "length = %lld\n", length);
607*4882a593Smuzhiyun 	if (nent_p)
608*4882a593Smuzhiyun 		*nent_p = nent;
609*4882a593Smuzhiyun 	return sgl;
610*4882a593Smuzhiyun }
611*4882a593Smuzhiyun EXPORT_SYMBOL(sgl_alloc_order);
612*4882a593Smuzhiyun 
613*4882a593Smuzhiyun /**
614*4882a593Smuzhiyun  * sgl_alloc - allocate a scatterlist and its pages
615*4882a593Smuzhiyun  * @length: Length in bytes of the scatterlist
616*4882a593Smuzhiyun  * @gfp: Memory allocation flags
617*4882a593Smuzhiyun  * @nent_p: [out] Number of entries in the scatterlist
618*4882a593Smuzhiyun  *
619*4882a593Smuzhiyun  * Returns: A pointer to an initialized scatterlist or %NULL upon failure.
620*4882a593Smuzhiyun  */
sgl_alloc(unsigned long long length,gfp_t gfp,unsigned int * nent_p)621*4882a593Smuzhiyun struct scatterlist *sgl_alloc(unsigned long long length, gfp_t gfp,
622*4882a593Smuzhiyun 			      unsigned int *nent_p)
623*4882a593Smuzhiyun {
624*4882a593Smuzhiyun 	return sgl_alloc_order(length, 0, false, gfp, nent_p);
625*4882a593Smuzhiyun }
626*4882a593Smuzhiyun EXPORT_SYMBOL(sgl_alloc);
627*4882a593Smuzhiyun 
628*4882a593Smuzhiyun /**
629*4882a593Smuzhiyun  * sgl_free_n_order - free a scatterlist and its pages
630*4882a593Smuzhiyun  * @sgl: Scatterlist with one or more elements
631*4882a593Smuzhiyun  * @nents: Maximum number of elements to free
632*4882a593Smuzhiyun  * @order: Second argument for __free_pages()
633*4882a593Smuzhiyun  *
634*4882a593Smuzhiyun  * Notes:
635*4882a593Smuzhiyun  * - If several scatterlists have been chained and each chain element is
636*4882a593Smuzhiyun  *   freed separately then it's essential to set nents correctly to avoid that a
637*4882a593Smuzhiyun  *   page would get freed twice.
638*4882a593Smuzhiyun  * - All pages in a chained scatterlist can be freed at once by setting @nents
639*4882a593Smuzhiyun  *   to a high number.
640*4882a593Smuzhiyun  */
sgl_free_n_order(struct scatterlist * sgl,int nents,int order)641*4882a593Smuzhiyun void sgl_free_n_order(struct scatterlist *sgl, int nents, int order)
642*4882a593Smuzhiyun {
643*4882a593Smuzhiyun 	struct scatterlist *sg;
644*4882a593Smuzhiyun 	struct page *page;
645*4882a593Smuzhiyun 	int i;
646*4882a593Smuzhiyun 
647*4882a593Smuzhiyun 	for_each_sg(sgl, sg, nents, i) {
648*4882a593Smuzhiyun 		if (!sg)
649*4882a593Smuzhiyun 			break;
650*4882a593Smuzhiyun 		page = sg_page(sg);
651*4882a593Smuzhiyun 		if (page)
652*4882a593Smuzhiyun 			__free_pages(page, order);
653*4882a593Smuzhiyun 	}
654*4882a593Smuzhiyun 	kfree(sgl);
655*4882a593Smuzhiyun }
656*4882a593Smuzhiyun EXPORT_SYMBOL(sgl_free_n_order);
657*4882a593Smuzhiyun 
658*4882a593Smuzhiyun /**
659*4882a593Smuzhiyun  * sgl_free_order - free a scatterlist and its pages
660*4882a593Smuzhiyun  * @sgl: Scatterlist with one or more elements
661*4882a593Smuzhiyun  * @order: Second argument for __free_pages()
662*4882a593Smuzhiyun  */
sgl_free_order(struct scatterlist * sgl,int order)663*4882a593Smuzhiyun void sgl_free_order(struct scatterlist *sgl, int order)
664*4882a593Smuzhiyun {
665*4882a593Smuzhiyun 	sgl_free_n_order(sgl, INT_MAX, order);
666*4882a593Smuzhiyun }
667*4882a593Smuzhiyun EXPORT_SYMBOL(sgl_free_order);
668*4882a593Smuzhiyun 
669*4882a593Smuzhiyun /**
670*4882a593Smuzhiyun  * sgl_free - free a scatterlist and its pages
671*4882a593Smuzhiyun  * @sgl: Scatterlist with one or more elements
672*4882a593Smuzhiyun  */
sgl_free(struct scatterlist * sgl)673*4882a593Smuzhiyun void sgl_free(struct scatterlist *sgl)
674*4882a593Smuzhiyun {
675*4882a593Smuzhiyun 	sgl_free_order(sgl, 0);
676*4882a593Smuzhiyun }
677*4882a593Smuzhiyun EXPORT_SYMBOL(sgl_free);
678*4882a593Smuzhiyun 
679*4882a593Smuzhiyun #endif /* CONFIG_SGL_ALLOC */
680*4882a593Smuzhiyun 
__sg_page_iter_start(struct sg_page_iter * piter,struct scatterlist * sglist,unsigned int nents,unsigned long pgoffset)681*4882a593Smuzhiyun void __sg_page_iter_start(struct sg_page_iter *piter,
682*4882a593Smuzhiyun 			  struct scatterlist *sglist, unsigned int nents,
683*4882a593Smuzhiyun 			  unsigned long pgoffset)
684*4882a593Smuzhiyun {
685*4882a593Smuzhiyun 	piter->__pg_advance = 0;
686*4882a593Smuzhiyun 	piter->__nents = nents;
687*4882a593Smuzhiyun 
688*4882a593Smuzhiyun 	piter->sg = sglist;
689*4882a593Smuzhiyun 	piter->sg_pgoffset = pgoffset;
690*4882a593Smuzhiyun }
691*4882a593Smuzhiyun EXPORT_SYMBOL(__sg_page_iter_start);
692*4882a593Smuzhiyun 
sg_page_count(struct scatterlist * sg)693*4882a593Smuzhiyun static int sg_page_count(struct scatterlist *sg)
694*4882a593Smuzhiyun {
695*4882a593Smuzhiyun 	return PAGE_ALIGN(sg->offset + sg->length) >> PAGE_SHIFT;
696*4882a593Smuzhiyun }
697*4882a593Smuzhiyun 
__sg_page_iter_next(struct sg_page_iter * piter)698*4882a593Smuzhiyun bool __sg_page_iter_next(struct sg_page_iter *piter)
699*4882a593Smuzhiyun {
700*4882a593Smuzhiyun 	if (!piter->__nents || !piter->sg)
701*4882a593Smuzhiyun 		return false;
702*4882a593Smuzhiyun 
703*4882a593Smuzhiyun 	piter->sg_pgoffset += piter->__pg_advance;
704*4882a593Smuzhiyun 	piter->__pg_advance = 1;
705*4882a593Smuzhiyun 
706*4882a593Smuzhiyun 	while (piter->sg_pgoffset >= sg_page_count(piter->sg)) {
707*4882a593Smuzhiyun 		piter->sg_pgoffset -= sg_page_count(piter->sg);
708*4882a593Smuzhiyun 		piter->sg = sg_next(piter->sg);
709*4882a593Smuzhiyun 		if (!--piter->__nents || !piter->sg)
710*4882a593Smuzhiyun 			return false;
711*4882a593Smuzhiyun 	}
712*4882a593Smuzhiyun 
713*4882a593Smuzhiyun 	return true;
714*4882a593Smuzhiyun }
715*4882a593Smuzhiyun EXPORT_SYMBOL(__sg_page_iter_next);
716*4882a593Smuzhiyun 
sg_dma_page_count(struct scatterlist * sg)717*4882a593Smuzhiyun static int sg_dma_page_count(struct scatterlist *sg)
718*4882a593Smuzhiyun {
719*4882a593Smuzhiyun 	return PAGE_ALIGN(sg->offset + sg_dma_len(sg)) >> PAGE_SHIFT;
720*4882a593Smuzhiyun }
721*4882a593Smuzhiyun 
__sg_page_iter_dma_next(struct sg_dma_page_iter * dma_iter)722*4882a593Smuzhiyun bool __sg_page_iter_dma_next(struct sg_dma_page_iter *dma_iter)
723*4882a593Smuzhiyun {
724*4882a593Smuzhiyun 	struct sg_page_iter *piter = &dma_iter->base;
725*4882a593Smuzhiyun 
726*4882a593Smuzhiyun 	if (!piter->__nents || !piter->sg)
727*4882a593Smuzhiyun 		return false;
728*4882a593Smuzhiyun 
729*4882a593Smuzhiyun 	piter->sg_pgoffset += piter->__pg_advance;
730*4882a593Smuzhiyun 	piter->__pg_advance = 1;
731*4882a593Smuzhiyun 
732*4882a593Smuzhiyun 	while (piter->sg_pgoffset >= sg_dma_page_count(piter->sg)) {
733*4882a593Smuzhiyun 		piter->sg_pgoffset -= sg_dma_page_count(piter->sg);
734*4882a593Smuzhiyun 		piter->sg = sg_next(piter->sg);
735*4882a593Smuzhiyun 		if (!--piter->__nents || !piter->sg)
736*4882a593Smuzhiyun 			return false;
737*4882a593Smuzhiyun 	}
738*4882a593Smuzhiyun 
739*4882a593Smuzhiyun 	return true;
740*4882a593Smuzhiyun }
741*4882a593Smuzhiyun EXPORT_SYMBOL(__sg_page_iter_dma_next);
742*4882a593Smuzhiyun 
743*4882a593Smuzhiyun /**
744*4882a593Smuzhiyun  * sg_miter_start - start mapping iteration over a sg list
745*4882a593Smuzhiyun  * @miter: sg mapping iter to be started
746*4882a593Smuzhiyun  * @sgl: sg list to iterate over
747*4882a593Smuzhiyun  * @nents: number of sg entries
748*4882a593Smuzhiyun  *
749*4882a593Smuzhiyun  * Description:
750*4882a593Smuzhiyun  *   Starts mapping iterator @miter.
751*4882a593Smuzhiyun  *
752*4882a593Smuzhiyun  * Context:
753*4882a593Smuzhiyun  *   Don't care.
754*4882a593Smuzhiyun  */
sg_miter_start(struct sg_mapping_iter * miter,struct scatterlist * sgl,unsigned int nents,unsigned int flags)755*4882a593Smuzhiyun void sg_miter_start(struct sg_mapping_iter *miter, struct scatterlist *sgl,
756*4882a593Smuzhiyun 		    unsigned int nents, unsigned int flags)
757*4882a593Smuzhiyun {
758*4882a593Smuzhiyun 	memset(miter, 0, sizeof(struct sg_mapping_iter));
759*4882a593Smuzhiyun 
760*4882a593Smuzhiyun 	__sg_page_iter_start(&miter->piter, sgl, nents, 0);
761*4882a593Smuzhiyun 	WARN_ON(!(flags & (SG_MITER_TO_SG | SG_MITER_FROM_SG)));
762*4882a593Smuzhiyun 	miter->__flags = flags;
763*4882a593Smuzhiyun }
764*4882a593Smuzhiyun EXPORT_SYMBOL(sg_miter_start);
765*4882a593Smuzhiyun 
sg_miter_get_next_page(struct sg_mapping_iter * miter)766*4882a593Smuzhiyun static bool sg_miter_get_next_page(struct sg_mapping_iter *miter)
767*4882a593Smuzhiyun {
768*4882a593Smuzhiyun 	if (!miter->__remaining) {
769*4882a593Smuzhiyun 		struct scatterlist *sg;
770*4882a593Smuzhiyun 
771*4882a593Smuzhiyun 		if (!__sg_page_iter_next(&miter->piter))
772*4882a593Smuzhiyun 			return false;
773*4882a593Smuzhiyun 
774*4882a593Smuzhiyun 		sg = miter->piter.sg;
775*4882a593Smuzhiyun 
776*4882a593Smuzhiyun 		miter->__offset = miter->piter.sg_pgoffset ? 0 : sg->offset;
777*4882a593Smuzhiyun 		miter->piter.sg_pgoffset += miter->__offset >> PAGE_SHIFT;
778*4882a593Smuzhiyun 		miter->__offset &= PAGE_SIZE - 1;
779*4882a593Smuzhiyun 		miter->__remaining = sg->offset + sg->length -
780*4882a593Smuzhiyun 				     (miter->piter.sg_pgoffset << PAGE_SHIFT) -
781*4882a593Smuzhiyun 				     miter->__offset;
782*4882a593Smuzhiyun 		miter->__remaining = min_t(unsigned long, miter->__remaining,
783*4882a593Smuzhiyun 					   PAGE_SIZE - miter->__offset);
784*4882a593Smuzhiyun 	}
785*4882a593Smuzhiyun 
786*4882a593Smuzhiyun 	return true;
787*4882a593Smuzhiyun }
788*4882a593Smuzhiyun 
789*4882a593Smuzhiyun /**
790*4882a593Smuzhiyun  * sg_miter_skip - reposition mapping iterator
791*4882a593Smuzhiyun  * @miter: sg mapping iter to be skipped
792*4882a593Smuzhiyun  * @offset: number of bytes to plus the current location
793*4882a593Smuzhiyun  *
794*4882a593Smuzhiyun  * Description:
795*4882a593Smuzhiyun  *   Sets the offset of @miter to its current location plus @offset bytes.
796*4882a593Smuzhiyun  *   If mapping iterator @miter has been proceeded by sg_miter_next(), this
797*4882a593Smuzhiyun  *   stops @miter.
798*4882a593Smuzhiyun  *
799*4882a593Smuzhiyun  * Context:
800*4882a593Smuzhiyun  *   Don't care if @miter is stopped, or not proceeded yet.
801*4882a593Smuzhiyun  *   Otherwise, preemption disabled if the SG_MITER_ATOMIC is set.
802*4882a593Smuzhiyun  *
803*4882a593Smuzhiyun  * Returns:
804*4882a593Smuzhiyun  *   true if @miter contains the valid mapping.  false if end of sg
805*4882a593Smuzhiyun  *   list is reached.
806*4882a593Smuzhiyun  */
sg_miter_skip(struct sg_mapping_iter * miter,off_t offset)807*4882a593Smuzhiyun bool sg_miter_skip(struct sg_mapping_iter *miter, off_t offset)
808*4882a593Smuzhiyun {
809*4882a593Smuzhiyun 	sg_miter_stop(miter);
810*4882a593Smuzhiyun 
811*4882a593Smuzhiyun 	while (offset) {
812*4882a593Smuzhiyun 		off_t consumed;
813*4882a593Smuzhiyun 
814*4882a593Smuzhiyun 		if (!sg_miter_get_next_page(miter))
815*4882a593Smuzhiyun 			return false;
816*4882a593Smuzhiyun 
817*4882a593Smuzhiyun 		consumed = min_t(off_t, offset, miter->__remaining);
818*4882a593Smuzhiyun 		miter->__offset += consumed;
819*4882a593Smuzhiyun 		miter->__remaining -= consumed;
820*4882a593Smuzhiyun 		offset -= consumed;
821*4882a593Smuzhiyun 	}
822*4882a593Smuzhiyun 
823*4882a593Smuzhiyun 	return true;
824*4882a593Smuzhiyun }
825*4882a593Smuzhiyun EXPORT_SYMBOL(sg_miter_skip);
826*4882a593Smuzhiyun 
827*4882a593Smuzhiyun /**
828*4882a593Smuzhiyun  * sg_miter_next - proceed mapping iterator to the next mapping
829*4882a593Smuzhiyun  * @miter: sg mapping iter to proceed
830*4882a593Smuzhiyun  *
831*4882a593Smuzhiyun  * Description:
832*4882a593Smuzhiyun  *   Proceeds @miter to the next mapping.  @miter should have been started
833*4882a593Smuzhiyun  *   using sg_miter_start().  On successful return, @miter->page,
834*4882a593Smuzhiyun  *   @miter->addr and @miter->length point to the current mapping.
835*4882a593Smuzhiyun  *
836*4882a593Smuzhiyun  * Context:
837*4882a593Smuzhiyun  *   Preemption disabled if SG_MITER_ATOMIC.  Preemption must stay disabled
838*4882a593Smuzhiyun  *   till @miter is stopped.  May sleep if !SG_MITER_ATOMIC.
839*4882a593Smuzhiyun  *
840*4882a593Smuzhiyun  * Returns:
841*4882a593Smuzhiyun  *   true if @miter contains the next mapping.  false if end of sg
842*4882a593Smuzhiyun  *   list is reached.
843*4882a593Smuzhiyun  */
sg_miter_next(struct sg_mapping_iter * miter)844*4882a593Smuzhiyun bool sg_miter_next(struct sg_mapping_iter *miter)
845*4882a593Smuzhiyun {
846*4882a593Smuzhiyun 	sg_miter_stop(miter);
847*4882a593Smuzhiyun 
848*4882a593Smuzhiyun 	/*
849*4882a593Smuzhiyun 	 * Get to the next page if necessary.
850*4882a593Smuzhiyun 	 * __remaining, __offset is adjusted by sg_miter_stop
851*4882a593Smuzhiyun 	 */
852*4882a593Smuzhiyun 	if (!sg_miter_get_next_page(miter))
853*4882a593Smuzhiyun 		return false;
854*4882a593Smuzhiyun 
855*4882a593Smuzhiyun 	miter->page = sg_page_iter_page(&miter->piter);
856*4882a593Smuzhiyun 	miter->consumed = miter->length = miter->__remaining;
857*4882a593Smuzhiyun 
858*4882a593Smuzhiyun 	if (miter->__flags & SG_MITER_ATOMIC)
859*4882a593Smuzhiyun 		miter->addr = kmap_atomic(miter->page) + miter->__offset;
860*4882a593Smuzhiyun 	else
861*4882a593Smuzhiyun 		miter->addr = kmap(miter->page) + miter->__offset;
862*4882a593Smuzhiyun 
863*4882a593Smuzhiyun 	return true;
864*4882a593Smuzhiyun }
865*4882a593Smuzhiyun EXPORT_SYMBOL(sg_miter_next);
866*4882a593Smuzhiyun 
867*4882a593Smuzhiyun /**
868*4882a593Smuzhiyun  * sg_miter_stop - stop mapping iteration
869*4882a593Smuzhiyun  * @miter: sg mapping iter to be stopped
870*4882a593Smuzhiyun  *
871*4882a593Smuzhiyun  * Description:
872*4882a593Smuzhiyun  *   Stops mapping iterator @miter.  @miter should have been started
873*4882a593Smuzhiyun  *   using sg_miter_start().  A stopped iteration can be resumed by
874*4882a593Smuzhiyun  *   calling sg_miter_next() on it.  This is useful when resources (kmap)
875*4882a593Smuzhiyun  *   need to be released during iteration.
876*4882a593Smuzhiyun  *
877*4882a593Smuzhiyun  * Context:
878*4882a593Smuzhiyun  *   Preemption disabled if the SG_MITER_ATOMIC is set.  Don't care
879*4882a593Smuzhiyun  *   otherwise.
880*4882a593Smuzhiyun  */
sg_miter_stop(struct sg_mapping_iter * miter)881*4882a593Smuzhiyun void sg_miter_stop(struct sg_mapping_iter *miter)
882*4882a593Smuzhiyun {
883*4882a593Smuzhiyun 	WARN_ON(miter->consumed > miter->length);
884*4882a593Smuzhiyun 
885*4882a593Smuzhiyun 	/* drop resources from the last iteration */
886*4882a593Smuzhiyun 	if (miter->addr) {
887*4882a593Smuzhiyun 		miter->__offset += miter->consumed;
888*4882a593Smuzhiyun 		miter->__remaining -= miter->consumed;
889*4882a593Smuzhiyun 
890*4882a593Smuzhiyun 		if ((miter->__flags & SG_MITER_TO_SG) &&
891*4882a593Smuzhiyun 		    !PageSlab(miter->page))
892*4882a593Smuzhiyun 			flush_kernel_dcache_page(miter->page);
893*4882a593Smuzhiyun 
894*4882a593Smuzhiyun 		if (miter->__flags & SG_MITER_ATOMIC) {
895*4882a593Smuzhiyun 			WARN_ON_ONCE(preemptible());
896*4882a593Smuzhiyun 			kunmap_atomic(miter->addr);
897*4882a593Smuzhiyun 		} else
898*4882a593Smuzhiyun 			kunmap(miter->page);
899*4882a593Smuzhiyun 
900*4882a593Smuzhiyun 		miter->page = NULL;
901*4882a593Smuzhiyun 		miter->addr = NULL;
902*4882a593Smuzhiyun 		miter->length = 0;
903*4882a593Smuzhiyun 		miter->consumed = 0;
904*4882a593Smuzhiyun 	}
905*4882a593Smuzhiyun }
906*4882a593Smuzhiyun EXPORT_SYMBOL(sg_miter_stop);
907*4882a593Smuzhiyun 
908*4882a593Smuzhiyun /**
909*4882a593Smuzhiyun  * sg_copy_buffer - Copy data between a linear buffer and an SG list
910*4882a593Smuzhiyun  * @sgl:		 The SG list
911*4882a593Smuzhiyun  * @nents:		 Number of SG entries
912*4882a593Smuzhiyun  * @buf:		 Where to copy from
913*4882a593Smuzhiyun  * @buflen:		 The number of bytes to copy
914*4882a593Smuzhiyun  * @skip:		 Number of bytes to skip before copying
915*4882a593Smuzhiyun  * @to_buffer:		 transfer direction (true == from an sg list to a
916*4882a593Smuzhiyun  *			 buffer, false == from a buffer to an sg list)
917*4882a593Smuzhiyun  *
918*4882a593Smuzhiyun  * Returns the number of copied bytes.
919*4882a593Smuzhiyun  *
920*4882a593Smuzhiyun  **/
sg_copy_buffer(struct scatterlist * sgl,unsigned int nents,void * buf,size_t buflen,off_t skip,bool to_buffer)921*4882a593Smuzhiyun size_t sg_copy_buffer(struct scatterlist *sgl, unsigned int nents, void *buf,
922*4882a593Smuzhiyun 		      size_t buflen, off_t skip, bool to_buffer)
923*4882a593Smuzhiyun {
924*4882a593Smuzhiyun 	unsigned int offset = 0;
925*4882a593Smuzhiyun 	struct sg_mapping_iter miter;
926*4882a593Smuzhiyun 	unsigned int sg_flags = SG_MITER_ATOMIC;
927*4882a593Smuzhiyun 
928*4882a593Smuzhiyun 	if (to_buffer)
929*4882a593Smuzhiyun 		sg_flags |= SG_MITER_FROM_SG;
930*4882a593Smuzhiyun 	else
931*4882a593Smuzhiyun 		sg_flags |= SG_MITER_TO_SG;
932*4882a593Smuzhiyun 
933*4882a593Smuzhiyun 	sg_miter_start(&miter, sgl, nents, sg_flags);
934*4882a593Smuzhiyun 
935*4882a593Smuzhiyun 	if (!sg_miter_skip(&miter, skip))
936*4882a593Smuzhiyun 		return 0;
937*4882a593Smuzhiyun 
938*4882a593Smuzhiyun 	while ((offset < buflen) && sg_miter_next(&miter)) {
939*4882a593Smuzhiyun 		unsigned int len;
940*4882a593Smuzhiyun 
941*4882a593Smuzhiyun 		len = min(miter.length, buflen - offset);
942*4882a593Smuzhiyun 
943*4882a593Smuzhiyun 		if (to_buffer)
944*4882a593Smuzhiyun 			memcpy(buf + offset, miter.addr, len);
945*4882a593Smuzhiyun 		else
946*4882a593Smuzhiyun 			memcpy(miter.addr, buf + offset, len);
947*4882a593Smuzhiyun 
948*4882a593Smuzhiyun 		offset += len;
949*4882a593Smuzhiyun 	}
950*4882a593Smuzhiyun 
951*4882a593Smuzhiyun 	sg_miter_stop(&miter);
952*4882a593Smuzhiyun 
953*4882a593Smuzhiyun 	return offset;
954*4882a593Smuzhiyun }
955*4882a593Smuzhiyun EXPORT_SYMBOL(sg_copy_buffer);
956*4882a593Smuzhiyun 
957*4882a593Smuzhiyun /**
958*4882a593Smuzhiyun  * sg_copy_from_buffer - Copy from a linear buffer to an SG list
959*4882a593Smuzhiyun  * @sgl:		 The SG list
960*4882a593Smuzhiyun  * @nents:		 Number of SG entries
961*4882a593Smuzhiyun  * @buf:		 Where to copy from
962*4882a593Smuzhiyun  * @buflen:		 The number of bytes to copy
963*4882a593Smuzhiyun  *
964*4882a593Smuzhiyun  * Returns the number of copied bytes.
965*4882a593Smuzhiyun  *
966*4882a593Smuzhiyun  **/
sg_copy_from_buffer(struct scatterlist * sgl,unsigned int nents,const void * buf,size_t buflen)967*4882a593Smuzhiyun size_t sg_copy_from_buffer(struct scatterlist *sgl, unsigned int nents,
968*4882a593Smuzhiyun 			   const void *buf, size_t buflen)
969*4882a593Smuzhiyun {
970*4882a593Smuzhiyun 	return sg_copy_buffer(sgl, nents, (void *)buf, buflen, 0, false);
971*4882a593Smuzhiyun }
972*4882a593Smuzhiyun EXPORT_SYMBOL(sg_copy_from_buffer);
973*4882a593Smuzhiyun 
974*4882a593Smuzhiyun /**
975*4882a593Smuzhiyun  * sg_copy_to_buffer - Copy from an SG list to a linear buffer
976*4882a593Smuzhiyun  * @sgl:		 The SG list
977*4882a593Smuzhiyun  * @nents:		 Number of SG entries
978*4882a593Smuzhiyun  * @buf:		 Where to copy to
979*4882a593Smuzhiyun  * @buflen:		 The number of bytes to copy
980*4882a593Smuzhiyun  *
981*4882a593Smuzhiyun  * Returns the number of copied bytes.
982*4882a593Smuzhiyun  *
983*4882a593Smuzhiyun  **/
sg_copy_to_buffer(struct scatterlist * sgl,unsigned int nents,void * buf,size_t buflen)984*4882a593Smuzhiyun size_t sg_copy_to_buffer(struct scatterlist *sgl, unsigned int nents,
985*4882a593Smuzhiyun 			 void *buf, size_t buflen)
986*4882a593Smuzhiyun {
987*4882a593Smuzhiyun 	return sg_copy_buffer(sgl, nents, buf, buflen, 0, true);
988*4882a593Smuzhiyun }
989*4882a593Smuzhiyun EXPORT_SYMBOL(sg_copy_to_buffer);
990*4882a593Smuzhiyun 
991*4882a593Smuzhiyun /**
992*4882a593Smuzhiyun  * sg_pcopy_from_buffer - Copy from a linear buffer to an SG list
993*4882a593Smuzhiyun  * @sgl:		 The SG list
994*4882a593Smuzhiyun  * @nents:		 Number of SG entries
995*4882a593Smuzhiyun  * @buf:		 Where to copy from
996*4882a593Smuzhiyun  * @buflen:		 The number of bytes to copy
997*4882a593Smuzhiyun  * @skip:		 Number of bytes to skip before copying
998*4882a593Smuzhiyun  *
999*4882a593Smuzhiyun  * Returns the number of copied bytes.
1000*4882a593Smuzhiyun  *
1001*4882a593Smuzhiyun  **/
sg_pcopy_from_buffer(struct scatterlist * sgl,unsigned int nents,const void * buf,size_t buflen,off_t skip)1002*4882a593Smuzhiyun size_t sg_pcopy_from_buffer(struct scatterlist *sgl, unsigned int nents,
1003*4882a593Smuzhiyun 			    const void *buf, size_t buflen, off_t skip)
1004*4882a593Smuzhiyun {
1005*4882a593Smuzhiyun 	return sg_copy_buffer(sgl, nents, (void *)buf, buflen, skip, false);
1006*4882a593Smuzhiyun }
1007*4882a593Smuzhiyun EXPORT_SYMBOL(sg_pcopy_from_buffer);
1008*4882a593Smuzhiyun 
1009*4882a593Smuzhiyun /**
1010*4882a593Smuzhiyun  * sg_pcopy_to_buffer - Copy from an SG list to a linear buffer
1011*4882a593Smuzhiyun  * @sgl:		 The SG list
1012*4882a593Smuzhiyun  * @nents:		 Number of SG entries
1013*4882a593Smuzhiyun  * @buf:		 Where to copy to
1014*4882a593Smuzhiyun  * @buflen:		 The number of bytes to copy
1015*4882a593Smuzhiyun  * @skip:		 Number of bytes to skip before copying
1016*4882a593Smuzhiyun  *
1017*4882a593Smuzhiyun  * Returns the number of copied bytes.
1018*4882a593Smuzhiyun  *
1019*4882a593Smuzhiyun  **/
sg_pcopy_to_buffer(struct scatterlist * sgl,unsigned int nents,void * buf,size_t buflen,off_t skip)1020*4882a593Smuzhiyun size_t sg_pcopy_to_buffer(struct scatterlist *sgl, unsigned int nents,
1021*4882a593Smuzhiyun 			  void *buf, size_t buflen, off_t skip)
1022*4882a593Smuzhiyun {
1023*4882a593Smuzhiyun 	return sg_copy_buffer(sgl, nents, buf, buflen, skip, true);
1024*4882a593Smuzhiyun }
1025*4882a593Smuzhiyun EXPORT_SYMBOL(sg_pcopy_to_buffer);
1026*4882a593Smuzhiyun 
1027*4882a593Smuzhiyun /**
1028*4882a593Smuzhiyun  * sg_zero_buffer - Zero-out a part of a SG list
1029*4882a593Smuzhiyun  * @sgl:		 The SG list
1030*4882a593Smuzhiyun  * @nents:		 Number of SG entries
1031*4882a593Smuzhiyun  * @buflen:		 The number of bytes to zero out
1032*4882a593Smuzhiyun  * @skip:		 Number of bytes to skip before zeroing
1033*4882a593Smuzhiyun  *
1034*4882a593Smuzhiyun  * Returns the number of bytes zeroed.
1035*4882a593Smuzhiyun  **/
sg_zero_buffer(struct scatterlist * sgl,unsigned int nents,size_t buflen,off_t skip)1036*4882a593Smuzhiyun size_t sg_zero_buffer(struct scatterlist *sgl, unsigned int nents,
1037*4882a593Smuzhiyun 		       size_t buflen, off_t skip)
1038*4882a593Smuzhiyun {
1039*4882a593Smuzhiyun 	unsigned int offset = 0;
1040*4882a593Smuzhiyun 	struct sg_mapping_iter miter;
1041*4882a593Smuzhiyun 	unsigned int sg_flags = SG_MITER_ATOMIC | SG_MITER_TO_SG;
1042*4882a593Smuzhiyun 
1043*4882a593Smuzhiyun 	sg_miter_start(&miter, sgl, nents, sg_flags);
1044*4882a593Smuzhiyun 
1045*4882a593Smuzhiyun 	if (!sg_miter_skip(&miter, skip))
1046*4882a593Smuzhiyun 		return false;
1047*4882a593Smuzhiyun 
1048*4882a593Smuzhiyun 	while (offset < buflen && sg_miter_next(&miter)) {
1049*4882a593Smuzhiyun 		unsigned int len;
1050*4882a593Smuzhiyun 
1051*4882a593Smuzhiyun 		len = min(miter.length, buflen - offset);
1052*4882a593Smuzhiyun 		memset(miter.addr, 0, len);
1053*4882a593Smuzhiyun 
1054*4882a593Smuzhiyun 		offset += len;
1055*4882a593Smuzhiyun 	}
1056*4882a593Smuzhiyun 
1057*4882a593Smuzhiyun 	sg_miter_stop(&miter);
1058*4882a593Smuzhiyun 	return offset;
1059*4882a593Smuzhiyun }
1060*4882a593Smuzhiyun EXPORT_SYMBOL(sg_zero_buffer);
1061