Skip to content

Commit

Permalink
Add assertion and make variables unsigned in abd_alloc_chunks()
Browse files Browse the repository at this point in the history
Clang's static analyzer pointed out that if alloc_pages >= nr_pages
before the loop, the value of page will be undefined and will be used
anyway. This should not be possible, but as cleanup, we add an
assertion. We also recognize that the local variables should be unsigned
in the first place, so we make them unsigned. This is not enough to
avoid the need for the assertion, since there is still the case that
alloc_pages == nr_pages and nr_pages == 0, which the assertion
implicitly checks.

Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Reviewed-by: Brian Atkinson <batkinson@lanl.gov>
Signed-off-by: Richard Yao <richard.yao@alumni.stonybrook.edu>
Closes openzfs#14456
  • Loading branch information
ryao authored and behlendorf committed Feb 6, 2023
1 parent cfb4961 commit 6695368
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions module/os/linux/zfs/abd_os.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,18 +272,20 @@ abd_alloc_chunks(abd_t *abd, size_t size)
struct page *page, *tmp_page = NULL;
gfp_t gfp = __GFP_NOWARN | GFP_NOIO;
gfp_t gfp_comp = (gfp | __GFP_NORETRY | __GFP_COMP) & ~__GFP_RECLAIM;
int max_order = MIN(zfs_abd_scatter_max_order, MAX_ORDER - 1);
int nr_pages = abd_chunkcnt_for_bytes(size);
int chunks = 0, zones = 0;
unsigned int max_order = MIN(zfs_abd_scatter_max_order, MAX_ORDER - 1);
unsigned int nr_pages = abd_chunkcnt_for_bytes(size);
unsigned int chunks = 0, zones = 0;
size_t remaining_size;
int nid = NUMA_NO_NODE;
int alloc_pages = 0;
unsigned int alloc_pages = 0;

INIT_LIST_HEAD(&pages);

ASSERT3U(alloc_pages, <, nr_pages);

while (alloc_pages < nr_pages) {
unsigned chunk_pages;
int order;
unsigned int chunk_pages;
unsigned int order;

order = MIN(highbit64(nr_pages - alloc_pages) - 1, max_order);
chunk_pages = (1U << order);
Expand Down

0 comments on commit 6695368

Please sign in to comment.