2
votes

I am trying to open a shm file for data sharing between kernel and user process. Below is my kernel module code. Sys log output indicates that no errors were encountered. Once the module is loaded, I don't see the file that I'm trying to create in /dev/shm. What am I doing wrong?

#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/shmem_fs.h>

static int __init moduleLoader(void) {
    struct file *shfile;

    printk(KERN_INFO "shkmod is loading...\n");

    shfile = shmem_file_setup("MyShmFile",sizeof(int),0);
    printk("shmem_file_setup returned %p\n",shfile);
    if (IS_ERR(shfile))
        printk("shmem_file_setup returned error.\n");
    printk("shfile is: %s\n", shfile->f_path.dentry->d_name.name);

    printk(KERN_INFO "shkmod is done loading.\n");
    return 0;
}

static void __exit moduleUnloader(void) {
    printk(KERN_INFO "shkmod was unloaded.\n");
    return;
}

module_init(moduleLoader);
module_exit(moduleUnloader);

MODULE_AUTHOR("Yaron Shragai");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Lorem ipsum dolor sit amet");

Here is the output in the sys log:

Jun  6 15:25:59 yaron-VirtualBox kernel: [457160.335482] shkmod is loading...
Jun  6 15:25:59 yaron-VirtualBox kernel: [457160.335487] shmem_file_setup returned ffff8801fef53b00
Jun  6 15:25:59 yaron-VirtualBox kernel: [457160.335488] shfile is: MyShmFile
Jun  6 15:25:59 yaron-VirtualBox kernel: [457160.335488] shkmod is done loading.

$ uname -a
Linux yaron-VirtualBox 4.4.0-57-generic #78-Ubuntu SMP Fri Dec 9 23:50:32 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

1
add "C" tag to your question that will help you.danglingpointer
OK, added "c" tag! :-)Yaron Shragai

1 Answers

1
votes
/**
 * shmem_file_setup - get an unlinked file living in tmpfs

The comment explicitly states the file is not going to appear anywhere.

 * @name: name for dentry (to be seen in /proc/<pid>/maps

... and the name is to have something to be shown in maps.

 * @size: size to be set for the file
 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
 */
struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
{
        return __shmem_file_setup(name, size, flags, 0);
}

It makes sense though. You did not specify any filesystem, just used a name which is a relative path to begin with. Why did you expect the file to show up in /dev specifically?

As usual the real question is what is the actual problem you are working on.

If it happens that a shared file is indeed the way to go, userspace should create one and tell the kernel to use it.