0

I am an absolute newbie on Linux kernel. Sincere apologies if this has been answered. I have spent many hours and could not resolve it and hence decided to ask (reading Linux device drivers book as well). My problem statement: I would like to read a proc file (/proc/pid/maps) in my kernel module (a few more). There are numerous examples on proc_create which create a file and then write/read to it. I just want a read to the existing proc file. It appears all the previous options have been deprecated (read_proc, create_proc_read_entry and so on). An option that I read is to call proc_pid_maps_operations from task_mmu.c. This is involved when /proc/pid/maps is called? Is that the right approach? Or I can abstract it.

Code snippet of proc_create from various tutorials is here. The moment I change the name to an existing file, insmod fails.

        if (!proc_create( "testcpuinfo", // define ENTRY_NAME "hello_world"
                      0,             // permissions 0644 
                      NULL,          // proc directory
                      &fops))        // file_operations
    {
            printk("ERROR! proc_create\n");
            remove_proc_entry(ENTRY_NAME, NULL);
            return -ENOMEM;
    }
Dsrivast
  • 39
  • 1
  • 10
  • 1
    It's wrong approach to use a dedicated (for user space) ABI to read information that kernel keeps internally. – 0andriy Jan 06 '20 at 18:06
  • ok got it. thanks. Does it mean that I use proc_pid_maps_operations from task_mmu.c? That is the routine called by base.c. Any suggestions? – Dsrivast Jan 06 '20 at 18:46
  • "I would like to read a proc file (/proc/pid/maps) in my kernel module" - Whether a file is created with `proc_create` or by other means, its reading is the same. See that question about reading files in the kernel module: https://stackoverflow.com/questions/1184274/read-write-files-within-a-linux-kernel-module – Tsyvarev Jan 09 '20 at 06:01

1 Answers1

0

I asked this question as I wanted to filter out /proc/pid/maps. It has a large number of entries and impacting the performance of my workload. Thanks to @0andriy and @Tsyvarev for guiding me as a newbie. I have included the code I have to filter and generate modified maps. I have attached code snippet in case it helps a newbie like me in generating their version of /proc/pid/maps.

  static int proc_show(struct seq_file *s, void *v) {
  struct task_struct *task;
  struct pid *pid_struct;
  struct mm_struct *mm;
  struct vm_area_struct *vma;
  unsigned long start, end;
  const char *region = NULL;

  // Look for task which PID was provided as parameter, falling back to current task if not found
  if(pid == 0) {
    printk(KERN_INFO "pages_activity: no pid argument provided, using current process instead\n");
    task = current;
  } else {
    pid_struct = find_get_pid(pid);
    if(pid_struct == NULL) {
      printk(KERN_INFO "pages_activity: process with pid %d not found, using current process instead\n", pid);
      task = current;
    } else {
      task = pid_task(pid_struct, PIDTYPE_PID);
    }
  }
  mm = task->mm;
  vma = mm->mmap;
Dsrivast
  • 39
  • 1
  • 10