Skip to contentSkip to conversation

PCI/P2PDMA: Reset page reference count when page mapping fails

PATCH Applied Alistair Popple 4 participants 4 messages Jan 12 – Jan 13

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 PCI/P2PDMA: Reset page reference count when page mapping fails

When mapping a p2pdma page the page reference count is initialised to 1 prior to calling vm_insert_page(). This is to avoid vm_insert_page() warning if the page refcount is zero. Prior to setting the page count there is a check to ensure the page is currently free (ie. has a zero reference count).

However vm_insert_page() can fail. In this case the pages are freed back to the genalloc pool, but that does not reset the page refcount. So a future allocation of the same page will see the elevated page refcount from the previous set_page_count() call triggering the VM_WARN_ON_ONCE_PAGE checking that the page is free.

Fix this by resetting the page refcount back to zero using set_page_count(). Note that put_page() is not used because that would result in freeing the page twice due to implicitly calling p2pdma_folio_free().

Fixes b7e282378773 ("mm/mm_init: move p2pdma page refcount initialisation to p2pdma")

---

This was found by inspection. I don't currently have a good setup that exercises the p2pmem_alloc_mmap() path so this has only been compile tested - additional testing would be appreciated.

---
 drivers/pci/p2pdma.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/pci/p2pdma.c +6 -0
index dd64ec830fdd..3b29246b9e86 100644
@@ -152,6 +152,12 @@ static int p2pmem_alloc_mmap(struct file *filp, struct kobject *kobj,
 		ret = vm_insert_page(vma, vaddr, page);
 		if (ret) {
 			gen_pool_free(p2pdma->pool, (uintptr_t)kaddr, len);
+
+			/*
+			 * Reset the page count. We don't use put_page() because
+			 * we don't want to trigger the p2pdma_folio_free() path.
+			 */
+			set_page_count(page, 0);
 			percpu_ref_put(ref);
 			return ret;
 		}
Signature · 1 line
2.51.0
Acked-by on v1 1/1
#1 lore raw
Quoted 36 lines · Alistair Popple

When mapping a p2pdma page the page reference count is initialised to 1 prior to calling vm_insert_page(). This is to avoid vm_insert_page() warning if the page refcount is zero. Prior to setting the page count there is a check to ensure the page is currently free (ie. has a zero reference count).

However vm_insert_page() can fail. In this case the pages are freed back to the genalloc pool, but that does not reset the page refcount. So a future allocation of the same page will see the elevated page refcount from the previous set_page_count() call triggering the VM_WARN_ON_ONCE_PAGE checking that the page is free.

Fix this by resetting the page refcount back to zero using set_page_count(). Note that put_page() is not used because that would result in freeing the page twice due to implicitly calling p2pdma_folio_free().

Fixes b7e282378773 ("mm/mm_init: move p2pdma page refcount initialisation to p2pdma")

---

This was found by inspection. I don't currently have a good setup that exercises the p2pmem_alloc_mmap() path so this has only been compile tested - additional testing would be appreciated.

---
 drivers/pci/p2pdma.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/pci/p2pdma.c +0 -0
index dd64ec830fdd..3b29246b9e86 100644
@@ -152,6 +152,12 @@ static int p2pmem_alloc_mmap(struct file *filp, struct kobject *kobj,
 		ret = vm_insert_page(vma, vaddr, page);
 		if (ret) {

drivers/pci/p2pdma.c static int p2pmem_alloc_mmap(struct file *filp, struct kobject *kobj, · line 163

Quoted 10 lines
drivers/pci/p2pdma.c +6 -0
 			gen_pool_free(p2pdma->pool, (uintptr_t)kaddr, len);
+
+			/*
+			 * Reset the page count. We don't use put_page() because
+			 * we don't want to trigger the p2pdma_folio_free() path.
+			 */
+			set_page_count(page, 0);
 			percpu_ref_put(ref);
 			return ret;
 		}

The change looks good!

#2 lore raw
In reply to Alistair Popple
Quoted 12 lines · Alistair Popple

When mapping a p2pdma page the page reference count is initialised to 1 prior to calling vm_insert_page(). This is to avoid vm_insert_page() warning if the page refcount is zero. Prior to setting the page count there is a check to ensure the page is currently free (ie. has a zero reference count).

However vm_insert_page() can fail. In this case the pages are freed back to the genalloc pool, but that does not reset the page refcount. So a future allocation of the same page will see the elevated page refcount from the previous set_page_count() call triggering the VM_WARN_ON_ONCE_PAGE checking that the page is free.

Quoted 7 lines

Fix this by resetting the page refcount back to zero using set_page_count(). Note that put_page() is not used because that would result in freeing the page twice due to implicitly calling p2pdma_folio_free().

Fixes b7e282378773 ("mm/mm_init: move p2pdma page refcount initialisation to p2pdma")

Applied to pci/p2pdma for v6.20, thanks!

Quoted 29 lines

---

This was found by inspection. I don't currently have a good setup that exercises the p2pmem_alloc_mmap() path so this has only been compile tested - additional testing would be appreciated.

---
 drivers/pci/p2pdma.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/pci/p2pdma.c +6 -0
index dd64ec830fdd..3b29246b9e86 100644
@@ -152,6 +152,12 @@ static int p2pmem_alloc_mmap(struct file *filp, struct kobject *kobj,
 		ret = vm_insert_page(vma, vaddr, page);
 		if (ret) {
 			gen_pool_free(p2pdma->pool, (uintptr_t)kaddr, len);
+
+			/*
+			 * Reset the page count. We don't use put_page() because
+			 * we don't want to trigger the p2pdma_folio_free() path.
+			 */
+			set_page_count(page, 0);
 			percpu_ref_put(ref);
 			return ret;
 		}
Signature · 1 line
2.51.0
maintainer Reviewed-by on v1 1/1
#3 lore raw
In reply to Alistair Popple

Hi Alistair,

Quoted 12 lines · Alistair Popple

When mapping a p2pdma page the page reference count is initialised to 1 prior to calling vm_insert_page(). This is to avoid vm_insert_page() warning if the page refcount is zero. Prior to setting the page count there is a check to ensure the page is currently free (ie. has a zero reference count).

However vm_insert_page() can fail. In this case the pages are freed back to the genalloc pool, but that does not reset the page refcount. So a future allocation of the same page will see the elevated page refcount from the previous set_page_count() call triggering the VM_WARN_ON_ONCE_PAGE checking that the page is free.

Quoted 7 lines

Fix this by resetting the page refcount back to zero using set_page_count(). Note that put_page() is not used because that would result in freeing the page twice due to implicitly calling p2pdma_folio_free().

Fixes b7e282378773 ("mm/mm_init: move p2pdma page refcount initialisation to p2pdma")

I had time today to pull out some old hardware and run this patch through my old p2pdma smoke tests. I don't think the tests exercise the change, but it looks correct to me on review.

Thanks for the fix!

Logan