Skip to contentSkip to conversation

evl/memory: allow system heap inspection using GDB

PATCH In review Qichen Qiu 2 participants 2 messages Dec 17, 2024 – Jan 1, 2025

lore
Share & tools
lore

Qichen Qiu

author
#0 lore raw

PATCH 1/1 evl/memory: allow system heap inspection using GDB

GDB was unable to inspect the evl shared heap mapping regions(i.e. __current_evl_window) due to limitations imposed by the VM_IO flag when ptrace accesses memory regions that were mapped using remap_pfn_range. This restriction hindered effective debugging.

To resolve this issue, the shared heap is now allocated using vzalloc, and the memory is mapped with vm_insert_page, which work seamlessly with GDB and ptrace.

---
 kernel/evl/control.c | 15 +++++++++++++--
 kernel/evl/memory.c  |  6 +++---
 2 files changed, 16 insertions(+), 5 deletions(-)
kernel/evl/control.c +13 -2
index 2d1b44f3b0dc..32764a9fc08b 100644
@@ -338,13 +338,24 @@ static long control_ioctl(struct file *filp, unsigned int cmd,
 static int control_mmap(struct file *filp, struct vm_area_struct *vma)
 {
 	void *p = evl_get_heap_base(&evl_shared_heap);
-	unsigned long pfn = __pa(p) >> PAGE_SHIFT;
 	size_t len = vma->vm_end - vma->vm_start;
+	unsigned long addr = vma->vm_start;
+	int err;
+	struct page *page;
 
 	if (len != evl_shm_size)
 		return -EINVAL;
+
+	while (addr < vma->vm_end) {
+		page = vmalloc_to_page(p);
+		err = vm_insert_page(vma, addr, page);
+		if (err < 0)
+			return err;
+		addr += PAGE_SIZE;
+		p += PAGE_SIZE;
+	}
 
-	return remap_pfn_range(vma, vma->vm_start, pfn, len, PAGE_SHARED);
+	return 0;
 }
 
 static const struct file_operations control_fops = {
kernel/evl/memory.c +3 -3
index 1da5be623d69..053a377396d2 100644
@@ -630,13 +630,13 @@ static int init_shared_heap(void)
 		CONFIG_EVL_NR_MONITORS *
 		sizeof(struct evl_monitor_state);
 	size = PAGE_ALIGN(size);
-	mem = kzalloc(size, GFP_KERNEL);
+	mem = vzalloc(size);
 	if (mem == NULL)
 		return -ENOMEM;
 
 	ret = evl_init_heap(&evl_shared_heap, mem, size);
 	if (ret) {
-		kfree(mem);
+		vfree(mem);
 		return ret;
 	}
 
@@ -650,7 +650,7 @@ static void cleanup_shared_heap(void)
 	void *membase = evl_get_heap_base(&evl_shared_heap);
 
 	evl_destroy_heap(&evl_shared_heap);
-	kfree(membase);
+	vfree(membase);
 }
 
 static int init_system_heap(void)
Signature · 1 line
2.34.1
#1 lore raw
Quoted 67 lines · Qichen Qiu

GDB was unable to inspect the evl shared heap mapping regions(i.e. __current_evl_window) due to limitations imposed by the VM_IO flag when ptrace accesses memory regions that were mapped using remap_pfn_range. This restriction hindered effective debugging.

To resolve this issue, the shared heap is now allocated using vzalloc, and the memory is mapped with vm_insert_page, which work seamlessly with GDB and ptrace.

---
 kernel/evl/control.c | 15 +++++++++++++--
 kernel/evl/memory.c  |  6 +++---
 2 files changed, 16 insertions(+), 5 deletions(-)
kernel/evl/control.c +13 -2
index 2d1b44f3b0dc..32764a9fc08b 100644
@@ -338,13 +338,24 @@ static long control_ioctl(struct file *filp, unsigned int cmd,
 static int control_mmap(struct file *filp, struct vm_area_struct *vma)
 {
 	void *p = evl_get_heap_base(&evl_shared_heap);
-	unsigned long pfn = __pa(p) >> PAGE_SHIFT;
 	size_t len = vma->vm_end - vma->vm_start;
+	unsigned long addr = vma->vm_start;
+	int err;
+	struct page *page;
 
 	if (len != evl_shm_size)
 		return -EINVAL;
+
+	while (addr < vma->vm_end) {
+		page = vmalloc_to_page(p);
+		err = vm_insert_page(vma, addr, page);
+		if (err < 0)
+			return err;
+		addr += PAGE_SIZE;
+		p += PAGE_SIZE;
+	}
 
-	return remap_pfn_range(vma, vma->vm_start, pfn, len, PAGE_SHARED);
+	return 0;
 }
 
 static const struct file_operations control_fops = {
kernel/evl/memory.c +2 -2
index 1da5be623d69..053a377396d2 100644
@@ -630,13 +630,13 @@ static int init_shared_heap(void)
 		CONFIG_EVL_NR_MONITORS *
 		sizeof(struct evl_monitor_state);
 	size = PAGE_ALIGN(size);
-	mem = kzalloc(size, GFP_KERNEL);
+	mem = vzalloc(size);
 	if (mem == NULL)
 		return -ENOMEM;
 
 	ret = evl_init_heap(&evl_shared_heap, mem, size);
 	if (ret) {
-		kfree(mem);
+		vfree(mem);
 		return ret;
 	}
 

kernel/evl/memory.c static void cleanup_shared_heap(void) · line 656

Quoted 8 lines
kernel/evl/memory.c +1 -1
@@ -650,7 +650,7 @@ static void cleanup_shared_heap(void)
 	void *membase = evl_get_heap_base(&evl_shared_heap);
 
 	evl_destroy_heap(&evl_shared_heap);
-	kfree(membase);
+	vfree(membase);
 }
 
 static int init_system_heap(void)

Merged (6.1.y, 6.6.y, 6.12), thanks.

Signature · 1 line
Philippe.