[v2] fs/hugetlbfs/inode.c: mm/memory-failure.c: fix hugetlbfs hwpoison handling

Message ID 20240112180840.367006-1-sidhartha.kumar@oracle.com
State New
Headers
Series [v2] fs/hugetlbfs/inode.c: mm/memory-failure.c: fix hugetlbfs hwpoison handling |

Commit Message

Sidhartha Kumar Jan. 12, 2024, 6:08 p.m. UTC
  has_extra_refcount() makes the assumption that the page cache adds a ref
count of 1 and subtracts this in the extra_pins case. Commit a08c7193e4f1
(mm/filemap: remove hugetlb special casing in filemap.c) modifies
__filemap_add_folio() by calling folio_ref_add(folio, nr); for all cases
(including hugtetlb) where nr is the number of pages in the folio. We
should adjust the number of references coming from the page cache by
subtracing the number of pages rather than 1.

In hugetlbfs_read_iter(), folio_test_has_hwpoisoned() is testing the wrong
flag as, in the hugetlb case, memory-failure code calls
folio_test_set_hwpoison() to indicate poison. folio_test_hwpoison() is the
correct function to test for that flag.

After these fixes, the hugetlb hwpoison read selftest passes all cases.

Fixes: a08c7193e4f1 ("mm/filemap: remove hugetlb special casing in filemap.c")
Closes: https://lore.kernel.org/linux-mm/20230713001833.3778937-1-jiaqiyan@google.com/T/#m8e1469119e5b831bbd05d495f96b842e4a1c5519
Cc: <stable@vger.kernel.org> # 6.7+
Signed-off-by: Sidhartha Kumar <sidhartha.kumar@oracle.com>
Reported-by: Muhammad Usama Anjum <usama.anjum@collabora.com>
---

v1 -> v2:
    move ref_count adjustment to if(extra_pins) block as that represents
    ref counts from the page cache per Miaohe Lin.

 fs/hugetlbfs/inode.c | 2 +-
 mm/memory-failure.c  | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
  

Comments

Muhammad Usama Anjum Jan. 15, 2024, 7:49 a.m. UTC | #1
On 1/12/24 11:08 PM, Sidhartha Kumar wrote:
> has_extra_refcount() makes the assumption that the page cache adds a ref
> count of 1 and subtracts this in the extra_pins case. Commit a08c7193e4f1
> (mm/filemap: remove hugetlb special casing in filemap.c) modifies
> __filemap_add_folio() by calling folio_ref_add(folio, nr); for all cases
> (including hugtetlb) where nr is the number of pages in the folio. We
> should adjust the number of references coming from the page cache by
> subtracing the number of pages rather than 1.
> 
> In hugetlbfs_read_iter(), folio_test_has_hwpoisoned() is testing the wrong
> flag as, in the hugetlb case, memory-failure code calls
> folio_test_set_hwpoison() to indicate poison. folio_test_hwpoison() is the
> correct function to test for that flag.
> 
> After these fixes, the hugetlb hwpoison read selftest passes all cases.
> 
> Fixes: a08c7193e4f1 ("mm/filemap: remove hugetlb special casing in filemap.c")
> Closes: https://lore.kernel.org/linux-mm/20230713001833.3778937-1-jiaqiyan@google.com/T/#m8e1469119e5b831bbd05d495f96b842e4a1c5519
> Cc: <stable@vger.kernel.org> # 6.7+
> Signed-off-by: Sidhartha Kumar <sidhartha.kumar@oracle.com>
> Reported-by: Muhammad Usama Anjum <usama.anjum@collabora.com>
Tested-by: Muhammad Usama Anjum <usama.anjum@collabora.com>

