Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * Copyright (c) 2000-2005 Silicon Graphics, Inc.
0004  * All Rights Reserved.
0005  */
0006 #ifndef __XFS_SUPPORT_KMEM_H__
0007 #define __XFS_SUPPORT_KMEM_H__
0008 
0009 #include <linux/slab.h>
0010 #include <linux/sched.h>
0011 #include <linux/mm.h>
0012 #include <linux/vmalloc.h>
0013 
0014 /*
0015  * General memory allocation interfaces
0016  */
0017 
0018 typedef unsigned __bitwise xfs_km_flags_t;
0019 #define KM_NOFS     ((__force xfs_km_flags_t)0x0004u)
0020 #define KM_MAYFAIL  ((__force xfs_km_flags_t)0x0008u)
0021 #define KM_ZERO     ((__force xfs_km_flags_t)0x0010u)
0022 #define KM_NOLOCKDEP    ((__force xfs_km_flags_t)0x0020u)
0023 
0024 /*
0025  * We use a special process flag to avoid recursive callbacks into
0026  * the filesystem during transactions.  We will also issue our own
0027  * warnings, so we explicitly skip any generic ones (silly of us).
0028  */
0029 static inline gfp_t
0030 kmem_flags_convert(xfs_km_flags_t flags)
0031 {
0032     gfp_t   lflags;
0033 
0034     BUG_ON(flags & ~(KM_NOFS | KM_MAYFAIL | KM_ZERO | KM_NOLOCKDEP));
0035 
0036     lflags = GFP_KERNEL | __GFP_NOWARN;
0037     if (flags & KM_NOFS)
0038         lflags &= ~__GFP_FS;
0039 
0040     /*
0041      * Default page/slab allocator behavior is to retry for ever
0042      * for small allocations. We can override this behavior by using
0043      * __GFP_RETRY_MAYFAIL which will tell the allocator to retry as long
0044      * as it is feasible but rather fail than retry forever for all
0045      * request sizes.
0046      */
0047     if (flags & KM_MAYFAIL)
0048         lflags |= __GFP_RETRY_MAYFAIL;
0049 
0050     if (flags & KM_ZERO)
0051         lflags |= __GFP_ZERO;
0052 
0053     if (flags & KM_NOLOCKDEP)
0054         lflags |= __GFP_NOLOCKDEP;
0055 
0056     return lflags;
0057 }
0058 
0059 extern void *kmem_alloc(size_t, xfs_km_flags_t);
0060 static inline void  kmem_free(const void *ptr)
0061 {
0062     kvfree(ptr);
0063 }
0064 
0065 
0066 static inline void *
0067 kmem_zalloc(size_t size, xfs_km_flags_t flags)
0068 {
0069     return kmem_alloc(size, flags | KM_ZERO);
0070 }
0071 
0072 /*
0073  * Zone interfaces
0074  */
0075 static inline struct page *
0076 kmem_to_page(void *addr)
0077 {
0078     if (is_vmalloc_addr(addr))
0079         return vmalloc_to_page(addr);
0080     return virt_to_page(addr);
0081 }
0082 
0083 #endif /* __XFS_SUPPORT_KMEM_H__ */