Skip to contentSkip to conversation

mm/hugetlb: fix memory offline failure due to hwpoisoned file hugetlb

PATCH Superseded Jinjiang Tu 2 participants 6 messages Mar 18 – Mar 20

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm/hugetlb: fix memory offline failure due to hwpoisoned file hugetlb

When a file hugetlb folio triggers UCE, me_huge_page() will keep the hugetlb folio in pagcahe with refcount increased and PG_hwpoison set. Even after the hugetlb file is deleted, the hugetlb folio is still leaked.

If we want to offline the memory block that the hwpoisoned hugetlb folio belongs to, it fails in dissolve_free_hugetlb_folios() due to the hwpoisoned hugetlb folio isn't free.

I can reproduce this issue with the following steps in qemu:
1) echo offline >/sys/devices/system/memory/auto_online_blocks
2) in qemu monitor:

       object_add memory-backend-ram,id=mem10,size=1G
       device_add pc-dimm,id=dimm1,memdev=mem10,node=2

3) echo online_movable > /sys/devices/system/node/node2/memory136/state
4) echo 5 > /sys/devices/system/node/node2/hugepages/hugepages-2048kB/nr_hugepages
5) run ./hugetlb_file. This process will receive SIGBUS.
6) remove the hugetlbfs file.
7) echo offline > /sys/devices/system/node/node2/memory136/state

hugetlb_file.c:
fd = open("/dev/hugepages/my_hugepage_file", O_CREAT | O_RDWR, 0755); fallocate(fd, 0, 0, HUGEPAGE_SIZE * 2);
addr = mmap(NULL, HUGEPAGE_SIZE * 2, PROT_READ | PROT_WRITE,

		MAP_SHARED | MAP_HUGETLB, fd, 0);

memset(addr, 0xaa, HUGEPAGE_SIZE * 2);
madvise(addr, HUGEPAGE_SIZE, MADV_HWPOISON);

To fix it, when deleting hugetlb folio from pagecache, mark the hugetlb folio temporary, and put the refcount increased by memory-failure. After the hugetlb folio is deleted from pagecache, the refcount is decreased to zero and the hugetlb folio is dissolved.

---
 fs/hugetlbfs/inode.c | 5 +++++
 1 file changed, 5 insertions(+)
fs/hugetlbfs/inode.c +5 -0
index 3f70c47981de..6bebe2e67f3e 100644
@@ -603,6 +603,11 @@ static void remove_inode_hugepages(struct inode *inode, loff_t lstart,
 							index, truncate_op))
 				freed++;
 
+			if (unlikely(folio_test_hwpoison(folio))) {
+				folio_set_hugetlb_temporary(folio);
+				folio_put(folio);
+			}
+
 			mutex_unlock(&hugetlb_fault_mutex_table[hash]);
 		}
 		folio_batch_release(&fbatch);
Signature · 1 line
2.43.0
#1 lore raw
Quoted 23 lines · Jinjiang Tu

When a file hugetlb folio triggers UCE, me_huge_page() will keep the hugetlb folio in pagcahe with refcount increased and PG_hwpoison set. Even after the hugetlb file is deleted, the hugetlb folio is still leaked.

If we want to offline the memory block that the hwpoisoned hugetlb folio belongs to, it fails in dissolve_free_hugetlb_folios() due to the hwpoisoned hugetlb folio isn't free.

I can reproduce this issue with the following steps in qemu:
1) echo offline >/sys/devices/system/memory/auto_online_blocks
2) in qemu monitor:

       object_add memory-backend-ram,id=mem10,size=1G
       device_add pc-dimm,id=dimm1,memdev=mem10,node=2

3) echo online_movable > /sys/devices/system/node/node2/memory136/state
4) echo 5 > /sys/devices/system/node/node2/hugepages/hugepages-2048kB/nr_hugepages
5) run ./hugetlb_file. This process will receive SIGBUS.
6) remove the hugetlbfs file.
7) echo offline > /sys/devices/system/node/node2/memory136/state

hugetlb_file.c:
fd = open("/dev/hugepages/my_hugepage_file", O_CREAT | O_RDWR, 0755); fallocate(fd, 0, 0, HUGEPAGE_SIZE * 2);
addr = mmap(NULL, HUGEPAGE_SIZE * 2, PROT_READ | PROT_WRITE,

Quoted 8 lines
		MAP_SHARED | MAP_HUGETLB, fd, 0);