> ---
> 
> v1 -> v2:
>     move ref_count adjustment to if(extra_pins) block as that represents
>     ref counts from the page cache per Miaohe Lin.
> 
>  fs/hugetlbfs/inode.c | 2 +-
>  mm/memory-failure.c  | 2 +-
>  2 files changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c
> index 36132c9125f9..3a248e4f7e93 100644
> --- a/fs/hugetlbfs/inode.c
> +++ b/fs/hugetlbfs/inode.c
> @@ -340,7 +340,7 @@ static ssize_t hugetlbfs_read_iter(struct kiocb *iocb, struct iov_iter *to)
>  		} else {
>  			folio_unlock(folio);
>  
> -			if (!folio_test_has_hwpoisoned(folio))
> +			if (!folio_test_hwpoison(folio))
>  				want = nr;
>  			else {
>  				/*
> diff --git a/mm/memory-failure.c b/mm/memory-failure.c
> index d8c853b35dbb..ef7ae73b65bd 100644
> --- a/mm/memory-failure.c
> +++ b/mm/memory-failure.c
> @@ -976,7 +976,7 @@ static bool has_extra_refcount(struct page_state *ps, struct page *p,
>  	int count = page_count(p) - 1;
>  
>  	if (extra_pins)
> -		count -= 1;
> +		count -= folio_nr_pages(page_folio(p));
>  
>  	if (count > 0) {
>  		pr_err("%#lx: %s still referenced by %d users\n",
  
Muchun Song Jan. 15, 2024, 12:02 p.m. UTC | #2
On 2024/1/13 02:08, Sidhartha Kumar wrote:
> has_extra_refcount() makes the assumption that the page cache adds a ref
> count of 1 and subtracts this in the extra_pins case. Commit a08c7193e4f1
> (mm/filemap: remove hugetlb special casing in filemap.c) modifies
> __filemap_add_folio() by calling folio_ref_add(folio, nr); for all cases
> (including hugtetlb) where nr is the number of pages in the folio. We
> should adjust the number of references coming from the page cache by
> subtracing the number of pages rather than 1.
>
> In hugetlbfs_read_iter(), folio_test_has_hwpoisoned() is testing the wrong
> flag as, in the hugetlb case, memory-failure code calls
> folio_test_set_hwpoison() to indicate poison. folio_test_hwpoison() is the
> correct function to test for that flag.
>
> After these fixes, the hugetlb hwpoison read selftest passes all cases.
>
> Fixes: a08c7193e4f1 ("mm/filemap: remove hugetlb special casing in filemap.c")
> Closes: https://lore.kernel.org/linux-mm/20230713001833.3778937-1-jiaqiyan@google.com/T/#m8e1469119e5b831bbd05d495f96b842e4a1c5519
> Cc: <stable@vger.kernel.org> # 6.7+
> Signed-off-by: Sidhartha Kumar <sidhartha.kumar@oracle.com>
> Reported-by: Muhammad Usama Anjum <usama.anjum@collabora.com>
> ---
>
> v1 -> v2:
>      move ref_count adjustment to if(extra_pins) block as that represents
>      ref counts from the page cache per Miaohe Lin.
>
>   fs/hugetlbfs/inode.c | 2 +-
>   mm/memory-failure.c  | 2 +-
>   2 files changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c
> index 36132c9125f9..3a248e4f7e93 100644
> --- a/fs/hugetlbfs/inode.c
> +++ b/fs/hugetlbfs/inode.c
> @@ -340,7 +340,7 @@ static ssize_t hugetlbfs_read_iter(struct kiocb *iocb, struct iov_iter *to)
>   		} else {
>   			folio_unlock(folio);
>   
> -			if (!folio_test_has_hwpoisoned(folio))
> +			if (!folio_test_hwpoison(folio))
>   				want = nr;
>   			else {
>   				/*
> diff --git a/mm/memory-failure.c b/mm/memory-failure.c
> index d8c853b35dbb..ef7ae73b65bd 100644
> --- a/mm/memory-failure.c
> +++ b/mm/memory-failure.c
> @@ -976,7 +976,7 @@ static bool has_extra_refcount(struct page_state *ps, struct page *p,
>   	int count = page_count(p) - 1;
>   
>   	if (extra_pins)
> -		count -= 1;
> +		count -= folio_nr_pages(page_folio(p));

IIUC, It seems than this also fix shmem of THP, because 
has_extra_refcount() is called
from me_pagecache_clean(), right?

>   
>   	if (count > 0) {
>   		pr_err("%#lx: %s still referenced by %d users\n",
  
Matthew Wilcox Jan. 15, 2024, 3:08 p.m. UTC | #3
On Mon, Jan 15, 2024 at 08:02:51PM +0800, Muchun Song wrote:
> On 2024/1/13 02:08, Sidhartha Kumar wrote:
> > has_extra_refcount() makes the assumption that the page cache adds a ref
> > count of 1 and subtracts this in the extra_pins case. Commit a08c7193e4f1
> > (mm/filemap: remove hugetlb special casing in filemap.c) modifies
> > __filemap_add_folio() by calling folio_ref_add(folio, nr); for all cases
> > (including hugtetlb) where nr is the number of pages in the folio. We
> > should adjust the number of references coming from the page cache by
> > subtracing the number of pages rather than 1.
> > 
> > In hugetlbfs_read_iter(), folio_test_has_hwpoisoned() is testing the wrong
> > flag as, in the hugetlb case, memory-failure code calls
> > folio_test_set_hwpoison() to indicate poison. folio_test_hwpoison() is the
> > correct function to test for that flag.
> > 
> > After these fixes, the hugetlb hwpoison read selftest passes all cases.
> > 
> > Fixes: a08c7193e4f1 ("mm/filemap: remove hugetlb special casing in filemap.c")
> > Closes: https://lore.kernel.org/linux-mm/20230713001833.3778937-1-jiaqiyan@google.com/T/#m8e1469119e5b831bbd05d495f96b842e4a1c5519
> > Cc: <stable@vger.kernel.org> # 6.7+
> > Signed-off-by: Sidhartha Kumar <sidhartha.kumar@oracle.com>
> > Reported-by: Muhammad Usama Anjum <usama.anjum@collabora.com>
> > ---
> > 
> > v1 -> v2:
> >      move ref_count adjustment to if(extra_pins) block as that represents
> >      ref counts from the page cache per Miaohe Lin.
> > 
> >   fs/hugetlbfs/inode.c | 2 +-
> >   mm/memory-failure.c  | 2 +-
> >   2 files changed, 2 insertions(+), 2 deletions(-)
> > 
> > diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c
> > index 36132c9125f9..3a248e4f7e93 100644
> > --- a/fs/hugetlbfs/inode.c
> > +++ b/fs/hugetlbfs/inode.c
> > @@ -340,7 +340,7 @@ static ssize_t hugetlbfs_read_iter(struct kiocb *iocb, struct iov_iter *to)
> >   		} else {
> >   			folio_unlock(folio);
> > -			if (!folio_test_has_hwpoisoned(folio))
> > +			if (!folio_test_hwpoison(folio))
> >   				want = nr;
> >   			else {
> >   				/*
> > diff --git a/mm/memory-failure.c b/mm/memory-failure.c
> > index d8c853b35dbb..ef7ae73b65bd 100644
> > --- a/mm/memory-failure.c
> > +++ b/mm/memory-failure.c
> > @@ -976,7 +976,7 @@ static bool has_extra_refcount(struct page_state *ps, struct page *p,
> >   	int count = page_count(p) - 1;
> >   	if (extra_pins)
> > -		count -= 1;
> > +		count -= folio_nr_pages(page_folio(p));
> 
> IIUC, It seems than this also fix shmem of THP, because has_extra_refcount()
> is called
> from me_pagecache_clean(), right?

Usually we split THPs before we get to this point, but we always poison
an entire hugetlb page.  This should probably change, but it's not high
on my list of things to do.
  
Muchun Song Jan. 16, 2024, 2:43 a.m. UTC | #4
> On Jan 13, 2024, at 02:08, Sidhartha Kumar <sidhartha.kumar@oracle.com> wrote:
> 
> has_extra_refcount() makes the assumption that the page cache adds a ref
> count of 1 and subtracts this in the extra_pins case. Commit a08c7193e4f1
> (mm/filemap: remove hugetlb special casing in filemap.c) modifies
> __filemap_add_folio() by calling folio_ref_add(folio, nr); for all cases
> (including hugtetlb) where nr is the number of pages in the folio. We
> should adjust the number of references coming from the page cache by
> subtracing the number of pages rather than 1.
> 
> In hugetlbfs_read_iter(), folio_test_has_hwpoisoned() is testing the wrong
> flag as, in the hugetlb case, memory-failure code calls
> folio_test_set_hwpoison() to indicate poison. folio_test_hwpoison() is the
> correct function to test for that flag.
> 
> After these fixes, the hugetlb hwpoison read selftest passes all cases.
> 
> Fixes: a08c7193e4f1 ("mm/filemap: remove hugetlb special casing in filemap.c")
> Closes: https://lore.kernel.org/linux-mm/20230713001833.3778937-1-jiaqiyan@google.com/T/#m8e1469119e5b831bbd05d495f96b842e4a1c5519
> Cc: <stable@vger.kernel.org> # 6.7+
> Signed-off-by: Sidhartha Kumar <sidhartha.kumar@oracle.com>
> Reported-by: Muhammad Usama Anjum <usama.anjum@collabora.com>

Acked-by: Muchun Song <muchun.song@linux.dev>

Thanks.
  

Patch

diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c
index 36132c9125f9..3a248e4f7e93 100644
--- a/fs/hugetlbfs/inode.c
+++ b/fs/hugetlbfs/inode.c
@@ -340,7 +340,7 @@  static ssize_t hugetlbfs_read_iter(struct kiocb *iocb, struct iov_iter *to)
 		} else {
 			folio_unlock(folio);
 
-			if (!folio_test_has_hwpoisoned(folio))
+			if (!folio_test_hwpoison(folio))
 				want = nr;
 			else {
 				/*
diff --git a/mm/memory-failure.c b/mm/memory-failure.c
index d8c853b35dbb..ef7ae73b65bd 100644
--- a/mm/memory-failure.c
+++ b/mm/memory-failure.c
@@ -976,7 +976,7 @@  static bool has_extra_refcount(struct page_state *ps, struct page *p,
 	int count = page_count(p) - 1;
 
 	if (extra_pins)
-		count -= 1;
+		count -= folio_nr_pages(page_folio(p));
 
 	if (count > 0) {
 		pr_err("%#lx: %s still referenced by %d users\n",