Nishad Kamdar | 508578f | 2020-05-12 16:54:17 -0700 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 3 | * Copyright (c) 2000-2005 Silicon Graphics, Inc. |
| 4 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | */ |
| 6 | #ifndef __XFS_SUPPORT_KMEM_H__ |
| 7 | #define __XFS_SUPPORT_KMEM_H__ |
| 8 | |
| 9 | #include <linux/slab.h> |
| 10 | #include <linux/sched.h> |
| 11 | #include <linux/mm.h> |
Christoph Hellwig | bdfb043 | 2010-01-20 21:55:30 +0000 | [diff] [blame] | 12 | #include <linux/vmalloc.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | |
| 14 | /* |
Nathan Scott | 8758280 | 2006-03-14 13:18:19 +1100 | [diff] [blame] | 15 | * General memory allocation interfaces |
| 16 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
Al Viro | 77ba787 | 2012-04-02 06:24:04 -0400 | [diff] [blame] | 18 | typedef unsigned __bitwise xfs_km_flags_t; |
Al Viro | 77ba787 | 2012-04-02 06:24:04 -0400 | [diff] [blame] | 19 | #define KM_NOFS ((__force xfs_km_flags_t)0x0004u) |
| 20 | #define KM_MAYFAIL ((__force xfs_km_flags_t)0x0008u) |
Gu Zheng | 359d992 | 2013-11-04 18:21:05 +0800 | [diff] [blame] | 21 | #define KM_ZERO ((__force xfs_km_flags_t)0x0010u) |
Darrick J. Wong | 6dcde60 | 2020-05-26 09:33:11 -0700 | [diff] [blame] | 22 | #define KM_NOLOCKDEP ((__force xfs_km_flags_t)0x0020u) |
Nathan Scott | 8758280 | 2006-03-14 13:18:19 +1100 | [diff] [blame] | 23 | |
| 24 | /* |
| 25 | * We use a special process flag to avoid recursive callbacks into |
| 26 | * the filesystem during transactions. We will also issue our own |
| 27 | * warnings, so we explicitly skip any generic ones (silly of us). |
| 28 | */ |
| 29 | static inline gfp_t |
Al Viro | 77ba787 | 2012-04-02 06:24:04 -0400 | [diff] [blame] | 30 | kmem_flags_convert(xfs_km_flags_t flags) |
Nathan Scott | 8758280 | 2006-03-14 13:18:19 +1100 | [diff] [blame] | 31 | { |
| 32 | gfp_t lflags; |
| 33 | |
Darrick J. Wong | 6dcde60 | 2020-05-26 09:33:11 -0700 | [diff] [blame] | 34 | BUG_ON(flags & ~(KM_NOFS | KM_MAYFAIL | KM_ZERO | KM_NOLOCKDEP)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
Tetsuo Handa | 707e0dd | 2019-08-26 12:06:22 -0700 | [diff] [blame] | 36 | lflags = GFP_KERNEL | __GFP_NOWARN; |
| 37 | if (flags & KM_NOFS) |
| 38 | lflags &= ~__GFP_FS; |
Gu Zheng | 359d992 | 2013-11-04 18:21:05 +0800 | [diff] [blame] | 39 | |
Michal Hocko | 91c63ecd | 2017-07-12 14:36:49 -0700 | [diff] [blame] | 40 | /* |
| 41 | * Default page/slab allocator behavior is to retry for ever |
| 42 | * for small allocations. We can override this behavior by using |
| 43 | * __GFP_RETRY_MAYFAIL which will tell the allocator to retry as long |
| 44 | * as it is feasible but rather fail than retry forever for all |
| 45 | * request sizes. |
| 46 | */ |
| 47 | if (flags & KM_MAYFAIL) |
| 48 | lflags |= __GFP_RETRY_MAYFAIL; |
| 49 | |
Gu Zheng | 359d992 | 2013-11-04 18:21:05 +0800 | [diff] [blame] | 50 | if (flags & KM_ZERO) |
| 51 | lflags |= __GFP_ZERO; |
| 52 | |
Darrick J. Wong | 6dcde60 | 2020-05-26 09:33:11 -0700 | [diff] [blame] | 53 | if (flags & KM_NOLOCKDEP) |
| 54 | lflags |= __GFP_NOLOCKDEP; |
| 55 | |
Nathan Scott | 8758280 | 2006-03-14 13:18:19 +1100 | [diff] [blame] | 56 | return lflags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Al Viro | 77ba787 | 2012-04-02 06:24:04 -0400 | [diff] [blame] | 59 | extern void *kmem_alloc(size_t, xfs_km_flags_t); |
Wang, Yalin | f3d2155 | 2015-02-02 09:54:18 +1100 | [diff] [blame] | 60 | static inline void kmem_free(const void *ptr) |
| 61 | { |
| 62 | kvfree(ptr); |
| 63 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
Christoph Hellwig | bdfb043 | 2010-01-20 21:55:30 +0000 | [diff] [blame] | 65 | |
Gu Zheng | 359d992 | 2013-11-04 18:21:05 +0800 | [diff] [blame] | 66 | static inline void * |
| 67 | kmem_zalloc(size_t size, xfs_km_flags_t flags) |
| 68 | { |
| 69 | return kmem_alloc(size, flags | KM_ZERO); |
| 70 | } |
| 71 | |
Nathan Scott | 8758280 | 2006-03-14 13:18:19 +1100 | [diff] [blame] | 72 | /* |
| 73 | * Zone interfaces |
| 74 | */ |
Christoph Hellwig | 72945d8 | 2019-06-28 19:27:19 -0700 | [diff] [blame] | 75 | static inline struct page * |
| 76 | kmem_to_page(void *addr) |
| 77 | { |
| 78 | if (is_vmalloc_addr(addr)) |
| 79 | return vmalloc_to_page(addr); |
| 80 | return virt_to_page(addr); |
| 81 | } |
| 82 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | #endif /* __XFS_SUPPORT_KMEM_H__ */ |