Print Virtual Address of mem_map using a proc file

2019-09-10 19:00发布

问题:

I have to print the contents of the mem_map variable in the kernel.

However when I compile my code by issuing make I see:

WARNING: "mem_map" [/home/babak/code/module/mem_map.ko] undefined!

from:

make -C /home/babak/code/linux-3.19.5 M=/home/babak/code/module modules
make[1]: Entering directory '/home/babak/code/linux-3.19.5'
  CC [M]  /home/babak/code/module/mem_map.o
  Building modules, stage 2.
  MODPOST 1 modules
WARNING: "mem_map" [/home/babak/code/module/mem_map.ko] undefined!
  LD [M]  /home/babak/code/module/mem_map.ko
make[1]: Leaving directory '/home/babak/code/linux-3.19.5'

There are the headers I have included, my understanding is that is mem_map is supposed to be in the mmzone.h I can't figure out why its not picking up the variable.

#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>

#include <linux/types.h> /* size_t */
#include <linux/fcntl.h> /* O_ACCMODE */
#include <asm/switch_to.h>
#include <asm/uaccess.h> /* copy_from/to_user */
#include <linux/fs.h>       // for basic filesystem
#include <linux/proc_fs.h>  // for the proc filesystem
#include <linux/seq_file.h> // for sequence files
#include <linux/mmzone.h>

MODULE_LICENSE("Dual BSD/GPL");

static struct proc_dir_entry* proc_file;


/* memory map functions */
int mem_map_show(struct seq_file *m, void *v);
//virtual_to_physical
inline unsigned long virt_to_phy(unsigned long addr);

inline unsigned long virt_to_phy(unsigned long addr){
    return __pa(addr);
}


int mem_map_show(struct seq_file *m, void *v){

    int ret_val = 0;

    printk(KERN_INFO "Proc file read \n");
    ret_val =  seq_printf(m, "mem_map virt addr: %p \n", mem_map);
    ret_val += seq_printf(m, "mem_map phys addr: %lu \n",virt_to_phy((unsigned long)mem_map));
    ret_val += seq_printf(m, "mem_map phys pages: %lu \n", (long unsigned int)get_num_physpages);

    return ret_val;
}

static int mem_map_open(struct inode *inode, struct file *file){
    return single_open(file, mem_map_show, NULL);
}

struct file_operations mem_map_fops = {
    .owner = THIS_MODULE,
    .open = mem_map_open,
    .read = seq_read,
    .llseek = seq_lseek,
    .release = single_release,
};

static int __init mem_map_init(void){
    printk(KERN_INFO "Loaded mem_map module\n");
    proc_file = proc_create("mem_map", 0, NULL, &mem_map_fops);
    if(!proc_file){
        printk(KERN_ALERT "Error: Could not initialize /proc/mem_map");
        return -ENOMEM;
    }   
    return 0;
}

static void __exit mem_map_exit(void){
    remove_proc_entry("mem_map",NULL);  
    printk(KERN_INFO "Proc file unloaded \n");
}


/* Declaration of the init and exit functions */
module_init(mem_map_init);
module_exit(mem_map_exit);

回答1:

Warning at linker stage like

WARNING: "mem_map" [/home/babak/code/module/mem_map.ko] undefined!

means that mem_map is declared, but its definition is not accessible for modules.

So you need search statements like EXPORT_SYMBOL(mem_map) or EXPORT_SYMBOL_GPL(mem_map) in sources (.c files).

Found that for kernel 3.19 (mm/memory.c):

#ifndef CONFIG_NEED_MULTIPLE_NODES
/* use the per-pgdat data instead for discontigmem - mbligh */
unsigned long max_mapnr;
struct page *mem_map;

EXPORT_SYMBOL(max_mapnr);
EXPORT_SYMBOL(mem_map);
#endif

Probably, you haven't CONFIG_NEED_MULTIPLE_NODES kernel's configuration set, so symbol mem_map is not exported(and even not defined).