2 * Copyright (c) 2008 Yahoo!, Inc.
4 * Written by: John Baldwin <jhb@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the author nor the names of any co-contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * $FreeBSD: src/sys/kern/subr_sglist.c,v 1.3 2009/08/21 02:59:07 jhb Exp $
34 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
39 #include <sys/sglist.h>
44 #include <vm/vm_map.h>
48 static MALLOC_DEFINE(M_SGLIST
, "sglist", "scatter/gather lists");
51 * Convenience macros to save the state of an sglist so it can be restored
52 * if an append attempt fails. Since sglist's only grow we only need to
53 * save the current count of segments and the length of the ending segment.
54 * Earlier segments will not be changed by an append, and the only change
55 * that can occur to the ending segment is that it can be extended.
62 #define SGLIST_SAVE(sg, sgsave) do { \
63 (sgsave).sg_nseg = (sg)->sg_nseg; \
64 if ((sgsave).sg_nseg > 0) \
65 (sgsave).ss_len = (sg)->sg_segs[(sgsave).sg_nseg - 1].ss_len; \
67 (sgsave).ss_len = 0; \
70 #define SGLIST_RESTORE(sg, sgsave) do { \
71 (sg)->sg_nseg = (sgsave).sg_nseg; \
72 if ((sgsave).sg_nseg > 0) \
73 (sg)->sg_segs[(sgsave).sg_nseg - 1].ss_len = (sgsave).ss_len; \
77 * Append a single (paddr, len) to a sglist. sg is the list and ss is
78 * the current segment in the list. If we run out of segments then
79 * EFBIG will be returned.
82 _sglist_append_range(struct sglist
*sg
, struct sglist_seg
**ssp
,
83 vm_paddr_t paddr
, size_t len
)
85 struct sglist_seg
*ss
;
88 if (ss
->ss_paddr
+ ss
->ss_len
== paddr
)
91 if (sg
->sg_nseg
== sg
->sg_maxseg
)
103 * Worker routine to append a virtual address range (either kernel or
104 * user) to a scatter/gather list.
107 _sglist_append_buf(struct sglist
*sg
, void *buf
, size_t len
, pmap_t pmap
,
110 struct sglist_seg
*ss
;
111 vm_offset_t vaddr
, offset
;
121 /* Do the first page. It may have an offset. */
122 vaddr
= (vm_offset_t
)buf
;
123 offset
= vaddr
& PAGE_MASK
;
125 paddr
= pmap_extract(pmap
, vaddr
);
127 paddr
= pmap_kextract(vaddr
);
128 seglen
= MIN(len
, PAGE_SIZE
- offset
);
129 if (sg
->sg_nseg
== 0) {
131 ss
->ss_paddr
= paddr
;
135 ss
= &sg
->sg_segs
[sg
->sg_nseg
- 1];
136 error
= _sglist_append_range(sg
, &ss
, paddr
, seglen
);
146 seglen
= MIN(len
, PAGE_SIZE
);
148 paddr
= pmap_extract(pmap
, vaddr
);
150 paddr
= pmap_kextract(vaddr
);
151 error
= _sglist_append_range(sg
, &ss
, paddr
, seglen
);
164 * Determine the number of scatter/gather list elements needed to
165 * describe a kernel virtual address range.
168 sglist_count(void *buf
, size_t len
)
170 vm_offset_t vaddr
, vendaddr
;
171 vm_paddr_t lastaddr
, paddr
;
177 vaddr
= trunc_page((vm_offset_t
)buf
);
178 vendaddr
= (vm_offset_t
)buf
+ len
;
180 lastaddr
= pmap_kextract(vaddr
);
182 while (vaddr
< vendaddr
) {
183 paddr
= pmap_kextract(vaddr
);
184 if (lastaddr
+ PAGE_SIZE
!= paddr
)
193 * Allocate a scatter/gather list along with 'nsegs' segments. The
194 * 'mflags' parameters are the same as passed to kmalloc(9). The caller
195 * should use sglist_free() to free this list.
198 sglist_alloc(int nsegs
, int mflags
)
202 sg
= kmalloc(sizeof(struct sglist
) + nsegs
* sizeof(struct sglist_seg
),
206 sglist_init(sg
, nsegs
, (struct sglist_seg
*)(sg
+ 1));
211 * Free a scatter/gather list allocated via sglist_allc().
214 sglist_free(struct sglist
*sg
)
217 if (refcount_release(&sg
->sg_refs
))
222 * Append the segments to describe a single kernel virtual address
223 * range to a scatter/gather list. If there are insufficient
224 * segments, then this fails with EFBIG.
227 sglist_append(struct sglist
*sg
, void *buf
, size_t len
)
232 if (sg
->sg_maxseg
== 0)
234 SGLIST_SAVE(sg
, save
);
235 error
= _sglist_append_buf(sg
, buf
, len
, NULL
, NULL
);
237 SGLIST_RESTORE(sg
, save
);
242 * Append a single physical address range to a scatter/gather list.
243 * If there are insufficient segments, then this fails with EFBIG.
246 sglist_append_phys(struct sglist
*sg
, vm_paddr_t paddr
, size_t len
)
248 struct sglist_seg
*ss
;
252 if (sg
->sg_maxseg
== 0)
257 if (sg
->sg_nseg
== 0) {
258 sg
->sg_segs
[0].ss_paddr
= paddr
;
259 sg
->sg_segs
[0].ss_len
= len
;
263 ss
= &sg
->sg_segs
[sg
->sg_nseg
- 1];
264 SGLIST_SAVE(sg
, save
);
265 error
= _sglist_append_range(sg
, &ss
, paddr
, len
);
267 SGLIST_RESTORE(sg
, save
);
272 * Append the segments that describe a single mbuf chain to a
273 * scatter/gather list. If there are insufficient segments, then this
277 sglist_append_mbuf(struct sglist
*sg
, struct mbuf
*m0
)
283 if (sg
->sg_maxseg
== 0)
287 SGLIST_SAVE(sg
, save
);
288 for (m
= m0
; m
!= NULL
; m
= m
->m_next
) {
290 error
= sglist_append(sg
, m
->m_data
, m
->m_len
);
292 SGLIST_RESTORE(sg
, save
);
301 * Append the segments that describe a single user address range to a
302 * scatter/gather list. If there are insufficient segments, then this
306 sglist_append_user(struct sglist
*sg
, void *buf
, size_t len
, struct thread
*td
)
311 if (sg
->sg_maxseg
== 0)
313 SGLIST_SAVE(sg
, save
);
314 error
= _sglist_append_buf(sg
, buf
, len
,
315 vmspace_pmap(td
->td_proc
->p_vmspace
), NULL
);
317 SGLIST_RESTORE(sg
, save
);
322 * Append the segments that describe a single uio to a scatter/gather
323 * list. If there are insufficient segments, then this fails with
327 sglist_append_uio(struct sglist
*sg
, struct uio
*uio
)
331 size_t resid
, minlen
;
335 if (sg
->sg_maxseg
== 0)
338 resid
= uio
->uio_resid
;
341 if (uio
->uio_segflg
== UIO_USERSPACE
) {
342 KASSERT(uio
->uio_td
!= NULL
,
343 ("sglist_append_uio: USERSPACE but no thread"));
344 pmap
= vmspace_pmap(uio
->uio_td
->td_proc
->p_vmspace
);
349 SGLIST_SAVE(sg
, save
);
350 for (i
= 0; i
< uio
->uio_iovcnt
&& resid
!= 0; i
++) {
352 * Now at the first iovec to load. Load each iovec
353 * until we have exhausted the residual count.
355 minlen
= MIN(resid
, iov
[i
].iov_len
);
357 error
= _sglist_append_buf(sg
, iov
[i
].iov_base
, minlen
,
360 SGLIST_RESTORE(sg
, save
);
370 * Append the segments that describe at most 'resid' bytes from a
371 * single uio to a scatter/gather list. If there are insufficient
372 * segments, then only the amount that fits is appended.
375 sglist_consume_uio(struct sglist
*sg
, struct uio
*uio
, size_t resid
)
382 if (sg
->sg_maxseg
== 0)
385 if (uio
->uio_segflg
== UIO_USERSPACE
) {
386 KASSERT(uio
->uio_td
!= NULL
,
387 ("sglist_consume_uio: USERSPACE but no thread"));
388 pmap
= vmspace_pmap(uio
->uio_td
->td_proc
->p_vmspace
);
393 while (resid
> 0 && uio
->uio_resid
) {
405 * Try to append this iovec. If we run out of room,
406 * then break out of the loop.
408 error
= _sglist_append_buf(sg
, iov
->iov_base
, len
, pmap
, &done
);
409 iov
->iov_base
= (char *)iov
->iov_base
+ done
;
410 iov
->iov_len
-= done
;
411 uio
->uio_resid
-= done
;
412 uio
->uio_offset
+= done
;
421 * Allocate and populate a scatter/gather list to describe a single
422 * kernel virtual address range.
425 sglist_build(void *buf
, size_t len
, int mflags
)
433 nsegs
= sglist_count(buf
, len
);
434 sg
= sglist_alloc(nsegs
, mflags
);
437 if (sglist_append(sg
, buf
, len
) != 0) {
445 * Clone a new copy of a scatter/gather list.
448 sglist_clone(struct sglist
*sg
, int mflags
)
454 new = sglist_alloc(sg
->sg_maxseg
, mflags
);
457 new->sg_nseg
= sg
->sg_nseg
;
458 bcopy(sg
->sg_segs
, new->sg_segs
, sizeof(struct sglist_seg
) *
464 * Calculate the total length of the segments described in a
465 * scatter/gather list.
468 sglist_length(struct sglist
*sg
)
474 for (i
= 0; i
< sg
->sg_nseg
; i
++)
475 space
+= sg
->sg_segs
[i
].ss_len
;
480 * Split a scatter/gather list into two lists. The scatter/gather
481 * entries for the first 'length' bytes of the 'original' list are
482 * stored in the '*head' list and are removed from 'original'.
484 * If '*head' is NULL, then a new list will be allocated using
485 * 'mflags'. If M_NOWAIT is specified and the allocation fails,
486 * ENOMEM will be returned.
488 * If '*head' is not NULL, it should point to an empty sglist. If it
489 * does not have enough room for the remaining space, then EFBIG will
490 * be returned. If '*head' is not empty, then EINVAL will be
493 * If 'original' is shared (refcount > 1), then EDOOFUS will be
497 sglist_split(struct sglist
*original
, struct sglist
**head
, size_t length
,
504 if (original
->sg_refs
> 1)
507 /* Figure out how big of a sglist '*head' has to hold. */
511 for (i
= 0; i
< original
->sg_nseg
; i
++) {
512 space
+= original
->sg_segs
[i
].ss_len
;
514 if (space
>= length
) {
516 * If 'length' falls in the middle of a
517 * scatter/gather list entry, then 'split'
518 * holds how much of that entry will remain in
521 split
= space
- length
;
526 /* Nothing to do, so leave head empty. */
531 sg
= sglist_alloc(count
, mflags
);
537 if (sg
->sg_maxseg
< count
)
539 if (sg
->sg_nseg
!= 0)
543 /* Copy 'count' entries to 'sg' from 'original'. */
544 bcopy(original
->sg_segs
, sg
->sg_segs
, count
*
545 sizeof(struct sglist_seg
));
549 * If we had to split a list entry, fixup the last entry in
550 * 'sg' and the new first entry in 'original'. We also
551 * decrement 'count' by 1 since we will only be removing
552 * 'count - 1' segments from 'original' now.
556 sg
->sg_segs
[count
].ss_len
-= split
;
557 original
->sg_segs
[count
].ss_paddr
=
558 sg
->sg_segs
[count
].ss_paddr
+ split
;
559 original
->sg_segs
[count
].ss_len
= split
;
562 /* Trim 'count' entries from the front of 'original'. */
563 original
->sg_nseg
-= count
;
564 bcopy(original
->sg_segs
+ count
, original
->sg_segs
, count
*
565 sizeof(struct sglist_seg
));
570 * Append the scatter/gather list elements in 'second' to the
571 * scatter/gather list 'first'. If there is not enough space in
572 * 'first', EFBIG is returned.
575 sglist_join(struct sglist
*first
, struct sglist
*second
)
577 struct sglist_seg
*flast
, *sfirst
;
580 /* If 'second' is empty, there is nothing to do. */
581 if (second
->sg_nseg
== 0)
585 * If the first entry in 'second' can be appended to the last entry
586 * in 'first' then set append to '1'.
589 flast
= &first
->sg_segs
[first
->sg_nseg
- 1];
590 sfirst
= &second
->sg_segs
[0];
591 if (first
->sg_nseg
!= 0 &&
592 flast
->ss_paddr
+ flast
->ss_len
== sfirst
->ss_paddr
)
595 /* Make sure 'first' has enough room. */
596 if (first
->sg_nseg
+ second
->sg_nseg
- append
> first
->sg_maxseg
)
599 /* Merge last in 'first' and first in 'second' if needed. */
601 flast
->ss_len
+= sfirst
->ss_len
;
603 /* Append new segments from 'second' to 'first'. */
604 bcopy(first
->sg_segs
+ first
->sg_nseg
, second
->sg_segs
+ append
,
605 (second
->sg_nseg
- append
) * sizeof(struct sglist_seg
));
606 first
->sg_nseg
+= second
->sg_nseg
- append
;
607 sglist_reset(second
);
612 * Generate a new scatter/gather list from a range of an existing
613 * scatter/gather list. The 'offset' and 'length' parameters specify
614 * the logical range of the 'original' list to extract. If that range
615 * is not a subset of the length of 'original', then EINVAL is
616 * returned. The new scatter/gather list is stored in '*slice'.
618 * If '*slice' is NULL, then a new list will be allocated using
619 * 'mflags'. If M_NOWAIT is specified and the allocation fails,
620 * ENOMEM will be returned.
622 * If '*slice' is not NULL, it should point to an empty sglist. If it
623 * does not have enough room for the remaining space, then EFBIG will
624 * be returned. If '*slice' is not empty, then EINVAL will be
628 sglist_slice(struct sglist
*original
, struct sglist
**slice
, size_t offset
,
629 size_t length
, int mflags
)
632 size_t space
, end
, foffs
, loffs
;
639 /* Figure out how many segments '*slice' needs to have. */
640 end
= offset
+ length
;
645 for (i
= 0; i
< original
->sg_nseg
; i
++) {
646 space
+= original
->sg_segs
[i
].ss_len
;
647 if (space
> offset
) {
649 * When we hit the first segment, store its index
650 * in 'fseg' and the offset into the first segment
651 * of 'offset' in 'foffs'.
655 foffs
= offset
- (space
-
656 original
->sg_segs
[i
].ss_len
);
661 * When we hit the last segment, break out of
662 * the loop. Store the amount of extra space
663 * at the end of this segment in 'loffs'.
672 /* If we never hit 'end', then 'length' ran off the end, so fail. */
676 if (*slice
== NULL
) {
677 sg
= sglist_alloc(count
, mflags
);
683 if (sg
->sg_maxseg
< count
)
685 if (sg
->sg_nseg
!= 0)
690 * Copy over 'count' segments from 'original' starting at
693 bcopy(original
->sg_segs
+ fseg
, sg
->sg_segs
,
694 count
* sizeof(struct sglist_seg
));
697 /* Fixup first and last segments if needed. */
699 sg
->sg_segs
[0].ss_paddr
+= foffs
;
700 sg
->sg_segs
[0].ss_len
-= foffs
;
703 sg
->sg_segs
[count
- 1].ss_len
-= loffs
;