mm: Implement ECC handling for pfn with no struct page
Subsystems (12)
[email protected]
maintainerv2 cover mm: Implement ECC handling for pfn with no struct page
From: Ankit Agrawal <[email protected]>
The kernel MM currently handles ECC errors / poison only on memory page backed by struct page. As part of [1], the nvgrace-gpu-vfio-pci module maps the device memory to user VA (Qemu) using remap_pfn_range without being added to the kernel. These pages are not backed by struct page.
Implement a new ECC handling for memory without struct pages. Kernel MM expose registration APIs to allow modules that are managing the device to register its memory region and a callback function. MM then tracks such regions using interval tree.
The mechanism is largely similar to that of ECC on pfn with struct pages. If there is an ECC error on a pfn, MM uses the registered memory failure callback function to notify the module of the faulty PFN, so that the module may take any required action. The pfn is then unmapped in Stage-2. When the VM tries to access the page, it gets trapped in KVM, which calls the vm ops fault function. If the module fault function returns VM_FAULT_HWPOISON, KVM sends a BUS_MCEERR_AR to the usermode (Qemu) mapped to the poisoned page.
Lastly, nvgrace-gpu-vfio-pci module make use of the new mechanism to get poison handling support on the device memory.
Patch generated over v6.7-rc2 and with [1] applied. [1] is currently under review.
[1] https://lore.kernel.org/all/[email protected]/
---
Link for v1: https://lore.kernel.org/all/[email protected]/
v1 -> v2
- Change poisoned page tracking from bitmap to hashtable.
- Addressed miscellaneous comments in v1.
Ankit Agrawal (4):
mm: handle poisoning of pfn without struct pages
mm: Add poison error check in fixup_user_fault() for mapped pfn
mm: Change ghes code to allow poison of non-struct pfn vfio/nvgpu: register device memory for poison handling
drivers/acpi/apei/ghes.c | 12 +-- drivers/vfio/pci/nvgrace-gpu/main.c | 123 ++++++++++++++++++++++- drivers/vfio/vfio_main.c | 3 +- include/linux/memory-failure.h | 22 +++++ include/linux/mm.h | 1 + include/ras/ras_event.h | 1 + mm/Kconfig | 1 + mm/gup.c | 2 +- mm/memory-failure.c | 146 +++++++++++++++++++++++----- virt/kvm/kvm_main.c | 6 ++ 10 files changed, 278 insertions(+), 39 deletions(-) create mode 100644 include/linux/memory-failure.h
Signature · 1 line
2.17.1
[email protected]
maintainerPATCH v2 1/4 mm: handle poisoning of pfn without struct pages
From: Ankit Agrawal <[email protected]>
The kernel MM currently does not handle ECC errors / poison on a memory region that is not backed by struct pages. If a memory region is mapped using remap_pfn_range(), but not added to the kernel, MM will not have associated struct pages. Add a new mechanism to handle memory failure on such memory.
Make kernel MM expose a function to allow modules managing the device memory to register a failure function and the physical address space associated with the device memory. MM maintains this information as interval tree. The registered memory failure function is used by MM to notify the kernel module managing the PFN, so that the module may take any required action. The module for example may use the information to track the poisoned pages.
In this implementation, kernel MM follows the following sequence similar (mostly) to the memory_failure() handler for struct page backed memory:
1. memory_failure() is triggered on reception of a poison error. An absence of struct page is detected and consequently memory_failure_pfn() is executed.
2. memory_failure_pfn() call the newly introduced failure handler exposed by the module managing the poisoned memory to notify it of the problematic PFN.
3. memory_failure_pfn() unmaps the stage-2 mapping to the PFN.
4. memory_failure_pfn() collects the processes mapped to the PFN.
5. memory_failure_pfn() sends SIGBUS (BUS_MCEERR_AO) to all the processes mapping the faulty PFN using kill_procs().
6. An access to the faulty PFN by an operation in VM at a later point is trapped and user_mem_abort() is called.
7. The vma ops fault function gets called due to the absence of Stage-2 mapping. It is expected to return VM_FAULT_HWPOISON on the PFN.
8. __gfn_to_pfn_memslot() then returns KVM_PFN_ERR_HWPOISON, which cause the poison with SIGBUS (BUS_MCEERR_AR) to be sent to the QEMU process through kvm_send_hwpoison_signal().
--- include/linux/memory-failure.h | 22 +++++ include/linux/mm.h | 1 + include/ras/ras_event.h | 1 + mm/Kconfig | 1 + mm/memory-failure.c | 146 +++++++++++++++++++++++++++------ 5 files changed, 146 insertions(+), 25 deletions(-) create mode 100644 include/linux/memory-failure.h
include/linux/memory-failure.h new file +22 -0
@@ -0,0 +1,22 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_MEMORY_FAILURE_H +#define _LINUX_MEMORY_FAILURE_H + +#include <linux/interval_tree.h> + +struct pfn_address_space ; + +struct pfn_address_space_ops { +void (*failure)(struct pfn_address_space *pfn_space,unsigned long pfn); +}; + +struct pfn_address_space { +struct interval_tree_node node; +const struct pfn_address_space_ops *ops; +struct address_space *mapping; +}; + +int register_pfn_address_space (struct pfn_address_space *pfn_space); +void unregister_pfn_address_space (struct pfn_address_space *pfn_space); + +#endif /* _LINUX_MEMORY_FAILURE_H */
include/linux/mm.h +1 -0
@@ -4007,6 +4007,7 @@ enum mf_action_page_type {MF_MSG_BUDDY ,MF_MSG_DAX ,MF_MSG_UNSPLIT_THP , +MF_MSG_PFN_MAP , MF_MSG_UNKNOWN, };
include/ras/ras_event.h +1 -0
@@ -373,6 +373,7 @@ TRACE_EVENT(aer_event,EM (MF_MSG_BUDDY ,"free buddy page" ) \ EM ( MF_MSG_DAX, "dax page" ) \ EM ( MF_MSG_UNSPLIT_THP, "unsplit thp" ) \ + EM ( MF_MSG_PFN_MAP, "non struct page pfn" ) \ EMe ( MF_MSG_UNKNOWN, "unknown page" )/*
mm/Kconfig +1 -0
@@ -774,6 +774,7 @@ config MEMORY_FAILURE depends on ARCH_SUPPORTS_MEMORY_FAILURE bool "Enable recovery from hardware memory errors" select MEMORY_ISOLATION + select INTERVAL_TREE select RAS help Enables code to recover from some memory failures on systems
mm/memory-failure.c +121 -25
239 lines not on this page · raw message
Signature · 1 line
2.17.1
[email protected]
maintainerPATCH v2 2/4 mm: Add poison error check in fixup_user_fault() for mapped pfn
In reply to [email protected]From: Ankit Agrawal <[email protected]>
The fixup_user_fault() currently does not expect a VM_FAULT_HWPOISON and hence does not check for it while calling vm_fault_to_errno(). Since we now have a new code path which can trigger such case, change fixup_user_fault to look for VM_FAULT_HWPOISON.
Also make hva_to_pfn_remapped check for -EHWPOISON and communicate the poison fault up to the user_mem_abort().
--- mm/gup.c | 2 +- virt/kvm/kvm_main.c | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-)
mm/gup.c +1 -1
@@ -1414,7 +1414,7 @@ int fixup_user_fault(struct mm_struct *mm, }if (ret &VM_FAULT_ERROR ) { -int err =vm_fault_to_errno (ret,0 ); +int err =vm_fault_to_errno (ret,FOLL_HWPOISON );if (err)return err;
virt/kvm/kvm_main.c +6 -0
@@ -2731,6 +2731,12 @@ kvm_pfn_t hva_to_pfn(unsigned long addr, bool atomic, bool interruptible, r =hva_to_pfn_remapped (vma, addr, write_fault, writable, &pfn);if (r == -EAGAIN ) goto retry; + +if (r == -EHWPOISON ) { + pfn =KVM_PFN_ERR_HWPOISON ; + goto exit; + } +if (r <0 ) pfn =KVM_PFN_ERR_FAULT ; }else {
Signature · 1 line
2.17.1
Sean Christopherson
reviewerRe: [PATCH v2 2/4] mm: Add poison error check in fixup_user_fault() for mapped pfn
Quoted 9 lines · On Thu, Nov 23, 2023, [email protected] wrote:
From: Ankit Agrawal <[email protected]>
The
fixup_user_fault()currently does not expect aVM_FAULT_HWPOISONand hence does not check for it while callingvm_fault_to_errno(). Since we now have a new code path which can trigger such case, changefixup_user_faultto look forVM_FAULT_HWPOISON.Also make
hva_to_pfn_remappedcheck for-EHWPOISONand communicate the poison fault up to theuser_mem_abort().
I would much prefer the KVM change be split out to its own patch, I see no reason why it needs to be bundled with the fixup_user_fault() change. KVM will set pfn to KVM_PFN_ERR_FAULT before and after the fixup_user_fault() change.
Quoted 25 lines
Signed-off-by Ankit Agrawal <[email protected]>--- mm/gup.c | 2 +- virt/kvm/kvm_main.c | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-)mm/gup.c +1
-1@@ -1414,7 +1414,7 @@ int fixup_user_fault(struct mm_struct *mm, }if (ret &VM_FAULT_ERROR ) { -int err =vm_fault_to_errno (ret,0 ); +int err =vm_fault_to_errno (ret,FOLL_HWPOISON );if (err)return err;virt/kvm/kvm_main.c +0
-0@@ -2731,6 +2731,12 @@ kvm_pfn_t hva_to_pfn(unsigned long addr, bool atomic, bool interruptible, r =hva_to_pfn_remapped (vma, addr, write_fault, writable, &pfn);
virt/kvm/kvm_main.c kvm_pfn_t hva_to_pfn(unsigned long addr, bool atomic, bool interruptible, · line 2741
Quoted 10 lines
virt/kvm/kvm_main.c +6
-0if (r == -EAGAIN ) goto retry; + +if (r == -EHWPOISON ) { + pfn =KVM_PFN_ERR_HWPOISON ; + goto exit; + } +if (r <0 ) pfn =KVM_PFN_ERR_FAULT ;
I vote for
if (r == -EHWPOISON) pfn = KVM_PFN_ERR_HWPOISON; else if (r < 0) pfn = KVM_PFN_ERR_FAULT;
or even opportunstically fix the < 0 weirdness:
if (r == -EHWPOISON) pfn = KVM_PFN_ERR_HWPOISON; else if (r) pfn = KVM_PFN_ERR_FAULT;
It's rather confusing to see a goto in one error path but an effective fallthrough in a different error path, i.e. gives the impression that KVM_PFN_ERR_HWPOISON has some special behavior that doesn't apply to KVM_PFN_ERR_FAULT.
[email protected]
maintainerPATCH v2 3/4 mm: Change ghes code to allow poison of non-struct pfn
In reply to [email protected]From: Ankit Agrawal <[email protected]>
The GHES code allows calling of memory_failure() on the PFNs that pass the pfn_valid() check. This contract is broken for the remapped PFNs which fails the check and ghes_do_memory_failure() returns without triggering memory_failure().
Update code to allow memory_failure() call on PFNs failing pfn_valid().
--- drivers/acpi/apei/ghes.c | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-)
drivers/acpi/apei/ghes.c +1 -11
@@ -471,20 +471,10 @@ static void ghes_kick_task_work(struct callback_head *head)static bool ghes_do_memory_failure (u64 physical_addr,int flags) { -unsigned long pfn; -if (!IS_ENABLED (CONFIG_ACPI_APEI_MEMORY_FAILURE ))return false; - pfn =PHYS_PFN (physical_addr); -if (!pfn_valid (pfn) && !arch_is_platform_page (physical_addr)) { - pr_warn_ratelimited(FW_WARN GHES_PFX -"Invalid address in generic error data: %#llx\n" , - physical_addr); -return false; - } - -memory_failure_queue (pfn, flags); +memory_failure_queue (PHYS_PFN (physical_addr), flags);return true; }
Signature · 1 line
2.17.1
Borislav Petkov
maintainerRe: [PATCH v2 3/4] mm: Change ghes code to allow poison of non-struct pfn
drivers/acpi/apei/ghes.c static void ghes_kick_task_work(struct callback_head *head) · line 485
Quoted 7 lines · On Thu, Nov 23, 2023 at 06:05:11AM +0530, [email protected] wrote:
drivers/acpi/apei/ghes.c +0
-7- pfn =PHYS_PFN (physical_addr); -if (!pfn_valid (pfn) && !arch_is_platform_page (physical_addr)) { - pr_warn_ratelimited(FW_WARN GHES_PFX -"Invalid address in generic error data: %#llx\n" , - physical_addr); -return false; - }
You don't just remove a pfn valid test just because your weird device can't stomach it - you extend it, like
3ad6fd77a2d6 ("x86/sgx: Add check for SGX pages to ghes_do_memory_failure()")
did, for example.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteJason Gunthorpe
reviewerRe: [PATCH v2 3/4] mm: Change ghes code to allow poison of non-struct pfn
Quoted 11 lines · Borislav Petkov
drivers/acpi/apei/ghes.c static void ghes_kick_task_work(struct callback_head *head) · line 485
Quoted 7 lines · On Thu, Nov 23, 2023 at 06:05:11AM +0530, [email protected] wrote:
drivers/acpi/apei/ghes.c +0
-7- pfn =PHYS_PFN (physical_addr); -if (!pfn_valid (pfn) && !arch_is_platform_page (physical_addr)) { - pr_warn_ratelimited(FW_WARN GHES_PFX -"Invalid address in generic error data: %#llx\n" , - physical_addr); -return false; - }You don't just remove a pfn valid test just because your weird device can't stomach it - you extend it, like
It wasn't removed. patch 1 moved it to memory_failure() where it makes a lot more sense.
Jason
Borislav Petkov
maintainerRe: [PATCH v2 3/4] mm: Change ghes code to allow poison of non-struct pfn
Quoted 2 lines · Jason Gunthorpe
It wasn't removed. patch 1 moved it to
memory_failure()where it makes a lot more sense.
Why is this a separate patch then?
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteAnkit Agrawal
maintainerRe: [PATCH v2 3/4] mm: Change ghes code to allow poison of non-struct pfn
Quoted 4 lines
Quoted 2 lines
It wasn't removed. patch 1 moved it to
memory_failure()where it makes a lot more sense.Why is this a separate patch then?
This was done to keep ghes code separate from the memory failure code. I can merge them if that is preferable.
Jason Gunthorpe
reviewerRe: [PATCH v2 3/4] mm: Change ghes code to allow poison of non-struct pfn
Quoted 7 lines · Ankit Agrawal
Quoted 4 lines
Quoted 2 lines
It wasn't removed. patch 1 moved it to
memory_failure()where it makes a lot more sense.Why is this a separate patch then?
This was done to keep ghes code separate from the memory failure code. I can merge them if that is preferable.
A single patch to move just this code could be a good idea
Jason
[email protected]
maintainerPATCH v2 4/4 vfio/nvgpu: register device memory for poison handling
In reply to [email protected]From: Ankit Agrawal <[email protected]>
The nvgrace-gpu-vfio-pci module [1] maps the device memory to the user VA (Qemu) using remap_pfn_range() without adding the memory to the kernel. The device memory pages are not backed by struct page. Patches 1-3 implements the mechanism to handle ECC/poison on memory page without struct page and expose a registration function. This new mechanism is leveraged here.
The module registers its memory region with the kernel MM for ECC handling using the register_pfn_address_space() registration API exposed by the kernel. It also defines a failure callback function pfn_memory_failure() to get the poisoned PFN from the MM.
The module track poisoned PFN using a hastable. The PFN is communicated by the kernel MM to the module through the failure function, which push the appropriate memory offset to the hashtable.
The module also defines a VMA fault ops for the module. It returns VM_FAULT_HWPOISON in case the memory offset is found in the hashtable.
[1] https://lore.kernel.org/all/[email protected]/
--- drivers/vfio/pci/nvgrace-gpu/main.c | 123 +++++++++++++++++++++++++++- drivers/vfio/vfio_main.c | 3 +- 2 files changed, 124 insertions(+), 2 deletions(-)
drivers/vfio/pci/nvgrace-gpu/main.c +122 -1
@@ -6,6 +6,16 @@#include <linux/pci.h> #include <linux/vfio_pci_core.h> #include <linux/vfio.h> +#ifdef CONFIG_MEMORY_FAILURE +#include <linux/bitmap.h> +#include <linux/memory-failure.h> +#include <linux/hashtable.h> +#endif + +struct h_node { +unsigned long mem_offset; +struct hlist_node node; +};struct nvgrace_gpu_vfio_pci_core_device {struct vfio_pci_core_device core_device;
@@ -13,8 +23,96 @@ struct nvgrace_gpu_vfio_pci_core_device {size_t memlength;void *memmap;struct mutex memmap_lock; +#ifdef CONFIG_MEMORY_FAILURE +struct pfn_address_space pfn_address_space; +DECLARE_HASHTABLE (htbl,8 ); +#endif +}; + +#ifdef CONFIG_MEMORY_FAILURE +static void +nvgrace_gpu_vfio_pci_pfn_memory_failure (struct pfn_address_space *pfn_space, +unsigned long pfn) +{ +struct nvgrace_gpu_vfio_pci_core_device *nvdev =container_of ( + pfn_space, struct nvgrace_gpu_vfio_pci_core_device, pfn_address_space); +unsigned long mem_offset = pfn - pfn_space->node.start; +struct h_node *ecc; + +if (mem_offset >= (nvdev->memlength >>PAGE_SHIFT )) +return ; + +/* +* MM has called to notify a poisoned page. Track that in the hastable. +*/ + ecc = (struct h_node *)(vzalloc (sizeof (struct h_node ))); + ecc->mem_offset = mem_offset; +hash_add (nvdev->htbl, &(ecc->node), ecc->mem_offset); +} + +struct pfn_address_space_ops nvgrace_gpu_vfio_pci_pas_ops = { + .failure = nvgrace_gpu_vfio_pci_pfn_memory_failure, }; +static int +nvgrace_gpu_vfio_pci_register_pfn_range (struct nvgrace_gpu_vfio_pci_core_device *nvdev, +struct vm_area_struct *vma) +{ +unsigned long nr_pages; +int ret =0 ; + + nr_pages = nvdev->memlength >>PAGE_SHIFT ; + + nvdev->pfn_address_space.node.start = vma->vm_pgoff; + nvdev->pfn_address_space.node.last = vma->vm_pgoff + nr_pages -1 ; + nvdev->pfn_address_space.ops = &nvgrace_gpu_vfio_pci_pas_ops; + nvdev->pfn_address_space.mapping = vma->vm_file->f_mapping; + + ret =register_pfn_address_space (&(nvdev->pfn_address_space)); + +return ret; +} + +extern struct vfio_device *vfio_device_from_file (struct file *file); + +static vm_fault_t nvgrace_gpu_vfio_pci_fault (struct vm_fault *vmf) +{ +unsigned long mem_offset = vmf->pgoff - vmf->vma->vm_pgoff; +struct vfio_device *core_vdev; +struct nvgrace_gpu_vfio_pci_core_device *nvdev; +bool found = false; +struct h_node *cur; + +if (!(vmf->vma->vm_file)) + goto error_exit; + + core_vdev =vfio_device_from_file (vmf->vma->vm_file); + +if (!core_vdev) + goto error_exit; + + nvdev =container_of (core_vdev, + struct nvgrace_gpu_vfio_pci_core_device, core_device.vdev); + +if (mem_offset < (nvdev->memlength >>PAGE_SHIFT )) { +/* +* Check if the page is poisoned. +*/ +hash_for_each_possible (nvdev->htbl, cur, node, mem_offset) { +if (cur->mem_offset == mem_offset) +return VM_FAULT_HWPOISON ; + } + } + +error_exit: +return VM_FAULT_ERROR ; +} + +static const struct vm_operations_struct nvgrace_gpu_vfio_pci_mmap_ops = { + .fault = nvgrace_gpu_vfio_pci_fault, +}; +#endif +static int nvgrace_gpu_vfio_pci_open_device (struct vfio_device *core_vdev) { struct vfio_pci_core_device *vdev =
@@ -46,6 +144,9 @@ static void nvgrace_gpu_vfio_pci_close_device(struct vfio_device *core_vdev)mutex_destroy (&nvdev->memmap_lock); +#ifdef CONFIG_MEMORY_FAILURE +unregister_pfn_address_space (&(nvdev->pfn_address_space)); +#endif vfio_pci_core_close_device (core_vdev); }
@@ -103,8 +204,12 @@ static int nvgrace_gpu_vfio_pci_mmap(struct vfio_device *core_vdev,return ret; vma->vm_pgoff = start_pfn; +#ifdef CONFIG_MEMORY_FAILURE + vma->vm_ops = &nvgrace_gpu_vfio_pci_mmap_ops; -return 0 ; + ret =nvgrace_gpu_vfio_pci_register_pfn_range (nvdev, vma); +#endif +return ret; } static long
@@ -413,6 +518,12 @@ nvgrace_gpu_vfio_pci_fetch_memory_property(struct pci_dev *pdev, nvdev->memlength = memlength; +#ifdef CONFIG_MEMORY_FAILURE +/* +* Initialize the hashtable tracking the poisoned pages. +*/ +hash_init (nvdev->htbl); +#endif return ret; }
@@ -448,6 +559,16 @@ static void nvgrace_gpu_vfio_pci_remove(struct pci_dev *pdev) {struct nvgrace_gpu_vfio_pci_core_device *nvdev =nvgrace_gpu_drvdata (pdev);struct vfio_pci_core_device *vdev = &nvdev->core_device; +#ifdef CONFIG_MEMORY_FAILURE +struct h_node *cur; +unsigned long bkt; +struct hlist_node *tmp_node; + +hash_for_each_safe (nvdev->htbl, bkt, tmp_node, cur, node) { +hash_del (&cur->node); +vfree (cur); + } +#endif vfio_pci_core_unregister_device (vdev);vfio_put_device (&vdev->vdev);
drivers/vfio/vfio_main.c +2 -1
@@ -1319,7 +1319,7 @@ const struct file_operations vfio_device_fops = { .mmap = vfio_device_fops_mmap, }; -static struct vfio_device *vfio_device_from_file (struct file *file) +struct vfio_device *vfio_device_from_file (struct file *file) {struct vfio_device_file *df = file->private_data;
@@ -1327,6 +1327,7 @@ static struct vfio_device *vfio_device_from_file(struct file *file)return NULL ;return df->device; } +EXPORT_SYMBOL_GPL (vfio_device_from_file);/** * vfio_file_is_valid - True if the file is valid vfio file
Signature · 1 line
2.17.1