内核IS_ERR宏解析 【转】

本文探讨了Linux内核中处理错误指针的机制,包括如何通过ERR_PTR将错误号转化为指针,如何通过PTR_ERR将指针转化为错误号,以及如何通过IS_ERR判断返回的指针是否为错误信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自:http://blog.chinaunix.net/uid-20196318-id-28769.html

最近在使用filp_open打开文件时遇到到一个问题,当打开一个并不存在的文件时,filp_open返回值值为0xfffffffe,而并不是0(NULL),这是因为内核对返回指针的函数做了特殊处理。内核中的函数常常返回指针,通常如果调用出错,会返回NULL空指针,但linux做了更精妙的处理,能够通过返回的指针体现出来。

 

对任何一个指针,必然有三种情况:一种是有效指针,一种是NULL,空指针,一种是错误指针,或者说无效指针。而所谓的错误指针就是指其已经到达了最后一个page,比如对于32bit的系统来说,内核空间最高地址0xffffffff,那么最后一个page就是指的0xfffff000~0xffffffff(以4K大小页为例)。这段地址是被保留的,如果超过这个地址,则肯定是错误的。

 

linux/err.h中包含了这一机制的处理,主要通过IS_ERR, PTR_ERR, ERR_PTR几个宏。

/*

 * 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 should be a per-architecture thing, to allow different

 * error and pointer decisions.

 */

#define MAX_ERRNO       4095

 

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

 

/* 将错误号转化为指针,由于错误号在-1000~0间,返回的指针会落在最后一页  */

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);

}

 

所以对于内核中返回的指针,检查错误的方式不是if(!retptr),而是if( IS_ERR(retptr) 或

If( IS_ERR_VALUE(retptr) )。

struct pinctrl *pinctrl1; struct pinctrl_state *pins_default; struct pinctrl_state *eint_as_int, *eint_output0, *eint_output1, *rst_output0, *rst_output1; int tpd_get_gpio_info(struct platform_device *pdev) { int ret; TPD_DMESG("*** %s enter start***, %d\n", __func__, __LINE__); pinctrl1 = devm_pinctrl_get(&pdev->dev); if (IS_ERR(pinctrl1)) { ret = PTR_ERR(pinctrl1); dev_info(&pdev->dev, "fwq Cannot find pinctrl1!\n"); return ret; } pins_default = pinctrl_lookup_state(pinctrl1, "default"); if (IS_ERR(pins_default)) { ret = PTR_ERR(pins_default); TPD_DMESG("Cannot find pinctrl default %d!\n", ret); } eint_as_int = pinctrl_lookup_state(pinctrl1, "state_eint_as_int"); if (IS_ERR(eint_as_int)) { ret = PTR_ERR(eint_as_int); TPD_DMESG("Cannot find pinctrl state_eint_as_int!\n"); return ret; } eint_output0 = pinctrl_lookup_state(pinctrl1, "state_eint_output0"); if (IS_ERR(eint_output0)) { ret = PTR_ERR(eint_output0); TPD_DMESG("Cannot find pinctrl state_eint_output0!\n"); return ret; } eint_output1 = pinctrl_lookup_state(pinctrl1, "state_eint_output1"); if (IS_ERR(eint_output1)) { ret = PTR_ERR(eint_output1); TPD_DMESG("Cannot find pinctrl state_eint_output1!\n"); return ret; } if (tpd_dts_data.tpd_use_ext_gpio == false) { rst_output0 = pinctrl_lookup_state(pinctrl1, "state_rst_output0"); if (IS_ERR(rst_output0)) { ret = PTR_ERR(rst_output0); TPD_DMESG("Cannot find pinctrl state_rst_output0!\n"); return ret; } rst_output1 = pinctrl_lookup_state(pinctrl1, "state_rst_output1"); if (IS_ERR(rst_output1)) { ret = PTR_ERR(rst_output1); TPD_DMESG("Cannot find pinctrl state_rst_output1!\n"); return ret; } } TPD_DMESG("*** %s enter end***, %d\n", __func__, __LINE__); return 0; }
最新发布
07-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值