// SPDX-License-Identifier: GPL-2.0 // Copyright(c) 2018 Intel Corporation. All rights reserved. #include #include #include #include #include #include "internal.h" #include "shuffle.h" DEFINE_STATIC_KEY_FALSE(page_alloc_shuffle_key); static unsigned long shuffle_state __ro_after_init; /* * Depending on the architecture, module parameter parsing may run * before, or after the cache detection. SHUFFLE_FORCE_DISABLE prevents, * or reverts the enabling of the shuffle implementation. SHUFFLE_ENABLE * attempts to turn on the implementation, but aborts if it finds * SHUFFLE_FORCE_DISABLE already set. */ __meminit void page_alloc_shuffle(enum mm_shuffle_ctl ctl) { if (ctl == SHUFFLE_FORCE_DISABLE) set_bit(SHUFFLE_FORCE_DISABLE, &shuffle_state); if (test_bit(SHUFFLE_FORCE_DISABLE, &shuffle_state)) { if (test_and_clear_bit(SHUFFLE_ENABLE, &shuffle_state)) static_branch_disable(&page_alloc_shuffle_key); } else if (ctl == SHUFFLE_ENABLE && !test_and_set_bit(SHUFFLE_ENABLE, &shuffle_state)) static_branch_enable(&page_alloc_shuffle_key); } static bool shuffle_param; static int shuffle_show(char *buffer, const struct kernel_param *kp) { return sprintf(buffer, "%c\n", test_bit(SHUFFLE_ENABLE, &shuffle_state) ? 'Y' : 'N'); } static __meminit int shuffle_store(const char *val, const struct kernel_param *kp) { int rc = param_set_bool(val, kp); if (rc < 0) return rc; if (shuffle_param) page_alloc_shuffle(SHUFFLE_ENABLE); else page_alloc_shuffle(SHUFFLE_FORCE_DISABLE); return 0; } module_param_call(shuffle, shuffle_store, shuffle_show, &shuffle_param, 0400); /* * For two pages to be swapped in the shuffle, they must be free (on a * 'free_area' lru), have the same order, and have the same migratetype. */ static struct page * __meminit shuffle_valid_page(unsigned long pfn, int order) { struct page *page; /* * Given we're dealing with randomly selected pfns in a zone we * need to ask questions like... */ /* ...is the pfn even in the memmap? */ if (!pfn_valid_within(pfn)) return NULL; /* ...is the pfn in a present section or a hole? */ if (!pfn_in_present_section(pfn)) return NULL; /* ...is the page free and currently on a free_area list? */ page = pfn_to_page(pfn); if (!PageBuddy(page)) return NULL; /* * ...is the page on the same list as the page we will * shuffle it with? */ if (page_order(page) != order) return NULL; return page; } /* * Fisher-Yates shuffle the freelist which prescribes iterating through an * array, pfns in this case, and randomly swapping each entry with another in * the span, end_pfn - start_pfn. * * To keep the implementation simple it does not attempt to correct for sources * of bias in the distribution, like modulo bias or pseudo-random number * generator bias. I.e. the expectation is that this shuffling raises the bar * for attacks that exploit the predictability of page allocations, but need not * be a perfect shuffle. */ #define SHUFFLE_RETRY 10 void __meminit __shuffle_zone(struct zone *z) { unsigned long i, flags; unsigned long start_pfn = z->zone_start_pfn; unsigned long end_pfn = zone_end_pfn(z); const int order = SHUFFLE_ORDER; const int order_pages = 1 << order; spin_lock_irqsave(&z->lock, flags); start_pfn = ALIGN(start_pfn, order_pages); for (i = start_pfn; i < end_pfn; i += order_pages) { unsigned long j; int migratetype, retry; struct page *page_i, *page_j; /* * We expect page_i, in the sub-range of a zone being added * (@start_pfn to @end_pfn), to more likely be valid compared to * page_j randomly selected in the span @zone_start_pfn to * @spanned_pages. */ page_i = shuffle_valid_page(i, order); if (!page_i) continue; for (retry = 0; retry < SHUFFLE_RETRY; retry++) { /* * Pick a random order aligned page in the zone span as * a swap target. If the selected pfn is a hole, retry * up to SHUFFLE_RETRY attempts find a random valid pfn * in the zone. */ j = z->zone_start_pfn + ALIGN_DOWN(get_random_long() % z->spanned_pages, order_pages); page_j = shuffle_valid_page(j, order); if (page_j && page_j != page_i) break; } if (retry >= SHUFFLE_RETRY) { pr_debug("%s: failed to swap %#lx\n", __func__, i); continue; } /* * Each migratetype corresponds to its own list, make sure the * types match otherwise we're moving pages to lists where they * do not belong. */ migratetype = get_pageblock_migratetype(page_i); if (get_pageblock_migratetype(page_j) != migratetype) { pr_debug("%s: migratetype mismatch %#lx\n", __func__, i); continue; } list_swap(&page_i->lru, &page_j->lru); pr_debug("%s: swap: %#lx -> %#lx\n", __func__, i, j); /* take it easy on the zone lock */ if ((i % (100 * order_pages)) == 0) { spin_unlock_irqrestore(&z->lock, flags); cond_resched(); spin_lock_irqsave(&z->lock, flags); } } spin_unlock_irqrestore(&z->lock, flags); } /** * shuffle_free_memory - reduce the predictability of the page allocator * @pgdat: node page data */ void __meminit __shuffle_free_memory(pg_data_t *pgdat) { struct zone *z; for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++) shuffle_zone(z); } bool shuffle_pick_tail(void) { static u64 rand; static u8 rand_bits; bool ret; /* * The lack of locking is deliberate. If 2 threads race to * update the rand state it just adds to the entropy. */ if (rand_bits == 0) { rand_bits = 64; rand = get_random_u64(); } ret = rand & 1; rand_bits--; rand >>= 1; return ret; } .c?id=398a153b16b09a68739928d4502455db9725ac86'>sched: fix build error in kernel/sched_rt.c when RT_GROUP_SCHED && !SMPGregory Haskins1-94/+170 2009-01-14sched: de CPP-ify the scheduler codeGregory Haskins1-2/+4 2009-01-11cpumask: reduce stack usage in find_lowest_rqMike Travis1-14/+22 2009-01-11Merge branch 'sched/latest' of git://git.kernel.org/pub/scm/linux/kernel/git/...Ingo Molnar1-100/+224 2009-01-03sched: put back some stack hog changes that were undone in kernel/sched.cMike Travis1-1/+2 2009-01-03Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/rusty/...Mike Travis1-1/+1 2008-12-29RT: fix push_rt_task() to handle dequeue_pushable properlyGregory Haskins1-12/+22 2008-12-29sched: create "pushable_tasks" list to limit pushing to one attemptGregory Haskins1-18/+101 2008-12-29sched: add sched_class->needs_post_schedule() memberGregory Haskins1-10/+14 2008-12-29sched: only try to push a task on wakeup if it is migratableGregory Haskins1-1/+2 2008-12-29sched: use highest_prio.next to optimize pull operationsGregory Haskins1-0/+12 2008-12-29sched: use highest_prio.curr for pull thresholdGregory Haskins1-25/+6 2008-12-29sched: track the next-highest priority on each runqueueGregory Haskins1-20/+61 2008-12-29sched: cleanup inc/dec_rt_tasksGregory Haskins1-24/+16 2008-12-25Merge branch 'sched/urgent'; commit 'v2.6.28' into sched/coreIngo Molnar1-1/+1 2008-12-16sched: use RCU variant of list traversal in for_each_leaf_rt_rq()Bharata B Rao1-1/+1 2008-12-12Merge branch 'sched/core' into cpus4096Ingo Molnar1-4/+0 2008-11-28sched: move double_unlock_balance() higherAlexey Dobriyan1-4/+0 2008-11-26sched: convert local_cpu_mask to cpumask_var_t, fixRusty Russell1-8/+8 2008-11-24sched: convert remaining old-style cpumask operatorsRusty Russell1-9/+9 2008-11-24sched: convert local_cpu_mask to cpumask_var_t.Rusty Russell1-2/+11 2008-11-24sched: convert check_preempt_equal_prio to cpumask_var_t.Rusty Russell1-5/+10 2008-11-24sched: convert struct root_domain to cpumask_var_t.Rusty Russell1-13/+13 2008-11-24sched: wrap sched_group and sched_domain cpumask accesses.Rusty Russell1-1/+2 2008-11-06sched, lockdep: inline double_unlock_balance()Sripathi Kodi1-1/+2 2008-11-03sched/rt: small optimization to update_curr_rt()Dimitri Sivanich1-2/+2 2008-10-24Merge commit 'v2.6.28-rc1' into sched/urgentIngo Molnar1-1/+3 2008-10-22sched: add CONFIG_SMP consistencyLi Zefan1-3/+2 2008-10-20Merge branches 'timers/clocksource', 'timers/hrtimers', 'timers/nohz', 'timer...Thomas Gleixner1-13/+62 2008-10-04sched_rt.c: resch needed in rt_rq_enqueue() for the root rt_rqDario Faggioli1-4/+4 2008-09-23sched: add some comments to the bandwidth codePeter Zijlstra1-0/+42 2008-09-23Merge branches 'sched/urgent' and 'sched/rt' into sched/develIngo Molnar1-1/+5 2008-09-22sched: wakeup preempt when small overlapPeter Zijlstra1-1/+1 2008-09-14timers: fix itimer/many thread hangFrank Mayhar1-1/+3 2008-09-11sched: fix 2.6.27-rc5 couldn't boot on tulsa machine randomlyZhang, Yanmin1-0/+1 2008-08-28sched: rt-bandwidth accounting fixPeter Zijlstra1-6/+5 2008-08-28sched: fix sched_rt_rq_enqueue() resched idleJohn Blackwood1-0/+2 2008-08-19sched: rt-bandwidth group disable fixesPeter Zijlstra1-1/+4 2008-08-19sched: rt-bandwidth accounting fixPeter Zijlstra1-6/+5 2008-08-14sched: fix rt-bandwidth hotplug racePeter Zijlstra1-1/+1 2008-08-11lockdep: re-annotate scheduler runqueuesPeter Zijlstra1-3/+5