Sponsored Content
Operating Systems Linux Linux Device Driver: avoid mem copy from/to user/kernel space Post 302387425 by agaurav on Friday 15th of January 2010 05:15:11 PM
Old 01-15-2010
Linux Device Driver: avoid mem copy from/to user/kernel space

I recently started working with Linux and wrote my first device driver for a hardware chip controlled by a host CPU running Linux 2.6.x kernel.

1. The user space process makes an IOCTL call with pointer to a user memory buffer.
2. The kernel device driver in the big switch-case of IOCTL, first does a copy to kernel space
3. The driver then processes the memory, eventually writing words to the hardware blocks as appropriate

The memory copying is a performance hit because of limited cycles available on this embedded CPU. How does one avoid unnecessary memory copy between user space and kernel space? Suppose there is a buffer in the user-space that needs to be read by the kernel space? I believe that the typical code in the kernel mode device driver is:

....
copy_from_user(kernel_dst_buffer, user_src_buffer, length);
use kernel_dst_buffer;
...

I want to avoid the copy_from_user and was trying to read up my "Linux Device Drivers book by Corbet/Rubini" and search online as well. It appears that one of the following will be used:
get_user_pages
mmap
shm_get

but I can't figure out. It appears extremely difficult/complicated for a simple task of sharing memory between user/kernel modes. Is that how it is? I couldn't even get any sample code for sharing memory though there appears to be sample code for just about everything. Maybe my search strings are broken.

Any pointers?

Thanks a lot!
Gaurav
 

9 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Kernel and Device Driver Programming

I am looking for a guide on how to program for either the Linux or FreeBSD (includes 4.4BSD, NetBSD or OpenBSD) kernel. I would prefer to learn how to write device drivers, but anything would help. If you know, please email me at *removed* or leave a post here Regards, Farhan (0 Replies)
Discussion started by: Farhan
0 Replies

2. UNIX for Dummies Questions & Answers

Need Book for Linux Basics and Linux device driver.

hai friend, I am new to Linux, i need a book for Linux basic and Linux device driver .. plz send the book name and author(Easy to learn and mostly used by programmers) and also send the link if it is available in Net... Thank you. ... (0 Replies)
Discussion started by: sundar.lsr
0 Replies

3. UNIX for Advanced & Expert Users

wake up user space thread from kernel space ISR

Hello, I'm searching for a proper way to let the kernel space ISR(implemented in a kernel module) wake up a user space thread on a hardware interrupt. Except for sending a real-time signal, is it possible to use a semaphore? I've searched it on google, but it seems impossible to share a... (0 Replies)
Discussion started by: aaronwong
0 Replies

4. UNIX for Dummies Questions & Answers

Linux Device Driver: how can an ISR wake up a user-thread?

Hi all, Is it possible to do the following in Linux (kernel 2.6.x): - A user-space thread goes to "sleep". Using any call/mechanism - On a hardware generated interrupt, the Interrupt handler (ISR) "wakes" the sleeping user-thread. I have seen wait_event() and wake_up() but it appears... (1 Reply)
Discussion started by: agaurav
1 Replies

5. UNIX for Dummies Questions & Answers

Kernel/ user space and high/ low mem

Need some clarification on this.... 1. how are kernel/ user spaces and high/low memory related? 2. What do they all mean when i have the kernel command line as: "console=ttyS0,115200 root=/dev/sda2 rw mem=exactmap memmap=1M@0 memmap=96M@1M irqpoll" or 2. what do mem and memmap mean in... (3 Replies)
Discussion started by: dragonpoint
3 Replies

6. Fedora

Is Kernel module is the same as a device driver?

I have been reading prep questions for my second unix academy exam, and there's a nuance, I'm not sure I understand it correctly. I've been under impression from my readings of book by Evi Nemeth and from unix academy DVDs I've been watching, that kernel's modules are drivers. I think of it, as... (25 Replies)
Discussion started by: newlinuxuser1
25 Replies

7. UNIX for Advanced & Expert Users

Certification For Linux Device Driver Programming

