Linux 内核IS_ERR函数

本文介绍Linux 4.4内核IS_ERR()函数。

文件:include/linux/err.h,定义如下:

#define MAX_ERRNO   4095

## 对于64位系统,判断x是否在0xffff ffff ffff f001 ~ 0xffff ffff ffff ffff
#define IS_ERR_VALUE(x) unlikely((x) >= (unsigned long)-MAX_ERRNO)

static inline long __must_check PTR_ERR(__force const void *ptr)
{
    return (long) ptr;
}

static inline bool __must_check IS_ERR(__force const void *ptr)
{
    return IS_ERR_VALUE((unsigned long)ptr);
}

一、功能

功能:判断传入的指针ptr是否有效。

Linux内核指针包括:有效指针、空指针(NULL)和错误指针。

Linux内核用最后4K空间保存指针的出错码,64位的系统地址为:0xffff ffff ffff f001 ~ 0xffff ffff ffff ffff

如果是错误指针,配合PTR_ERR()函数判断错误代码。

文件:include/uapi/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 */
...

文件:include/uapi/asm-generic/errno.h,定义如下:

#define EDEADLK     35  /* Resource deadlock would occur */
#define ENAMETOOLONG    36  /* File name too long */
#define ENOLCK      37  /* No record locks available */

#define ENOSYS      38  /* Invalid system call number */

#define ENOTEMPTY   39  /* Directory not empty */
#define ELOOP       40  /* Too many symbolic links encountered */
...

文件:include/linux/errno.h,定义如下:

#define ERESTARTSYS 512
#define ERESTARTNOINTR  513
#define ERESTARTNOHAND  514 /* restart if no handler.. */
#define ENOIOCTLCMD 515 /* No ioctl command */
#define ERESTART_RESTARTBLOCK 516 /* restart by calling sys_restart_syscall */
#define EPROBE_DEFER    517 /* Driver requests probe retry */
...

二、说明

1)inline:内联函数。内联函数的代码会直接嵌入到调用它的位置,调用几次就嵌入几次。

2)__must_check:指调用函数一定要处理函数的返回值,否则编译器会给出警告。

3)__force:表示变量可进行强制转换。

4)unlikely后续专门介绍。

4)(unsigned long)-MAX_ERRNO:用补码的方式表示-4095,64位系统为0xffff ffff ffff f001

三、示例

文件:drivers/gpu/drm/rockchip/rockchip_drm_drv.c

    printk(KERN_ERR" ==== unsigned long 0x%lx \n", (unsigned long)-MAX_ERRNO);

    ...
    private->devfreq = devfreq_get_devfreq_by_phandle(dev, 0);
    printk(KERN_ERR"==== IS_ERR(private->devfreq) %d , private->devfreq %p\n", \
        IS_ERR(private->devfreq), private->devfreq);
    
    ## 1、判断是否有效指针
    if (IS_ERR(private->devfreq)) {
        ## 2、判断错误码
        if (PTR_ERR(private->devfreq) == -EPROBE_DEFER) {
            parent_np = of_parse_phandle(np, "devfreq", 0);
            if (parent_np &&
                of_device_is_available(parent_np)) {
                ...
            } else {
                ## 3、运行到此处
                dev_info(dev, "dmc is disabled\n");
            }
        } else {
            ...
        }
        ...
    }
    ...

    private->hdmi_pll.pll = devm_clk_get(dev, "hdmi-tmds-pll");
    printk(KERN_ERR"==== IS_ERR(private->hdmi_pll.pll) %d , private->hdmi_pll.pll %p\n", \
            IS_ERR(private->hdmi_pll.pll), private->hdmi_pll.pll);

程序运行后,相关日志如下:

[    2.270871] ==== unsigned long 0xfffffffffffff001
[    2.271322] ==== IS_ERR(private->devfreq) 1 , private->devfreq fffffffffffffdfb
[    2.271993] rockchip-drm display-subsystem: dmc is disabled
[    2.272504] ==== IS_ERR(private->hdmi_pll.pll) 0 , private->hdmi_pll.pll ffffffc0782e6340

通过上面日志可以看出:

1)private->devfreq值为0xfffffffffffffdfb(值为-517),对应错误码EPROBE_DEFER,此时IS_ERR返回1。

2)private->hdmi_pll.pll值为0xffffffc0782e6340,是有效指针,IS_ERR返回0。

你可能感兴趣的:(Linux 内核IS_ERR函数)