gfp.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. #define __GFP_NOTRACK ((__force gfp_t)0x200000u) /* Don't track with kmemcheck */
  51. /*
  52. * This may seem redundant, but it's a way of annotating false positives vs.
  53. * allocations that simply cannot be supported (e.g. page tables).
  54. */
  55. #define __GFP_NOTRACK_FALSE_POSITIVE (__GFP_NOTRACK)
  56. #define __GFP_BITS_SHIFT 22 /* Room for 22 __GFP_FOO bits */
  57. #define __GFP_BITS_MASK ((__force gfp_t)((1 << __GFP_BITS_SHIFT) - 1))
  58. /* This equals 0, but use constants in case they ever change */
  59. #define GFP_NOWAIT (GFP_ATOMIC & ~__GFP_HIGH)
  60. /* GFP_ATOMIC means both !wait (__GFP_WAIT not set) and use emergency pool */
  61. #define GFP_ATOMIC (__GFP_HIGH)
  62. #define GFP_NOIO (__GFP_WAIT)
  63. #define GFP_NOFS (__GFP_WAIT | __GFP_IO)
  64. #define GFP_KERNEL (__GFP_WAIT | __GFP_IO | __GFP_FS)
  65. #define GFP_TEMPORARY (__GFP_WAIT | __GFP_IO | __GFP_FS | \
  66. __GFP_RECLAIMABLE)
  67. #define GFP_USER (__GFP_WAIT | __GFP_IO | __GFP_FS | __GFP_HARDWALL)
  68. #define GFP_HIGHUSER (__GFP_WAIT | __GFP_IO | __GFP_FS | __GFP_HARDWALL | \
  69. __GFP_HIGHMEM)
  70. #define GFP_HIGHUSER_MOVABLE (__GFP_WAIT | __GFP_IO | __GFP_FS | \
  71. __GFP_HARDWALL | __GFP_HIGHMEM | \
  72. __GFP_MOVABLE)
  73. #ifdef CONFIG_NUMA
  74. #define GFP_THISNODE (__GFP_THISNODE | __GFP_NOWARN | __GFP_NORETRY)
  75. #else
  76. #define GFP_THISNODE ((__force gfp_t)0)
  77. #endif
  78. /* This mask makes up all the page movable related flags */
  79. #define GFP_MOVABLE_MASK (__GFP_RECLAIMABLE|__GFP_MOVABLE)
  80. /* Control page allocator reclaim behavior */
  81. #define GFP_RECLAIM_MASK (__GFP_WAIT|__GFP_HIGH|__GFP_IO|__GFP_FS|\
  82. __GFP_NOWARN|__GFP_REPEAT|__GFP_NOFAIL|\
  83. __GFP_NORETRY|__GFP_NOMEMALLOC)
  84. /* Control allocation constraints */
  85. #define GFP_CONSTRAINT_MASK (__GFP_HARDWALL|__GFP_THISNODE)
  86. /* Do not use these with a slab allocator */
  87. #define GFP_SLAB_BUG_MASK (__GFP_DMA32|__GFP_HIGHMEM|~__GFP_BITS_MASK)
  88. /* Flag - indicates that the buffer will be suitable for DMA. Ignored on some
  89. platforms, used as appropriate on others */
  90. #define GFP_DMA __GFP_DMA
  91. /* 4GB DMA on some platforms */
  92. #define GFP_DMA32 __GFP_DMA32
  93. /* Convert GFP flags to their corresponding migrate type */
  94. static inline int allocflags_to_migratetype(gfp_t gfp_flags)
  95. {
  96. WARN_ON((gfp_flags & GFP_MOVABLE_MASK) == GFP_MOVABLE_MASK);
  97. if (unlikely(page_group_by_mobility_disabled))
  98. return MIGRATE_UNMOVABLE;
  99. /* Group based on mobility */
  100. return (((gfp_flags & __GFP_MOVABLE) != 0) << 1) |
  101. ((gfp_flags & __GFP_RECLAIMABLE) != 0);
  102. }
  103. static inline enum zone_type gfp_zone(gfp_t flags)
  104. {
  105. #ifdef CONFIG_ZONE_DMA
  106. if (flags & __GFP_DMA)
  107. return ZONE_DMA;
  108. #endif
  109. #ifdef CONFIG_ZONE_DMA32
  110. if (flags & __GFP_DMA32)
  111. return ZONE_DMA32;
  112. #endif
  113. if ((flags & (__GFP_HIGHMEM | __GFP_MOVABLE)) ==
  114. (__GFP_HIGHMEM | __GFP_MOVABLE))
  115. return ZONE_MOVABLE;
  116. #ifdef CONFIG_HIGHMEM
  117. if (flags & __GFP_HIGHMEM)
  118. return ZONE_HIGHMEM;
  119. #endif
  120. return ZONE_NORMAL;
  121. }
  122. /*
  123. * There is only one page-allocator function, and two main namespaces to
  124. * it. The alloc_page*() variants return 'struct page *' and as such
  125. * can allocate highmem pages, the *get*page*() variants return
  126. * virtual kernel addresses to the allocated page(s).
  127. */
  128. static inline int gfp_zonelist(gfp_t flags)
  129. {
  130. if (NUMA_BUILD && unlikely(flags & __GFP_THISNODE))
  131. return 1;
  132. return 0;
  133. }
  134. /*
  135. * We get the zone list from the current node and the gfp_mask.
  136. * This zone list contains a maximum of MAXNODES*MAX_NR_ZONES zones.
  137. * There are two zonelists per node, one for all zones with memory and
  138. * one containing just zones from the node the zonelist belongs to.
  139. *
  140. * For the normal case of non-DISCONTIGMEM systems the NODE_DATA() gets
  141. * optimized to &contig_page_data at compile-time.
  142. */
  143. static inline struct zonelist *node_zonelist(int nid, gfp_t flags)
  144. {
  145. return NODE_DATA(nid)->node_zonelists + gfp_zonelist(flags);
  146. }
  147. #ifndef HAVE_ARCH_FREE_PAGE
  148. static inline void arch_free_page(struct page *page, int order) { }
  149. #endif
  150. #ifndef HAVE_ARCH_ALLOC_PAGE
  151. static inline void arch_alloc_page(struct page *page, int order) { }
  152. #endif
  153. struct page *
  154. __alloc_pages_internal(gfp_t gfp_mask, unsigned int order,
  155. struct zonelist *zonelist, nodemask_t *nodemask);
  156. static inline struct page *
  157. __alloc_pages(gfp_t gfp_mask, unsigned int order,
  158. struct zonelist *zonelist)
  159. {
  160. return __alloc_pages_internal(gfp_mask, order, zonelist, NULL);
  161. }
  162. static inline struct page *
  163. __alloc_pages_nodemask(gfp_t gfp_mask, unsigned int order,
  164. struct zonelist *zonelist, nodemask_t *nodemask)
  165. {
  166. return __alloc_pages_internal(gfp_mask, order, zonelist, nodemask);
  167. }
  168. static inline struct page *alloc_pages_node(int nid, gfp_t gfp_mask,
  169. unsigned int order)
  170. {
  171. if (unlikely(order >= MAX_ORDER))
  172. return NULL;
  173. /* Unknown node is current node */
  174. if (nid < 0)
  175. nid = numa_node_id();
  176. return __alloc_pages(gfp_mask, order, node_zonelist(nid, gfp_mask));
  177. }
  178. #ifdef CONFIG_NUMA
  179. extern struct page *alloc_pages_current(gfp_t gfp_mask, unsigned order);
  180. static inline struct page *
  181. alloc_pages(gfp_t gfp_mask, unsigned int order)
  182. {
  183. if (unlikely(order >= MAX_ORDER))
  184. return NULL;
  185. return alloc_pages_current(gfp_mask, order);
  186. }
  187. extern struct page *alloc_page_vma(gfp_t gfp_mask,
  188. struct vm_area_struct *vma, unsigned long addr);
  189. #else
  190. #define alloc_pages(gfp_mask, order) \
  191. alloc_pages_node(numa_node_id(), gfp_mask, order)
  192. #define alloc_page_vma(gfp_mask, vma, addr) alloc_pages(gfp_mask, 0)
  193. #endif
  194. #define alloc_page(gfp_mask) alloc_pages(gfp_mask, 0)
  195. extern unsigned long __get_free_pages(gfp_t gfp_mask, unsigned int order);
  196. extern unsigned long get_zeroed_page(gfp_t gfp_mask);
  197. void *alloc_pages_exact(size_t size, gfp_t gfp_mask);
  198. void free_pages_exact(void *virt, size_t size);
  199. #define __get_free_page(gfp_mask) \
  200. __get_free_pages((gfp_mask),0)
  201. #define __get_dma_pages(gfp_mask, order) \
  202. __get_free_pages((gfp_mask) | GFP_DMA,(order))
  203. extern void __free_pages(struct page *page, unsigned int order);
  204. extern void free_pages(unsigned long addr, unsigned int order);
  205. extern void free_hot_page(struct page *page);
  206. extern void free_cold_page(struct page *page);
  207. #define __free_page(page) __free_pages((page), 0)
  208. #define free_page(addr) free_pages((addr),0)
  209. void page_alloc_init(void);
  210. void drain_zone_pages(struct zone *zone, struct per_cpu_pages *pcp);
  211. void drain_all_pages(void);
  212. void drain_local_pages(void *dummy);
  213. #endif /* __LINUX_GFP_H */