memset(addr, 0xaa, HUGEPAGE_SIZE * 2);
madvise(addr, HUGEPAGE_SIZE, MADV_HWPOISON);

To fix it, when deleting hugetlb folio from pagecache, mark the hugetlb folio temporary, and put the refcount increased by memory-failure. After the hugetlb folio is deleted from pagecache, the refcount is decreased to zero and the hugetlb folio is dissolved.

Thanks for your patch.

Quoted 11 lines
---
 fs/hugetlbfs/inode.c | 5 +++++
 1 file changed, 5 insertions(+)
fs/hugetlbfs/inode.c +0 -0
index 3f70c47981de..6bebe2e67f3e 100644

fs/hugetlbfs/inode.c static void remove_inode_hugepages(struct inode *inode, loff_t lstart, · line 607

Quoted 5 lines
fs/hugetlbfs/inode.c +2 -0
@@ -603,6 +603,11 @@ static void remove_inode_hugepages(struct inode *inode, loff_t lstart,
 							index, truncate_op))
 				freed++;
 
+			if (unlikely(folio_test_hwpoison(folio))) {
+				folio_set_hugetlb_temporary(folio);

I think it is not needed to mark the hugetlb folio as temporary because offline_pages() will call dissolve_free_hugetlb_folios().

fs/hugetlbfs/inode.c static void remove_inode_hugepages(struct inode *inode, loff_t lstart, · line 608

Quoted 1 line
fs/hugetlbfs/inode.c +1 -0
+				folio_put(folio);

__get_huge_page_for_hwpoison() will always set hwpoison for hugetlb folio even without page refcnt increased. So this folio_put() might be unexpected. Please see [1] for detail.

Thanks.
.

#2 lore raw
Quoted 55 lines · Miaohe Lin
Quoted 23 lines · Jinjiang Tu

When a file hugetlb folio triggers UCE, me_huge_page() will keep the hugetlb folio in pagcahe with refcount increased and PG_hwpoison set. Even after the hugetlb file is deleted, the hugetlb folio is still leaked.

If we want to offline the memory block that the hwpoisoned hugetlb folio belongs to, it fails in dissolve_free_hugetlb_folios() due to the hwpoisoned hugetlb folio isn't free.

I can reproduce this issue with the following steps in qemu:
1) echo offline >/sys/devices/system/memory/auto_online_blocks
2) in qemu monitor:

       object_add memory-backend-ram,id=mem10,size=1G
       device_add pc-dimm,id=dimm1,memdev=mem10,node=2

3) echo online_movable > /sys/devices/system/node/node2/memory136/state
4) echo 5 > /sys/devices/system/node/node2/hugepages/hugepages-2048kB/nr_hugepages
5) run ./hugetlb_file. This process will receive SIGBUS.
6) remove the hugetlbfs file.
7) echo offline > /sys/devices/system/node/node2/memory136/state

hugetlb_file.c:
fd = open("/dev/hugepages/my_hugepage_file", O_CREAT | O_RDWR, 0755); fallocate(fd, 0, 0, HUGEPAGE_SIZE * 2);
addr = mmap(NULL, HUGEPAGE_SIZE * 2, PROT_READ | PROT_WRITE,

Quoted 8 lines
		MAP_SHARED | MAP_HUGETLB, fd, 0);

memset(addr, 0xaa, HUGEPAGE_SIZE * 2);
madvise(addr, HUGEPAGE_SIZE, MADV_HWPOISON);

To fix it, when deleting hugetlb folio from pagecache, mark the hugetlb folio temporary, and put the refcount increased by memory-failure. After the hugetlb folio is deleted from pagecache, the refcount is decreased to zero and the hugetlb folio is dissolved.

Thanks for your patch.

Quoted 11 lines
---
 fs/hugetlbfs/inode.c | 5 +++++
 1 file changed, 5 insertions(+)
fs/hugetlbfs/inode.c +0 -0
index 3f70c47981de..6bebe2e67f3e 100644

