Skip to contentSkip to conversation

buffer: convert block_truncate_page() to use a folio

bug report Dan Carpenter 2 participants 2 messages Jun 14, 2023 – Jun 15, 2023 VFS

lore
Share & tools
lore
#0 lore raw

Hello Matthew Wilcox (Oracle),

The patch dd69ce3382a2: "buffer: convert block_truncate_page() to use a folio" from Jun 12, 2023, leads to the following Smatch static checker warning:

fs/buffer.c:1066 grow_dev_page() error: 'folio' dereferencing possible ERR_PTR()

This one seems like a false positive, If you call __filemap_get_folio() with __GFP_NOFAIL then it only returns valid pointers, right?

fs/buffer.c:2689 block_truncate_page() warn: 'folio' is an error pointer or valid fs/buffer.c:2692 block_truncate_page() error: 'folio' dereferencing possible ERR_PTR()

fs/buffer.c

    2679         length = from & (blocksize - 1);
    2680 
    2681         /* Block boundary? Nothing to do */
    2682         if (!length)
    2683                 return 0;
    2684 
    2685         length = blocksize - length;
    2686         iblock = (sector_t)index << (PAGE_SHIFT - inode->i_blkbits);
    2687         
    2688         folio = filemap_grab_folio(mapping, index);

--> 2689 if (!folio)

This should be IS_ERR().

    2690                 return -ENOMEM;
    2691 
    2692         bh = folio_buffers(folio);
                                    ^^^^^

Dereferenced.

    2693         if (!bh) {
    2694                 folio_create_empty_buffers(folio, blocksize, 0);
    2695                 bh = folio_buffers(folio);
    2696         }
    2697 
    2698         /* Find the buffer that contains "offset" */

regards,
dan carpenter

#1 lore raw
Quoted 10 lines · Dan Carpenter

Hello Matthew Wilcox (Oracle),

The patch dd69ce3382a2: "buffer: convert block_truncate_page() to use a folio" from Jun 12, 2023, leads to the following Smatch static checker warning:

fs/buffer.c:1066 grow_dev_page() error: 'folio' dereferencing possible ERR_PTR()

This one seems like a false positive, If you call __filemap_get_folio() with __GFP_NOFAIL then it only returns valid pointers, right?

That's right. There was no check for NULL before, and I looked at it carefully before deciding that I didn't need to add a check for an error when I converted it from find_or_create_page().

Quoted 14 lines

fs/buffer.c:2689 block_truncate_page() warn: 'folio' is an error pointer or valid fs/buffer.c:2692 block_truncate_page() error: 'folio' dereferencing possible ERR_PTR()

fs/buffer.c

    2679         length = from & (blocksize - 1);
    2680 
    2681         /* Block boundary? Nothing to do */
    2682         if (!length)
    2683                 return 0;
    2684 
    2685         length = blocksize - length;
    2686         iblock = (sector_t)index << (PAGE_SHIFT - inode->i_blkbits);
    2687         
    2688         folio = filemap_grab_folio(mapping, index);
Quoted 3 lines

--> 2689 if (!folio)

This should be IS_ERR().

Yup, that was sloppy.

Andrew, can you add this -fix to "buffer: Convert block_truncate_page() to use a folio"?

fs/buffer.c +2 -2
index 5a5b0c9d9769..248968dbde31 100644
@@ -2617,8 +2617,8 @@ int block_truncate_page(struct address_space *mapping,
 	iblock = (sector_t)index << (PAGE_SHIFT - inode->i_blkbits);
 	
 	folio = filemap_grab_folio(mapping, index);
-	if (!folio)
-		return -ENOMEM;
+	if (IS_ERR(folio))
+		return PTR_ERR(folio);
 
 	bh = folio_buffers(folio);
 	if (!bh) {