内核模块无法链接-找不到符号Mutex_lock_nested [英] kernel module won't link - symbol mutex_lock_nested not found
问题描述
我正在尝试在x64上为Linux 3.10.45构建内核模块(用于硬件的压力测试工具). 到目前为止,在添加互斥锁之前,它似乎还可以正常工作.
I am trying to build a kernel module (stress-test tool for a hardware) for a Linux 3.10.45 on x64. So far it seemed to work fine, until adding a mutex.
我添加了互斥锁,并使用了互斥锁,函数互斥锁,互斥锁和互斥锁.
I added mutex using and the functions mutex_init, mutex_lock, mutex_unlock and mutex_destroy.
构建模块不会产生任何错误或警告,但是在加载'insmod'时,dmesg中会显示错误消息:
Building the module yielded no errors or warnings, but when loading with 'insmod', there are error messages in dmesg:
[76603.744551] tryBlk: Unknown symbol mutex_lock_nested (err 0)
[76603.744574] tryBlk: Unknown symbol mutex_destroy (err 0)
我发现一个提示,即未知符号"有时会有助于添加MODULE_LICENSE("GPL v2")行.
I found a hint that with 'Unknown symbol', it sometimes helps to add the MODULE_LICENSE("GPL v2") line.
没有区别.
看着linux/mutex.h,我发现只有定义了符号CONFIG_DEBUG_LOCK_ALLOC时,mutex_lock才会定义为Mutex_lock_nested.检查一下,它似乎是在我的.config文件中定义的. (不记得碰它.它基本上只是kernel.org的内核,已构建).
Looking at linux/mutex.h, I found that the mutex_lock will only define to mutex_lock_nested if the symbol CONFIG_DEBUG_LOCK_ALLOC is defined. Checking this, it seems to be defined in my .config . (Cannot remember touching it. It is basically just a kernel from kernel.org, built).
这有问题吗?我是否需要手动向模块添加其他内容以使其具有此调试功能?
Is there a problem with this? Do I need to manually add something else to my module to make it build with this debug-feature?
试图更改包含文件和顺序.没什么.
Tried to change include files and sequence. No difference.
系统正在运行Debian-7'Wheezy'x64,内核更改为3.10.45.
System is running Debian-7 'Wheezy' x64, with kernel changed to a 3.10.45 .
使用互斥锁的文件:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/mutex.h>
#include <linux/vmalloc.h>
#include <linux/uaccess.h>
#include "ring.h"
struct RingBuf
{
unsigned char *buffer;
unsigned int size;
unsigned int inp,outp;
struct mutex mtx;
};
static int _bytesavail(struct RingBuf *self);
static int _spaceavail(struct RingBuf *self);
struct RingBuf *RingBuf_init(unsigned int size)
{
struct RingBuf *self;
if( size<16 )size=16;
if( size>0x10000000u )return 0;
if( size & (size-1) )
{
unsigned int ns;
// is not a power of 2.
size = size<<1;
while(1)
{
ns=size&(size-1);
if(!ns)break;
size=ns;
}
}
self = (struct RingBuf*)vmalloc(sizeof(*self)+size);
memset( self , 0 , sizeof(*self) );
self->buffer = (unsigned char*)(self+1);
self->size = size;
self->inp = 0;
self->outp = 0;
mutex_init( &(self->mtx) );
return self;
}
void RingBuf_uninit(struct RingBuf *self)
{
if(!self)return;
mutex_lock( &(self->mtx) );
mutex_destroy( &(self->mtx) );
memset( self , 0xFE , sizeof(*self) );
vfree(self);
}
int RingBuf_add(struct RingBuf *self,const void *data,int num)
{
int cpy;
if(num<=0)return 0;
mutex_lock( &(self->mtx) );
// check amount to copy
cpy = _spaceavail(self);
if(cpy>num)cpy=num;
// one part or split
if( self->inp+cpy <= self->size )
{
// one chunk
memcpy( self->buffer+self->inp , data , cpy );
}else{
int p1 = (self->size-self->inp);
// wrapped
memcpy( self->buffer+self->inp , data , p1 );
memcpy( self->buffer , ((const unsigned char*)data)+p1 , cpy-p1 );
}
self->inp = (self->inp+cpy) & (self->size-1) ;
mutex_unlock( &(self->mtx) );
return cpy;
}
int RingBuf_get(struct RingBuf *self,void *data,int num)
{
int cpy;
if(num<=0)return 0;
mutex_lock( &(self->mtx) );
// check amount to copy
cpy = _bytesavail(self);
if(cpy>num)cpy=num;
// one part or split
if( self->outp+cpy <= self->size )
{
// one chunk
memcpy( data , self->buffer+self->outp , cpy );
}else{
int p1 = (self->size-self->outp);
// wrapped
memcpy( data , self->buffer+self->outp , p1 );
memcpy( ((unsigned char*)data)+p1 , self->buffer , cpy-p1 );
}
self->outp = (self->outp+cpy) & (self->size-1) ;
mutex_unlock( &(self->mtx) );
return cpy;
}
int RingBuf_get_user(struct RingBuf *self,void __user *data,int num)
{
int cpy;
int ret;
if(num<=0)return 0;
mutex_lock( &(self->mtx) );
// check amount to copy
cpy = _bytesavail(self);
if(cpy>num)cpy=num;
// one part or split
if( self->outp+cpy <= self->size )
{
// one chunk
ret = copy_to_user( data , self->buffer+self->outp , cpy );
}else{
int p1 = (self->size-self->outp);
// wrapped
ret = copy_to_user( data , self->buffer+self->outp , p1 );
if(!ret)
ret = copy_to_user( ((unsigned char*)data)+p1 , self->buffer , cpy-p1 );
}
if(ret)return -1;
self->outp = (self->outp+cpy) & (self->size-1) ;
mutex_unlock( &(self->mtx) );
return cpy;
}
int RingBuf_numBytes(struct RingBuf *self)
{
int result;
mutex_lock( &(self->mtx) );
result = _bytesavail(self);
mutex_unlock( &(self->mtx) );
return result;
}
static int _bytesavail(struct RingBuf *self)
{
return (self->inp-self->outp)&(self->size-1);
}
static int _spaceavail(struct RingBuf *self)
{
return (self->outp-self->inp-1)&(self->size-1);
}
推荐答案
我刚刚发现它在某种程度上取决于我如何构建模块.
I just found that it somehow depends on how I am building the module.
互斥锁的内容位于另一个源文件(上面列出的文件)中.当我将其剪切/粘贴到模块的第一个主源中时,它就可以工作.
The mutex stuff is in another source-file (the one listed above). When I cut/paste it into the first main-source of the module, it works.
因此,如何创建一个跨多个源文件的模块是一个问题.
So it is a problem of how to create a module spanning several source files.
我的(不正确地工作)Makefile是:
My (NOT CORRECTLY WORKING) Makefile is:
obj-m += tryBlk.o
tryBlk-objs := ring.o
它的构建没有警告,但是没有正确找到'ring.o'文件中的内核符号.
It builds without warnings, but the kernel-symbols in the 'ring.o' file are not correctly found.
================================================
===============================================
我现在也解决了另一个问题.要共享,makefile不好.
I also solved the other problem now. To share, the makefile was bad.
这是更好的makefile:
Here the better makefile:
obj-m := tryBlk.o
tryBlk-objs := tryBlk_main.o ring.o
我还需要将主体部分从"tryBlk.c"重命名为"tryBlk_main.c"
I also needed to rename the main part from 'tryBlk.c' to 'tryBlk_main.c'
这篇关于内核模块无法链接-找不到符号Mutex_lock_nested的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!