Use and description of is_err ()

Source: Internet
Author: User

Thank you for sharing fudan_abc. This article is based on his "Linux, I am an API with the same spirit as hub (3)".

Post address: http://blog.csdn.net/fudan_abc/archive/2007/08/09/1734839.aspx

 

People are bored and sometimes difficult to express in words. the following is_err text is only intended for boring you. if you are not interested in memory management, skip to the next section. to understand is_err (), you must first know that there is a kind of space called kernel space. It doesn't matter if you don't know it clearly. I didn't know it very well, I thought I was an angel when I took this course on the operating system. later, when I went to the Microsoft Global Technical Center (gstc) for an interview last year, the manager asked me to explain the term and asked me to talk about the kernel space and user space, in fact, I am also puzzled. I just want to become a technical support engineer at Microsoft, but I still need to understand the kernel. What do you mean? When I was in middle school, didn't the teacher tell me that as long as I learned mathematics and physics well, I wouldn't be afraid to go all over the world? Forget it. I don't want to think about these sad stories. Combined with the is_err () code, it comes from include/Linux/err. h:

8 /*

9 * kernel pointers have redundant information, so we can use

10 * scheme where we can return either an error code or a dentry

11 * pointer with the same return value.

12 *

13 * This shoshould be a per-architecture thing, to allow different

14 * error and pointer decisions.

15 */

16 # define max_errno. 4095

17

18 # ifndef _ Assembly __

19

20 # define is_err_value (x) unlikely (x)> = (unsigned long)-max_errno)

21

22 static inline void * err_ptr (long error)

23 {

24 return (void *) error;

25}

26

27 static inline long ptr_err (const void * PTR)

28 {

29 return (long) PTR;

30}

31

32 static inline long is_err (const void * PTR)

33 {

34 return is_err_value (unsigned long) PTR );

35}

36

37 # endif

About the kernel space, I just want to say that all drivers run in the kernel space. Although the kernel space is large, it is always limited. you must know that even our vast great motherland has limited space and only 9.6 million square kilometers. Therefore, the kernel space is of course a limited space, in this limited space, the last page is specially 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 and NULL pointer, one is an error pointer or an invalid pointer. the so-called error pointer means that it has reached the last page. 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, and the average person is not allowed to take half a step. If you find a pointer pointing to an address in this range, congratulations, your code is definitely wrong.

So are you curious, why should we leave the last page for the kernel space on the good end? Isn't this an out-of-the-box question? Obviously, I have 1000 yuan, so I have to say that I can only use 900 yuan. sorry, you are wrong. It is not just a waste of pages, but a full use of resources to use one thing as two.

Have you seen the max_errno on lines 16? A macro is defined as 4095. max_errno is the maximum error number. In the Linux kernel, there are many possibilities for errors, because there are many errors, just like a person in jail, maybe like Chi Zhiqiang, rape a girl in her career may be like Zhang Jun, because she robbed Changsha youyi mall and Agricultural Bank, or Ma Jiajue Daxia, finally let the hammer speak. for Linux Kernel errors, let's take a look at the include/ASM-generic/errno-base.h file:

# 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 Error */

# 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 link */

# 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 program open files */

# Define enotty 25/* not a typewriter */

# Define etxtbsy 26/* Text File busy */

# Define efbig 27/* file too large */

# Define enospc 28/* no space left on device */

# Define espipe 29/* illegal seek */

# Define erofs 30/* read-only file system */

# Define emlink 31/* too worker links */

# Define epipe 32/* broken pipe */

# Define Edom 33/* Math argument out of domain of func */

# Define erange 34/* Math result not representable */

The most common examples are-ebusy,-einval,-enodev,-epipe,-eagain,-enomem. I believe you have written code, debugged code, as long as you have used Linux, you may have seen these errors, because they often occur. these are all in each architecture, and each architecture also defines some of its own error code. these things are also macros. They actually correspond to some numbers, which are called error numbers. for linux kernels, No matter any architecture, the maximum number of error numbers will not exceed 4095. and 4095 is exactly 1 smaller than 4 K, that is, 4096 minus 1. we know that a page may be 4 K or more, for example, 8 K, but at least it is 4 K, therefore, setting aside a page allows us to record the kernel space pointer errors. what does it mean? For example, in is_err (), it is used to judge whether the pointer returned by kthread_run () is incorrect. If the pointer does not point to the last page, there is no problem. The application is successful, if the Pointer Points to the last page, it indicates that this is not a valid pointer, and what is saved in 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. we didn't call ptr_err () Here, because is_err () is the only thing that determines the function, while ptr_err () Only returns the error code, that is, providing 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 (). After all, man is easy.

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/llxmedici/archive/2011/03/30/6289872.aspx

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.