mmzone.c 2.3 KB

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