kmem.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  4. * All Rights Reserved.
  5. */
  6. #ifndef __XFS_SUPPORT_KMEM_H__
  7. #define __XFS_SUPPORT_KMEM_H__
  8. #include <linux/slab.h>
  9. #include <linux/sched.h>
  10. #include <linux/mm.h>
  11. #include <linux/vmalloc.h>
  12. /*
  13. * General memory allocation interfaces
  14. */
  15. typedef unsigned __bitwise xfs_km_flags_t;
  16. #define KM_NOFS ((__force xfs_km_flags_t)0x0004u)
  17. #define KM_MAYFAIL ((__force xfs_km_flags_t)0x0008u)
  18. #define KM_ZERO ((__force xfs_km_flags_t)0x0010u)
  19. #define KM_NOLOCKDEP ((__force xfs_km_flags_t)0x0020u)
  20. /*
  21. * We use a special process flag to avoid recursive callbacks into
  22. * the filesystem during transactions. We will also issue our own
  23. * warnings, so we explicitly skip any generic ones (silly of us).
  24. */
  25. static inline gfp_t
  26. kmem_flags_convert(xfs_km_flags_t flags)
  27. {
  28. gfp_t lflags;
  29. BUG_ON(flags & ~(KM_NOFS | KM_MAYFAIL | KM_ZERO | KM_NOLOCKDEP));
  30. lflags = GFP_KERNEL | __GFP_NOWARN;
  31. if (flags & KM_NOFS)
  32. lflags &= ~__GFP_FS;
  33. /*
  34. * Default page/slab allocator behavior is to retry for ever
  35. * for small allocations. We can override this behavior by using
  36. * __GFP_RETRY_MAYFAIL which will tell the allocator to retry as long
  37. * as it is feasible but rather fail than retry forever for all
  38. * request sizes.
  39. */
  40. if (flags & KM_MAYFAIL)
  41. lflags |= __GFP_RETRY_MAYFAIL;
  42. if (flags & KM_ZERO)
  43. lflags |= __GFP_ZERO;
  44. if (flags & KM_NOLOCKDEP)
  45. lflags |= __GFP_NOLOCKDEP;
  46. return lflags;
  47. }
  48. extern void *kmem_alloc(size_t, xfs_km_flags_t);
  49. extern void *kmem_alloc_io(size_t size, int align_mask, xfs_km_flags_t flags);
  50. extern void *kmem_alloc_large(size_t size, xfs_km_flags_t);
  51. static inline void kmem_free(const void *ptr)
  52. {
  53. kvfree(ptr);
  54. }
  55. static inline void *
  56. kmem_zalloc(size_t size, xfs_km_flags_t flags)
  57. {
  58. return kmem_alloc(size, flags | KM_ZERO);
  59. }
  60. /*
  61. * Zone interfaces
  62. */
  63. #define kmem_zone kmem_cache
  64. #define kmem_zone_t struct kmem_cache
  65. static inline struct page *
  66. kmem_to_page(void *addr)
  67. {
  68. if (is_vmalloc_addr(addr))
  69. return vmalloc_to_page(addr);
  70. return virt_to_page(addr);
  71. }
  72. #endif /* __XFS_SUPPORT_KMEM_H__ */