mmzone.c 2.4 KB

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