1 // SPDX-License-Identifier: GPL-2.0
3 * KUnit userspace memory allocation resource management.
5 #include <kunit/resource.h>
6 #include <kunit/test.h>
7 #include <linux/kthread.h>
10 struct kunit_vm_mmap_resource {
15 /* vm_mmap() arguments */
16 struct kunit_vm_mmap_params {
25 /* Create and attach a new mm if it doesn't already exist. */
26 static int kunit_attach_mm(void)
33 /* arch_pick_mmap_layout() is only sane with MMU systems. */
34 if (!IS_ENABLED(CONFIG_MMU))
41 /* Define the task size. */
42 mm->task_size = TASK_SIZE;
44 /* Make sure we can allocate new VMAs. */
45 arch_pick_mmap_layout(mm, ¤t->signal->rlim[RLIMIT_STACK]);
47 /* Attach the mm. It will be cleaned up when the process dies. */
53 static int kunit_vm_mmap_init(struct kunit_resource *res, void *context)
55 struct kunit_vm_mmap_params *p = context;
56 struct kunit_vm_mmap_resource vres;
59 ret = kunit_attach_mm();
64 vres.addr = vm_mmap(p->file, p->addr, p->len, p->prot, p->flag, p->offset);
67 res->data = kmemdup(&vres, sizeof(vres), GFP_KERNEL);
69 vm_munmap(vres.addr, vres.size);
76 static void kunit_vm_mmap_free(struct kunit_resource *res)
78 struct kunit_vm_mmap_resource *vres = res->data;
81 * Since this is executed from the test monitoring process,
82 * the test's mm has already been torn down. We don't need
83 * to run vm_munmap(vres->addr, vres->size), only clean up
91 unsigned long kunit_vm_mmap(struct kunit *test, struct file *file,
92 unsigned long addr, unsigned long len,
93 unsigned long prot, unsigned long flag,
96 struct kunit_vm_mmap_params params = {
104 struct kunit_vm_mmap_resource *vres;
106 vres = kunit_alloc_resource(test,
115 EXPORT_SYMBOL_GPL(kunit_vm_mmap);
117 MODULE_IMPORT_NS("EXPORTED_FOR_KUNIT_TESTING");