Skip to content

Commit 9a40401

Browse files
committed
lib/scatterlist: Do not limit max_segment to PAGE_ALIGNED values
The main intention of the max_segment argument to __sg_alloc_table_from_pages() is to match the DMA layer segment size set by dma_set_max_seg_size(). Restricting the input to be page aligned makes it impossible to just connect the DMA layer to this API. The only reason for a page alignment here is because the algorithm will overshoot the max_segment if it is not a multiple of PAGE_SIZE. Simply fix the alignment before starting and don't expose this implementation detail to the callers. A future patch will completely remove SCATTERLIST_MAX_SEGMENT. Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
1 parent 16e7483 commit 9a40401

1 file changed

Lines changed: 8 additions & 4 deletions

File tree

lib/scatterlist.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ static struct scatterlist *get_next_sg(struct sg_table *table,
404404
* @n_pages: Number of pages in the pages array
405405
* @offset: Offset from start of the first page to the start of a buffer
406406
* @size: Number of valid bytes in the buffer (after offset)
407-
* @max_segment: Maximum size of a scatterlist node in bytes (page aligned)
407+
* @max_segment: Maximum size of a scatterlist element in bytes
408408
* @prv: Last populated sge in sgt
409409
* @left_pages: Left pages caller have to set after this call
410410
* @gfp_mask: GFP allocation mask
@@ -435,7 +435,12 @@ struct scatterlist *__sg_alloc_table_from_pages(struct sg_table *sgt,
435435
unsigned int added_nents = 0;
436436
struct scatterlist *s = prv;
437437

438-
if (WARN_ON(!max_segment || offset_in_page(max_segment)))
438+
/*
439+
* The algorithm below requires max_segment to be aligned to PAGE_SIZE
440+
* otherwise it can overshoot.
441+
*/
442+
max_segment = ALIGN_DOWN(max_segment, PAGE_SIZE);
443+
if (WARN_ON(max_segment < PAGE_SIZE))
439444
return ERR_PTR(-EINVAL);
440445

441446
if (IS_ENABLED(CONFIG_ARCH_NO_SG_CHAIN) && prv)
@@ -542,8 +547,7 @@ int sg_alloc_table_from_pages(struct sg_table *sgt, struct page **pages,
542547
unsigned long size, gfp_t gfp_mask)
543548
{
544549
return PTR_ERR_OR_ZERO(__sg_alloc_table_from_pages(sgt, pages, n_pages,
545-
offset, size, SCATTERLIST_MAX_SEGMENT,
546-
NULL, 0, gfp_mask));
550+
offset, size, UINT_MAX, NULL, 0, gfp_mask));
547551
}
548552
EXPORT_SYMBOL(sg_alloc_table_from_pages);
549553

0 commit comments

Comments
 (0)