2 * Copyright (C) 2007 Jens Axboe <jens.axboe@oracle.com>
4 * Scatterlist handling helpers.
6 * This source code is licensed under the GNU General Public License,
7 * Version 2. See the file COPYING for more details.
9 #include <linux/export.h>
10 #include <linux/slab.h>
11 #include <linux/scatterlist.h>
12 #include <linux/highmem.h>
13 #include <linux/kmemleak.h>
16 * sg_next - return the next scatterlist entry in a list
17 * @sg: The current sg entry
20 * Usually the next entry will be @sg@ + 1, but if this sg element is part
21 * of a chained scatterlist, it could jump to the start of a new
25 struct scatterlist
*sg_next(struct scatterlist
*sg
)
27 #ifdef CONFIG_DEBUG_SG
28 BUG_ON(sg
->sg_magic
!= SG_MAGIC
);
34 if (unlikely(sg_is_chain(sg
)))
35 sg
= sg_chain_ptr(sg
);
39 EXPORT_SYMBOL(sg_next
);
42 * sg_nents - return total count of entries in scatterlist
43 * @sg: The scatterlist
46 * Allows to know how many entries are in sg, taking into acount
50 int sg_nents(struct scatterlist
*sg
)
53 for (nents
= 0; sg
; sg
= sg_next(sg
))
57 EXPORT_SYMBOL(sg_nents
);
61 * sg_last - return the last scatterlist entry in a list
62 * @sgl: First entry in the scatterlist
63 * @nents: Number of entries in the scatterlist
66 * Should only be used casually, it (currently) scans the entire list
67 * to get the last entry.
69 * Note that the @sgl@ pointer passed in need not be the first one,
70 * the important bit is that @nents@ denotes the number of entries that
74 struct scatterlist
*sg_last(struct scatterlist
*sgl
, unsigned int nents
)
76 #ifndef ARCH_HAS_SG_CHAIN
77 struct scatterlist
*ret
= &sgl
[nents
- 1];
79 struct scatterlist
*sg
, *ret
= NULL
;
82 for_each_sg(sgl
, sg
, nents
, i
)
86 #ifdef CONFIG_DEBUG_SG
87 BUG_ON(sgl
[0].sg_magic
!= SG_MAGIC
);
88 BUG_ON(!sg_is_last(ret
));
92 EXPORT_SYMBOL(sg_last
);
95 * sg_init_table - Initialize SG table
97 * @nents: Number of entries in table
100 * If this is part of a chained sg table, sg_mark_end() should be
101 * used only on the last table part.
104 void sg_init_table(struct scatterlist
*sgl
, unsigned int nents
)
106 memset(sgl
, 0, sizeof(*sgl
) * nents
);
107 #ifdef CONFIG_DEBUG_SG
110 for (i
= 0; i
< nents
; i
++)
111 sgl
[i
].sg_magic
= SG_MAGIC
;
114 sg_mark_end(&sgl
[nents
- 1]);
116 EXPORT_SYMBOL(sg_init_table
);
119 * sg_init_one - Initialize a single entry sg list
121 * @buf: Virtual address for IO
125 void sg_init_one(struct scatterlist
*sg
, const void *buf
, unsigned int buflen
)
127 sg_init_table(sg
, 1);
128 sg_set_buf(sg
, buf
, buflen
);
130 EXPORT_SYMBOL(sg_init_one
);
133 * The default behaviour of sg_alloc_table() is to use these kmalloc/kfree
136 static struct scatterlist
*sg_kmalloc(unsigned int nents
, gfp_t gfp_mask
)
138 if (nents
== SG_MAX_SINGLE_ALLOC
) {
140 * Kmemleak doesn't track page allocations as they are not
141 * commonly used (in a raw form) for kernel data structures.
142 * As we chain together a list of pages and then a normal
143 * kmalloc (tracked by kmemleak), in order to for that last
144 * allocation not to become decoupled (and thus a
145 * false-positive) we need to inform kmemleak of all the
146 * intermediate allocations.
148 void *ptr
= (void *) __get_free_page(gfp_mask
);
149 kmemleak_alloc(ptr
, PAGE_SIZE
, 1, gfp_mask
);
152 return kmalloc(nents
* sizeof(struct scatterlist
), gfp_mask
);
155 static void sg_kfree(struct scatterlist
*sg
, unsigned int nents
)
157 if (nents
== SG_MAX_SINGLE_ALLOC
) {
159 free_page((unsigned long) sg
);
165 * __sg_free_table - Free a previously mapped sg table
166 * @table: The sg table header to use
167 * @max_ents: The maximum number of entries per single scatterlist
168 * @free_fn: Free function
171 * Free an sg table previously allocated and setup with
172 * __sg_alloc_table(). The @max_ents value must be identical to
173 * that previously used with __sg_alloc_table().
176 void __sg_free_table(struct sg_table
*table
, unsigned int max_ents
,
179 struct scatterlist
*sgl
, *next
;
181 if (unlikely(!table
->sgl
))
185 while (table
->orig_nents
) {
186 unsigned int alloc_size
= table
->orig_nents
;
187 unsigned int sg_size
;
190 * If we have more than max_ents segments left,
191 * then assign 'next' to the sg table after the current one.
192 * sg_size is then one less than alloc size, since the last
193 * element is the chain pointer.
195 if (alloc_size
> max_ents
) {
196 next
= sg_chain_ptr(&sgl
[max_ents
- 1]);
197 alloc_size
= max_ents
;
198 sg_size
= alloc_size
- 1;
200 sg_size
= alloc_size
;
204 table
->orig_nents
-= sg_size
;
205 free_fn(sgl
, alloc_size
);
211 EXPORT_SYMBOL(__sg_free_table
);
214 * sg_free_table - Free a previously allocated sg table
215 * @table: The mapped sg table header
218 void sg_free_table(struct sg_table
*table
)
220 __sg_free_table(table
, SG_MAX_SINGLE_ALLOC
, sg_kfree
);
222 EXPORT_SYMBOL(sg_free_table
);
225 * __sg_alloc_table - Allocate and initialize an sg table with given allocator
226 * @table: The sg table header to use
227 * @nents: Number of entries in sg list
228 * @max_ents: The maximum number of entries the allocator returns per call
229 * @gfp_mask: GFP allocation mask
230 * @alloc_fn: Allocator to use
233 * This function returns a @table @nents long. The allocator is
234 * defined to return scatterlist chunks of maximum size @max_ents.
235 * Thus if @nents is bigger than @max_ents, the scatterlists will be
236 * chained in units of @max_ents.
239 * If this function returns non-0 (eg failure), the caller must call
240 * __sg_free_table() to cleanup any leftover allocations.
243 int __sg_alloc_table(struct sg_table
*table
, unsigned int nents
,
244 unsigned int max_ents
, gfp_t gfp_mask
,
245 sg_alloc_fn
*alloc_fn
)
247 struct scatterlist
*sg
, *prv
;
250 #ifndef ARCH_HAS_SG_CHAIN
251 BUG_ON(nents
> max_ents
);
254 memset(table
, 0, sizeof(*table
));
259 unsigned int sg_size
, alloc_size
= left
;
261 if (alloc_size
> max_ents
) {
262 alloc_size
= max_ents
;
263 sg_size
= alloc_size
- 1;
265 sg_size
= alloc_size
;
269 sg
= alloc_fn(alloc_size
, gfp_mask
);
272 * Adjust entry count to reflect that the last
273 * entry of the previous table won't be used for
274 * linkage. Without this, sg_kfree() may get
278 table
->nents
= ++table
->orig_nents
;
283 sg_init_table(sg
, alloc_size
);
284 table
->nents
= table
->orig_nents
+= sg_size
;
287 * If this is the first mapping, assign the sg table header.
288 * If this is not the first mapping, chain previous part.
291 sg_chain(prv
, max_ents
, sg
);
296 * If no more entries after this one, mark the end
299 sg_mark_end(&sg
[sg_size
- 1]);
306 EXPORT_SYMBOL(__sg_alloc_table
);
309 * sg_alloc_table - Allocate and initialize an sg table
310 * @table: The sg table header to use
311 * @nents: Number of entries in sg list
312 * @gfp_mask: GFP allocation mask
315 * Allocate and initialize an sg table. If @nents@ is larger than
316 * SG_MAX_SINGLE_ALLOC a chained sg table will be setup.
319 int sg_alloc_table(struct sg_table
*table
, unsigned int nents
, gfp_t gfp_mask
)
323 ret
= __sg_alloc_table(table
, nents
, SG_MAX_SINGLE_ALLOC
,
324 gfp_mask
, sg_kmalloc
);
326 __sg_free_table(table
, SG_MAX_SINGLE_ALLOC
, sg_kfree
);
330 EXPORT_SYMBOL(sg_alloc_table
);
333 * sg_alloc_table_from_pages - Allocate and initialize an sg table from
335 * @sgt: The sg table header to use
336 * @pages: Pointer to an array of page pointers
337 * @n_pages: Number of pages in the pages array
338 * @offset: Offset from start of the first page to the start of a buffer
339 * @size: Number of valid bytes in the buffer (after offset)
340 * @gfp_mask: GFP allocation mask
343 * Allocate and initialize an sg table from a list of pages. Contiguous
344 * ranges of the pages are squashed into a single scatterlist node. A user
345 * may provide an offset at a start and a size of valid data in a buffer
346 * specified by the page array. The returned sg table is released by
350 * 0 on success, negative error on failure
352 int sg_alloc_table_from_pages(struct sg_table
*sgt
,
353 struct page
**pages
, unsigned int n_pages
,
354 unsigned long offset
, unsigned long size
,
359 unsigned int cur_page
;
361 struct scatterlist
*s
;
363 /* compute number of contiguous chunks */
365 for (i
= 1; i
< n_pages
; ++i
)
366 if (page_to_pfn(pages
[i
]) != page_to_pfn(pages
[i
- 1]) + 1)
369 ret
= sg_alloc_table(sgt
, chunks
, gfp_mask
);
373 /* merging chunks and putting them into the scatterlist */
375 for_each_sg(sgt
->sgl
, s
, sgt
->orig_nents
, i
) {
376 unsigned long chunk_size
;
379 /* look for the end of the current chunk */
380 for (j
= cur_page
+ 1; j
< n_pages
; ++j
)
381 if (page_to_pfn(pages
[j
]) !=
382 page_to_pfn(pages
[j
- 1]) + 1)
385 chunk_size
= ((j
- cur_page
) << PAGE_SHIFT
) - offset
;
386 sg_set_page(s
, pages
[cur_page
], min(size
, chunk_size
), offset
);
394 EXPORT_SYMBOL(sg_alloc_table_from_pages
);
397 * sg_miter_start - start mapping iteration over a sg list
398 * @miter: sg mapping iter to be started
399 * @sgl: sg list to iterate over
400 * @nents: number of sg entries
403 * Starts mapping iterator @miter.
408 void sg_miter_start(struct sg_mapping_iter
*miter
, struct scatterlist
*sgl
,
409 unsigned int nents
, unsigned int flags
)
411 memset(miter
, 0, sizeof(struct sg_mapping_iter
));
414 miter
->__nents
= nents
;
416 WARN_ON(!(flags
& (SG_MITER_TO_SG
| SG_MITER_FROM_SG
)));
417 miter
->__flags
= flags
;
419 EXPORT_SYMBOL(sg_miter_start
);
422 * sg_miter_next - proceed mapping iterator to the next mapping
423 * @miter: sg mapping iter to proceed
426 * Proceeds @miter to the next mapping. @miter should have been started
427 * using sg_miter_start(). On successful return, @miter->page,
428 * @miter->addr and @miter->length point to the current mapping.
431 * Preemption disabled if SG_MITER_ATOMIC. Preemption must stay disabled
432 * till @miter is stopped. May sleep if !SG_MITER_ATOMIC.
435 * true if @miter contains the next mapping. false if end of sg
438 bool sg_miter_next(struct sg_mapping_iter
*miter
)
440 unsigned int off
, len
;
442 /* check for end and drop resources from the last iteration */
446 sg_miter_stop(miter
);
448 /* get to the next sg if necessary. __offset is adjusted by stop */
449 while (miter
->__offset
== miter
->__sg
->length
) {
450 if (--miter
->__nents
) {
451 miter
->__sg
= sg_next(miter
->__sg
);
457 /* map the next page */
458 off
= miter
->__sg
->offset
+ miter
->__offset
;
459 len
= miter
->__sg
->length
- miter
->__offset
;
461 miter
->page
= nth_page(sg_page(miter
->__sg
), off
>> PAGE_SHIFT
);
463 miter
->length
= min_t(unsigned int, len
, PAGE_SIZE
- off
);
464 miter
->consumed
= miter
->length
;
466 if (miter
->__flags
& SG_MITER_ATOMIC
)
467 miter
->addr
= kmap_atomic(miter
->page
) + off
;
469 miter
->addr
= kmap(miter
->page
) + off
;
473 EXPORT_SYMBOL(sg_miter_next
);
476 * sg_miter_stop - stop mapping iteration
477 * @miter: sg mapping iter to be stopped
480 * Stops mapping iterator @miter. @miter should have been started
481 * started using sg_miter_start(). A stopped iteration can be
482 * resumed by calling sg_miter_next() on it. This is useful when
483 * resources (kmap) need to be released during iteration.
486 * Preemption disabled if the SG_MITER_ATOMIC is set. Don't care
489 void sg_miter_stop(struct sg_mapping_iter
*miter
)
491 WARN_ON(miter
->consumed
> miter
->length
);
493 /* drop resources from the last iteration */
495 miter
->__offset
+= miter
->consumed
;
497 if (miter
->__flags
& SG_MITER_TO_SG
)
498 flush_kernel_dcache_page(miter
->page
);
500 if (miter
->__flags
& SG_MITER_ATOMIC
) {
501 WARN_ON_ONCE(preemptible());
502 kunmap_atomic(miter
->addr
);
512 EXPORT_SYMBOL(sg_miter_stop
);
515 * sg_copy_buffer - Copy data between a linear buffer and an SG list
517 * @nents: Number of SG entries
518 * @buf: Where to copy from
519 * @buflen: The number of bytes to copy
520 * @to_buffer: transfer direction (non zero == from an sg list to a
521 * buffer, 0 == from a buffer to an sg list
523 * Returns the number of copied bytes.
526 static size_t sg_copy_buffer(struct scatterlist
*sgl
, unsigned int nents
,
527 void *buf
, size_t buflen
, int to_buffer
)
529 unsigned int offset
= 0;
530 struct sg_mapping_iter miter
;
532 unsigned int sg_flags
= SG_MITER_ATOMIC
;
535 sg_flags
|= SG_MITER_FROM_SG
;
537 sg_flags
|= SG_MITER_TO_SG
;
539 sg_miter_start(&miter
, sgl
, nents
, sg_flags
);
541 local_irq_save(flags
);
543 while (sg_miter_next(&miter
) && offset
< buflen
) {
546 len
= min(miter
.length
, buflen
- offset
);
549 memcpy(buf
+ offset
, miter
.addr
, len
);
551 memcpy(miter
.addr
, buf
+ offset
, len
);
556 sg_miter_stop(&miter
);
558 local_irq_restore(flags
);
563 * sg_copy_from_buffer - Copy from a linear buffer to an SG list
565 * @nents: Number of SG entries
566 * @buf: Where to copy from
567 * @buflen: The number of bytes to copy
569 * Returns the number of copied bytes.
572 size_t sg_copy_from_buffer(struct scatterlist
*sgl
, unsigned int nents
,
573 void *buf
, size_t buflen
)
575 return sg_copy_buffer(sgl
, nents
, buf
, buflen
, 0);
577 EXPORT_SYMBOL(sg_copy_from_buffer
);
580 * sg_copy_to_buffer - Copy from an SG list to a linear buffer
582 * @nents: Number of SG entries
583 * @buf: Where to copy to
584 * @buflen: The number of bytes to copy
586 * Returns the number of copied bytes.
589 size_t sg_copy_to_buffer(struct scatterlist
*sgl
, unsigned int nents
,
590 void *buf
, size_t buflen
)
592 return sg_copy_buffer(sgl
, nents
, buf
, buflen
, 1);
594 EXPORT_SYMBOL(sg_copy_to_buffer
);