linux-kernel,linux-device-driver,mmap,virtual-memory,page-tables , Driver mmap operation Page table creation

Driver mmap operation Page table creation


Tag: linux-kernel,linux-device-driver,mmap,virtual-memory,page-tables

I am trying to understand mmap operation in a driver. I know why we are doing mmap. I am confused with the following 2 implementations of mmap:

int my_mmap(struct file *filp, struct vm_area_struct *vma)
    vma->vm_ops = &mmap_vm_ops;
    vma->vm_flags |= VM_RESERVED;
    /* assign the file private data to the vm private data */
    vma->vm_private_data = filp->private_data;
    return 0;


static int simple_remap_mmap(struct file *filp, struct vm_area_struct *vma)
    if (remap_pfn_range(vma, vma->vm_start, vm->vm_pgoff,
                vma->vm_end - vma->vm_start,
        return -EAGAIN;

    vma->vm_ops = &simple_remap_vm_ops;
    return 0;

The first example is a simple implementation that does not modify page tables..but the 2nd does.

So what is the rationale in the 2nd approach. 1st example also works properly.


The second example simply maps all the requested pages.

The first examples does not immediately map any page. When the user-space application tries to access one of those pages, a page fault happens, and the page fault handler (.fault) in mmap_vm_ops must then actually map that page. This makes sense if you estimate that most of the pages will not actually be accessed, and if the actual mapping takes too much time or memory.


CPU new features enabled in Linux kernel

If the CPU has some new features, how does the Linux kernel handle this ? From what I remembered, PAE does not need to rebuild the entire the system and Linux seems like just installing the driver and things work. Say, if the CPU provides more execution modes, Do we...

Calling complete() on automatic variable of type struct completion defined in “waiting” thread

I try to understand use of completion in a piece of code. Basically, one kernel thread creates automatic variable struct completion which is, I assume, allocated on the thread's stack. Then it pushes pointer of the completion struct to another thread (using fifo) and waits for completion. struct completion done;...

Intercept ELF loader in linux kernel: fs/binfmt_elf.c file via loadable kernel module

I am new to kernel coding and at present I am working with ELF files which have been modified a little bit for the security purposes for which I need to look at some of it's custom section headers and extract the unique code encryption key from it for the...

Why Device Tree Structure (DTS) file is needed both in bootloader and kernel source code?

Bootloaders like U-Boot that do not implement the Open Firmware API, are expected to pass to the kernel a binary form of the flattened device tree - LinuxFDTBlob Why are the DTS files present in the kernel as well? Isn't that redundant? As an example, U-boot source code has...

How to offload NAPI poll function to workqueue

I'm working with linux 3.3, Ethernet driver for smsc911x. and I want to move the NAPI poll function to workqueue. My questions are : 1. How do I pass the NAPI poll function arguments to the work_struct? 2. How do I get the NAPI poll function arguments back from the...

schedule automate shell script running not as ROOT

I have a shell script that I want to run automatically every day at 08 AM, and I am not authorised to use the crontab because I don't have root permission My home directory is /home/user1/. Any suggestions?...

How to share same header files between kernel modules and userspace applications.

I want to implement a simple module in which an ioctl() method is used. In the kernel module, I use kernel macros, such as _IO(), _IOWR(), etc., to define my own ioctl sub-commands. In facts, I don't care the actual values of these definitions for that I will always use...

How to install section 9 of man command in debian

As a newbie of Linux Driver Developer, I often need to find a kernel API's usage. In this post's answer, amrith92 said there exit section 9 of man command. After testing on my Debian, however, It seems no section 9 on in my system. How could I install section 9...

ret_from_syscall source code and when it is called

In the below call trace we see function called ret_from_syscall. Which function is this ? When it will called during system call ? Where is the corresponding code for this ? May 7 16:40:34.322086 warn TCU-0 kernel: [cf83ddc0] [00009751] 0x9751 (unreliable) May 7 16:40:34.322086 warn TCU-0 kernel: [cf83ddd0] [c00469ac] do_syslog+0x198/0x424...

Read from device file

I have a kernel module myModule with major number 100 and minor number 120. Here is what I did: make all sudo insmod ./myModule.ko mknod ~/myModule c 100 120 When I check the log with cat /proc/syslog then it shows the printk() things from the init function. But I have...

When to Update ALSA Audio Driver Buffer Pointer

I am writing an USB Audio Playback driver using ALSA APIs. For that I was trying to understand existing audio drivers in Linux kernel. But I get confused on when to update the kernel audio buffer pointer. We know kernel puts new audio data in a ring buffer and our...

How do I know the last sched time of a process

I current run into an issue that a process seems stuck somehow, it just doesn't gets scheduled, the status is always 'S'. I have monitored sched_switch_task trace by debugfs for a while, didn't see the process get scheduled. So I would like to know when is that last time scheduled...

How to build Linux kernel to support SO_ATTACH_BPF socket option?

I want to build a application which supports eBPF on CentOS 7 (the kernel version is 3.10.0): if(setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, prog_fd, sizeof(prog_f)) { ...... } So I download a 4.0.5 version, make the following configurations on: CONFIG_BPF=y CONFIG_BPF_SYSCALL=y Then follow this link to build and install a 4.0.5 kernel. After...

Linux boot commands for memory boot on am335x

I am using am335x based custom board. I have only uart port for loading images to memory. I was able to get u-boot working. What are the load addresses for the linux kernel and the dts file. What will be boot command for running the loaded kernel from memory.

Passing IPC(Instructions/Cycles) continuously to other function or variable

I am trying to read the performance counters and get the IPC. I need to use IPC to control few machine specific parameters. I am using shell script to do the same. Please see the code below: while true do retval=./perf periodic -e instructions -e cycles -s 50 -d -m...

disabling CONFIG_NET_DMA

I'm facing the exact problem mentioned in the following thread :- Using DMA API in linux kernel but channel is never available I'm not able to see any DMA channel in /sys/class/dma. The solution for this has been suggested as to disable the CONFIG_NET_DMA. But can you guys tell me...

How to divide the L2 cache between the cores on a ARM Cortex-A7?

Context and goal I'd like to run two fully standalone applications on my Olimex A20 Lime platform that run a ARM Cortex-A7. The goal is to dedicate one core to each application. So far so good. Now I'd like to divide the L2 cache between the cores in the following...

I want to sleep while holding a mutex

I need to write to and read from a Ethernet chip's FIFO and for that I want to use DMA controller. So I modified an existing driver and made the process to sleep using wait_event_interruptible after triggering DMA transfer and made it woken up by ISR using wake_up_interruptible. My problem...

Measure memory usage of a certain Linux driver module

I want to check whether the kernel driver module which I just finished has a memory leak problem. But I don't know how to do it in kernel space. Dynamic memory allocation is very hard to handle I think. Anyone has the experience in debugging such memory problem in kernel...

unknown field 'ioctl' specified in initializer

I implement a simple example to study the usage of ioctl interface according to the book LDD3. When I compiling the codes, unknown field 'ioctl' specified in initializer. is reported. I guess the difference of kernel version between LDD3's and mine causes this error. I'm now using Debian 8 with...

Why do we have memory zones in linux?

I was reading this on a page that: Because of hardware limitations, the kernel cannot treat all pages as identical. Some pages, because of their physical address in memory, cannot be used for certain tasks. Because of this limitation, the kernel divides pages into different zones. I was wondering about...

How does the kernel separate threads from processes

Suppose I have a browser process like Firefox, that has pid = 123. Firefox has 5 opened tabs each running in a separate thread, so in total it has 5 threads. So I want to know in depth, how the kernel will separate the process into the thread to execute...

Traversing `vm_area_struct`'s of a process

As an extension of an answer to Traversing all the physical pages of a process and data here, I had a small question. In the first answer as suggested to traverse the physical pages of a process, struct vm_area_struct *vma = 0; unsigned long vpage; if (task->mm && task->mm->mmap)...

Linux: Reading the output of readlink /proc/pid/exe within a Bash Script

So I am writing a bash script which will run through all of the process ids in /proc/[pid] and read the executable that was used to run it. From what I have had a looked at, the /proc filesystem contains the /proc/[pid]/exe symbolic link. Within the bash script I am...

why is u8 u16 u32 u64 used instead of unsigned int in kernel programming

I see u8 u16 u32 u64 data types being used in kernel code. And I am wondering why is there need to use u8 or u16 or u32 or u64 and not unsigned int?

linux kernel driver debug log

I'm writing a Linux kernel driver for a device that has quite a bit going on and a bunch of interaction from user-space. In order to debug and troubleshoot these interactions I'm storing them in a data array in the driver and need to export this data to user-space via...

Missing headers for kernel in bzImage loaded on qemu

I am trying to compile a kernel module of kernel version 3.2.0 on a linux kernel bzImage loaded on a qemu emulator, however I am getting the following error: /lib/modules/3.2.0/build no such file or directory. I tried to look up solutions over the net but nothing specific related to solve...

Why my process has been killed?

I have a program, written on C/C++ by myself, that is killed by Linux. The message "killed" appears. Willing to dig out the problem I observed inside the file /var/log/kern.log: Out of memory: Kill process 3915 (my_proj) score 236 or sacrifice child Killed process 3915 (my_proj) total-vm:5503376kB, anon-rss:3857420kB, file-rss:40kB I...

OpenWRT make is failing because kernel is asking for config options interactively

I am on the chaos calmer repo and I have a custom kernel configuration that I need used. The kernel configuration file is located at /opt/OpenWRT/target/linux/x86/config-3.18 I've tried running make kernel_oldconfig, but that does not solve the issue. I've tried running make kernel_menuconfig and that fails to rectify the problem...

Sofware pwm as linux kernel module

I need to implement PWM-module for linux kernel, it should be high resolution ( will be cool more than 10kHz on my 400MHz ARM cpu) Can anyone give me an advice, what I can use for that purpose? Kernel version is Device hasn't a harware PWM driver. Now I...

What does it indicate if /proc/PID/maps shows zero for all addresses?

I'm debugging a problem with a Linux DNS server. Curiously, when I look at /proc/PID/maps for the DNS server process, this is what I get: 00000000-00000000 r-xp 00000000 00:0e 2344 /usr/sbin/unbound 00000000-00000000 rw-p 00000000 00:0e 2344 /usr/sbin/unbound 00000000-00000000 ---p 00000000 00:00 0 00000000-00000000 rw-p 00000000 00:00 0 [heap] 00000000-00000000 rw-p...

Infinite loop when I read from /proc which is implemented with seq_file

I wrote a simple code snippet to learn the usage of /proc file system. When I finished, everything is ok except the reading from this file causes an infinite loop. Some codes show below: static struct seq_operations proc_seq_ops = { .start = proc_seq_start, .next = proc_seq_next, .stop = proc_seq_stop, .show...

Dynamically find the address at which the kernel is loaded

I'd like to be able to programmatically find out the address at which the linux kernel is loaded. If there are tools out there that already do that, I'd be willing to use them. However, inspecting the PARAMS_PHYS field in the .config during the kernel build is not an option....

Buildroot custom kernel under 1MB

I am trying to build minimal kernel under 1 Mb with Buildroot. It is intended for small board with qspi memory and basic functionality, ethernet, usb, spi, and some GPIO's. Basic terminal access via ssh and UART. My first thoughts are if it is even possible to modify kernel .config...

Add module to angstrom kernel

I'm working on the Altera cyclone V Soc FPGA Dev Kit. I'm using the GSRD 14.1 Angstrom provides by ( [email protected]_cyclone5:~# uname -a Linux socfpga_cyclone5 3.10.31-ltsi This kernel don't support usb serial device and I think that I need to add usbserial and maybe usbcore drivers to communicate with...

What fpu_xrstor_checking does? extra commentary?

I am doing my own version of do_fork() (for many purposes). So, basically, I am copying the process from one place to another, possibly to another machine. Copying the code and pasting it here would be tough. But this explanation should good enough, I believe. My code works most of...

How the kernel functions work?

Normally during a function call the function gets pushed into the stack part of the process and the calling function waits. But the program stack requires a process right? But the kernel is not part of any process (by kernel I mean the part that does file management and interprocess...

Coding of Admin-Guest login section in an OS

I developed an authentication algorithm for user authentication as part of my masters thesis and implemented it in PHP. I intend to know how can I implement the algorithm for operating system login, in which language (for linux)? also where the code will reside because it will not be click...

how to get a clock from a device tree node

I have the following issue: I want to define the clock a CPU should use during frequency transitions in the device tree rather than in the clock driver code (in this way it will be more generic). I want to define the "transition-clock" property in the device tree, something like:...

Compiling linux module

I've got homework in Operating Systems course, in which I have to write some module. We use Red Hat 8.0 with customed linux kernel I have a makefile for compiling the module, but I fail getting it to work. the module source code is in snake.c and the makefile...