fs/hugetlbfs/inode.c static void remove_inode_hugepages(struct inode *inode, loff_t lstart, · line 607

Quoted 5 lines
fs/hugetlbfs/inode.c +2 -0
@@ -603,6 +603,11 @@ static void remove_inode_hugepages(struct inode *inode, loff_t lstart,
 							index, truncate_op))
 				freed++;
 
+			if (unlikely(folio_test_hwpoison(folio))) {
+				folio_set_hugetlb_temporary(folio);

I think it is not needed to mark the hugetlb folio as temporary because offline_pages() will call dissolve_free_hugetlb_folios().

Quoted 5 lines

fs/hugetlbfs/inode.c static void remove_inode_hugepages(struct inode *inode, loff_t lstart, · line 608

Quoted 1 line
fs/hugetlbfs/inode.c +1 -0
+				folio_put(folio);

__get_huge_page_for_hwpoison() will always set hwpoison for hugetlb folio even without page refcnt increased. So this folio_put() might be unexpected. Please see [1] for detail.

Sorry, I forgot to put the link.

[1] https://lore.kernel.org/all/[email protected]/

Quoted 4 lines

Thanks.
.

#3 lore raw
In reply to Miaohe Lin
Quoted 47 lines · Miaohe Lin
Quoted 27 lines · Jinjiang Tu

When a file hugetlb folio triggers UCE, me_huge_page() will keep the hugetlb folio in pagcahe with refcount increased and PG_hwpoison set. Even after the hugetlb file is deleted, the hugetlb folio is still leaked.

If we want to offline the memory block that the hwpoisoned hugetlb folio belongs to, it fails in dissolve_free_hugetlb_folios() due to the hwpoisoned hugetlb folio isn't free.

I can reproduce this issue with the following steps in qemu:
1) echo offline >/sys/devices/system/memory/auto_online_blocks
2) in qemu monitor:

        object_add memory-backend-ram,id=mem10,size=1G
        device_add pc-dimm,id=dimm1,memdev=mem10,node=2

3) echo online_movable > /sys/devices/system/node/node2/memory136/state
4) echo 5 > /sys/devices/system/node/node2/hugepages/hugepages-2048kB/nr_hugepages
5) run ./hugetlb_file. This process will receive SIGBUS.
6) remove the hugetlbfs file.
7) echo offline > /sys/devices/system/node/node2/memory136/state

hugetlb_file.c:

   fd = open("/dev/hugepages/my_hugepage_file", O_CREAT | O_RDWR, 0755);
   fallocate(fd, 0, 0, HUGEPAGE_SIZE * 2);
   addr = mmap(NULL, HUGEPAGE_SIZE * 2, PROT_READ | PROT_WRITE,
		MAP_SHARED | MAP_HUGETLB, fd, 0);
   memset(addr, 0xaa, HUGEPAGE_SIZE * 2);
   madvise(addr, HUGEPAGE_SIZE, MADV_HWPOISON);
Quoted 4 lines

To fix it, when deleting hugetlb folio from pagecache, mark the hugetlb folio temporary, and put the refcount increased by memory-failure. After the hugetlb folio is deleted from pagecache, the refcount is decreased to zero and the hugetlb folio is dissolved.

Thanks for your patch.

Quoted 13 lines
---
  fs/hugetlbfs/inode.c | 5 +++++
  1 file changed, 5 insertions(+)
fs/hugetlbfs/inode.c +0 -0
index 3f70c47981de..6bebe2e67f3e 100644
@@ -603,6 +603,11 @@ static void remove_inode_hugepages(struct inode *inode, loff_t lstart,
  							index, truncate_op))
  				freed++;
 
Quoted 4 lines

