123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535 |
- #include <linux/memcontrol.h>
- #include <linux/writeback.h>
- #include <linux/pagemap.h>
- #include <linux/atomic.h>
- #include <linux/module.h>
- #include <linux/swap.h>
- #include <linux/fs.h>
- #include <linux/mm.h>
- #define EVICTION_SHIFT (RADIX_TREE_EXCEPTIONAL_ENTRY + \
- 1 + NODES_SHIFT + MEM_CGROUP_ID_SHIFT)
- #define EVICTION_MASK (~0UL >> EVICTION_SHIFT)
- static unsigned int bucket_order __read_mostly;
- static void *pack_shadow(int memcgid, pg_data_t *pgdat, unsigned long eviction,
- bool workingset)
- {
- eviction >>= bucket_order;
- eviction = (eviction << MEM_CGROUP_ID_SHIFT) | memcgid;
- eviction = (eviction << NODES_SHIFT) | pgdat->node_id;
- eviction = (eviction << 1) | workingset;
- eviction = (eviction << RADIX_TREE_EXCEPTIONAL_SHIFT);
- return (void *)(eviction | RADIX_TREE_EXCEPTIONAL_ENTRY);
- }
- static void unpack_shadow(void *shadow, int *memcgidp, pg_data_t **pgdat,
- unsigned long *evictionp, bool *workingsetp)
- {
- unsigned long entry = (unsigned long)shadow;
- int memcgid, nid;
- bool workingset;
- entry >>= RADIX_TREE_EXCEPTIONAL_SHIFT;
- workingset = entry & 1;
- entry >>= 1;
- nid = entry & ((1UL << NODES_SHIFT) - 1);
- entry >>= NODES_SHIFT;
- memcgid = entry & ((1UL << MEM_CGROUP_ID_SHIFT) - 1);
- entry >>= MEM_CGROUP_ID_SHIFT;
- *memcgidp = memcgid;
- *pgdat = NODE_DATA(nid);
- *evictionp = entry << bucket_order;
- *workingsetp = workingset;
- }
- void *workingset_eviction(struct address_space *mapping, struct page *page)
- {
- struct pglist_data *pgdat = page_pgdat(page);
- struct mem_cgroup *memcg = page_memcg(page);
- int memcgid = mem_cgroup_id(memcg);
- unsigned long eviction;
- struct lruvec *lruvec;
-
- VM_BUG_ON_PAGE(PageLRU(page), page);
- VM_BUG_ON_PAGE(page_count(page), page);
- VM_BUG_ON_PAGE(!PageLocked(page), page);
- lruvec = mem_cgroup_lruvec(pgdat, memcg);
- eviction = atomic_long_inc_return(&lruvec->inactive_age);
- return pack_shadow(memcgid, pgdat, eviction, PageWorkingset(page));
- }
- void workingset_refault(struct page *page, void *shadow)
- {
- unsigned long refault_distance;
- struct pglist_data *pgdat;
- unsigned long active_file;
- struct mem_cgroup *memcg;
- unsigned long eviction;
- struct lruvec *lruvec;
- unsigned long refault;
- bool workingset;
- int memcgid;
- unpack_shadow(shadow, &memcgid, &pgdat, &eviction, &workingset);
- rcu_read_lock();
-
- memcg = mem_cgroup_from_id(memcgid);
- if (!mem_cgroup_disabled() && !memcg)
- goto out;
- lruvec = mem_cgroup_lruvec(pgdat, memcg);
- refault = atomic_long_read(&lruvec->inactive_age);
- active_file = lruvec_lru_size(lruvec, LRU_ACTIVE_FILE, MAX_NR_ZONES);
-
- refault_distance = (refault - eviction) & EVICTION_MASK;
- inc_node_state(pgdat, WORKINGSET_REFAULT);
-
- if (refault_distance > active_file)
- goto out;
- SetPageActive(page);
- atomic_long_inc(&lruvec->inactive_age);
- inc_node_state(pgdat, WORKINGSET_ACTIVATE);
-
- if (workingset) {
- SetPageWorkingset(page);
- inc_node_state(pgdat, WORKINGSET_RESTORE);
- }
- out:
- rcu_read_unlock();
- }
- void workingset_activation(struct page *page)
- {
- struct mem_cgroup *memcg;
- struct lruvec *lruvec;
- rcu_read_lock();
-
- memcg = page_memcg_rcu(page);
- if (!mem_cgroup_disabled() && !memcg)
- goto out;
- lruvec = mem_cgroup_lruvec(page_pgdat(page), memcg);
- atomic_long_inc(&lruvec->inactive_age);
- out:
- rcu_read_unlock();
- }
- struct list_lru workingset_shadow_nodes;
- static unsigned long count_shadow_nodes(struct shrinker *shrinker,
- struct shrink_control *sc)
- {
- unsigned long shadow_nodes;
- unsigned long max_nodes;
- unsigned long pages;
-
- local_irq_disable();
- shadow_nodes = list_lru_shrink_count(&workingset_shadow_nodes, sc);
- local_irq_enable();
- if (sc->memcg) {
- pages = mem_cgroup_node_nr_lru_pages(sc->memcg, sc->nid,
- LRU_ALL_FILE);
- } else {
- pages = node_page_state(NODE_DATA(sc->nid), NR_ACTIVE_FILE) +
- node_page_state(NODE_DATA(sc->nid), NR_INACTIVE_FILE);
- }
-
- max_nodes = pages >> (1 + RADIX_TREE_MAP_SHIFT - 3);
- if (shadow_nodes <= max_nodes)
- return 0;
- return shadow_nodes - max_nodes;
- }
- static enum lru_status shadow_lru_isolate(struct list_head *item,
- struct list_lru_one *lru,
- spinlock_t *lru_lock,
- void *arg)
- {
- struct address_space *mapping;
- struct radix_tree_node *node;
- unsigned int i;
- int ret;
-
- node = container_of(item, struct radix_tree_node, private_list);
- mapping = node->private_data;
-
- if (!spin_trylock(&mapping->tree_lock)) {
- spin_unlock(lru_lock);
- ret = LRU_RETRY;
- goto out;
- }
- list_lru_isolate(lru, item);
- spin_unlock(lru_lock);
-
- BUG_ON(!workingset_node_shadows(node));
- BUG_ON(workingset_node_pages(node));
- for (i = 0; i < RADIX_TREE_MAP_SIZE; i++) {
- if (node->slots[i]) {
- BUG_ON(!radix_tree_exceptional_entry(node->slots[i]));
- node->slots[i] = NULL;
- workingset_node_shadows_dec(node);
- BUG_ON(!mapping->nrexceptional);
- mapping->nrexceptional--;
- }
- }
- BUG_ON(workingset_node_shadows(node));
- inc_node_state(page_pgdat(virt_to_page(node)), WORKINGSET_NODERECLAIM);
- if (!__radix_tree_delete_node(&mapping->page_tree, node))
- BUG();
- spin_unlock(&mapping->tree_lock);
- ret = LRU_REMOVED_RETRY;
- out:
- local_irq_enable();
- cond_resched();
- local_irq_disable();
- spin_lock(lru_lock);
- return ret;
- }
- static unsigned long scan_shadow_nodes(struct shrinker *shrinker,
- struct shrink_control *sc)
- {
- unsigned long ret;
-
- local_irq_disable();
- ret = list_lru_shrink_walk(&workingset_shadow_nodes, sc,
- shadow_lru_isolate, NULL);
- local_irq_enable();
- return ret;
- }
- static struct shrinker workingset_shadow_shrinker = {
- .count_objects = count_shadow_nodes,
- .scan_objects = scan_shadow_nodes,
- .seeks = DEFAULT_SEEKS,
- .flags = SHRINKER_NUMA_AWARE | SHRINKER_MEMCG_AWARE,
- };
- static struct lock_class_key shadow_nodes_key;
- static int __init workingset_init(void)
- {
- unsigned int timestamp_bits;
- unsigned int max_order;
- int ret;
- BUILD_BUG_ON(BITS_PER_LONG < EVICTION_SHIFT);
-
- timestamp_bits = BITS_PER_LONG - EVICTION_SHIFT;
- max_order = fls_long(totalram_pages - 1);
- if (max_order > timestamp_bits)
- bucket_order = max_order - timestamp_bits;
- pr_info("workingset: timestamp_bits=%d max_order=%d bucket_order=%u\n",
- timestamp_bits, max_order, bucket_order);
- ret = __list_lru_init(&workingset_shadow_nodes, true, &shadow_nodes_key);
- if (ret)
- goto err;
- ret = register_shrinker(&workingset_shadow_shrinker);
- if (ret)
- goto err_list_lru;
- return 0;
- err_list_lru:
- list_lru_destroy(&workingset_shadow_nodes);
- err:
- return ret;
- }
- module_init(workingset_init);
|