IS_ERR in Linux kernel

Source: Internet
Author: User
IS_ERR (), PTR_ERR (), and ERR_PTR () in the IS_ERRlinux kernel in Linux kernel often encounter IS_ERR when viewing the kernel source code, for example, in linux/arch/arm/kernel/sys_arm.c, [plain] www.2cto. comasmlinkageintsys_execve (ch... IS_ERR (), PTR_ERR (), and ERR_PTR () in the IS_ERR Linux kernel in the Linux kernel often encounter IS_ERR when looking at the kernel source code, for example, in linux/arch/arm/kernel/sys_arm.c [plain] www.2cto.com asmlinkage int sys_execve (char _ user * filenamei, char _ user * argv, char _ user * envp, struct pt_regs * regs) {int error; char * filename; filename = getname (filenamei); error = PTR_ERR (filename ); if (IS_ERR (filename) goto out; error = do_execve (filename, argv, envp, regs); putname (filename); out: return error ;} the IS_ERR macro is defined in include/linux/err. h, as shown below: www.2cto.com [plain] # ifndef _ LINUX_ERR_H # define _ LINUX_ERR_H # include # Include/** Kernel pointers have redundant information, so we can use a * scheme where we can return either an error code or a dentry * pointer with the same return value. ** This shoshould be a per-architecture thing, to allow different * error and pointer decisions. */# define IS_ERR_VALUE (x) unlikely (x)> (unsigned long)-1000L) static inline void * ERR_PTR (long error) {return (void *) error ;} Static inline long PTR_ERR (const void * ptr) {return (long) ptr;} static inline long IS_ERR (const void * ptr) {return IS_ERR_VALUE (unsigned long) ptr );} # endif/* _ LINUX_ERR_H */next we will analyze this code to see the clever design ideas in the kernel. To understand IS_ERR (), first understand the kernel space. All drivers run in the kernel space. Although the kernel space is large, it is always limited. in this space, the last page is reserved, that is to say, the pointer to the last page in the kernel space cannot be used by ordinary people. In other words, any pointer involved in writing a device driver must be in three situations: valid pointer, NULL, NULL pointer, error pointer, or invalid pointer. The so-called error pointer means that it has reached the last page, that is, the kernel uses the last page to capture errors. For example, for a 32-bit system, if the maximum kernel space address is 0 xffffffff, the last page is 0xfffff000 ~ 0 xffffffff (assume that 4 K is a page), the address is retained. Why does the kernel space leave the last page? We know that a page may be 4 k or more, such as 8 k, but at least it is 4 k, therefore, setting aside a page allows us to record the kernel space pointer errors. The pointer returned by the kernel generally points to the page boundary (4 k boundary), that is, ptr & 0 xfff = 0. If you find a pointer pointing to an address in this range, your code will definitely go wrong. IS_ERR () is to judge whether the pointer is wrong. if the pointer does not point to the last page, there is no problem. if the pointer points to the last page, it indicates that this is actually not a valid pointer, this pointer is actually an error code. Generally, IS_ERR () is used to determine whether an error is returned. If yes, PTR_ERR () is called to return the error code. Therefore, to determine whether a pointer is valid, use the following method: # define IS_ERR_VALUE (x) unlikely (x)> (unsigned long)-1000L) (unsigned long) -1000L should be (unsigned long)-0x1000L! (Because-0x1000 is 0xFFFFF000), this should be a kernel bug! [Plain] # define MAX_ERRNO 4095 # define IS_ERR_VALUE (x) unlikely (x)> = (unsigned long)-MAX_ERRNO) is defined in kernel 2.6.30.4 as follows) that is, whether it is between (0xfffff000, 0 xffffffff). Therefore, you can use IS_ERR () to determine whether the return value of the kernel function is a valid pointer. Note the intention of using unlikely () here! For PTR_ERR () and ERR_PTR (), only the following values are forcibly converted. Now I should know why I add a negative number like-ENOSYS when writing the returned error code. PTR_ERR () only returns the error code, that is, provides a message to the caller. if you only need to know whether an error occurs and do not care about the error, you certainly do not need to call PTR_ERR (). And the value of our error code is defined in the memory like this (asm-generic/errno-base.h): [plain] ...... # define EPERM 1/* Operation not permitted */# define ENOENT 2/* No such file or directory */# define ESRCH 3/* No such process */# define EINTR 4/ * Interrupted system call */# define EIO 5/* I/O error */# define ENXIO 6/* No such device or address */# define E2BIG 7/* Argument list too long */# define ENOEXEC 8/* Exec format erro R */# define EBADF 9/* Bad file number */# define ECHILD 10/* No child processes */# define EAGAIN 11/* Try again */# define ENOMEM 12/ * Out of memory */# define EACCES 13/* Permission denied */# define EFAULT 14/* Bad address */# define ENOTBLK 15/* Block device required */# define EBUSY 16/* Device or resource busy */# define EEXIST 17/* File exists */# define EXDEV 18/* Cross-device l Ink */# define ENODEV 19/* No such device */# define ENOTDIR 20/* Not a directory */# define EISDIR 21/* Is a directory */# define EINVAL 22 /* Invalid argument */# define ENFILE 23/* File table overflow */# define EMFILE 24/* Too submit open files */# define ENOTTY 25/* Not a typewriter */ # define ETXTBSY 26/* Text file busy */# define EFBIG 27/* File too large */# define ENOSPC 28/* No sp Ace left on device */# define ESPIPE 29/* Illegal seek */# define EROFS 30/* Read-only file system */# define EMLINK 31/* Too export links */ # define EPIPE 32/* Broken pipe */# define EDOM 33/* Math argument out of domain of func */# define ERANGE 34/* Math result not representable */... ..... if the pointer points to the last page, it indicates that this is not a valid pointer. This pointer is actually an error code. Generally, IS_ERR () is used to determine whether an error is returned. If yes, PTR_ERR () is called to return the error code.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.