fs/hugetlbfs/inode.c static void remove_inode_hugepages(struct inode *inode, loff_t lstart, · line 607

Quoted 2 lines
fs/hugetlbfs/inode.c +2 -0
+			if (unlikely(folio_test_hwpoison(folio))) {
+				folio_set_hugetlb_temporary(folio);

I think it is not needed to mark the hugetlb folio as temporary because offline_pages() will call dissolve_free_hugetlb_folios().

Indeed. I was intended to avoid this hugetlb allocated again, but dequeue_hugetlb_folio_node_exact() will check PG_hwpoison.

Quoted 5 lines

fs/hugetlbfs/inode.c static void remove_inode_hugepages(struct inode *inode, loff_t lstart, · line 608

Quoted 2 lines
fs/hugetlbfs/inode.c +1 -0
+				folio_put(folio);

__get_huge_page_for_hwpoison() will always set hwpoison for hugetlb folio even without page refcnt increased. So this folio_put() might be unexpected. Please see [1] for detail.

When memory-failure races with migration, __get_huge_page_for_hwpoison() will set hwpoison without page refcnt increased. In common case, ref is increased before setting hwpoison.

Quoted 2 lines

Thanks.
.

#4 lore raw
Quoted 57 lines · Jinjiang Tu
Quoted 39 lines · Miaohe Lin
Quoted 27 lines · Jinjiang Tu

When a file hugetlb folio triggers UCE, me_huge_page() will keep the hugetlb folio in pagcahe with refcount increased and PG_hwpoison set. Even after the hugetlb file is deleted, the hugetlb folio is still leaked.

If we want to offline the memory block that the hwpoisoned hugetlb folio belongs to, it fails in dissolve_free_hugetlb_folios() due to the hwpoisoned hugetlb folio isn't free.

I can reproduce this issue with the following steps in qemu:
  1) echo offline >/sys/devices/system/memory/auto_online_blocks
  2) in qemu monitor:
        object_add memory-backend-ram,id=mem10,size=1G
        device_add pc-dimm,id=dimm1,memdev=mem10,node=2
  3) echo online_movable > /sys/devices/system/node/node2/memory136/state
  4) echo 5 > /sys/devices/system/node/node2/hugepages/hugepages-2048kB/nr_hugepages
  5) run ./hugetlb_file. This process will receive SIGBUS.
  6) remove the hugetlbfs file.
  7) echo offline > /sys/devices/system/node/node2/memory136/state

hugetlb_file.c:
   fd = open("/dev/hugepages/my_hugepage_file", O_CREAT | O_RDWR, 0755); fallocate(fd, 0, 0, HUGEPAGE_SIZE * 2);
   addr = mmap(NULL, HUGEPAGE_SIZE * 2, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_HUGETLB, fd, 0);
   memset(addr, 0xaa, HUGEPAGE_SIZE * 2);
   madvise(addr, HUGEPAGE_SIZE, MADV_HWPOISON);

Quoted 4 lines

To fix it, when deleting hugetlb folio from pagecache, mark the hugetlb folio temporary, and put the refcount increased by memory-failure. After the hugetlb folio is deleted from pagecache, the refcount is decreased to zero and the hugetlb folio is dissolved.

Thanks for your patch.

Quoted 5 lines

---
  fs/hugetlbfs/inode.c | 5 +++++

  1 file changed, 5 insertions(+)
Quoted 11 lines

fs/hugetlbfs/inode.c static void remove_inode_hugepages(struct inode *inode, loff_t lstart, · line 606

Quoted 9 lines
fs/hugetlbfs/inode.c +1 -0
index 3f70c47981de..6bebe2e67f3e 100644
@@ -603,6 +603,11 @@ static void remove_inode_hugepages(struct inode *inode, loff_t lstart,
                               index, truncate_op))
                   freed++;
   +            if (unlikely(folio_test_hwpoison(folio))) {
+                folio_set_hugetlb_temporary(folio);

I think it is not needed to mark the hugetlb folio as temporary because offline_pages() will call dissolve_free_hugetlb_folios().

Indeed. I was intended to avoid this hugetlb allocated again, but dequeue_hugetlb_folio_node_exact() will check PG_hwpoison.

Quoted 1 line
Quoted 8 lines
Quoted 4 lines

fs/hugetlbfs/inode.c static void remove_inode_hugepages(struct inode *inode, loff_t lstart, · line 608

Quoted 1 line
fs/hugetlbfs/inode.c +1 -0
+                folio_put(folio);

__get_huge_page_for_hwpoison() will always set hwpoison for hugetlb folio even without page refcnt increased. So this folio_put() might be unexpected. Please see [1] for detail.

When memory-failure races with migration, __get_huge_page_for_hwpoison() will set hwpoison without page refcnt increased. In common case, ref is increased before setting hwpoison.

Right. And in those rare cases, VM_BUG_ON_PAGE(page_ref_count(page) == 0, page) will be triggered in folio_put().

Thanks.
.

#5 lore raw
Quoted 59 lines · Miaohe Lin
Quoted 58 lines · Jinjiang Tu
Quoted 39 lines · Miaohe Lin
Quoted 27 lines · Jinjiang Tu

When a file hugetlb folio triggers UCE, me_huge_page() will keep the hugetlb folio in pagcahe with refcount increased and PG_hwpoison set. Even after the hugetlb file is deleted, the hugetlb folio is still leaked.

If we want to offline the memory block that the hwpoisoned hugetlb folio belongs to, it fails in dissolve_free_hugetlb_folios() due to the hwpoisoned hugetlb folio isn't free.

I can reproduce this issue with the following steps in qemu:
  1) echo offline >/sys/devices/system/memory/auto_online_blocks
  2) in qemu monitor:
        object_add memory-backend-ram,id=mem10,size=1G
        device_add pc-dimm,id=dimm1,memdev=mem10,node=2
  3) echo online_movable > /sys/devices/system/node/node2/memory136/state
  4) echo 5 > /sys/devices/system/node/node2/hugepages/hugepages-2048kB/nr_hugepages
  5) run ./hugetlb_file. This process will receive SIGBUS.
  6) remove the hugetlbfs file.
  7) echo offline > /sys/devices/system/node/node2/memory136/state

