2016-04-13 64 views
3

我使用mmap在內核中創建了共享內存段。我需要從內核和用戶空間訪問這個映射的內存。我應該使用什麼機制來保護內存不受併發訪問? 我想有這樣的:保護內核和用戶空間之間的共享內存段

內核模塊:

#include <linux/module.h> 
#include <linux/kernel.h> 
#include <linux/init.h> 
#include <linux/fs.h> 
#include <linux/debugfs.h> 
#include <linux/slab.h> 
#include <linux/mm.h> 

#ifndef VM_RESERVED 
# define VM_RESERVED (VM_DONTEXPAND | VM_DONTDUMP) 
#endif 

struct dentry *file; 

struct mmap_info 
{ 
    char *data;    
    int reference;  
}; 

void mmap_open(struct vm_area_struct *vma) 
{ 
    struct mmap_info *info = (struct mmap_info *)vma->vm_private_data; 
    info->reference++; 
} 

void mmap_close(struct vm_area_struct *vma) 
{ 
    struct mmap_info *info = (struct mmap_info *)vma->vm_private_data; 
    info->reference--; 
} 

static int mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf) 
{ 
    struct page *page; 
    struct mmap_info *info;  

    info = (struct mmap_info *)vma->vm_private_data; 
    if (!info->data) 
    { 
     printk("No data\n"); 
     return 0;  
    } 
    page = virt_to_page(info->data);  
    get_page(page); 
    vmf->page = page;    
    return 0; 
} 

struct vm_operations_struct mmap_vm_ops = 
{ 
    .open =  mmap_open, 
    .close = mmap_close, 
    .fault = mmap_fault,  
}; 

int op_mmap(struct file *filp, struct vm_area_struct *vma) 
{ 
    vma->vm_ops = &mmap_vm_ops; 
    vma->vm_flags |= VM_RESERVED;  
    vma->vm_private_data = filp->private_data; 
    mmap_open(vma); 
    return 0; 
} 

int mmapfop_close(struct inode *inode, struct file *filp) 
{ 
    struct mmap_info *info = filp->private_data; 
    free_page((unsigned long)info->data); 
    kfree(info); 
    filp->private_data = NULL; 
    return 0; 
} 

int mmapfop_open(struct inode *inode, struct file *filp) 
{ 
    struct mmap_info *info = kmalloc(sizeof(struct mmap_info), GFP_KERNEL);  
    info->data = (char *)get_zeroed_page(GFP_KERNEL); 
    memcpy(info->data, "hello from kernel this is file: ", 32); 
    memcpy(info->data + 32, filp->f_dentry->d_name.name, strlen(filp->f_dentry->d_name.name)); 
    /* assign this info struct to the file */ 
    filp->private_data = info; 
    return 0; 
} 

static const struct file_operations mmap_fops = { 
    .open = mmapfop_open, 
    .release = mmapfop_close, 
    .mmap = op_mmap, 
}; 

static int __init mmapexample_module_init(void) 
{ 
    file = debugfs_create_file("mmap_example", 0644, NULL, NULL, &mmap_fops); 
    return 0; 
} 

static void __exit mmapexample_module_exit(void) 
{ 
    debugfs_remove(file); 
} 

module_init(mmapexample_module_init); 
module_exit(mmapexample_module_exit); 
MODULE_LICENSE("GPL"); 

用戶空間:

#include <stdio.h> 
#include <string.h> 
#include <fcntl.h> 
#include <sys/mman.h> 

#define PAGE_SIZE  4096 

int main (int argc, char **argv) 
{ 
    int configfd; 
    char * address = NULL; 

    configfd = open("/sys/kernel/debug/mmap_example", O_RDWR); 
    if(configfd < 0) 
    { 
     perror("Open call failed"); 
     return -1; 
    } 

    address = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, configfd, 0); 
    if (address == MAP_FAILED) 
    { 
     perror("mmap operation failed"); 
     return -1; 
    } 
    printf("Initial message: %s\n", address); 
    memcpy(address + 11 , "*user*", 6); 
    printf("Changed message: %s\n", address); 
    close(configfd);  
    return 0; 
} 

但鎖。

+0

請將相關代碼**複製到問題本身**中。一個問題應該是自立的,而不是假設你需要閱讀外部鏈接來理解其核心內容。 –

+0

ok,複製粘貼代碼 –

回答

1

內核空間和用戶空間沒有共享併發訪問保護機制。如果你想要它們,你需要自己實現它們。

它可以是某種互斥的,你的內核模塊中實現,並通過特殊的ioctl請求從用戶空間訪問:

內核:

DECLARE_WAIT_QUEUE_HEAD(wq); 
int my_mutex_val = 0; 

/* 
* Lock mutex. 
* 
* May be used directly by the kernel or via 'ioctl(MY_CMD_LOCK)' by user. 
*/ 
void my_mutex_lock(void) 
{ 
    spin_lock(&wq.lock); 
    wait_event_interruptible_locked(&wq, my_mutex_val == 0); 
    my_mutex_val = 1; 
    spin_unlock(&wq.lock); 
} 

/* 
* Unlock mutex. 
* 
* May be used directly by the kernel or via 'ioctl(MY_CMD_UNLOCK)' by user. 
*/ 
void my_mutex_unlock(void) 
{ 
    spin_lock(&wq.lock); 
    my_mutex_val = 0; 
    wake_up(&wq); 
    spin_unlock(&wq.lock); 
} 

long unlocked_ioctl (struct file * filp, unsigned int cmd, unsigned long val) 
{ 
    switch(cmd) { 
    case MY_CMD_LOCK: 
     my_mutex_lock(); 
    break; 
    case MY_CMD_UNLOCK: 
     my_mutex_unlock(); 
    break; 
    } 
} 

用戶:

int main() 
{ 
    ... 
    ioctl(MY_CMD_LOCK); 
    <read data> 
    ioctl(MY_CMD_UNLOCK); 
    ... 
} 

它可以是某種自旋鎖,其值存儲在mmap-ed區域(對於內核空間和用戶空間均可見)。

在任何情況下,內核模塊都應該準備好的情況下,當用戶空間應用程序未遵循鎖定約定。這可能會取消對內核生成的mmap-ed區域內容的任何期望,但在這種情況下,內核模塊不應該崩潰。 [這就是上面代碼中沒有使用標準內核的struct mutex的原因:用戶空間可能使用不正確]。

0

ioctl的問題在於,您每次需要訪問共享信息 - >數據時都需要內核切換。如果那沒關係,那麼ioctl是好的 - 但是爲什麼不直接做一個標準的字符讀/寫文件操作呢?

你也可以嘗試一個無鎖機制。在共享信息 - >數據區域添加一個障礙變量。當用戶需要訪問時,它會對barrier變量執行一個atomic_compare_and_xchg,直到它被設置爲0(未使用),然後將其設置爲1.當內核需要訪問時,它將執行相同操作,但將其設置爲2.請參閱gcc原子內置文件。

+0

另外,用戶空間和內核空間都會在不訪問共享內存區域時將barrier變量設置爲0。 – dturvene

相關問題