内核中判断返回指针是否错误方法

内核中判断返回指针是否错误的方法:使用IS_ERR或者IS_ERR_OR_NULL

参考include/linux/err.h

#define MAX_ERRNO  4095

 

#ifndef __ASSEMBLY__

 

#define IS_ERR_VALUE(x) unlikely((x) >= (unsigned long)-MAX_ERRNO)

 

static inline void * __must_check ERR_PTR(long error)

{

       return (void *) error;

}

 

static inline long __must_check PTR_ERR(const void *ptr)

{

       return (long) ptr;

}

 

static inline long __must_check IS_ERR(const void *ptr)

{

       return IS_ERR_VALUE((unsigned long)ptr);

}

 

static inline long __must_check IS_ERR_OR_NULL(const void *ptr)

{

       return !ptr || IS_ERR_VALUE((unsigned long)ptr);

}

 

内核空间是一个有限的空间,而在这有限的空间中,其最后一个page是专门保留的,也就是说一般人不可能用到内核空间最后一个page的指针.换句话说,你在写设备驱动程序的过程中,涉及到的任何一个指针,必然有三种情况,一种是有效指针,一种是NULL,空指针,一种是错误指针,或者说无效指针.而所谓的错误指针就是指其已经到达了最后一个page.比如对于32bit的系统来说,内核空间最高地址0xffffffff,那么最后一个page就是指的0xfffff000~0xffffffff(假设4k一个page).这段地址是被保留的,一般人不得越雷池半步,如果你发现你的一个指针指向这个范围中的某个地址,那么你的代码肯定出错了。

这里不仅不是浪费一个page,反而是充分利用资源,把一个东西当两个东西来用.

宏MAX_ERRNO(定义为4095)是最大错误号,Linux内核中,出错有多种可能.关于Linux内核中的错误,我们看一下include/asm-generic/errno-base.h文件:

#ifndef _ASM_GENERIC_ERRNO_BASE_H

#define _ASM_GENERIC_ERRNO_BASE_H

 

#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 many 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 many links */

#define    EPIPE           32    /* Broken pipe */

#define    EDOM           33    /* Math argument out of domain of func */

#define    ERANGE              34    /* Math result not representable */

 

#endif

 

最常见的几个是-EBUSY,-EINVAL,-ENODEV,-EPIPE,-EAGAIN,-ENOMEM.这些是每个体系结构里都有的,另外各个体系结构也都定义了自己的一些错误代码.这些东西当然也都是宏,实际上对应的是一些数字,这个数字就叫做错误号.而对于Linux内核来说,不管任何体系结构,最多最多,错误号不会超过4095.而4095又正好是比4k小1,即4096减1.而我们知道一个page可能是4k,也可能是更多,比如8k,但至少它也是4k,所以留出一个page出来就可以让我们把内核空间的指针来记录错误了.

 

转自:http://blog.csdn.net/adaptiver/article/details/8660217

posted @ 2016-09-21 10:56  yuxi_o  阅读(989)  评论(0编辑  收藏  举报