hugetlb_file.c:
   fd = open("/dev/hugepages/my_hugepage_file", O_CREAT | O_RDWR, 0755); fallocate(fd, 0, 0, HUGEPAGE_SIZE * 2);
   addr = mmap(NULL, HUGEPAGE_SIZE * 2, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_HUGETLB, fd, 0);
   memset(addr, 0xaa, HUGEPAGE_SIZE * 2);
   madvise(addr, HUGEPAGE_SIZE, MADV_HWPOISON);

Quoted 4 lines

To fix it, when deleting hugetlb folio from pagecache, mark the hugetlb folio temporary, and put the refcount increased by memory-failure. After the hugetlb folio is deleted from pagecache, the refcount is decreased to zero and the hugetlb folio is dissolved.

Thanks for your patch.

Quoted 5 lines
---
   fs/hugetlbfs/inode.c | 5 +++++
   1 file changed, 5 insertions(+)
Quoted 11 lines

fs/hugetlbfs/inode.c static void remove_inode_hugepages(struct inode *inode, loff_t lstart, · line 606

Quoted 9 lines
fs/hugetlbfs/inode.c +1 -0
index 3f70c47981de..6bebe2e67f3e 100644
@@ -603,6 +603,11 @@ static void remove_inode_hugepages(struct inode *inode, loff_t lstart,
                               index, truncate_op))
                   freed++;
   +            if (unlikely(folio_test_hwpoison(folio))) {
+                folio_set_hugetlb_temporary(folio);

I think it is not needed to mark the hugetlb folio as temporary because offline_pages() will call dissolve_free_hugetlb_folios().

Indeed. I was intended to avoid this hugetlb allocated again, but dequeue_hugetlb_folio_node_exact() will check PG_hwpoison.

Quoted 4 lines

fs/hugetlbfs/inode.c static void remove_inode_hugepages(struct inode *inode, loff_t lstart, · line 608

Quoted 1 line
fs/hugetlbfs/inode.c +1 -0
+                folio_put(folio);

__get_huge_page_for_hwpoison() will always set hwpoison for hugetlb folio even without page refcnt increased. So this folio_put() might be unexpected. Please see [1] for detail.

Quoted 7 lines
Quoted 3 lines

When memory-failure races with migration, __get_huge_page_for_hwpoison() will set hwpoison without page refcnt increased. In common case, ref is increased before setting hwpoison.

Right. And in those rare cases, VM_BUG_ON_PAGE(page_ref_count(page) == 0, page) will be triggered in folio_put().

Thanks.

hwpoisoned base page is forced to offline. test_pages_isolated() and __offline_isolated_pages() just skip the hwpoisoned pfn regardless of it's refcount.

I originally tried to force offline hwpoisoned hugetlb too, but it leads to wrong counts (i.e., HugePages_Total).

Quoted 1 line

.