Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* thread_info.h: common low-level thread information accessors |
| 2 | * |
| 3 | * Copyright (C) 2002 David Howells (dhowells@redhat.com) |
| 4 | * - Incorporating suggestions made by Linus Torvalds |
| 5 | */ |
| 6 | |
| 7 | #ifndef _LINUX_THREAD_INFO_H |
| 8 | #define _LINUX_THREAD_INFO_H |
| 9 | |
Steven Rostedt | ce6bd42 | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 10 | #include <linux/types.h> |
Al Viro | edd63a2 | 2012-04-27 13:42:45 -0400 | [diff] [blame] | 11 | #include <linux/bug.h> |
Mark Rutland | 53d74d0 | 2016-10-19 19:28:12 +0100 | [diff] [blame^] | 12 | #include <linux/restart_block.h> |
Thomas Gleixner | a332d86 | 2008-02-10 09:04:12 +0100 | [diff] [blame] | 13 | |
Andy Lutomirski | c65eacb | 2016-09-13 14:29:24 -0700 | [diff] [blame] | 14 | #ifdef CONFIG_THREAD_INFO_IN_TASK |
Andy Lutomirski | c65eacb | 2016-09-13 14:29:24 -0700 | [diff] [blame] | 15 | #define current_thread_info() ((struct thread_info *)current) |
| 16 | #endif |
| 17 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/bitops.h> |
| 19 | #include <asm/thread_info.h> |
| 20 | |
| 21 | #ifdef __KERNEL__ |
| 22 | |
Thomas Gleixner | 2889f60 | 2012-05-05 15:05:41 +0000 | [diff] [blame] | 23 | #ifdef CONFIG_DEBUG_STACK_USAGE |
Vladimir Davydov | 5d09705 | 2016-01-14 15:18:21 -0800 | [diff] [blame] | 24 | # define THREADINFO_GFP (GFP_KERNEL_ACCOUNT | __GFP_NOTRACK | \ |
| 25 | __GFP_ZERO) |
Thomas Gleixner | 2889f60 | 2012-05-05 15:05:41 +0000 | [diff] [blame] | 26 | #else |
Vladimir Davydov | 5d09705 | 2016-01-14 15:18:21 -0800 | [diff] [blame] | 27 | # define THREADINFO_GFP (GFP_KERNEL_ACCOUNT | __GFP_NOTRACK) |
Thomas Gleixner | 2889f60 | 2012-05-05 15:05:41 +0000 | [diff] [blame] | 28 | #endif |
| 29 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | /* |
| 31 | * flag set/clear/test wrappers |
| 32 | * - pass TIF_xxxx constants to these functions |
| 33 | */ |
| 34 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | static inline void set_ti_thread_flag(struct thread_info *ti, int flag) |
| 36 | { |
Jeremy Fitzhardinge | 5548fec | 2008-01-30 13:30:55 +0100 | [diff] [blame] | 37 | set_bit(flag, (unsigned long *)&ti->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | static inline void clear_ti_thread_flag(struct thread_info *ti, int flag) |
| 41 | { |
Jeremy Fitzhardinge | 5548fec | 2008-01-30 13:30:55 +0100 | [diff] [blame] | 42 | clear_bit(flag, (unsigned long *)&ti->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | static inline int test_and_set_ti_thread_flag(struct thread_info *ti, int flag) |
| 46 | { |
Jeremy Fitzhardinge | 5548fec | 2008-01-30 13:30:55 +0100 | [diff] [blame] | 47 | return test_and_set_bit(flag, (unsigned long *)&ti->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | static inline int test_and_clear_ti_thread_flag(struct thread_info *ti, int flag) |
| 51 | { |
Jeremy Fitzhardinge | 5548fec | 2008-01-30 13:30:55 +0100 | [diff] [blame] | 52 | return test_and_clear_bit(flag, (unsigned long *)&ti->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | static inline int test_ti_thread_flag(struct thread_info *ti, int flag) |
| 56 | { |
Jeremy Fitzhardinge | 5548fec | 2008-01-30 13:30:55 +0100 | [diff] [blame] | 57 | return test_bit(flag, (unsigned long *)&ti->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Roman Zippel | 3b66a1e | 2005-11-13 16:06:59 -0800 | [diff] [blame] | 60 | #define set_thread_flag(flag) \ |
| 61 | set_ti_thread_flag(current_thread_info(), flag) |
| 62 | #define clear_thread_flag(flag) \ |
| 63 | clear_ti_thread_flag(current_thread_info(), flag) |
| 64 | #define test_and_set_thread_flag(flag) \ |
| 65 | test_and_set_ti_thread_flag(current_thread_info(), flag) |
| 66 | #define test_and_clear_thread_flag(flag) \ |
| 67 | test_and_clear_ti_thread_flag(current_thread_info(), flag) |
| 68 | #define test_thread_flag(flag) \ |
| 69 | test_ti_thread_flag(current_thread_info(), flag) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
Peter Zijlstra | ea81174 | 2013-09-11 12:43:13 +0200 | [diff] [blame] | 71 | #define tif_need_resched() test_thread_flag(TIF_NEED_RESCHED) |
| 72 | |
Kees Cook | 0f60a8e | 2016-07-12 16:19:48 -0700 | [diff] [blame] | 73 | #ifndef CONFIG_HAVE_ARCH_WITHIN_STACK_FRAMES |
| 74 | static inline int arch_within_stack_frames(const void * const stack, |
| 75 | const void * const stackend, |
| 76 | const void *obj, unsigned long len) |
| 77 | { |
| 78 | return 0; |
| 79 | } |
| 80 | #endif |
| 81 | |
Kees Cook | f5509cc | 2016-06-07 11:05:33 -0700 | [diff] [blame] | 82 | #ifdef CONFIG_HARDENED_USERCOPY |
| 83 | extern void __check_object_size(const void *ptr, unsigned long n, |
| 84 | bool to_user); |
| 85 | |
Kees Cook | a85d6b8 | 2016-09-07 09:39:32 -0700 | [diff] [blame] | 86 | static __always_inline void check_object_size(const void *ptr, unsigned long n, |
| 87 | bool to_user) |
Kees Cook | f5509cc | 2016-06-07 11:05:33 -0700 | [diff] [blame] | 88 | { |
Kees Cook | 81409e9 | 2016-08-31 16:04:21 -0700 | [diff] [blame] | 89 | if (!__builtin_constant_p(n)) |
| 90 | __check_object_size(ptr, n, to_user); |
Kees Cook | f5509cc | 2016-06-07 11:05:33 -0700 | [diff] [blame] | 91 | } |
| 92 | #else |
| 93 | static inline void check_object_size(const void *ptr, unsigned long n, |
| 94 | bool to_user) |
| 95 | { } |
| 96 | #endif /* CONFIG_HARDENED_USERCOPY */ |
| 97 | |
Roland McGrath | 4e4c22c | 2008-04-30 00:53:06 -0700 | [diff] [blame] | 98 | #endif /* __KERNEL__ */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | |
| 100 | #endif /* _LINUX_THREAD_INFO_H */ |