fs/jfs/jfs_metapage.c:245 __metapage_migrate_folio() error: we previously assumed 'mp' could be null (see line 235)
Subsystems (2)
Dan Carpenter
build botBCC: [email protected]
CC: [email protected]
TO: Shivank Garg <[email protected]>
CC: Andrew Morton <[email protected]>
CC: Linux Memory Management List <[email protected]>
tree: https://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git master head: 08710e696081d58163c8078e0e096be6d35c5fad commit: 39ed4d1a0e03ce3ac2145ee7ef0714c78bae9c61 [7893/8235] jfs: implement migrate_folio for jfs_metapage_aops
:::::: branch date: 3 hours ago
:::::: commit date: 6 days ago
config: i386-randconfig-141-20250502 (https://download.01.org/0day-ci/archive/20250507/[email protected]/config)
compiler: clang version 20.1.2 (https://github.com/llvm/llvm-project 58df0ef89dd64126512e4ee27b4ac3fd8ddf6247)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]> | Reported-by: Dan Carpenter <[email protected]> | Closes: https://lore.kernel.org/r/[email protected]/
smatch warnings:fs/jfs/jfs_metapage.c:245 __metapage_migrate_folio() error: we previously assumed 'mp' could be null (see line 235)
vim +/mp +245 fs/jfs/jfs_metapage.c
7fab479bebb96b Dave Kleikamp 2005-05-02 226
39ed4d1a0e03ce Shivank Garg 2025-04-30 227 static int __metapage_migrate_folio(struct address_space *mapping, struct folio *dst,
39ed4d1a0e03ce Shivank Garg 2025-04-30 228 struct folio *src, enum migrate_mode mode)
39ed4d1a0e03ce Shivank Garg 2025-04-30 229 {
39ed4d1a0e03ce Shivank Garg 2025-04-30 230 struct metapage *mp;
39ed4d1a0e03ce Shivank Garg 2025-04-30 231 int page_offset;
39ed4d1a0e03ce Shivank Garg 2025-04-30 232 int rc;
39ed4d1a0e03ce Shivank Garg 2025-04-30 233
39ed4d1a0e03ce Shivank Garg 2025-04-30 234 mp = folio_to_mp(src, 0);
39ed4d1a0e03ce Shivank Garg 2025-04-30 @235 if (mp && metapage_locked(mp))
39ed4d1a0e03ce Shivank Garg 2025-04-30 236 return -EAGAIN;
39ed4d1a0e03ce Shivank Garg 2025-04-30 237
39ed4d1a0e03ce Shivank Garg 2025-04-30 238 rc = filemap_migrate_folio(mapping, dst, src, mode);
39ed4d1a0e03ce Shivank Garg 2025-04-30 239 if (rc != MIGRATEPAGE_SUCCESS)
39ed4d1a0e03ce Shivank Garg 2025-04-30 240 return rc;
39ed4d1a0e03ce Shivank Garg 2025-04-30 241
39ed4d1a0e03ce Shivank Garg 2025-04-30 242 if (unlikely(insert_metapage(dst, mp)))
39ed4d1a0e03ce Shivank Garg 2025-04-30 243 return -EAGAIN;
39ed4d1a0e03ce Shivank Garg 2025-04-30 244
39ed4d1a0e03ce Shivank Garg 2025-04-30 @245 page_offset = mp->data - folio_address(src);
39ed4d1a0e03ce Shivank Garg 2025-04-30 246 mp->data = folio_address(dst) + page_offset;
39ed4d1a0e03ce Shivank Garg 2025-04-30 247 mp->folio = dst;
39ed4d1a0e03ce Shivank Garg 2025-04-30 248 remove_metapage(src, mp);
39ed4d1a0e03ce Shivank Garg 2025-04-30 249
39ed4d1a0e03ce Shivank Garg 2025-04-30 250 return MIGRATEPAGE_SUCCESS;
39ed4d1a0e03ce Shivank Garg 2025-04-30 251 }
39ed4d1a0e03ce Shivank Garg 2025-04-30 252 Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Quoted 40 lines · Dan Carpenter
tree: https://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git master head: 08710e696081d58163c8078e0e096be6d35c5fad commit: 39ed4d1a0e03ce3ac2145ee7ef0714c78bae9c61 [7893/8235] jfs: implement migrate_folio for jfs_metapage_aops config: i386-randconfig-141-20250502 (https://download.01.org/0day-ci/archive/20250507/[email protected]/config) compiler: clang version 20.1.2 (https://github.com/llvm/llvm-project 58df0ef89dd64126512e4ee27b4ac3fd8ddf6247)If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]> | Reported-by: Dan Carpenter <[email protected]> | Closes: https://lore.kernel.org/r/[email protected]/smatch warnings:
fs/jfs/jfs_metapage.c:245__metapage_migrate_folio()error: we previously assumed 'mp' could be null (see line 235)vim +/mp +245
fs/jfs/jfs_metapage.c39ed4d1a0e03ce Shivank Garg 2025-04-30 227 static int __metapage_migrate_folio(struct address_space *mapping, struct folio *dst, 39ed4d1a0e03ce Shivank Garg 2025-04-30 228 struct folio *src, enum migrate_mode mode) 39ed4d1a0e03ce Shivank Garg 2025-04-30 229 { 39ed4d1a0e03ce Shivank Garg 2025-04-30 230 struct metapage *mp; 39ed4d1a0e03ce Shivank Garg 2025-04-30 231 int page_offset; 39ed4d1a0e03ce Shivank Garg 2025-04-30 232 int rc; 39ed4d1a0e03ce Shivank Garg 2025-04-30 233 39ed4d1a0e03ce Shivank Garg 2025-04-30 234 mp = folio_to_mp(src, 0); 39ed4d1a0e03ce Shivank Garg 2025-04-30 @235 if (mp && metapage_locked(mp)) ^^If mp is NULL
39ed4d1a0e03ce Shivank Garg 2025-04-30 236 return -EAGAIN; 39ed4d1a0e03ce Shivank Garg 2025-04-30 237 39ed4d1a0e03ce Shivank Garg 2025-04-30 238 rc = filemap_migrate_folio(mapping, dst, src, mode); 39ed4d1a0e03ce Shivank Garg 2025-04-30 239 if (rc != MIGRATEPAGE_SUCCESS) 39ed4d1a0e03ce Shivank Garg 2025-04-30 240 return rc; 39ed4d1a0e03ce Shivank Garg 2025-04-30 241 39ed4d1a0e03ce Shivank Garg 2025-04-30 242 if (unlikely(insert_metapage(dst, mp))) 39ed4d1a0e03ce Shivank Garg 2025-04-30 243 return -EAGAIN; 39ed4d1a0e03ce Shivank Garg 2025-04-30 244 39ed4d1a0e03ce Shivank Garg 2025-04-30 @245 page_offset = mp->data - folio_address(src); ^^
Quoted 9 lines
Then this will crash.
39ed4d1a0e03ce Shivank Garg 2025-04-30 246 mp->data = folio_address(dst) + page_offset; 39ed4d1a0e03ce Shivank Garg 2025-04-30 247 mp->folio = dst; 39ed4d1a0e03ce Shivank Garg 2025-04-30 248 remove_metapage(src, mp); 39ed4d1a0e03ce Shivank Garg 2025-04-30 249 39ed4d1a0e03ce Shivank Garg 2025-04-30 250 return MIGRATEPAGE_SUCCESS; 39ed4d1a0e03ce Shivank Garg 2025-04-30 251 }
Hi Dan,
This is a false positive.
In metapage_migrate_folio(), it checks if (!src->private) and only calls __metapage_migrate_folio() if src->private is non-NULL
static int metapage_migrate_folio(struct address_space *mapping, struct folio *dst,
struct folio *src, enum migrate_mode mode)
{
...
if (!src->private) return filemap_migrate_folio(mapping, dst, src, mode); ... ... return __metapage_migrate_folio(mapping, dst, src, mode); }
Then __metapage_migrate_folio() calls mp = folio_to_mp(src, 0), which returns src->private. Which should not be NULL as previously checked.
I think we can skip the mp != NULL check from
Quoted 1 line
39ed4d1a0e03ceShivank Garg 2025-04-30 @235 if (mp &&metapage_locked(mp))
It seem redundant here.
Thanks,
Shivank
Andrew Morton
maintainerQuoted 16 lines · On Wed, 7 May 2025 17:19:52 +0530 Shivank Garg <[email protected]> wrote:
This is a false positive.
In
metapage_migrate_folio(), it checks if (!src->private) and only calls__metapage_migrate_folio()ifsrc->privateis non-NULLstatic int metapage_migrate_folio(struct
address_space*mapping, struct folio *dst,
struct folio *src, enummigrate_modemode)
{
...if (!src->private) return filemap_migrate_folio(mapping, dst, src, mode); ... ... return __metapage_migrate_folio(mapping, dst, src, mode); }
Quoted 6 lines
Then
__metapage_migrate_folio()calls mp =folio_to_mp(src, 0), which returnssrc->private. Which should not be NULL as previously checked.I think we can skip the mp != NULL check from
Quoted 1 line
39ed4d1a0e03ceShivank Garg 2025-04-30 @235 if (mp &&metapage_locked(mp))It seem redundant here.
Yes. Right now the code simply looks wrong:
mp = folio_to_mp(src, 0); if (mp && metapage_locked(mp)) return -EAGAIN; ... page_offset = mp->data - folio_address(src);
any reader of this will be worried. A code comment (at least) would save them concern and time. Reworking the code to eliminate doubt would be better.
Quoted 19 lines · Shivank Garg
Hi Dan,
This is a false positive.
In
metapage_migrate_folio(), it checks if (!src->private) and only calls__metapage_migrate_folio()ifsrc->privateis non-NULLstatic int metapage_migrate_folio(struct
address_space*mapping, struct folio *dst,
struct folio *src, enummigrate_modemode)
{
...if (!src->private) return filemap_migrate_folio(mapping, dst, src, mode); ... ... return __metapage_migrate_folio(mapping, dst, src, mode); }
Quoted 6 lines
Then
__metapage_migrate_folio()calls mp =folio_to_mp(src, 0), which returnssrc->private. Which should not be NULL as previously checked.I think we can skip the mp != NULL check from
Quoted 1 line
39ed4d1a0e03ceShivank Garg 2025-04-30 @235 if (mp &&metapage_locked(mp))It seem redundant here.
Yep. Just delete the unnecessary NULL check.
I only consider these a false positive if the NULL check is required but it doesn't lead to a crash. For example, there could be another condition which is equivalent to checking for NULL.
if (p) p->foo = 100; ... if (!p_valid(p)) return; p->foo = 99;
In this code, if you have the cross function database then Smatch doesn't print a warning because the NULL check is not required. But the cross function database is way too slow to use in the kbuild-bot.
regards,
dan carpenter
Quoted 58 lines · Dan Carpenter
Quoted 19 lines · Shivank Garg
Hi Dan,
This is a false positive.
In
metapage_migrate_folio(), it checks if (!src->private) and only calls__metapage_migrate_folio()ifsrc->privateis non-NULLstatic int metapage_migrate_folio(struct
address_space*mapping, struct folio *dst,
struct folio *src, enummigrate_modemode)
{
...if (!src->private) return filemap_migrate_folio(mapping, dst, src, mode); ... ... return __metapage_migrate_folio(mapping, dst, src, mode); }Quoted 6 lines
Then
__metapage_migrate_folio()calls mp =folio_to_mp(src, 0), which returnssrc->private. Which should not be NULL as previously checked.I think we can skip the mp != NULL check from
Quoted 1 line
39ed4d1a0e03ceShivank Garg 2025-04-30 @235 if (mp &&metapage_locked(mp))It seem redundant here.
Yep. Just delete the unnecessary NULL check.
I only consider these a false positive if the NULL check is required but it doesn't lead to a crash. For example, there could be another condition which is equivalent to checking for NULL.
if (p) p->foo = 100; ... if (!p_valid(p)) return; p->foo = 99;In this code, if you have the cross function database then Smatch doesn't print a warning because the NULL check is not required. But the cross function database is way too slow to use in the kbuild-bot.
regards,
dan carpenterYes. Right now the code simply looks wrong:
mp = folio_to_mp(src, 0); if (mp && metapage_locked(mp)) return -EAGAIN; ... page_offset = mp->data - folio_address(src);
Quoted 3 lines
any reader of this will be worried. A code comment (at least) would save them concern and time. Reworking the code to eliminate doubt would be better.
Hi Andrew, Dan,
Thank you for reviewing this and confirming. I've attached the patch for removing mp NULL check.
Best Regards,
Shivank
From 37688e00ed3e8c603778ee5271bb12ac11a32249 Mon Sep 17 00:00:00 2001
From: Shivank Garg <[email protected]>
Date: Thu, 8 May 2025 06:00:33 +0000
Subject: [PATCH] jfs: remove redundant NULL check in
__metapage_migrate_folio()
The NULL check for 'mp' is unnecessary since metapage_migrate_folio() already verifies src->private exists before calling this function.
--- fs/jfs/jfs_metapage.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/jfs/jfs_metapage.c +1 -1
@@ -238,7 +238,7 @@ static int __metapage_migrate_folio(struct address_space *mapping,int rc; mp =folio_to_mp (src,0 ); -if (mp &&metapage_locked (mp)) +if (metapage_locked (mp))return -EAGAIN ; rc =filemap_migrate_folio (mapping, dst, src, mode);
Signature · 1 line
2.34.1