linux/lib/kunit/user_alloc.c
Peter Zijlstra cdd30ebb1b module: Convert symbol namespace to string literal
Clean up the existing export namespace code along the same lines of
commit 33def8498f ("treewide: Convert macro and uses of __section(foo)
to __section("foo")") and for the same reason, it is not desired for the
namespace argument to be a macro expansion itself.

Scripted using

  git grep -l -e MODULE_IMPORT_NS -e EXPORT_SYMBOL_NS | while read file;
  do
    awk -i inplace '
      /^#define EXPORT_SYMBOL_NS/ {
        gsub(/__stringify\(ns\)/, "ns");
        print;
        next;
      }
      /^#define MODULE_IMPORT_NS/ {
        gsub(/__stringify\(ns\)/, "ns");
        print;
        next;
      }
      /MODULE_IMPORT_NS/ {
        $0 = gensub(/MODULE_IMPORT_NS\(([^)]*)\)/, "MODULE_IMPORT_NS(\"\\1\")", "g");
      }
      /EXPORT_SYMBOL_NS/ {
        if ($0 ~ /(EXPORT_SYMBOL_NS[^(]*)\(([^,]+),/) {
  	if ($0 !~ /(EXPORT_SYMBOL_NS[^(]*)\(([^,]+), ([^)]+)\)/ &&
  	    $0 !~ /(EXPORT_SYMBOL_NS[^(]*)\(\)/ &&
  	    $0 !~ /^my/) {
  	  getline line;
  	  gsub(/[[:space:]]*\\$/, "");
  	  gsub(/[[:space:]]/, "", line);
  	  $0 = $0 " " line;
  	}

  	$0 = gensub(/(EXPORT_SYMBOL_NS[^(]*)\(([^,]+), ([^)]+)\)/,
  		    "\\1(\\2, \"\\3\")", "g");
        }
      }
      { print }' $file;
  done

Requested-by: Masahiro Yamada <masahiroy@kernel.org>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://mail.google.com/mail/u/2/#inbox/FMfcgzQXKWgMmjdFwwdsfgxzKpVHWPlc
Acked-by: Greg KH <gregkh@linuxfoundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2024-12-02 11:34:44 -08:00

118 lines
2.5 KiB
C

// SPDX-License-Identifier: GPL-2.0
/*
* KUnit userspace memory allocation resource management.
*/
#include <kunit/resource.h>
#include <kunit/test.h>
#include <linux/kthread.h>
#include <linux/mm.h>
struct kunit_vm_mmap_resource {
unsigned long addr;
size_t size;
};
/* vm_mmap() arguments */
struct kunit_vm_mmap_params {
struct file *file;
unsigned long addr;
unsigned long len;
unsigned long prot;
unsigned long flag;
unsigned long offset;
};
/* Create and attach a new mm if it doesn't already exist. */
static int kunit_attach_mm(void)
{
struct mm_struct *mm;
if (current->mm)
return 0;
/* arch_pick_mmap_layout() is only sane with MMU systems. */
if (!IS_ENABLED(CONFIG_MMU))
return -EINVAL;
mm = mm_alloc();
if (!mm)
return -ENOMEM;
/* Define the task size. */
mm->task_size = TASK_SIZE;
/* Make sure we can allocate new VMAs. */
arch_pick_mmap_layout(mm, &current->signal->rlim[RLIMIT_STACK]);
/* Attach the mm. It will be cleaned up when the process dies. */
kthread_use_mm(mm);
return 0;
}
static int kunit_vm_mmap_init(struct kunit_resource *res, void *context)
{
struct kunit_vm_mmap_params *p = context;
struct kunit_vm_mmap_resource vres;
int ret;
ret = kunit_attach_mm();
if (ret)
return ret;
vres.size = p->len;
vres.addr = vm_mmap(p->file, p->addr, p->len, p->prot, p->flag, p->offset);
if (!vres.addr)
return -ENOMEM;
res->data = kmemdup(&vres, sizeof(vres), GFP_KERNEL);
if (!res->data) {
vm_munmap(vres.addr, vres.size);
return -ENOMEM;
}
return 0;
}
static void kunit_vm_mmap_free(struct kunit_resource *res)
{
struct kunit_vm_mmap_resource *vres = res->data;
/*
* Since this is executed from the test monitoring process,
* the test's mm has already been torn down. We don't need
* to run vm_munmap(vres->addr, vres->size), only clean up
* the vres.
*/
kfree(vres);
res->data = NULL;
}
unsigned long kunit_vm_mmap(struct kunit *test, struct file *file,
unsigned long addr, unsigned long len,
unsigned long prot, unsigned long flag,
unsigned long offset)
{
struct kunit_vm_mmap_params params = {
.file = file,
.addr = addr,
.len = len,
.prot = prot,
.flag = flag,
.offset = offset,
};
struct kunit_vm_mmap_resource *vres;
vres = kunit_alloc_resource(test,
kunit_vm_mmap_init,
kunit_vm_mmap_free,
GFP_KERNEL,
&params);
if (vres)
return vres->addr;
return 0;
}
EXPORT_SYMBOL_GPL(kunit_vm_mmap);
MODULE_IMPORT_NS("EXPORTED_FOR_KUNIT_TESTING");