简体   繁体   中英

Compiling linux kernel module show_mem routine

I am trying to invoke show_mem() from mm.h in a user defined kernel module. When I compile it shows show_mem undefined. I am running Ubuntu 14.04 and have a compiled linux kernel 3.19 .

/*
 * Author - [Deepak]
 */
#include <linux/module.h>   /* Needed by all modules */
#include <linux/kernel.h>   /* Needed for KERN_INFO */
#include <linux/init.h>     /* Needed for the macros */
#include <linux/mm.h>       /* Needed for show_mem */
#include <asm/cacheflush.h>
#include <linux/mm.h>


static char *user_data1 __initdata = "Hello World";
static int *user_data2 __initdata = 2;

static int __init starter(void)
{
    printk(KERN_INFO "[ds494] Loading Hello2 module - %s %d \n",user_data1,user_data2);
    show_mem(1);
    return 0;
}

static void __exit ending(void)
{
    printk(KERN_INFO "[ds494] Exiting Hello2 module - Goodbye World 2\n");
}

module_init(starter);
module_exit(ending);

And below is the make file -

obj-m += memmod.o

all:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

And I get the below error -

  **MODPOST 1 modules
WARNING: "show_mem" [/home/deepak/cs/hw/homework_4/memmod.ko]     undefined!
  LD [M]  /home/deepak/cs/hw/homework_4/memmod.ko
make[1]: Leaving directory '/home/deepak/Downloads/linux-3.19'**

Any suggestions, please.

Root cause .

You can't use show_mem() function in loadable modules, because it's not exported by EXPORT_SYMBOL .

Possible solutions .

Basically you have 3 options how to workaround this problem.

  1. Modifying kernel sources

    • download kernel source (see this )
    • add code for exporting this symbol ( like this , but this patch is intended for ARM architecture, for x86 see lib/show_mem.c )
    • build your module using those modified sources

    If you also need to run your module -- you will need to build your customized kernel first and run it (instead of Ubuntu stock kernel).

    It's not upstreamable solution though and quite frankly nobody will be able to use your module (you will need to provide modified kernel as well).

  2. Compiling your module as built-in module.

    It can be done inside of kernel tree, using obj-y instead of obj-m ). In that case you will be able to use show_mem() function. Just like the first option, this option implies modifying of kernel sources.

  3. Write your own show_mem() implementation.

    I'm not sure about this one, though, because it may turn out that you can't use API needed for this task in loadable module at all. It's also can be quite difficult to implement this.

Conclusion .

  • If it's only educational task (which I guess it is), I'd say go with first option.
  • If you really need to implement that as a loadable module, and you can't modify kernel sources, I afraid you only have 3rd option, which is hardest one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM