mmzone.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. #include <trace/hooks/mm.h>
  11. struct pglist_data *first_online_pgdat(void)
  12. {
  13. return NODE_DATA(first_online_node);
  14. }
  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. /*
  23. * next_zone - helper magic for for_each_zone()
  24. */
  25. struct zone *next_zone(struct zone *zone)
  26. {
  27. pg_data_t *pgdat = zone->zone_pgdat;
  28. if (zone < pgdat->node_zones + MAX_NR_ZONES - 1)
  29. zone++;
  30. else {
  31. pgdat = next_online_pgdat(pgdat);
  32. if (pgdat)
  33. zone = pgdat->node_zones;
  34. else
  35. zone = NULL;
  36. }
  37. return zone;
  38. }
  39. static inline int zref_in_nodemask(struct zoneref *zref, nodemask_t *nodes)
  40. {
  41. #ifdef CONFIG_NUMA
  42. return node_isset(zonelist_node_idx(zref), *nodes);
  43. #else
  44. return 1;
  45. #endif /* CONFIG_NUMA */
  46. }
  47. /* Returns the next zone at or below highest_zoneidx in a zonelist */
  48. struct zoneref *__next_zones_zonelist(struct zoneref *z,
  49. enum zone_type highest_zoneidx,
  50. nodemask_t *nodes)
  51. {
  52. /*
  53. * Find the next suitable zone to use for the allocation.
  54. * Only filter based on nodemask if it's set
  55. */
  56. if (unlikely(nodes == NULL))
  57. while (zonelist_zone_idx(z) > highest_zoneidx)
  58. z++;
  59. else
  60. while (zonelist_zone_idx(z) > highest_zoneidx ||
  61. (z->zone && !zref_in_nodemask(z, nodes)))
  62. z++;
  63. return z;
  64. }
  65. void lruvec_init(struct lruvec *lruvec)
  66. {
  67. enum lru_list lru;
  68. memset(lruvec, 0, sizeof(struct lruvec));
  69. spin_lock_init(&lruvec->lru_lock);
  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 page_cpupid_xchg_last(struct page *page, int cpupid)
  83. {
  84. unsigned long old_flags, flags;
  85. int last_cpupid;
  86. old_flags = READ_ONCE(page->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(&page->flags, &old_flags, flags)));
  93. return last_cpupid;
  94. }
  95. #endif
  96. enum zone_type gfp_zone(gfp_t flags)
  97. {
  98. trace_android_rvh_set_gfp_zone_flags(&flags);
  99. return __gfp_zone(flags);
  100. }