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


Driver mmap operation Page table creation

Question:

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;
    mmap_open(vma);
    return 0;
}

and

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,
                vma->vm_page_prot))
        return -EAGAIN;

    vma->vm_ops = &simple_remap_vm_ops;
    simple_vma_open(vma);
    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.


Answer:

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.


Related:


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


linux-kernel,openwrt
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...

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


c,linux,kernel,linux-device-driver
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?

Coding of Admin-Guest login section in an OS


linux,authentication,cmd,linux-kernel,operating-system
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 does the kernel separate threads from processes


linux,multithreading,linux-kernel
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...

How to install section 9 of man command in debian


linux,linux-device-driver,man
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...

schedule automate shell script running not as ROOT


python,linux,shell,unix,linux-kernel
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?...

Read from device file


c,kernel,linux-device-driver
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...

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


linux-kernel,arm,embedded-linux,u-boot,device-tree
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...

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


c,linux-kernel,kernel,kernel-module,elf
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...

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


linux-kernel,macros,linux-device-driver,header-files,ioctl
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 the kernel functions work?


linux-kernel,kernel
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...

Traversing `vm_area_struct`'s of a process


c,linux,linux-kernel,filesystems,mmap
As an extension of an answer to Traversing all the physical pages of a process and data here http://www.makelinux.net/ldd3/chp-15-sect-1, 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)...

How to offload NAPI poll function to workqueue


c,linux,linux-kernel,linux-device-driver,embedded-linux
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...

Linux boot commands for memory boot on am335x


linux-kernel,embedded-linux,beagleboneblack,beagleboard,u-boot
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.

disabling CONFIG_NET_DMA


linux,linux-device-driver,config,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...

Add module to angstrom kernel


linux,linux-kernel,angstrom-linux
I'm working on the Altera cyclone V Soc FPGA Dev Kit. I'm using the GSRD 14.1 Angstrom provides by rocketboards.com (http://www.rocketboards.org/foswiki/Documentation/GSRD141AngstromGettingStarted) [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...

Dynamically find the address at which the kernel is loaded


linux,linux-kernel
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


linux,linux-kernel,kernel,buildroot
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...

Why my process has been killed?


linux-kernel,out-of-memory,linuxmint
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...

Why do we have memory zones in linux?


linux-kernel,virtualization,intel
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...

I want to sleep while holding a mutex


linux,linux-kernel,locking,linux-device-driver
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...

Sofware pwm as linux kernel module


c,linux,linux-kernel,embedded,pwm
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 2.6.28.9. Device hasn't a harware PWM driver. Now I...

When to Update ALSA Audio Driver Buffer Pointer


linux-kernel,usb,linux-device-driver,alsa
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...

Missing headers for kernel in bzImage loaded on qemu


linux-kernel,kernel,header-files,kernel-module,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...

how to get a clock from a device tree node


c,linux-kernel,embedded,linux-device-driver,device-tree
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:...

Measure memory usage of a certain Linux driver module


memory,memory-leaks,linux-kernel,linux-device-driver
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...

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


caching,linux-kernel,arm,cortex-a,l2-cache
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...

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


linux,bash,linux-kernel,procfs
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...

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


linux,linux-kernel
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...

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


linux-kernel,linux-device-driver
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;...

linux kernel driver debug log


linux,linux-kernel,linux-device-driver
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...

How do I know the last sched time of a process


linux,process,linux-kernel,scheduling
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...

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


linux,linux-kernel,filesystems
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...

Compiling linux module


linux-kernel
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 2.4.18.14. 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...

How to build Linux kernel to support SO_ATTACH_BPF socket option?


c,linux,sockets,linux-kernel,bpf
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...

unknown field 'ioctl' specified in initializer


linux,linux-kernel,filesystems,ioctl
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...

CPU new features enabled in Linux kernel


linux-kernel,cpu
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...

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


linux,performance,shell,linux-kernel,performancecounter
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...

ret_from_syscall source code and when it is called


linux,linux-kernel,kernel,linux-device-driver,system-calls
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...

What fpu_xrstor_checking does? extra commentary?


linux-kernel,linux-device-driver,x86-64
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...