4
votes

I'm developing a Linux kernel module.I want to use mmap and proc filesystem to share memory between kernel and userspace. I've implemented some parts but I don't know how to complete it.

I want to write something (sk_buff) in kernel module and read them in userspace (read proc file and save the file). What should I do? I don't know the meaning of file_operations; should I implement fops.write or something else?

Here is my code demo, (a module for Linux 3.16):

#include <linux/version.h>
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/mm.h>
#include <linux/fs.h>
#include <linux/seq_file.h>

#define PROC_MEMSHARE_DIR "memshare"
#define PROC_MEMSHARE_INFO "phymem_info"
#define PROC_MMAP_FILE "mmap"

/* alloc one page. 4096 bytes */
#define PAGE_ORDER 0
/* this value can get from PAGE_ORDER */
#define PAGES_NUMBER 1

struct proc_dir_entry *proc_memshare_dir ;
unsigned long kernel_memaddr = 0;
unsigned long kernel_memsize= 0;

int proc_mmap(struct file *filp, struct vm_area_struct *vma)
{
    unsigned long page;
    page = virt_to_phys((void*)kernel_memaddr) >> PAGE_SHIFT;

    if (remap_pfn_range(vma, vma->vm_start, page, (vma->vm_end - vma->vm_start),
                vma->vm_page_prot))
    {
        printk("remap failed...");
        return -1;
    }
    vma->vm_flags |= (VM_DONTDUMP|VM_DONTEXPAND);
    printk("remap_pfn_rang page:[%lu] ok.\n", page);
    return 0;
}

static int proc_show_meminfo(struct seq_file *m, void *v) {
    seq_printf(m, "%08lx %lu\n",__pa(kernel_memaddr), kernel_memsize);
    return 0;
}

static int proc_open_meminfo(struct inode *inode, struct  file *file) {
    return single_open(file, proc_show_meminfo, NULL);
}

static const struct file_operations read_phymem_info_fops = {
    .owner = THIS_MODULE,
    .open = proc_open_meminfo,
    .read = seq_read,
    .llseek = seq_lseek,
    .release = seq_release
};

static const struct file_operations proc_mmap_fops = {
    .owner = THIS_MODULE,
    .mmap = proc_mmap
};

static int __init init(void)
{
    /* build proc dir "memshare"and two proc files: phymem_addr, phymem_size in the dir */
    proc_memshare_dir = proc_mkdir(PROC_MEMSHARE_DIR, NULL);
    proc_create_data(PROC_MEMSHARE_INFO, 0, proc_memshare_dir, &read_phymem_info_fops,NULL);
    proc_create_data(PROC_MMAP_FILE, 0, proc_memshare_dir, &proc_mmap_fops,NULL);

    /* alloc one page */
    kernel_memaddr =__get_free_pages(GFP_KERNEL, PAGE_ORDER);
    if (!kernel_memaddr) {
        printk("Allocate memory failure!/n");
    } else {
        SetPageReserved(virt_to_page(kernel_memaddr));
        kernel_memsize = PAGES_NUMBER * PAGE_SIZE;
        printk("Allocate memory success!. The phy mem addr=%08lx, size=%lu\n", __pa(kernel_memaddr), kernel_memsize);
    }
    return 0;
}

static void __exit fini(void)
{
    printk("The content written by user is: %s\n", (unsigned char*) kernel_memaddr);
    ClearPageReserved(virt_to_page(kernel_memaddr));
    free_pages(kernel_memaddr, PAGE_ORDER);
    remove_proc_entry(PROC_MEMSHARE_INFO, proc_memshare_dir);
    remove_proc_entry(PROC_MEMSHARE_DIR, NULL);
    return;
}
module_init(init);
module_exit(fini);
MODULE_LICENSE("GPL");
MOUDLE_AUTHOR("wack");
MODULE_DESCRIPTION("Kernel memory share module.");

And here is the userspace program:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/mman.h>

