mmzone.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * linux/mm/mmzone.c
  3. *
  4. * management codes for pgdats, zones and page flags
  5. */
  6. #include <linux/stddef.h>
  7. #include <linux/mm.h>
  8. #include <linux/mmzone.h>
  9. #include <linux/export.h>
  10. struct pglist_data *first_online_pgdat(void)
  11. {
  12. return NODE_DATA(first_online_node);
  13. }
  14. EXPORT_SYMBOL_GPL(first_online_pgdat);
  15. struct pglist_data *next_online_pgdat(struct pglist_data *pgdat)
  16. {
  17. int nid = next_online_node(pgdat->node_id);
  18. if (nid == MAX_NUMNODES)
  19. return NULL;
  20. return NODE_DATA(nid);
  21. }
  22. EXPORT_SYMBOL_GPL(next_online_pgdat);
  23. /*
  24. * next_zone - helper magic for for_each_zone()
  25. */
  26. struct zone *next_zone(struct zone *zone)
  27. {
  28. pg_data_t *pgdat = zone->zone_pgdat;
  29. if (zone < pgdat->node_zones + MAX_NR_ZONES - 1)
  30. zone++;
  31. else {
  32. pgdat = next_online_pgdat(pgdat);
  33. if (pgdat)
  34. zone = pgdat->node_zones;
  35. else
  36. zone = NULL;
  37. }
  38. return zone;
  39. }
  40. EXPORT_SYMBOL_GPL(next_zone);
  41. static inline int zref_in_nodemask(struct zoneref *zref, nodemask_t *nodes)
  42. {
  43. #ifdef CONFIG_NUMA
  44. return node_isset(zonelist_node_idx(zref), *nodes);
  45. #else
  46. return 1;
  47. #endif /* CONFIG_NUMA */
  48. }
  49. /* Returns the next zone at or below highest_zoneidx in a zonelist */
  50. struct zoneref *next_zones_zonelist(struct zoneref *z,
  51. enum zone_type highest_zoneidx,
  52. nodemask_t *nodes,
  53. struct zone **zone)
  54. {
  55. /*
  56. * Find the next suitable zone to use for the allocation.
  57. * Only filter based on nodemask if it's set
  58. */
  59. if (likely(nodes == NULL))
  60. while (zonelist_zone_idx(z) > highest_zoneidx)
  61. z++;
  62. else
  63. while (zonelist_zone_idx(z) > highest_zoneidx ||
  64. (z->zone && !zref_in_nodemask(z, nodes)))
  65. z++;
  66. *zone = zonelist_zone(z);
  67. return z;
  68. }
  69. #ifdef CONFIG_ARCH_HAS_HOLES_MEMORYMODEL
  70. int memmap_valid_within(unsigned long pfn,
  71. struct page *page, struct zone *zone)
  72. {
  73. if (page_to_pfn(page) != pfn)
  74. return 0;
  75. if (page_zone(page) != zone)
  76. return 0;
  77. return 1;
  78. }
  79. #endif /* CONFIG_ARCH_HAS_HOLES_MEMORYMODEL */
  80. void lruvec_init(struct lruvec *lruvec)
  81. {
  82. enum lru_list lru;
  83. memset(lruvec, 0, sizeof(struct lruvec));
  84. for_each_lru(lru)
  85. INIT_LIST_HEAD(&lruvec->lists[lru]);
  86. }
  87. #if defined(CONFIG_NUMA_BALANCING) && !defined(LAST_CPUPID_NOT_IN_PAGE_FLAGS)
  88. int page_cpupid_xchg_last(struct page *page, int cpupid)
  89. {
  90. unsigned long old_flags, flags;
  91. int last_cpupid;
  92. do {
  93. old_flags = flags = page->flags;
  94. last_cpupid = page_cpupid_last(page);
  95. flags &= ~(LAST_CPUPID_MASK << LAST_CPUPID_PGSHIFT);
  96. flags |= (cpupid & LAST_CPUPID_MASK) << LAST_CPUPID_PGSHIFT;
  97. } while (unlikely(cmpxchg(&page->flags, old_flags, flags) != old_flags));
  98. return last_cpupid;
  99. }
  100. #endif