ITKeyword,专注技术干货聚合推荐

注册 | 登录

unable to open / read text file from kernel module on linux kernel version 4.2.3

itPublisher 分享于

2021腾讯云限时秒杀,爆款1核2G云服务器298元/3年!(领取2860元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1062

2021阿里云最低价产品入口+领取代金券(老用户3折起),
入口地址https://www.aliyun.com/minisite/goods

推荐:Linux kernel & module program

Linux内核/模块开发常见问题集(FAQ)(转载) 1. 请推荐一些好的Linux内核参考书? a.《Linux Device Drivers, 2nd Edition》,有中文译本   b.《Understanding the

I have written a kernel module which I'm loading on kernel 4.2.3 . I am trying to read a simple text file in my init_module which basically loads some configuration data by reading the contents of the text file. This same code works on previous versions of kernel but not on 4.2.3 Below is my code snippet for reference :

struct file* pFile = NULL;
pFile = filp_open(fileName, mode, 0);
if(pFile != NULL){
if(IS_ERR(pFile))
{
  printk("<1>error %p for %s**\n", pFile, fileName);
  pFile = NULL;
}
else if(pFile->f_op->read == NULL || pFile->f_op->write == NULL)
{
  filp_close(pFile, 0);
  pFile = NULL;
}

In my case I am getting pFile->f_op->read function pointer as NULL. This code works fine for non text files like /proc/kallsyms which I am able to open & read. Please provide me some pointers as to is this a 4.2.3 kernel specific issue, how can i workaround this in my kernel module code ? Any pointers would be very helpful.

file linux-kernel kernel kernel-module
|
  this question
edited Oct 17 '15 at 7:17 Tejus Prasad 3,149 4 21 53 asked Oct 17 '15 at 7:13 atish 11 2      It's usually bad idea to mess with files in kernel. Why do you need to do that in the first place? Also, I'm encouraging you to stick to kernel coding style if you are expecting your code to go to the upstream (and frankly I've just skipped reading your code as it's completely unreadable for me). –  Sam Protsenko Oct 17 '15 at 11:34 1   yeah i know all that , but what is it in 4.2.3 whats causing this behaviour . Its a pretty simple piece of code and anyone who has dealt with kernel modules should be able to comprehend it easily . –  atish Oct 17 '15 at 17:08

 | 

1 Answers
1

.read is not the only interface which can implement reading from file. Files also may use .read_iter for that.

For reading file, instead of direct call to ->read, use

ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)

which takes into account every possibility.

Similary, for writting file

推荐:The Linux Kernel Module Programming Guide

原文地址:http://tldp.org/LDP/lkmpg/2.6/html/   Table of Contents Foreword 1. Authorship 2. Versioning and Notes 3. Acknowledgements 1. Introduction 1

ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)

should be used.

Reading from file to kernel's buffer

Because vfs_read expects buffer pointed to user-space memory (__user type attribute denotes that), passing in-kernel buffer will not work: it may cause compiler warning about inconsysency between expected and actual type of second parameter to vfs_read, and, more important, vfs_read will reject (by returning -EFAULT) buffer as pointed not to user space. But one can overcome this behaviour by changing user-space memory segment:

/*
 * Assume that `kernel_buf` points to kernel's memory and has type char*.
 */
char __user *user_buf = (__force char __user *)kernel_buf; // Make compiler happy.
mm_segment_t oldfs = get_fs(); // Store current use-space memory segment.
set_fs(KERNEL_DS); // Set user-space memory segment equal to kernel's one.

vfs_read(file, user_buf, count, pos);

set_fs(oldfs); // Restore user-space memory segment after reading.

|
  this answer
edited Oct 19 '15 at 7:31 answered Oct 17 '15 at 22:12 Tsyvarev 13.1k 3 10 29      I have tried vfs_read as well but same results . –  atish Oct 18 '15 at 5:08      What exactly you get with vfs_read? Does function call fail with error? Which error code it returns? vfs_read is what syscall read uses after it resolves file object. So, if vfs_read fails, reading file from user-space would likely fail too. –  Tsyvarev Oct 18 '15 at 15:08      I m getting error code EFAULT as return value from vfs_read . –  atish Oct 18 '15 at 17:51      EFAULT means that buf is not allocated by user. You probably use in-kernel buffer, which is not quite correct here. You may try to use __vfs_read instead vfs_read: it doesn't check buffer using access_ok. But nothing prevents .read interface of file to perform same check. –  Tsyvarev Oct 18 '15 at 21:08      I am using kmalloc (size , GFP_KERNEL ) to allocate buffer where i wish to read the contents of the file . Passing this buffer as the 2nd argument in vfs_read call . –  atish Oct 19 '15 at 3:52  |  show more comments

推荐:Linux Debugging 8 - Kernel Module

refer to http://tldp.org/LDP/lkmpg/2.6/html/   The Linux Kernel Module Programming Guide    


相关阅读排行


相关内容推荐

最新文章

×

×

请激活账号

为了能正常使用评论、编辑功能及以后陆续为用户提供的其他产品,请激活账号。

您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱

如果您没有收到激活邮件,请注意检查垃圾箱。