From 07a634682e8fa83b13c479c53ea9aa0d0c4064cb Mon Sep 17 00:00:00 2001 From: Akira Hayakawa Date: Sat, 18 May 2024 13:44:54 +0900 Subject: [PATCH] fix: rollback mapped pages on failure --- azbuse-kmod/src/azbuse.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/azbuse-kmod/src/azbuse.c b/azbuse-kmod/src/azbuse.c index 0c6d7a8..aea6a66 100644 --- a/azbuse-kmod/src/azbuse.c +++ b/azbuse-kmod/src/azbuse.c @@ -295,16 +295,31 @@ static unsigned int azbusectl_poll(struct file *filp, poll_table *wait) static int azbusectl_mmap(struct file *filp, struct vm_area_struct *vma) { struct azbuse_device *azb = filp->private_data; - int i; + int n = azb->azb_xfer_count; - int err = 0; - unsigned long cur = vma->vm_start; + int i; + int err_i, err = 0; + unsigned long cur; + + cur = vma->vm_start; for (i=0; iazb_xfer[i].azb_address >> PAGE_SHIFT; unsigned long len = azb->azb_xfer[i].n_pages << PAGE_SHIFT; - err |= remap_pfn_range(vma, cur, pfn, len, vma->vm_page_prot); + err = remap_pfn_range(vma, cur, pfn, len, vma->vm_page_prot); + if (err) { + err_i = i; + break; + } cur += len; } + + // Rollback on failure + for (i=0; iazb_xfer[i].azb_address >> PAGE_SHIFT; + unsigned long len = azb->azb_xfer[i].n_pages; + unmap_mapping_pages(vma->vm_file->f_mapping, pfn, len, 0); + } + return err; }