Linux kernel pointer judge

Analyzing core pointer

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

= kthread_run Task (xx_task, 0, "name");
IF (IS_ERR_OR_NULL (Task)!)
TASK_RUNNING = 1;
the else
Task = NULL;
increased protection kernel pointer, leaving the kernel virtual address space is 3G-> 4G, and and also set aside a basic 4k page memory address space (also part of this page is not enough 4k) corresponds to the expression of some of the error codes.
Misjudgment pointer way is IS_ERR_VALUE. IS_ERR_VALUE is determined whether the pointer is a pointer corresponding to the error code corresponding to the range of expression.
If you do not add this fault tolerant measures, operating an invalid address;

Published 38 original articles · won praise 5 · views 20000 +

Guess you like

Origin blog.csdn.net/zhiyanzhai563/article/details/79657615