Skip to content

Commit

Permalink
mm/page_alloc: do bulk array bounds check after checking populated el…
Browse files Browse the repository at this point in the history
…ements

Dan Carpenter reported the following

  The patch 0f87d9d: "mm/page_alloc: add an array-based interface
  to the bulk page allocator" from Apr 29, 2021, leads to the following
  static checker warning:

        mm/page_alloc.c:5338 __alloc_pages_bulk()
        warn: potentially one past the end of array 'page_array[nr_populated]'

The problem can occur if an array is passed in that is fully populated.
That potentially ends up allocating a single page and storing it past
the end of the array.  This patch returns 0 if the array is fully
populated.

Link: https://lkml.kernel.org/r/[email protected]
Fixes: 0f87d9d ("mm/page_alloc: add an array-based interface to the bulk page allocator")
Signed-off-by: Mel Gorman <[email protected]>
Reported-by: Dan Carpenter <[email protected]>
Cc: Jesper Dangaard Brouer <[email protected]>
Cc: Vlastimil Babka <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
gormanm authored and torvalds committed Jun 25, 2021
1 parent b08e50d commit b3b64eb
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions mm/page_alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -5056,6 +5056,10 @@ unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid,
while (page_array && nr_populated < nr_pages && page_array[nr_populated])
nr_populated++;

/* Already populated array? */
if (unlikely(page_array && nr_pages - nr_populated == 0))
return 0;

/* Use the single page allocator for one page. */
if (nr_pages - nr_populated == 1)
goto failed;
Expand Down

0 comments on commit b3b64eb

Please sign in to comment.