~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/lib/kunit/user_alloc.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0
  2 /*
  3  * KUnit userspace memory allocation resource management.
  4  */
  5 #include <kunit/resource.h>
  6 #include <kunit/test.h>
  7 #include <linux/kthread.h>
  8 #include <linux/mm.h>
  9 
 10 struct kunit_vm_mmap_resource {
 11         unsigned long addr;
 12         size_t size;
 13 };
 14 
 15 /* vm_mmap() arguments */
 16 struct kunit_vm_mmap_params {
 17         struct file *file;
 18         unsigned long addr;
 19         unsigned long len;
 20         unsigned long prot;
 21         unsigned long flag;
 22         unsigned long offset;
 23 };
 24 
 25 /* Create and attach a new mm if it doesn't already exist. */
 26 static int kunit_attach_mm(void)
 27 {
 28         struct mm_struct *mm;
 29 
 30         if (current->mm)
 31                 return 0;
 32 
 33         /* arch_pick_mmap_layout() is only sane with MMU systems. */
 34         if (!IS_ENABLED(CONFIG_MMU))
 35                 return -EINVAL;
 36 
 37         mm = mm_alloc();
 38         if (!mm)
 39                 return -ENOMEM;
 40 
 41         /* Define the task size. */
 42         mm->task_size = TASK_SIZE;
 43 
 44         /* Make sure we can allocate new VMAs. */
 45         arch_pick_mmap_layout(mm, &current->signal->rlim[RLIMIT_STACK]);
 46 
 47         /* Attach the mm. It will be cleaned up when the process dies. */
 48         kthread_use_mm(mm);
 49 
 50         return 0;
 51 }
 52 
 53 static int kunit_vm_mmap_init(struct kunit_resource *res, void *context)
 54 {
 55         struct kunit_vm_mmap_params *p = context;
 56         struct kunit_vm_mmap_resource vres;
 57         int ret;
 58 
 59         ret = kunit_attach_mm();
 60         if (ret)
 61                 return ret;
 62 
 63         vres.size = p->len;
 64         vres.addr = vm_mmap(p->file, p->addr, p->len, p->prot, p->flag, p->offset);
 65         if (!vres.addr)
 66                 return -ENOMEM;
 67         res->data = kmemdup(&vres, sizeof(vres), GFP_KERNEL);
 68         if (!res->data) {
 69                 vm_munmap(vres.addr, vres.size);
 70                 return -ENOMEM;
 71         }
 72 
 73         return 0;
 74 }
 75 
 76 static void kunit_vm_mmap_free(struct kunit_resource *res)
 77 {
 78         struct kunit_vm_mmap_resource *vres = res->data;
 79 
 80         /*
 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
 84          * the vres.
 85          */
 86 
 87         kfree(vres);
 88         res->data = NULL;
 89 }
 90 
 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,
 94                             unsigned long offset)
 95 {
 96         struct kunit_vm_mmap_params params = {
 97                 .file = file,
 98                 .addr = addr,
 99                 .len = len,
100                 .prot = prot,
101                 .flag = flag,
102                 .offset = offset,
103         };
104         struct kunit_vm_mmap_resource *vres;
105 
106         vres = kunit_alloc_resource(test,
107                                     kunit_vm_mmap_init,
108                                     kunit_vm_mmap_free,
109                                     GFP_KERNEL,
110                                     &params);
111         if (vres)
112                 return vres->addr;
113         return 0;
114 }
115 EXPORT_SYMBOL_GPL(kunit_vm_mmap);
116 
117 MODULE_IMPORT_NS(EXPORTED_FOR_KUNIT_TESTING);
118 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php