162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci#include <linux/mm.h>
362306a36Sopenharmony_ci#include <linux/mmzone.h>
462306a36Sopenharmony_ci#include <linux/page_reporting.h>
562306a36Sopenharmony_ci#include <linux/gfp.h>
662306a36Sopenharmony_ci#include <linux/export.h>
762306a36Sopenharmony_ci#include <linux/module.h>
862306a36Sopenharmony_ci#include <linux/delay.h>
962306a36Sopenharmony_ci#include <linux/scatterlist.h>
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci#include "page_reporting.h"
1262306a36Sopenharmony_ci#include "internal.h"
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci/* Initialize to an unsupported value */
1562306a36Sopenharmony_ciunsigned int page_reporting_order = -1;
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_cistatic int page_order_update_notify(const char *val, const struct kernel_param *kp)
1862306a36Sopenharmony_ci{
1962306a36Sopenharmony_ci	/*
2062306a36Sopenharmony_ci	 * If param is set beyond this limit, order is set to default
2162306a36Sopenharmony_ci	 * pageblock_order value
2262306a36Sopenharmony_ci	 */
2362306a36Sopenharmony_ci	return  param_set_uint_minmax(val, kp, 0, MAX_ORDER);
2462306a36Sopenharmony_ci}
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_cistatic const struct kernel_param_ops page_reporting_param_ops = {
2762306a36Sopenharmony_ci	.set = &page_order_update_notify,
2862306a36Sopenharmony_ci	/*
2962306a36Sopenharmony_ci	 * For the get op, use param_get_int instead of param_get_uint.
3062306a36Sopenharmony_ci	 * This is to make sure that when unset the initialized value of
3162306a36Sopenharmony_ci	 * -1 is shown correctly
3262306a36Sopenharmony_ci	 */
3362306a36Sopenharmony_ci	.get = &param_get_int,
3462306a36Sopenharmony_ci};
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_cimodule_param_cb(page_reporting_order, &page_reporting_param_ops,
3762306a36Sopenharmony_ci			&page_reporting_order, 0644);
3862306a36Sopenharmony_ciMODULE_PARM_DESC(page_reporting_order, "Set page reporting order");
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ci/*
4162306a36Sopenharmony_ci * This symbol is also a kernel parameter. Export the page_reporting_order
4262306a36Sopenharmony_ci * symbol so that other drivers can access it to control order values without
4362306a36Sopenharmony_ci * having to introduce another configurable parameter. Only one driver can
4462306a36Sopenharmony_ci * register with the page_reporting driver for the service, so we have just
4562306a36Sopenharmony_ci * one control parameter for the use case(which can be accessed in both
4662306a36Sopenharmony_ci * drivers)
4762306a36Sopenharmony_ci */
4862306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(page_reporting_order);
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ci#define PAGE_REPORTING_DELAY	(2 * HZ)
5162306a36Sopenharmony_cistatic struct page_reporting_dev_info __rcu *pr_dev_info __read_mostly;
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_cienum {
5462306a36Sopenharmony_ci	PAGE_REPORTING_IDLE = 0,
5562306a36Sopenharmony_ci	PAGE_REPORTING_REQUESTED,
5662306a36Sopenharmony_ci	PAGE_REPORTING_ACTIVE
5762306a36Sopenharmony_ci};
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ci/* request page reporting */
6062306a36Sopenharmony_cistatic void
6162306a36Sopenharmony_ci__page_reporting_request(struct page_reporting_dev_info *prdev)
6262306a36Sopenharmony_ci{
6362306a36Sopenharmony_ci	unsigned int state;
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci	/* Check to see if we are in desired state */
6662306a36Sopenharmony_ci	state = atomic_read(&prdev->state);
6762306a36Sopenharmony_ci	if (state == PAGE_REPORTING_REQUESTED)
6862306a36Sopenharmony_ci		return;
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci	/*
7162306a36Sopenharmony_ci	 * If reporting is already active there is nothing we need to do.
7262306a36Sopenharmony_ci	 * Test against 0 as that represents PAGE_REPORTING_IDLE.
7362306a36Sopenharmony_ci	 */
7462306a36Sopenharmony_ci	state = atomic_xchg(&prdev->state, PAGE_REPORTING_REQUESTED);
7562306a36Sopenharmony_ci	if (state != PAGE_REPORTING_IDLE)
7662306a36Sopenharmony_ci		return;
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_ci	/*
7962306a36Sopenharmony_ci	 * Delay the start of work to allow a sizable queue to build. For
8062306a36Sopenharmony_ci	 * now we are limiting this to running no more than once every
8162306a36Sopenharmony_ci	 * couple of seconds.
8262306a36Sopenharmony_ci	 */
8362306a36Sopenharmony_ci	schedule_delayed_work(&prdev->work, PAGE_REPORTING_DELAY);
8462306a36Sopenharmony_ci}
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_ci/* notify prdev of free page reporting request */
8762306a36Sopenharmony_civoid __page_reporting_notify(void)
8862306a36Sopenharmony_ci{
8962306a36Sopenharmony_ci	struct page_reporting_dev_info *prdev;
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ci	/*
9262306a36Sopenharmony_ci	 * We use RCU to protect the pr_dev_info pointer. In almost all
9362306a36Sopenharmony_ci	 * cases this should be present, however in the unlikely case of
9462306a36Sopenharmony_ci	 * a shutdown this will be NULL and we should exit.
9562306a36Sopenharmony_ci	 */
9662306a36Sopenharmony_ci	rcu_read_lock();
9762306a36Sopenharmony_ci	prdev = rcu_dereference(pr_dev_info);
9862306a36Sopenharmony_ci	if (likely(prdev))
9962306a36Sopenharmony_ci		__page_reporting_request(prdev);
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci	rcu_read_unlock();
10262306a36Sopenharmony_ci}
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_cistatic void
10562306a36Sopenharmony_cipage_reporting_drain(struct page_reporting_dev_info *prdev,
10662306a36Sopenharmony_ci		     struct scatterlist *sgl, unsigned int nents, bool reported)
10762306a36Sopenharmony_ci{
10862306a36Sopenharmony_ci	struct scatterlist *sg = sgl;
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ci	/*
11162306a36Sopenharmony_ci	 * Drain the now reported pages back into their respective
11262306a36Sopenharmony_ci	 * free lists/areas. We assume at least one page is populated.
11362306a36Sopenharmony_ci	 */
11462306a36Sopenharmony_ci	do {
11562306a36Sopenharmony_ci		struct page *page = sg_page(sg);
11662306a36Sopenharmony_ci		int mt = get_pageblock_migratetype(page);
11762306a36Sopenharmony_ci		unsigned int order = get_order(sg->length);
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ci		__putback_isolated_page(page, order, mt);
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ci		/* If the pages were not reported due to error skip flagging */
12262306a36Sopenharmony_ci		if (!reported)
12362306a36Sopenharmony_ci			continue;
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci		/*
12662306a36Sopenharmony_ci		 * If page was not comingled with another page we can
12762306a36Sopenharmony_ci		 * consider the result to be "reported" since the page
12862306a36Sopenharmony_ci		 * hasn't been modified, otherwise we will need to
12962306a36Sopenharmony_ci		 * report on the new larger page when we make our way
13062306a36Sopenharmony_ci		 * up to that higher order.
13162306a36Sopenharmony_ci		 */
13262306a36Sopenharmony_ci		if (PageBuddy(page) && buddy_order(page) == order)
13362306a36Sopenharmony_ci			__SetPageReported(page);
13462306a36Sopenharmony_ci	} while ((sg = sg_next(sg)));
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_ci	/* reinitialize scatterlist now that it is empty */
13762306a36Sopenharmony_ci	sg_init_table(sgl, nents);
13862306a36Sopenharmony_ci}
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_ci/*
14162306a36Sopenharmony_ci * The page reporting cycle consists of 4 stages, fill, report, drain, and
14262306a36Sopenharmony_ci * idle. We will cycle through the first 3 stages until we cannot obtain a
14362306a36Sopenharmony_ci * full scatterlist of pages, in that case we will switch to idle.
14462306a36Sopenharmony_ci */
14562306a36Sopenharmony_cistatic int
14662306a36Sopenharmony_cipage_reporting_cycle(struct page_reporting_dev_info *prdev, struct zone *zone,
14762306a36Sopenharmony_ci		     unsigned int order, unsigned int mt,
14862306a36Sopenharmony_ci		     struct scatterlist *sgl, unsigned int *offset)
14962306a36Sopenharmony_ci{
15062306a36Sopenharmony_ci	struct free_area *area = &zone->free_area[order];
15162306a36Sopenharmony_ci	struct list_head *list = &area->free_list[mt];
15262306a36Sopenharmony_ci	unsigned int page_len = PAGE_SIZE << order;
15362306a36Sopenharmony_ci	struct page *page, *next;
15462306a36Sopenharmony_ci	long budget;
15562306a36Sopenharmony_ci	int err = 0;
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_ci	/*
15862306a36Sopenharmony_ci	 * Perform early check, if free area is empty there is
15962306a36Sopenharmony_ci	 * nothing to process so we can skip this free_list.
16062306a36Sopenharmony_ci	 */
16162306a36Sopenharmony_ci	if (list_empty(list))
16262306a36Sopenharmony_ci		return err;
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_ci	spin_lock_irq(&zone->lock);
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_ci	/*
16762306a36Sopenharmony_ci	 * Limit how many calls we will be making to the page reporting
16862306a36Sopenharmony_ci	 * device for this list. By doing this we avoid processing any
16962306a36Sopenharmony_ci	 * given list for too long.
17062306a36Sopenharmony_ci	 *
17162306a36Sopenharmony_ci	 * The current value used allows us enough calls to process over a
17262306a36Sopenharmony_ci	 * sixteenth of the current list plus one additional call to handle
17362306a36Sopenharmony_ci	 * any pages that may have already been present from the previous
17462306a36Sopenharmony_ci	 * list processed. This should result in us reporting all pages on
17562306a36Sopenharmony_ci	 * an idle system in about 30 seconds.
17662306a36Sopenharmony_ci	 *
17762306a36Sopenharmony_ci	 * The division here should be cheap since PAGE_REPORTING_CAPACITY
17862306a36Sopenharmony_ci	 * should always be a power of 2.
17962306a36Sopenharmony_ci	 */
18062306a36Sopenharmony_ci	budget = DIV_ROUND_UP(area->nr_free, PAGE_REPORTING_CAPACITY * 16);
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_ci	/* loop through free list adding unreported pages to sg list */
18362306a36Sopenharmony_ci	list_for_each_entry_safe(page, next, list, lru) {
18462306a36Sopenharmony_ci		/* We are going to skip over the reported pages. */
18562306a36Sopenharmony_ci		if (PageReported(page))
18662306a36Sopenharmony_ci			continue;
18762306a36Sopenharmony_ci
18862306a36Sopenharmony_ci		/*
18962306a36Sopenharmony_ci		 * If we fully consumed our budget then update our
19062306a36Sopenharmony_ci		 * state to indicate that we are requesting additional
19162306a36Sopenharmony_ci		 * processing and exit this list.
19262306a36Sopenharmony_ci		 */
19362306a36Sopenharmony_ci		if (budget < 0) {
19462306a36Sopenharmony_ci			atomic_set(&prdev->state, PAGE_REPORTING_REQUESTED);
19562306a36Sopenharmony_ci			next = page;
19662306a36Sopenharmony_ci			break;
19762306a36Sopenharmony_ci		}
19862306a36Sopenharmony_ci
19962306a36Sopenharmony_ci		/* Attempt to pull page from list and place in scatterlist */
20062306a36Sopenharmony_ci		if (*offset) {
20162306a36Sopenharmony_ci			if (!__isolate_free_page(page, order)) {
20262306a36Sopenharmony_ci				next = page;
20362306a36Sopenharmony_ci				break;
20462306a36Sopenharmony_ci			}
20562306a36Sopenharmony_ci
20662306a36Sopenharmony_ci			/* Add page to scatter list */
20762306a36Sopenharmony_ci			--(*offset);
20862306a36Sopenharmony_ci			sg_set_page(&sgl[*offset], page, page_len, 0);
20962306a36Sopenharmony_ci
21062306a36Sopenharmony_ci			continue;
21162306a36Sopenharmony_ci		}
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_ci		/*
21462306a36Sopenharmony_ci		 * Make the first non-reported page in the free list
21562306a36Sopenharmony_ci		 * the new head of the free list before we release the
21662306a36Sopenharmony_ci		 * zone lock.
21762306a36Sopenharmony_ci		 */
21862306a36Sopenharmony_ci		if (!list_is_first(&page->lru, list))
21962306a36Sopenharmony_ci			list_rotate_to_front(&page->lru, list);
22062306a36Sopenharmony_ci
22162306a36Sopenharmony_ci		/* release lock before waiting on report processing */
22262306a36Sopenharmony_ci		spin_unlock_irq(&zone->lock);
22362306a36Sopenharmony_ci
22462306a36Sopenharmony_ci		/* begin processing pages in local list */
22562306a36Sopenharmony_ci		err = prdev->report(prdev, sgl, PAGE_REPORTING_CAPACITY);
22662306a36Sopenharmony_ci
22762306a36Sopenharmony_ci		/* reset offset since the full list was reported */
22862306a36Sopenharmony_ci		*offset = PAGE_REPORTING_CAPACITY;
22962306a36Sopenharmony_ci
23062306a36Sopenharmony_ci		/* update budget to reflect call to report function */
23162306a36Sopenharmony_ci		budget--;
23262306a36Sopenharmony_ci
23362306a36Sopenharmony_ci		/* reacquire zone lock and resume processing */
23462306a36Sopenharmony_ci		spin_lock_irq(&zone->lock);
23562306a36Sopenharmony_ci
23662306a36Sopenharmony_ci		/* flush reported pages from the sg list */
23762306a36Sopenharmony_ci		page_reporting_drain(prdev, sgl, PAGE_REPORTING_CAPACITY, !err);
23862306a36Sopenharmony_ci
23962306a36Sopenharmony_ci		/*
24062306a36Sopenharmony_ci		 * Reset next to first entry, the old next isn't valid
24162306a36Sopenharmony_ci		 * since we dropped the lock to report the pages
24262306a36Sopenharmony_ci		 */
24362306a36Sopenharmony_ci		next = list_first_entry(list, struct page, lru);
24462306a36Sopenharmony_ci
24562306a36Sopenharmony_ci		/* exit on error */
24662306a36Sopenharmony_ci		if (err)
24762306a36Sopenharmony_ci			break;
24862306a36Sopenharmony_ci	}
24962306a36Sopenharmony_ci
25062306a36Sopenharmony_ci	/* Rotate any leftover pages to the head of the freelist */
25162306a36Sopenharmony_ci	if (!list_entry_is_head(next, list, lru) && !list_is_first(&next->lru, list))
25262306a36Sopenharmony_ci		list_rotate_to_front(&next->lru, list);
25362306a36Sopenharmony_ci
25462306a36Sopenharmony_ci	spin_unlock_irq(&zone->lock);
25562306a36Sopenharmony_ci
25662306a36Sopenharmony_ci	return err;
25762306a36Sopenharmony_ci}
25862306a36Sopenharmony_ci
25962306a36Sopenharmony_cistatic int
26062306a36Sopenharmony_cipage_reporting_process_zone(struct page_reporting_dev_info *prdev,
26162306a36Sopenharmony_ci			    struct scatterlist *sgl, struct zone *zone)
26262306a36Sopenharmony_ci{
26362306a36Sopenharmony_ci	unsigned int order, mt, leftover, offset = PAGE_REPORTING_CAPACITY;
26462306a36Sopenharmony_ci	unsigned long watermark;
26562306a36Sopenharmony_ci	int err = 0;
26662306a36Sopenharmony_ci
26762306a36Sopenharmony_ci	/* Generate minimum watermark to be able to guarantee progress */
26862306a36Sopenharmony_ci	watermark = low_wmark_pages(zone) +
26962306a36Sopenharmony_ci		    (PAGE_REPORTING_CAPACITY << page_reporting_order);
27062306a36Sopenharmony_ci
27162306a36Sopenharmony_ci	/*
27262306a36Sopenharmony_ci	 * Cancel request if insufficient free memory or if we failed
27362306a36Sopenharmony_ci	 * to allocate page reporting statistics for the zone.
27462306a36Sopenharmony_ci	 */
27562306a36Sopenharmony_ci	if (!zone_watermark_ok(zone, 0, watermark, 0, ALLOC_CMA))
27662306a36Sopenharmony_ci		return err;
27762306a36Sopenharmony_ci
27862306a36Sopenharmony_ci	/* Process each free list starting from lowest order/mt */
27962306a36Sopenharmony_ci	for (order = page_reporting_order; order <= MAX_ORDER; order++) {
28062306a36Sopenharmony_ci		for (mt = 0; mt < MIGRATE_TYPES; mt++) {
28162306a36Sopenharmony_ci			/* We do not pull pages from the isolate free list */
28262306a36Sopenharmony_ci			if (is_migrate_isolate(mt))
28362306a36Sopenharmony_ci				continue;
28462306a36Sopenharmony_ci
28562306a36Sopenharmony_ci			err = page_reporting_cycle(prdev, zone, order, mt,
28662306a36Sopenharmony_ci						   sgl, &offset);
28762306a36Sopenharmony_ci			if (err)
28862306a36Sopenharmony_ci				return err;
28962306a36Sopenharmony_ci		}
29062306a36Sopenharmony_ci	}
29162306a36Sopenharmony_ci
29262306a36Sopenharmony_ci	/* report the leftover pages before going idle */
29362306a36Sopenharmony_ci	leftover = PAGE_REPORTING_CAPACITY - offset;
29462306a36Sopenharmony_ci	if (leftover) {
29562306a36Sopenharmony_ci		sgl = &sgl[offset];
29662306a36Sopenharmony_ci		err = prdev->report(prdev, sgl, leftover);
29762306a36Sopenharmony_ci
29862306a36Sopenharmony_ci		/* flush any remaining pages out from the last report */
29962306a36Sopenharmony_ci		spin_lock_irq(&zone->lock);
30062306a36Sopenharmony_ci		page_reporting_drain(prdev, sgl, leftover, !err);
30162306a36Sopenharmony_ci		spin_unlock_irq(&zone->lock);
30262306a36Sopenharmony_ci	}
30362306a36Sopenharmony_ci
30462306a36Sopenharmony_ci	return err;
30562306a36Sopenharmony_ci}
30662306a36Sopenharmony_ci
30762306a36Sopenharmony_cistatic void page_reporting_process(struct work_struct *work)
30862306a36Sopenharmony_ci{
30962306a36Sopenharmony_ci	struct delayed_work *d_work = to_delayed_work(work);
31062306a36Sopenharmony_ci	struct page_reporting_dev_info *prdev =
31162306a36Sopenharmony_ci		container_of(d_work, struct page_reporting_dev_info, work);
31262306a36Sopenharmony_ci	int err = 0, state = PAGE_REPORTING_ACTIVE;
31362306a36Sopenharmony_ci	struct scatterlist *sgl;
31462306a36Sopenharmony_ci	struct zone *zone;
31562306a36Sopenharmony_ci
31662306a36Sopenharmony_ci	/*
31762306a36Sopenharmony_ci	 * Change the state to "Active" so that we can track if there is
31862306a36Sopenharmony_ci	 * anyone requests page reporting after we complete our pass. If
31962306a36Sopenharmony_ci	 * the state is not altered by the end of the pass we will switch
32062306a36Sopenharmony_ci	 * to idle and quit scheduling reporting runs.
32162306a36Sopenharmony_ci	 */
32262306a36Sopenharmony_ci	atomic_set(&prdev->state, state);
32362306a36Sopenharmony_ci
32462306a36Sopenharmony_ci	/* allocate scatterlist to store pages being reported on */
32562306a36Sopenharmony_ci	sgl = kmalloc_array(PAGE_REPORTING_CAPACITY, sizeof(*sgl), GFP_KERNEL);
32662306a36Sopenharmony_ci	if (!sgl)
32762306a36Sopenharmony_ci		goto err_out;
32862306a36Sopenharmony_ci
32962306a36Sopenharmony_ci	sg_init_table(sgl, PAGE_REPORTING_CAPACITY);
33062306a36Sopenharmony_ci
33162306a36Sopenharmony_ci	for_each_zone(zone) {
33262306a36Sopenharmony_ci		err = page_reporting_process_zone(prdev, sgl, zone);
33362306a36Sopenharmony_ci		if (err)
33462306a36Sopenharmony_ci			break;
33562306a36Sopenharmony_ci	}
33662306a36Sopenharmony_ci
33762306a36Sopenharmony_ci	kfree(sgl);
33862306a36Sopenharmony_cierr_out:
33962306a36Sopenharmony_ci	/*
34062306a36Sopenharmony_ci	 * If the state has reverted back to requested then there may be
34162306a36Sopenharmony_ci	 * additional pages to be processed. We will defer for 2s to allow
34262306a36Sopenharmony_ci	 * more pages to accumulate.
34362306a36Sopenharmony_ci	 */
34462306a36Sopenharmony_ci	state = atomic_cmpxchg(&prdev->state, state, PAGE_REPORTING_IDLE);
34562306a36Sopenharmony_ci	if (state == PAGE_REPORTING_REQUESTED)
34662306a36Sopenharmony_ci		schedule_delayed_work(&prdev->work, PAGE_REPORTING_DELAY);
34762306a36Sopenharmony_ci}
34862306a36Sopenharmony_ci
34962306a36Sopenharmony_cistatic DEFINE_MUTEX(page_reporting_mutex);
35062306a36Sopenharmony_ciDEFINE_STATIC_KEY_FALSE(page_reporting_enabled);
35162306a36Sopenharmony_ci
35262306a36Sopenharmony_ciint page_reporting_register(struct page_reporting_dev_info *prdev)
35362306a36Sopenharmony_ci{
35462306a36Sopenharmony_ci	int err = 0;
35562306a36Sopenharmony_ci
35662306a36Sopenharmony_ci	mutex_lock(&page_reporting_mutex);
35762306a36Sopenharmony_ci
35862306a36Sopenharmony_ci	/* nothing to do if already in use */
35962306a36Sopenharmony_ci	if (rcu_dereference_protected(pr_dev_info,
36062306a36Sopenharmony_ci				lockdep_is_held(&page_reporting_mutex))) {
36162306a36Sopenharmony_ci		err = -EBUSY;
36262306a36Sopenharmony_ci		goto err_out;
36362306a36Sopenharmony_ci	}
36462306a36Sopenharmony_ci
36562306a36Sopenharmony_ci	/*
36662306a36Sopenharmony_ci	 * If the page_reporting_order value is not set, we check if
36762306a36Sopenharmony_ci	 * an order is provided from the driver that is performing the
36862306a36Sopenharmony_ci	 * registration. If that is not provided either, we default to
36962306a36Sopenharmony_ci	 * pageblock_order.
37062306a36Sopenharmony_ci	 */
37162306a36Sopenharmony_ci
37262306a36Sopenharmony_ci	if (page_reporting_order == -1) {
37362306a36Sopenharmony_ci		if (prdev->order > 0 && prdev->order <= MAX_ORDER)
37462306a36Sopenharmony_ci			page_reporting_order = prdev->order;
37562306a36Sopenharmony_ci		else
37662306a36Sopenharmony_ci			page_reporting_order = pageblock_order;
37762306a36Sopenharmony_ci	}
37862306a36Sopenharmony_ci
37962306a36Sopenharmony_ci	/* initialize state and work structures */
38062306a36Sopenharmony_ci	atomic_set(&prdev->state, PAGE_REPORTING_IDLE);
38162306a36Sopenharmony_ci	INIT_DELAYED_WORK(&prdev->work, &page_reporting_process);
38262306a36Sopenharmony_ci
38362306a36Sopenharmony_ci	/* Begin initial flush of zones */
38462306a36Sopenharmony_ci	__page_reporting_request(prdev);
38562306a36Sopenharmony_ci
38662306a36Sopenharmony_ci	/* Assign device to allow notifications */
38762306a36Sopenharmony_ci	rcu_assign_pointer(pr_dev_info, prdev);
38862306a36Sopenharmony_ci
38962306a36Sopenharmony_ci	/* enable page reporting notification */
39062306a36Sopenharmony_ci	if (!static_key_enabled(&page_reporting_enabled)) {
39162306a36Sopenharmony_ci		static_branch_enable(&page_reporting_enabled);
39262306a36Sopenharmony_ci		pr_info("Free page reporting enabled\n");
39362306a36Sopenharmony_ci	}
39462306a36Sopenharmony_cierr_out:
39562306a36Sopenharmony_ci	mutex_unlock(&page_reporting_mutex);
39662306a36Sopenharmony_ci
39762306a36Sopenharmony_ci	return err;
39862306a36Sopenharmony_ci}
39962306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(page_reporting_register);
40062306a36Sopenharmony_ci
40162306a36Sopenharmony_civoid page_reporting_unregister(struct page_reporting_dev_info *prdev)
40262306a36Sopenharmony_ci{
40362306a36Sopenharmony_ci	mutex_lock(&page_reporting_mutex);
40462306a36Sopenharmony_ci
40562306a36Sopenharmony_ci	if (prdev == rcu_dereference_protected(pr_dev_info,
40662306a36Sopenharmony_ci				lockdep_is_held(&page_reporting_mutex))) {
40762306a36Sopenharmony_ci		/* Disable page reporting notification */
40862306a36Sopenharmony_ci		RCU_INIT_POINTER(pr_dev_info, NULL);
40962306a36Sopenharmony_ci		synchronize_rcu();
41062306a36Sopenharmony_ci
41162306a36Sopenharmony_ci		/* Flush any existing work, and lock it out */
41262306a36Sopenharmony_ci		cancel_delayed_work_sync(&prdev->work);
41362306a36Sopenharmony_ci	}
41462306a36Sopenharmony_ci
41562306a36Sopenharmony_ci	mutex_unlock(&page_reporting_mutex);
41662306a36Sopenharmony_ci}
41762306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(page_reporting_unregister);
418