gfp.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #ifndef __LINUX_GFP_H
  2. #define __LINUX_GFP_H
  3. #include <linux/mmzone.h>
  4. #include <linux/stddef.h>
  5. #include <linux/linkage.h>
  6. #include <linux/topology.h>
  7. struct vm_area_struct;
  8. /*
  9. * GFP bitmasks..
  10. *
  11. * Zone modifiers (see linux/mmzone.h - low three bits)
  12. *
  13. * Do not put any conditional on these. If necessary modify the definitions
  14. * without the underscores and use the consistently. The definitions here may
  15. * be used in bit comparisons.
  16. */
  17. #define __GFP_DMA ((__force gfp_t)0x01u)
  18. #define __GFP_HIGHMEM ((__force gfp_t)0x02u)
  19. #define __GFP_DMA32 ((__force gfp_t)0x04u)
  20. /*
  21. * Action modifiers - doesn't change the zoning
  22. *
  23. * __GFP_REPEAT: Try hard to allocate the memory, but the allocation attempt
  24. * _might_ fail. This depends upon the particular VM implementation.
  25. *
  26. * __GFP_NOFAIL: The VM implementation _must_ retry infinitely: the caller
  27. * cannot handle allocation failures.
  28. *
  29. * __GFP_NORETRY: The VM implementation must not retry indefinitely.
  30. *
  31. * __GFP_MOVABLE: Flag that this page will be movable by the page migration
  32. * mechanism or reclaimed
  33. */
  34. #define __GFP_WAIT ((__force gfp_t)0x10u) /* Can wait and reschedule? */
  35. #define __GFP_HIGH ((__force gfp_t)0x20u) /* Should access emergency pools? */
  36. #define __GFP_IO ((__force gfp_t)0x40u) /* Can start physical IO? */
  37. #define __GFP_FS ((__force gfp_t)0x80u) /* Can call down to low-level FS? */
  38. #define __GFP_COLD ((__force gfp_t)0x100u) /* Cache-cold page required */
  39. #define __GFP_NOWARN ((__force gfp_t)0x200u) /* Suppress page allocation failure warning */
  40. #define __GFP_REPEAT ((__force gfp_t)0x400u) /* See above */
  41. #define __GFP_NOFAIL ((__force gfp_t)0x800u) /* See above */
  42. #define __GFP_NORETRY ((__force gfp_t)0x1000u)/* See above */
  43. #define __GFP_COMP ((__force gfp_t)0x4000u)/* Add compound page metadata */
  44. #define __GFP_ZERO ((__force gfp_t)0x8000u)/* Return zeroed page on success */
  45. #define __GFP_NOMEMALLOC ((__force gfp_t)0x10000u) /* Don't use emergency reserves */
  46. #define __GFP_HARDWALL ((__force gfp_t)0x20000u) /* Enforce hardwall cpuset memory allocs */
  47. #define __GFP_THISNODE ((__force gfp_t)0x40000u)/* No fallback, no policies */
  48. #define __GFP_RECLAIMABLE ((__force gfp_t)0x80000u) /* Page is reclaimable */
  49. #define __GFP_MOVABLE ((__force gfp_t)0x100000u) /* Page is movable */
  50. #ifdef CONFIG_KMEMCHECK
  51. #define __GFP_NOTRACK ((__force gfp_t)0x200000u) /* Don't track with kmemcheck */
  52. #else
  53. #define __GFP_NOTRACK ((__force gfp_t)0)
  54. #endif
  55. /*
  56. * This may seem redundant, but it's a way of annotating false positives vs.
  57. * allocations that simply cannot be supported (e.g. page tables).
  58. */
  59. #define __GFP_NOTRACK_FALSE_POSITIVE (__GFP_NOTRACK)
  60. #define __GFP_BITS_SHIFT 22 /* Room for 22 __GFP_FOO bits */
  61. #define __GFP_BITS_MASK ((__force gfp_t)((1 << __GFP_BITS_SHIFT) - 1))
  62. /* This equals 0, but use constants in case they ever change */
  63. #define GFP_NOWAIT (GFP_ATOMIC & ~__GFP_HIGH)
  64. /* GFP_ATOMIC means both !wait (__GFP_WAIT not set) and use emergency pool */
  65. #define GFP_ATOMIC (__GFP_HIGH)
  66. #define GFP_NOIO (__GFP_WAIT)
  67. #define GFP_NOFS (__GFP_WAIT | __GFP_IO)
  68. #define GFP_KERNEL (__GFP_WAIT | __GFP_IO | __GFP_FS)
  69. #define GFP_TEMPORARY (__GFP_WAIT | __GFP_IO | __GFP_FS | \
  70. __GFP_RECLAIMABLE)
  71. #define GFP_USER (__GFP_WAIT | __GFP_IO | __GFP_FS | __GFP_HARDWALL)
  72. #define GFP_HIGHUSER (__GFP_WAIT | __GFP_IO | __GFP_FS | __GFP_HARDWALL | \
  73. __GFP_HIGHMEM)
  74. #define GFP_HIGHUSER_MOVABLE (__GFP_WAIT | __GFP_IO | __GFP_FS | \
  75. __GFP_HARDWALL | __GFP_HIGHMEM | \
  76. __GFP_MOVABLE)
  77. #ifdef CONFIG_NUMA
  78. #define GFP_THISNODE (__GFP_THISNODE | __GFP_NOWARN | __GFP_NORETRY)
  79. #else
  80. #define GFP_THISNODE ((__force gfp_t)0)
  81. #endif
  82. /* This mask makes up all the page movable related flags */
  83. #define GFP_MOVABLE_MASK (__GFP_RECLAIMABLE|__GFP_MOVABLE)
  84. /* Control page allocator reclaim behavior */
  85. #define GFP_RECLAIM_MASK (__GFP_WAIT|__GFP_HIGH|__GFP_IO|__GFP_FS|\
  86. __GFP_NOWARN|__GFP_REPEAT|__GFP_NOFAIL|\
  87. __GFP_NORETRY|__GFP_NOMEMALLOC)
  88. /* Control allocation constraints */
  89. #define GFP_CONSTRAINT_MASK (__GFP_HARDWALL|__GFP_THISNODE)
  90. /* Do not use these with a slab allocator */
  91. #define GFP_SLAB_BUG_MASK (__GFP_DMA32|__GFP_HIGHMEM|~__GFP_BITS_MASK)
  92. /* Flag - indicates that the buffer will be suitable for DMA. Ignored on some
  93. platforms, used as appropriate on others */
  94. #define GFP_DMA __GFP_DMA
  95. /* 4GB DMA on some platforms */
  96. #define GFP_DMA32 __GFP_DMA32
  97. /* Convert GFP flags to their corresponding migrate type */
  98. static inline int allocflags_to_migratetype(gfp_t gfp_flags)
  99. {
  100. WARN_ON((gfp_flags & GFP_MOVABLE_MASK) == GFP_MOVABLE_MASK);
  101. if (unlikely(page_group_by_mobility_disabled))
  102. return MIGRATE_UNMOVABLE;
  103. /* Group based on mobility */
  104. return (((gfp_flags & __GFP_MOVABLE) != 0) << 1) |
  105. ((gfp_flags & __GFP_RECLAIMABLE) != 0);
  106. }
  107. static inline enum zone_type gfp_zone(gfp_t flags)
  108. {
  109. #ifdef CONFIG_ZONE_DMA
  110. if (flags & __GFP_DMA)
  111. return ZONE_DMA;
  112. #endif
  113. #ifdef CONFIG_ZONE_DMA32
  114. if (flags & __GFP_DMA32)
  115. return ZONE_DMA32;
  116. #endif
  117. if ((flags & (__GFP_HIGHMEM | __GFP_MOVABLE)) ==
  118. (__GFP_HIGHMEM | __GFP_MOVABLE))
  119. return ZONE_MOVABLE;
  120. #ifdef CONFIG_HIGHMEM
  121. if (flags & __GFP_HIGHMEM)
  122. return ZONE_HIGHMEM;
  123. #endif
  124. return ZONE_NORMAL;
  125. }
  126. /*
  127. * There is only one page-allocator function, and two main namespaces to
  128. * it. The alloc_page*() variants return 'struct page *' and as such
  129. * can allocate highmem pages, the *get*page*() variants return
  130. * virtual kernel addresses to the allocated page(s).
  131. */
  132. static inline int gfp_zonelist(gfp_t flags)
  133. {
  134. if (NUMA_BUILD && unlikely(flags & __GFP_THISNODE))
  135. return 1;
  136. return 0;
  137. }
  138. /*
  139. * We get the zone list from the current node and the gfp_mask.
  140. * This zone list contains a maximum of MAXNODES*MAX_NR_ZONES zones.
  141. * There are two zonelists per node, one for all zones with memory and
  142. * one containing just zones from the node the zonelist belongs to.
  143. *
  144. * For the normal case of non-DISCONTIGMEM systems the NODE_DATA() gets
  145. * optimized to &contig_page_data at compile-time.
  146. */
  147. static inline struct zonelist *node_zonelist(int nid, gfp_t flags)
  148. {
  149. return NODE_DATA(nid)->node_zonelists + gfp_zonelist(flags);
  150. }
  151. #ifndef HAVE_ARCH_FREE_PAGE
  152. static inline void arch_free_page(struct page *page, int order) { }
  153. #endif
  154. #ifndef HAVE_ARCH_ALLOC_PAGE
  155. static inline void arch_alloc_page(struct page *page, int order) { }
  156. #endif
  157. struct page *
  158. __alloc_pages_internal(gfp_t gfp_mask, unsigned int order,
  159. struct zonelist *zonelist, nodemask_t *nodemask);
  160. static inline struct page *
  161. __alloc_pages(gfp_t gfp_mask, unsigned int order,
  162. struct zonelist *zonelist)
  163. {
  164. return __alloc_pages_internal(gfp_mask, order, zonelist, NULL);
  165. }
  166. static inline struct page *
  167. __alloc_pages_nodemask(gfp_t gfp_mask, unsigned int order,
  168. struct zonelist *zonelist, nodemask_t *nodemask)
  169. {
  170. return __alloc_pages_internal(gfp_mask, order, zonelist, nodemask);
  171. }
  172. static inline struct page *alloc_pages_node(int nid, gfp_t gfp_mask,
  173. unsigned int order)
  174. {
  175. if (unlikely(order >= MAX_ORDER))
  176. return NULL;
  177. /* Unknown node is current node */
  178. if (nid < 0)
  179. nid = numa_node_id();
  180. return __alloc_pages(gfp_mask, order, node_zonelist(nid, gfp_mask));
  181. }
  182. #ifdef CONFIG_NUMA
  183. extern struct page *alloc_pages_current(gfp_t gfp_mask, unsigned order);
  184. static inline struct page *
  185. alloc_pages(gfp_t gfp_mask, unsigned int order)
  186. {
  187. if (unlikely(order >= MAX_ORDER))
  188. return NULL;
  189. return alloc_pages_current(gfp_mask, order);
  190. }
  191. extern struct page *alloc_page_vma(gfp_t gfp_mask,
  192. struct vm_area_struct *vma, unsigned long addr);
  193. #else
  194. #define alloc_pages(gfp_mask, order) \
  195. alloc_pages_node(numa_node_id(), gfp_mask, order)
  196. #define alloc_page_vma(gfp_mask, vma, addr) alloc_pages(gfp_mask, 0)
  197. #endif
  198. #define alloc_page(gfp_mask) alloc_pages(gfp_mask, 0)
  199. extern unsigned long __get_free_pages(gfp_t gfp_mask, unsigned int order);
  200. extern unsigned long get_zeroed_page(gfp_t gfp_mask);
  201. void *alloc_pages_exact(size_t size, gfp_t gfp_mask);
  202. void free_pages_exact(void *virt, size_t size);
  203. #define __get_free_page(gfp_mask) \
  204. __get_free_pages((gfp_mask),0)
  205. #define __get_dma_pages(gfp_mask, order) \
  206. __get_free_pages((gfp_mask) | GFP_DMA,(order))
  207. extern void __free_pages(struct page *page, unsigned int order);
  208. extern void free_pages(unsigned long addr, unsigned int order);
  209. extern void free_hot_page(struct page *page);
  210. extern void free_cold_page(struct page *page);
  211. #define __free_page(page) __free_pages((page), 0)
  212. #define free_page(addr) free_pages((addr),0)
  213. void page_alloc_init(void);
  214. void drain_zone_pages(struct zone *zone, struct per_cpu_pages *pcp);
  215. void drain_all_pages(void);
  216. void drain_local_pages(void *dummy);
  217. #endif /* __LINUX_GFP_H */