int main(int argc, char *argv[])
{
    char *str ;
    if (argc != 2) {
        printf("Usage: %s string\n", argv[0]);
        return 0;
    }

    unsigned long phymem_addr, phymem_size;
    char *map_addr;
    char s[256];
    int fd;

    /*get the physical address & size of allocated memory in kernel*/
    fd = open("/proc/memshare/phymem_info", O_RDONLY);
    if (fd < 0) {
        printf("cannot open file /proc/memshare/phymem_info\n");
        return 0;
    }
    read(fd, s, sizeof(s));
    sscanf(s, "%lx %lu", &phymem_addr,&phymem_size);
    close(fd);

    printf("phymem_addr=%lx, phymem_size=%lu\n", phymem_addr, phymem_size);
    /*memory map*/
    int map_fd = open("/proc/memshare/mmap",  O_RDWR|O_SYNC);
    if (map_fd < 0) {
        printf("cannot open file /proc/memshare/mmap\n");
        return -1;
    }
    map_addr = mmap(NULL, phymem_size, PROT_READ|PROT_WRITE, MAP_SHARED, map_fd, phymem_addr);
    if (map_addr ==MAP_FAILED) {
        perror("mmap");
        printf("MAP_FAILED : %s",map_addr);
        close(map_fd);
        return -1;
    } else{
        printf("mmap: %s \n",map_addr);
        printf("addr: %p \n",map_addr);
        printf("addr: %d \n",*map_addr);
    }
    //memcpy(map_addr, argv[1],sizeof(argv));
    strcpy(map_addr,argv[1]);
    memcpy(str,map_addr,256);
    printf("str is :%s \n",str);
    int ret = munmap(map_addr, phymem_size);
    if (ret) {
        printf("munmap failed:%d \n",ret);
    }
    close(map_fd);
    return 0;
}
1
I don't quite undestand: You have already implemented reading from your proc file (kernel writes to it, user read from it). So, what is a problem? Do you want to allow user to write your proc file?Tsyvarev
I want to write proc file in kernel module, and I don't know how to do. This code only read file but did not write...Can you tell me how to dump kernel message to proc file?(Indeed I want to dump sk_buff to the proc and read it in user space)wack
seq_file helps only with reading from the file. You should implement fops.write method for write into your proc file.Tsyvarev
In fact this is exactly what I wondered. What should I do in fops.write ? And how can I call this method fops.write in kernel module ?wack
I can write the memory kernel_memaddr directly, right ?wack

1 Answers

1
votes

The best I've been able to do on my own:

  • In this problem, I want to write sk_buff to shared memory and read it from user space.
  • Here, I use netfilter hook PRE_ROUTING, and in my hook I can get the skb.
  • On the question of how to write to shared memory, as suggested by user Tsyvarev I can write the memory I malloc directly.

First malloc shared memory:

kernel_memaddr = __get_free_pages(GFP_KERNEL, PAGE_ORDER);  //or use kmalloc vmalloc
SetPageReserved(virt_to_page(kernel_memaddr));

Userspace needs to know the memory physical address (proc read):

static int proc_show_meminfo(struct seq_file *m, void *v) {
    seq_printf(m, "%08lx %lu\n",__pa(kernel_memaddr), kernel_memsize);
    return 0;
}

In proc_mmap(), we need to remap_pfn_range the memory:

int proc_mmap(struct file *filp, struct vm_area_struct *vma)
{
    unsigned long page;
    page = virt_to_phys((void *)kernel_memaddr) >> PAGE_SHIFT;

    if( remap_pfn_range(vma, vma->vm_start, page, (vma->vm_end - vma->vm_start), 
         vma->vm_page_prot) )
    {
        printk("remap failed...");
        return -1;
    }
    vma->vm_flags |= (VM_DONTDUMP|VM_DONTEXPAND);
    printk("remap_pfn_rang page:[%lu] ok.\n", page);
    return 0;
}

When we want to write data to shared memory, just copy it:

memcpy((void *)(memaddr + offset),data, dataLen);   //data is what you want to write

And in user space, after we get kernel_memaddr, we use mmap() to get address

map_addr = mmap(NULL, phymem_size, PROT_READ|PROT_WRITE, MAP_SHARED, map_fd, phymem_addr);
memcpy(str,map_addr,256);