/* SPDX-License-Identifier: GPL-2.0 */1#ifndef __ASM_GENERIC_ACCESS_OK_H__2#define __ASM_GENERIC_ACCESS_OK_H__34/*5* Checking whether a pointer is valid for user space access.6* These definitions work on most architectures, but overrides can7* be used where necessary.8*/910/*11* architectures with compat tasks have a variable TASK_SIZE and should12* override this to a constant.13*/14#ifndef TASK_SIZE_MAX15#define TASK_SIZE_MAX TASK_SIZE16#endif1718#ifndef __access_ok19/*20* 'size' is a compile-time constant for most callers, so optimize for21* this case to turn the check into a single comparison against a constant22* limit and catch all possible overflows.23* On architectures with separate user address space (m68k, s390, parisc,24* sparc64) or those without an MMU, this should always return true.25*26* This version was originally contributed by Jonas Bonn for the27* OpenRISC architecture, and was found to be the most efficient28* for constant 'size' and 'limit' values.29*/30static inline int __access_ok(const void __user *ptr, unsigned long size)31{32unsigned long limit = TASK_SIZE_MAX;33unsigned long addr = (unsigned long)ptr;3435if (IS_ENABLED(CONFIG_ALTERNATE_USER_ADDRESS_SPACE) ||36!IS_ENABLED(CONFIG_MMU))37return true;3839return (size <= limit) && (addr <= (limit - size));40}41#endif4243#ifndef access_ok44#define access_ok(addr, size) likely(__access_ok(addr, size))45#endif4647#endif484950