mmzone.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. (zonelist_zone(z) && !zref_in_nodemask(z, nodes)))
  61. z++;
  62. return z;
  63. }
  64. void lruvec_init(struct lruvec *lruvec)
  65. {
  66. enum lru_list lru;
  67. memset(lruvec, 0, sizeof(struct lruvec));
  68. spin_lock_init(&lruvec->lru_lock);
  69. zswap_lruvec_state_init(lruvec);
  70. for_each_lru(lru)
  71. INIT_LIST_HEAD(&lruvec->lists[lru]);
  72. /*
  73. * The "Unevictable LRU" is imaginary: though its size is maintained,
  74. * it is never scanned, and unevictable pages are not threaded on it
  75. * (so that their lru fields can be reused to hold mlock_count).
  76. * Poison its list head, so that any operations on it would crash.
  77. */
  78. list_del(&lruvec->lists[LRU_UNEVICTABLE]);
  79. lru_gen_init_lruvec(lruvec);
  80. }
  81. #if defined(CONFIG_NUMA_BALANCING) && !defined(LAST_CPUPID_NOT_IN_PAGE_FLAGS)
  82. int folio_xchg_last_cpupid(struct folio *folio, int cpupid)
  83. {
  84. unsigned long old_flags, flags;
  85. int last_cpupid;
  86. old_flags = READ_ONCE(folio->flags);
  87. do {
  88. flags = old_flags;
  89. last_cpupid = (flags >> LAST_CPUPID_PGSHIFT) & LAST_CPUPID_MASK;
  90. flags &= ~(LAST_CPUPID_MASK << LAST_CPUPID_PGSHIFT);
  91. flags |= (cpupid & LAST_CPUPID_MASK) << LAST_CPUPID_PGSHIFT;
  92. } while (unlikely(!try_cmpxchg(&folio->flags, &old_flags, flags)));
  93. return last_cpupid;
  94. }
  95. #endif