mm/huge_memory: prevent potential NULL pointer dereference
Subsystems (2)
RFC 1/1 mm/huge_memory: prevent potential NULL pointer dereference
I just found this issue in the last linux-next Coverity report and it caught my attention.
I am not familiar with this code, therefore I am sending this patch as RFC because I am not 100% sure whether this is a false positive or not.
However, it seems potentially legit to me:
In __folio_split(), when looping over folios we dereference mapping before ensuring it is non-NULL.
Following code in the loop body performs such check, thus suggesting that mapping may be NULL and accessing it without any check may be dangerous.
Add NULL check before passing it to shmem_mapping().
Addresses-Coverity-ID: 1647614 ("FORWARD_NULL")
--- mm/huge_memory.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
mm/huge_memory.c +1 -1
@@ -3802,7 +3802,7 @@ static int __folio_split(struct folio *folio, unsigned int new_order,/* Some pages can be beyond EOF: drop them from cache */ if (new_folio->index >= end) { -if (shmem_mapping (mapping)) +if (mapping &&shmem_mapping (mapping)) nr_shmem_dropped +=folio_nr_pages (new_folio);else if (folio_test_clear_dirty (new_folio)) folio_account_cleaned(
Signature · 1 line
2.49.1
Quoted 14 lines · Antonio Quartulli
I just found this issue in the last linux-next Coverity report and it caught my attention.
I am not familiar with this code, therefore I am sending this patch as RFC because I am not 100% sure whether this is a false positive or not.
However, it seems potentially legit to me:In
__folio_split(), when looping over folios we dereferencemappingbefore ensuring it is non-NULL.Following code in the loop body performs such check, thus suggesting that
mappingmay be NULL and accessing it without any check may be dangerous.
Quoted 6 lines
Add NULL check before passing it to
shmem_mapping().Cc Zi Yan <[email protected]>Fixes 00527733d0dc ("mm/huge_memory: add two new (not yet used) functions for folio_split()")Addresses-Coverity-ID: 1647614 ("
FORWARD_NULL")Signed-off-by Antonio Quartulli <[email protected]>
Thanks for the patch :) but sorry it is in practice not a thing that can happen. See below for analysis.
mm/huge_memory.c static int __folio_split(struct folio *folio, unsigned int new_order, · line 3804
Quoted 12 lines
--- mm/huge_memory.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)mm/huge_memory.c +0
-0@@ -3802,7 +3802,7 @@ static int __folio_split(struct folio *folio, unsigned int new_order,/* Some pages can be beyond EOF: drop them from cache */ if (new_folio->index >= end) {
It's kind of not _hugely_ clear but, if anon - which implies no mapping - we set end == -1.
At which point this condition cannot evaluate true (index is at page granularity so even MAX_UINT64 would be page shifted and still not equal -1).
Under all other circumstances, mapping will be non-NULL.
Quoted 9 lines
mm/huge_memory.c +1
-2-if (shmem_mapping (mapping)) +if (mapping &&shmem_mapping (mapping)) nr_shmem_dropped +=folio_nr_pages (new_folio);else if (folio_test_clear_dirty (new_folio)) folio_account_cleaned( --2.49.1
Cheers, Lorenzo
Hi Lorenzo,
Quoted 6 lines · Lorenzo Stoakes
Quoted 6 lines
mm/huge_memory.c +0
-0@@ -3802,7 +3802,7 @@ static int __folio_split(struct folio *folio, unsigned int new_order,
Quoted 8 lines
mm/huge_memory.c static int __folio_split(struct folio *folio, unsigned int new_order, · line 3804
Quoted 2 lines
mm/huge_memory.c +0
-0/* Some pages can be beyond EOF: drop them from cache */ if (new_folio->index >= end) {It's kind of not _hugely_ clear but, if anon - which implies no mapping - we set end == -1.
At which point this condition cannot evaluate true (index is at page granularity so even
MAX_UINT64would be page shifted and still not equal -1).
I may be missing something, but why can't "index >= -1" be true?
In any case, with Zi's patch the NULL check comes before the rest so the problem does not exist anymore there.
Regards,
Quoted 14 lines
Under all other circumstances, mapping will be non-NULL.
Quoted 9 lines
mm/huge_memory.c +1
-2-if (shmem_mapping (mapping)) +if (mapping &&shmem_mapping (mapping)) nr_shmem_dropped +=folio_nr_pages (new_folio);else if (folio_test_clear_dirty (new_folio)) folio_account_cleaned( --2.49.1
Cheers, Lorenzo
Signature · 5 lines
Antonio Quartulli CEO and Co-Founder Mandelbit Srl https://www.mandelbit.com
Quoted 15 lines · Antonio Quartulli
Hi Lorenzo,
Quoted 12 lines · Lorenzo Stoakes
mm/huge_memory.c static int __folio_split(struct folio *folio, unsigned int new_order, · line 3804
Quoted 8 lines
mm/huge_memory.c +0
-0@@ -3802,7 +3802,7 @@ static int __folio_split(struct folio *folio, unsigned int new_order,/* Some pages can be beyond EOF: drop them from cache */ if (new_folio->index >= end) {It's kind of not _hugely_ clear but, if anon - which implies no mapping - we set end == -1.
Quoted 4 lines
Quoted 2 lines
At which point this condition cannot evaluate true (index is at page granularity so even
MAX_UINT64would be page shifted and still not equal -1).I may be missing something, but why can't "index >= -1" be true?
These are unsigned long's, -1 in two's complement this means -1 translates to the maximum possible unsigned long. Index cannot == the maximum number as this is a _page_ index so a folio would need to reference a page larger than could be represented in a 64-bit system for that to be so.
Quoted 29 lines
In any case, with Zi's patch the NULL check comes before the rest so the problem does not exist anymore there.
Regards,
Quoted 14 lines
Under all other circumstances, mapping will be non-NULL.
Quoted 9 lines
mm/huge_memory.c +1
-2-if (shmem_mapping (mapping)) +if (mapping &&shmem_mapping (mapping)) nr_shmem_dropped +=folio_nr_pages (new_folio);else if (folio_test_clear_dirty (new_folio)) folio_account_cleaned( --2.49.1
Cheers, Lorenzo
--
Antonio QuartulliCEO and Co-Founder
Mandelbit Srl
https://www.mandelbit.com
Quoted 19 lines · Lorenzo Stoakes
Quoted 18 lines · Antonio Quartulli
Hi Lorenzo,
Quoted 14 lines · Lorenzo Stoakes
mm/huge_memory.c static int __folio_split(struct folio *folio, unsigned int new_order, · line 3804
Quoted 8 lines
mm/huge_memory.c +0
-0@@ -3802,7 +3802,7 @@ static int __folio_split(struct folio *folio, unsigned int new_order,/* Some pages can be beyond EOF: drop them from cache */ if (new_folio->index >= end) {It's kind of not _hugely_ clear but, if anon - which implies no mapping - we set end == -1.
At which point this condition cannot evaluate true (index is at page granularity so even
MAX_UINT64would be page shifted and still not equal -1).
Quoted 6 lines
Quoted 1 line
I may be missing something, but why can't "index >= -1" be true?
These are unsigned long's, -1 in two's complement this means -1 translates to the maximum possible unsigned long. Index cannot == the maximum number as this is a _page_ index so a folio would need to reference a page larger than could be represented in a 64-bit system for that to be so.
Got it!
Thanks a lot for the clarification.
Cheers,
Signature · 5 lines
Antonio Quartulli CEO and Co-Founder Mandelbit Srl https://www.mandelbit.com
Zi Yan
reviewerQuoted 29 lines · Antonio Quartulli
I just found this issue in the last linux-next Coverity report and it caught my attention.
I am not familiar with this code, therefore I am sending this patch as RFC because I am not 100% sure whether this is a false positive or not.
However, it seems potentially legit to me:In
__folio_split(), when looping over folios we dereferencemappingbefore ensuring it is non-NULL.Following code in the loop body performs such check, thus suggesting that
mappingmay be NULL and accessing it without any check may be dangerous.Add NULL check before passing it to
shmem_mapping().Cc Zi Yan <[email protected]>Fixes 00527733d0dc ("mm/huge_memory: add two new (not yet used) functions for folio_split()")Addresses-Coverity-ID: 1647614 ("
FORWARD_NULL")Signed-off-by Antonio Quartulli <[email protected]>--- mm/huge_memory.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)mm/huge_memory.c +0
-0
mm/huge_memory.c static int __folio_split(struct folio *folio, unsigned int new_order, · line 3808
Quoted 8 lines
mm/huge_memory.c +1
-1@@ -3802,7 +3802,7 @@ static int __folio_split(struct folio *folio, unsigned int new_order,/* Some pages can be beyond EOF: drop them from cache */ if (new_folio->index >= end) { -if (shmem_mapping (mapping)) +if (mapping &&shmem_mapping (mapping)) nr_shmem_dropped +=folio_nr_pages (new_folio);else if (folio_test_clear_dirty (new_folio)) folio_account_cleaned(
Hi Antonio,
Is there a way of preventing Coverity/sparse from checking certain code? This non-NULL mapping issue pops up every time I touch the code.
Dan Carpenter reported the issue yesterday and I explained it is no issue[1]. The same report showed up when I added __split_unmapped_folio() back in February[2]
[1] https://lore.kernel.org/oe-kbuild/[email protected]/
[2] https://lore.kernel.org/linux-mm/[email protected]/
I wonder how many times I need to explain this, although I appreciate your effort to improve kernel.
Best Regards,
Yan, Zi
Quoted 41 lines · Zi Yan
Quoted 29 lines · Antonio Quartulli
I just found this issue in the last linux-next Coverity report and it caught my attention.
I am not familiar with this code, therefore I am sending this patch as RFC because I am not 100% sure whether this is a false positive or not.
However, it seems potentially legit to me:In
__folio_split(), when looping over folios we dereferencemappingbefore ensuring it is non-NULL.Following code in the loop body performs such check, thus suggesting that
mappingmay be NULL and accessing it without any check may be dangerous.Add NULL check before passing it to
shmem_mapping().Cc Zi Yan <[email protected]>Fixes 00527733d0dc ("mm/huge_memory: add two new (not yet used) functions for folio_split()")Addresses-Coverity-ID: 1647614 ("
FORWARD_NULL")Signed-off-by Antonio Quartulli <[email protected]>--- mm/huge_memory.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)mm/huge_memory.c +0
-0mm/huge_memory.c static int __folio_split(struct folio *folio, unsigned int new_order, · line 3808
Quoted 8 lines
mm/huge_memory.c +1
-1@@ -3802,7 +3802,7 @@ static int __folio_split(struct folio *folio, unsigned int new_order,/* Some pages can be beyond EOF: drop them from cache */ if (new_folio->index >= end) { -if (shmem_mapping (mapping)) +if (mapping &&shmem_mapping (mapping)) nr_shmem_dropped +=folio_nr_pages (new_folio);else if (folio_test_clear_dirty (new_folio)) folio_account_cleaned(Hi Antonio,
Quoted 2 lines
Is there a way of preventing Coverity/sparse from checking certain code? This non-NULL mapping issue pops up every time I touch the code.
Probably we could make that code more readable and achieve it:
mm/huge_memory.c +15 -14
@@ -3775,6 +3775,7 @@ static int __folio_split(struct folio *folio, unsigned int new_order,*/ for (new_folio =folio_next (folio); new_folio != next_folio; new_folio = next) { +unsigned long nr_pages =folio_nr_pages (new_folio); next =folio_next (new_folio); expected_refs =folio_expected_ref_count (new_folio) +1 ;
@@ -3782,20 +3783,20 @@ static int __folio_split(struct folio *folio, unsigned int new_order,lru_add_split_folio (folio, new_folio, lruvec, list); -/* Some pages can be beyond EOF: drop them from cache */ -if (new_folio->index >= end) { -if (shmem_mapping (mapping)) - nr_shmem_dropped +=folio_nr_pages (new_folio); -else if (folio_test_clear_dirty (new_folio)) -folio_account_cleaned ( - new_folio, -inode_to_wb (mapping->host)); -__filemap_remove_folio (new_folio,NULL ); -folio_put_refs (new_folio, -folio_nr_pages (new_folio)); - }else if (mapping) { -__xa_store (&mapping->i_pages, new_folio->index, - new_folio,0 ); +/* Add the new folio to the page cache ... */ +if (mapping) { +/* ... however, drop folios beyond EOF. */ +if (new_folio->index >= end) { +if (shmem_mapping (mapping)) + nr_shmem_dropped += nr_pages; +else if (folio_test_clear_dirty (new_folio)) +folio_account_cleaned (new_folio,inode_to_wb (mapping->host)); +__filemap_remove_folio (new_folio,NULL ); +folio_put_refs (new_folio, nr_pages); + }else { +__xa_store (&mapping->i_pages, + new_folio->index, new_folio,0 ); + } }else if (swap_cache) { __xa_store(&swap_cache->i_pages, swap_cache_index(new_folio->swap),
Having that logic in a helper would be cleaner, but not straight-forward due to things like nr_shmem_dropped.
Signature · 3 lines
Cheers, David / dhildenb
Zi Yan
reviewerQuoted 93 lines · David Hildenbrand
Quoted 41 lines · Zi Yan
Quoted 29 lines · Antonio Quartulli
I just found this issue in the last linux-next Coverity report and it caught my attention.
I am not familiar with this code, therefore I am sending this patch as RFC because I am not 100% sure whether this is a false positive or not.
However, it seems potentially legit to me:In
__folio_split(), when looping over folios we dereferencemappingbefore ensuring it is non-NULL.Following code in the loop body performs such check, thus suggesting that
mappingmay be NULL and accessing it without any check may be dangerous.Add NULL check before passing it to
shmem_mapping().Cc Zi Yan <[email protected]>Fixes 00527733d0dc ("mm/huge_memory: add two new (not yet used) functions for folio_split()")Addresses-Coverity-ID: 1647614 ("
FORWARD_NULL")Signed-off-by Antonio Quartulli <[email protected]>--- mm/huge_memory.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)mm/huge_memory.c +0
-0mm/huge_memory.c static int __folio_split(struct folio *folio, unsigned int new_order, · line 3808
Quoted 8 lines
mm/huge_memory.c +1
-1@@ -3802,7 +3802,7 @@ static int __folio_split(struct folio *folio, unsigned int new_order,/* Some pages can be beyond EOF: drop them from cache */ if (new_folio->index >= end) { -if (shmem_mapping (mapping)) +if (mapping &&shmem_mapping (mapping)) nr_shmem_dropped +=folio_nr_pages (new_folio);else if (folio_test_clear_dirty (new_folio)) folio_account_cleaned(Hi Antonio,
Quoted 2 lines
Is there a way of preventing Coverity/sparse from checking certain code? This non-NULL mapping issue pops up every time I touch the code.
Probably we could make that code more readable and achieve it:
mm/huge_memory.c +15
-13@@ -3775,6 +3775,7 @@ static int __folio_split(struct folio *folio, unsigned int new_order,*/ for (new_folio =folio_next (folio); new_folio != next_folio; new_folio = next) { +unsigned long nr_pages =folio_nr_pages (new_folio); next =folio_next (new_folio); expected_refs =folio_expected_ref_count (new_folio) +1 ;@@ -3782,20 +3783,20 @@ static int __folio_split(struct folio *folio, unsigned int new_order,lru_add_split_folio (folio, new_folio, lruvec, list); -/* Some pages can be beyond EOF: drop them from cache */ - if (new_folio->index >= end) { -if (shmem_mapping (mapping)) - nr_shmem_dropped +=folio_nr_pages (new_folio); -else if (folio_test_clear_dirty (new_folio)) -folio_account_cleaned ( - new_folio, -inode_to_wb (mapping->host)); -__filemap_remove_folio (new_folio,NULL ); -folio_put_refs (new_folio, -folio_nr_pages (new_folio)); - } else if (mapping) { -__xa_store (&mapping->i_pages, new_folio->index, - new_folio,0 ); +/* Add the new folio to the page cache ... */ + if (mapping) { +/* ... however, drop folios beyond EOF. */ +if (new_folio->index >= end) { +if (shmem_mapping (mapping)) + nr_shmem_dropped += nr_pages; +else if (folio_test_clear_dirty (new_folio)) +folio_account_cleaned (new_folio,inode_to_wb (mapping->host)); +__filemap_remove_folio (new_folio,NULL ); +folio_put_refs (new_folio, nr_pages); + }else { +__xa_store (&mapping->i_pages, + new_folio->index, new_folio,0 ); + } } else if (swap_cache) { __xa_store(&swap_cache->i_pages, swap_cache_index(new_folio->swap),
Quoted 2 lines
Having that logic in a helper would be cleaner, but not straight-forward due to things like
nr_shmem_dropped.
Looks good to me. Do you want to send a patch? Otherwise, I can send one.
Best Regards,
Yan, Zi
Quoted 95 lines · Zi Yan
Quoted 93 lines · David Hildenbrand
Quoted 41 lines · Zi Yan
Quoted 29 lines · Antonio Quartulli
I just found this issue in the last linux-next Coverity report and it caught my attention.
I am not familiar with this code, therefore I am sending this patch as RFC because I am not 100% sure whether this is a false positive or not.
However, it seems potentially legit to me:In
__folio_split(), when looping over folios we dereferencemappingbefore ensuring it is non-NULL.Following code in the loop body performs such check, thus suggesting that
mappingmay be NULL and accessing it without any check may be dangerous.Add NULL check before passing it to
shmem_mapping().Cc Zi Yan <[email protected]>Fixes 00527733d0dc ("mm/huge_memory: add two new (not yet used) functions for folio_split()")Addresses-Coverity-ID: 1647614 ("
FORWARD_NULL")Signed-off-by Antonio Quartulli <[email protected]>--- mm/huge_memory.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)mm/huge_memory.c +0
-0mm/huge_memory.c static int __folio_split(struct folio *folio, unsigned int new_order, · line 3808
Quoted 8 lines
mm/huge_memory.c +1
-1@@ -3802,7 +3802,7 @@ static int __folio_split(struct folio *folio, unsigned int new_order,/* Some pages can be beyond EOF: drop them from cache */ if (new_folio->index >= end) { -if (shmem_mapping (mapping)) +if (mapping &&shmem_mapping (mapping)) nr_shmem_dropped +=folio_nr_pages (new_folio);else if (folio_test_clear_dirty (new_folio)) folio_account_cleaned(Hi Antonio,
Quoted 2 lines
Is there a way of preventing Coverity/sparse from checking certain code? This non-NULL mapping issue pops up every time I touch the code.
Probably we could make that code more readable and achieve it:
mm/huge_memory.c +15
-13@@ -3775,6 +3775,7 @@ static int __folio_split(struct folio *folio, unsigned int new_order,*/ for (new_folio =folio_next (folio); new_folio != next_folio; new_folio = next) { +unsigned long nr_pages =folio_nr_pages (new_folio); next =folio_next (new_folio); expected_refs =folio_expected_ref_count (new_folio) +1 ;@@ -3782,20 +3783,20 @@ static int __folio_split(struct folio *folio, unsigned int new_order,lru_add_split_folio (folio, new_folio, lruvec, list); -/* Some pages can be beyond EOF: drop them from cache */ - if (new_folio->index >= end) { -if (shmem_mapping (mapping)) - nr_shmem_dropped +=folio_nr_pages (new_folio); -else if (folio_test_clear_dirty (new_folio)) -folio_account_cleaned ( - new_folio, -inode_to_wb (mapping->host)); -__filemap_remove_folio (new_folio,NULL ); -folio_put_refs (new_folio, -folio_nr_pages (new_folio)); - } else if (mapping) { -__xa_store (&mapping->i_pages, new_folio->index, - new_folio,0 ); +/* Add the new folio to the page cache ... */ + if (mapping) { +/* ... however, drop folios beyond EOF. */ +if (new_folio->index >= end) { +if (shmem_mapping (mapping)) + nr_shmem_dropped += nr_pages; +else if (folio_test_clear_dirty (new_folio)) +folio_account_cleaned (new_folio,inode_to_wb (mapping->host)); +__filemap_remove_folio (new_folio,NULL ); +folio_put_refs (new_folio, nr_pages); + }else { +__xa_store (&mapping->i_pages, + new_folio->index, new_folio,0 ); + } } else if (swap_cache) { __xa_store(&swap_cache->i_pages, swap_cache_index(new_folio->swap),
Quoted 4 lines
Quoted 2 lines
Having that logic in a helper would be cleaner, but not straight-forward due to things like
nr_shmem_dropped.Looks good to me. Do you want to send a patch? Otherwise, I can send one.
Please send on (you'll manage to clean this up further).
Thanks!
Signature · 3 lines
Cheers, David / dhildenb
Quoted 48 lines · Zi Yan
Quoted 29 lines · Antonio Quartulli
I just found this issue in the last linux-next Coverity report and it caught my attention.
I am not familiar with this code, therefore I am sending this patch as RFC because I am not 100% sure whether this is a false positive or not.
However, it seems potentially legit to me:In
__folio_split(), when looping over folios we dereferencemappingbefore ensuring it is non-NULL.Following code in the loop body performs such check, thus suggesting that
mappingmay be NULL and accessing it without any check may be dangerous.Add NULL check before passing it to
shmem_mapping().Cc Zi Yan <[email protected]>Fixes 00527733d0dc ("mm/huge_memory: add two new (not yet used) functions for folio_split()")Addresses-Coverity-ID: 1647614 ("
FORWARD_NULL")Signed-off-by Antonio Quartulli <[email protected]>--- mm/huge_memory.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)mm/huge_memory.c +0
-0mm/huge_memory.c static int __folio_split(struct folio *folio, unsigned int new_order, · line 3808
Quoted 8 lines
mm/huge_memory.c +1
-1@@ -3802,7 +3802,7 @@ static int __folio_split(struct folio *folio, unsigned int new_order,/* Some pages can be beyond EOF: drop them from cache */ if (new_folio->index >= end) { -if (shmem_mapping (mapping)) +if (mapping &&shmem_mapping (mapping)) nr_shmem_dropped +=folio_nr_pages (new_folio);else if (folio_test_clear_dirty (new_folio)) folio_account_cleaned(Hi Antonio,
Is there a way of preventing Coverity/sparse from checking certain code? This non-NULL mapping issue pops up every time I touch the code.
Dan Carpenter reported the issue yesterday and I explained it is no issue[1]. The same report showed up when I added
__split_unmapped_folio()back in February[2]
Quoted 5 lines
[1] https://lore.kernel.org/oe-kbuild/[email protected]/
[2] https://lore.kernel.org/linux-mm/[email protected]/I wonder how many times I need to explain this, although I appreciate your effort to improve kernel.
The way to disable static analysis is to wrap the line or function in #ifndef __CHECKER__. This is a bad option. We could probably count how many places do this using one set of fingers and toes. We don't like defines in .c files. I hate when static analysis makes the code messier and worse. We always say to fix the checker and leave the code alone.
We generally try to only report warnings once, but in this case you changed the name of the function so it shows up as new. All the scripts say if you change the name of the file or the function that's a new warning. When I'm searching for duplicate reports on lore, I use the name of the function as well. Plus, I looked at the surrounding code and everything on my screen was from Jul 7 so I assumed it was new.
The best option is to add a comment to the code. The next best option is to just endure the occasional false positive.
One idea might be to add a hash tag to static checker warnings/patches so we can manually search lore.kernel.org for the filename and hash tag before sending new bug reports/fixes.
regards,
dan carpenter