Hi All, I'm looking for role change to Linux device Driver developer. My current role has no connection to Linux Device driver development and hence to support my stand i want to do a certification for the same. I have googled but couldn't found any standard certification. I have submitted... (1 Reply)
Discussion started by: kg_gaurav
1 Replies

8. Shell Programming and Scripting

To handle the case during copy when: No space left on device

print "After create SubDir routine."; createSubDirs($fileDir); my $from = $ORACLE_HOME.$dirSep.$file; my $to = $bootstrapDir.$dirSep.$fileDir; if ($isWindows) { copy($from,... (1 Reply)
Discussion started by: ambarginni
1 Replies

9. Emergency UNIX and Linux Support

To handle the case during copy when: No space left on device

print "After create SubDir routine."; createSubDirs($fileDir); my $from = $ORACLE_HOME.$dirSep.$file; my $to = $bootstrapDir.$dirSep.$fileDir; if ($isWindows) { copy($from,... (2 Replies)
Discussion started by: ambarginni
2 Replies
KEXEC_LOAD(2)						     Linux Programmer's Manual						     KEXEC_LOAD(2)

NAME
kexec_load - load a new kernel for later execution SYNOPSIS
#include <linux/kexec.h> long kexec_load(unsigned long entry, unsigned long nr_segments, struct kexec_segment *segments, unsigned long flags); Note: There is no glibc wrapper for this system call; see NOTES. DESCRIPTION
The kexec_load() system call loads a new kernel that can be executed later by reboot(2). The flags argument is a mask whose high-order bits control the operation of the call. The following values can be specified in flags: KEXEC_ON_CRASH (since Linux 2.6.13) Execute the new kernel automatically on a system crash. KEXEC_PRESERVE_CONTEXT (since Linux 2.6.27) Preserve the system hardware and software states before executing the new kernel. This could be used for system suspend. This flag is only available if the kernel was configured with CONFIG_KEXEC_JUMP, and is only effective if nr_segments is greater than 0. The low-order bits of flags contain the architecture of the to-be-executed kernel. Specify (OR) the constant KEXEC_ARCH_DEFAULT to use the current architecture, or one of the following architecture constants KEXEC_ARCH_386, KEXEC_ARCH_X86_64, KEXEC_ARCH_PPC, KEXEC_ARCH_PPC64, KEXEC_ARCH_IA_64, KEXEC_ARCH_ARM, KEXEC_ARCH_S390, KEXEC_ARCH_SH, KEXEC_ARCH_MIPS, and KEXEC_ARCH_MIPS_LE. The architecture must be exe- cutable on the CPU of the system. The entry argument is the physical entry address in the kernel image. The nr_segments argument is the number of segments pointed to by the segments pointer. The segments argument is an array of kexec_segment structures which define the kernel layout: struct kexec_segment { void *buf; /* Buffer in user space */ size_t bufsz; /* Buffer length in user space */ void *mem; /* Physical address of kernel */ size_t memsz; /* Physical address length */ }; The kernel image defined by segments is copied from the calling process into previously reserved memory. RETURN VALUE
On success, kexec_load() returns 0. On error, -1 is returned and errno is set to indicate the error. ERRORS
EBUSY Another crash kernel is already being loaded or a crash kernel is already in use. EINVAL flags is invalid; or nr_segments is too large EPERM The caller does not have the CAP_SYS_BOOT capability. VERSIONS
The kexec_load() system call first appeared in Linux 2.6.13. CONFORMING TO
This system call is Linux-specific. NOTES
Currently, there is no glibc support for kexec_load(). Call it using syscall(2). The required constants are in the Linux kernel source file linux/kexec.h, which is not currently exported to glibc. Therefore, these con- stants must be defined manually. This system call is only available if the kernel was configured with CONFIG_KEXEC. SEE ALSO
reboot(2), syscall(2) COLOPHON
This page is part of release 3.44 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/. Linux 2012-07-13 KEXEC_LOAD(2)
All times are GMT -4. The time now is 10:35 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy