Linux内核指针判断

内核指针判断

https://blog.csdn.net/jasonchen_gbd/article/details/44968395
https://blog.csdn.net/xxu0123456789/article/details/6339625

#ifndef _LINUX_ERR_H
#define _LINUX_ERR_H

#include <linux/compiler.h>

#include <asm/errno.h>

/*
 * 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
#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(__force const void *ptr)
{
    return (long) ptr;
}
static inline long __must_check IS_ERR(__force const void *ptr)
{
    return IS_ERR_VALUE((unsigned long)ptr);
}
static inline long __must_check IS_ERR_OR_NULL(__force const void *ptr)
{
    return !ptr || IS_ERR_VALUE((unsigned long)ptr);
}
/**
 * ERR_CAST - Explicitly cast an error-valued pointer to another pointer type
 * @ptr: The pointer to cast.
 *
 * Explicitly cast an error-valued pointer to another pointer type in such a
 * way as to make it clear that's what's going on.
 */
static inline void * __must_check ERR_CAST(__force const void *ptr)
{
    /* cast away the const */
    return (void *) ptr;
}
static inline int __must_check PTR_ERR_OR_ZERO(__force const void *ptr)
{
    if (IS_ERR(ptr))
        return PTR_ERR(ptr);
    else
        return 0;
}
/* Deprecated */
#define PTR_RET(p) PTR_ERR_OR_ZERO(p)
#endif
#endif /* _LINUX_ERR_H */

task = kthread_run(xx_task, 0, “name”);
if (!IS_ERR_OR_NULL(task))
task_running = 1;
else
task = NULL;
增加了内核指针保护,留给内核的空间是3G–>4G的虚拟地址,并且还预留了一个基本页内存4k的地址空间(这部分还不够页4k)对应是一些错误码的表达。
判断错误指针的方式就是IS_ERR_VALUE。IS_ERR_VALUE就是判断指针是否对应是错误码对应的指针表达范围。
如果没有添加此容错的措施,操作无效地址的;

发布了38 篇原创文章 · 获赞 5 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/zhiyanzhai563/article/details/79657615
今日推荐