162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci// Copyright(c) 2018 Intel Corporation. All rights reserved.
362306a36Sopenharmony_ci
462306a36Sopenharmony_ci#include <linux/mm.h>
562306a36Sopenharmony_ci#include <linux/init.h>
662306a36Sopenharmony_ci#include <linux/mmzone.h>
762306a36Sopenharmony_ci#include <linux/random.h>
862306a36Sopenharmony_ci#include <linux/moduleparam.h>
962306a36Sopenharmony_ci#include "internal.h"
1062306a36Sopenharmony_ci#include "shuffle.h"
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ciDEFINE_STATIC_KEY_FALSE(page_alloc_shuffle_key);
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_cistatic bool shuffle_param;
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_cistatic __meminit int shuffle_param_set(const char *val,
1762306a36Sopenharmony_ci		const struct kernel_param *kp)
1862306a36Sopenharmony_ci{
1962306a36Sopenharmony_ci	if (param_set_bool(val, kp))
2062306a36Sopenharmony_ci		return -EINVAL;
2162306a36Sopenharmony_ci	if (*(bool *)kp->arg)
2262306a36Sopenharmony_ci		static_branch_enable(&page_alloc_shuffle_key);
2362306a36Sopenharmony_ci	return 0;
2462306a36Sopenharmony_ci}
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_cistatic const struct kernel_param_ops shuffle_param_ops = {
2762306a36Sopenharmony_ci	.set = shuffle_param_set,
2862306a36Sopenharmony_ci	.get = param_get_bool,
2962306a36Sopenharmony_ci};
3062306a36Sopenharmony_cimodule_param_cb(shuffle, &shuffle_param_ops, &shuffle_param, 0400);
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ci/*
3362306a36Sopenharmony_ci * For two pages to be swapped in the shuffle, they must be free (on a
3462306a36Sopenharmony_ci * 'free_area' lru), have the same order, and have the same migratetype.
3562306a36Sopenharmony_ci */
3662306a36Sopenharmony_cistatic struct page * __meminit shuffle_valid_page(struct zone *zone,
3762306a36Sopenharmony_ci						  unsigned long pfn, int order)
3862306a36Sopenharmony_ci{
3962306a36Sopenharmony_ci	struct page *page = pfn_to_online_page(pfn);
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci	/*
4262306a36Sopenharmony_ci	 * Given we're dealing with randomly selected pfns in a zone we
4362306a36Sopenharmony_ci	 * need to ask questions like...
4462306a36Sopenharmony_ci	 */
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci	/* ... is the page managed by the buddy? */
4762306a36Sopenharmony_ci	if (!page)
4862306a36Sopenharmony_ci		return NULL;
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ci	/* ... is the page assigned to the same zone? */
5162306a36Sopenharmony_ci	if (page_zone(page) != zone)
5262306a36Sopenharmony_ci		return NULL;
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ci	/* ...is the page free and currently on a free_area list? */
5562306a36Sopenharmony_ci	if (!PageBuddy(page))
5662306a36Sopenharmony_ci		return NULL;
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ci	/*
5962306a36Sopenharmony_ci	 * ...is the page on the same list as the page we will
6062306a36Sopenharmony_ci	 * shuffle it with?
6162306a36Sopenharmony_ci	 */
6262306a36Sopenharmony_ci	if (buddy_order(page) != order)
6362306a36Sopenharmony_ci		return NULL;
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci	return page;
6662306a36Sopenharmony_ci}
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci/*
6962306a36Sopenharmony_ci * Fisher-Yates shuffle the freelist which prescribes iterating through an
7062306a36Sopenharmony_ci * array, pfns in this case, and randomly swapping each entry with another in
7162306a36Sopenharmony_ci * the span, end_pfn - start_pfn.
7262306a36Sopenharmony_ci *
7362306a36Sopenharmony_ci * To keep the implementation simple it does not attempt to correct for sources
7462306a36Sopenharmony_ci * of bias in the distribution, like modulo bias or pseudo-random number
7562306a36Sopenharmony_ci * generator bias. I.e. the expectation is that this shuffling raises the bar
7662306a36Sopenharmony_ci * for attacks that exploit the predictability of page allocations, but need not
7762306a36Sopenharmony_ci * be a perfect shuffle.
7862306a36Sopenharmony_ci */
7962306a36Sopenharmony_ci#define SHUFFLE_RETRY 10
8062306a36Sopenharmony_civoid __meminit __shuffle_zone(struct zone *z)
8162306a36Sopenharmony_ci{
8262306a36Sopenharmony_ci	unsigned long i, flags;
8362306a36Sopenharmony_ci	unsigned long start_pfn = z->zone_start_pfn;
8462306a36Sopenharmony_ci	unsigned long end_pfn = zone_end_pfn(z);
8562306a36Sopenharmony_ci	const int order = SHUFFLE_ORDER;
8662306a36Sopenharmony_ci	const int order_pages = 1 << order;
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci	spin_lock_irqsave(&z->lock, flags);
8962306a36Sopenharmony_ci	start_pfn = ALIGN(start_pfn, order_pages);
9062306a36Sopenharmony_ci	for (i = start_pfn; i < end_pfn; i += order_pages) {
9162306a36Sopenharmony_ci		unsigned long j;
9262306a36Sopenharmony_ci		int migratetype, retry;
9362306a36Sopenharmony_ci		struct page *page_i, *page_j;
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ci		/*
9662306a36Sopenharmony_ci		 * We expect page_i, in the sub-range of a zone being added
9762306a36Sopenharmony_ci		 * (@start_pfn to @end_pfn), to more likely be valid compared to
9862306a36Sopenharmony_ci		 * page_j randomly selected in the span @zone_start_pfn to
9962306a36Sopenharmony_ci		 * @spanned_pages.
10062306a36Sopenharmony_ci		 */
10162306a36Sopenharmony_ci		page_i = shuffle_valid_page(z, i, order);
10262306a36Sopenharmony_ci		if (!page_i)
10362306a36Sopenharmony_ci			continue;
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci		for (retry = 0; retry < SHUFFLE_RETRY; retry++) {
10662306a36Sopenharmony_ci			/*
10762306a36Sopenharmony_ci			 * Pick a random order aligned page in the zone span as
10862306a36Sopenharmony_ci			 * a swap target. If the selected pfn is a hole, retry
10962306a36Sopenharmony_ci			 * up to SHUFFLE_RETRY attempts find a random valid pfn
11062306a36Sopenharmony_ci			 * in the zone.
11162306a36Sopenharmony_ci			 */
11262306a36Sopenharmony_ci			j = z->zone_start_pfn +
11362306a36Sopenharmony_ci				ALIGN_DOWN(get_random_long() % z->spanned_pages,
11462306a36Sopenharmony_ci						order_pages);
11562306a36Sopenharmony_ci			page_j = shuffle_valid_page(z, j, order);
11662306a36Sopenharmony_ci			if (page_j && page_j != page_i)
11762306a36Sopenharmony_ci				break;
11862306a36Sopenharmony_ci		}
11962306a36Sopenharmony_ci		if (retry >= SHUFFLE_RETRY) {
12062306a36Sopenharmony_ci			pr_debug("%s: failed to swap %#lx\n", __func__, i);
12162306a36Sopenharmony_ci			continue;
12262306a36Sopenharmony_ci		}
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_ci		/*
12562306a36Sopenharmony_ci		 * Each migratetype corresponds to its own list, make sure the
12662306a36Sopenharmony_ci		 * types match otherwise we're moving pages to lists where they
12762306a36Sopenharmony_ci		 * do not belong.
12862306a36Sopenharmony_ci		 */
12962306a36Sopenharmony_ci		migratetype = get_pageblock_migratetype(page_i);
13062306a36Sopenharmony_ci		if (get_pageblock_migratetype(page_j) != migratetype) {
13162306a36Sopenharmony_ci			pr_debug("%s: migratetype mismatch %#lx\n", __func__, i);
13262306a36Sopenharmony_ci			continue;
13362306a36Sopenharmony_ci		}
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_ci		list_swap(&page_i->lru, &page_j->lru);
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ci		pr_debug("%s: swap: %#lx -> %#lx\n", __func__, i, j);
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci		/* take it easy on the zone lock */
14062306a36Sopenharmony_ci		if ((i % (100 * order_pages)) == 0) {
14162306a36Sopenharmony_ci			spin_unlock_irqrestore(&z->lock, flags);
14262306a36Sopenharmony_ci			cond_resched();
14362306a36Sopenharmony_ci			spin_lock_irqsave(&z->lock, flags);
14462306a36Sopenharmony_ci		}
14562306a36Sopenharmony_ci	}
14662306a36Sopenharmony_ci	spin_unlock_irqrestore(&z->lock, flags);
14762306a36Sopenharmony_ci}
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_ci/*
15062306a36Sopenharmony_ci * __shuffle_free_memory - reduce the predictability of the page allocator
15162306a36Sopenharmony_ci * @pgdat: node page data
15262306a36Sopenharmony_ci */
15362306a36Sopenharmony_civoid __meminit __shuffle_free_memory(pg_data_t *pgdat)
15462306a36Sopenharmony_ci{
15562306a36Sopenharmony_ci	struct zone *z;
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_ci	for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
15862306a36Sopenharmony_ci		shuffle_zone(z);
15962306a36Sopenharmony_ci}
16062306a36Sopenharmony_ci
16162306a36Sopenharmony_cibool shuffle_pick_tail(void)
16262306a36Sopenharmony_ci{
16362306a36Sopenharmony_ci	static u64 rand;
16462306a36Sopenharmony_ci	static u8 rand_bits;
16562306a36Sopenharmony_ci	bool ret;
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_ci	/*
16862306a36Sopenharmony_ci	 * The lack of locking is deliberate. If 2 threads race to
16962306a36Sopenharmony_ci	 * update the rand state it just adds to the entropy.
17062306a36Sopenharmony_ci	 */
17162306a36Sopenharmony_ci	if (rand_bits == 0) {
17262306a36Sopenharmony_ci		rand_bits = 64;
17362306a36Sopenharmony_ci		rand = get_random_u64();
17462306a36Sopenharmony_ci	}
17562306a36Sopenharmony_ci
17662306a36Sopenharmony_ci	ret = rand & 1;
17762306a36Sopenharmony_ci
17862306a36Sopenharmony_ci	rand_bits--;
17962306a36Sopenharmony_ci	rand >>= 1;
18062306a36Sopenharmony_ci
18162306a36Sopenharmony_ci	return ret;
18262306a36Sopenharmony_ci}
183