18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * KASAN quarantine.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Author: Alexander Potapenko <glider@google.com>
68c2ecf20Sopenharmony_ci * Copyright (C) 2016 Google, Inc.
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Based on code by Dmitry Chernenkov.
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or
118c2ecf20Sopenharmony_ci * modify it under the terms of the GNU General Public License
128c2ecf20Sopenharmony_ci * version 2 as published by the Free Software Foundation.
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * This program is distributed in the hope that it will be useful, but
158c2ecf20Sopenharmony_ci * WITHOUT ANY WARRANTY; without even the implied warranty of
168c2ecf20Sopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
178c2ecf20Sopenharmony_ci * General Public License for more details.
188c2ecf20Sopenharmony_ci *
198c2ecf20Sopenharmony_ci */
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#include <linux/gfp.h>
228c2ecf20Sopenharmony_ci#include <linux/hash.h>
238c2ecf20Sopenharmony_ci#include <linux/kernel.h>
248c2ecf20Sopenharmony_ci#include <linux/mm.h>
258c2ecf20Sopenharmony_ci#include <linux/percpu.h>
268c2ecf20Sopenharmony_ci#include <linux/printk.h>
278c2ecf20Sopenharmony_ci#include <linux/shrinker.h>
288c2ecf20Sopenharmony_ci#include <linux/slab.h>
298c2ecf20Sopenharmony_ci#include <linux/srcu.h>
308c2ecf20Sopenharmony_ci#include <linux/string.h>
318c2ecf20Sopenharmony_ci#include <linux/types.h>
328c2ecf20Sopenharmony_ci#include <linux/cpuhotplug.h>
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci#include "../slab.h"
358c2ecf20Sopenharmony_ci#include "kasan.h"
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci/* Data structure and operations for quarantine queues. */
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci/*
408c2ecf20Sopenharmony_ci * Each queue is a signle-linked list, which also stores the total size of
418c2ecf20Sopenharmony_ci * objects inside of it.
428c2ecf20Sopenharmony_ci */
438c2ecf20Sopenharmony_cistruct qlist_head {
448c2ecf20Sopenharmony_ci	struct qlist_node *head;
458c2ecf20Sopenharmony_ci	struct qlist_node *tail;
468c2ecf20Sopenharmony_ci	size_t bytes;
478c2ecf20Sopenharmony_ci	bool offline;
488c2ecf20Sopenharmony_ci};
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci#define QLIST_INIT { NULL, NULL, 0 }
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_cistatic bool qlist_empty(struct qlist_head *q)
538c2ecf20Sopenharmony_ci{
548c2ecf20Sopenharmony_ci	return !q->head;
558c2ecf20Sopenharmony_ci}
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_cistatic void qlist_init(struct qlist_head *q)
588c2ecf20Sopenharmony_ci{
598c2ecf20Sopenharmony_ci	q->head = q->tail = NULL;
608c2ecf20Sopenharmony_ci	q->bytes = 0;
618c2ecf20Sopenharmony_ci}
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_cistatic void qlist_put(struct qlist_head *q, struct qlist_node *qlink,
648c2ecf20Sopenharmony_ci		size_t size)
658c2ecf20Sopenharmony_ci{
668c2ecf20Sopenharmony_ci	if (unlikely(qlist_empty(q)))
678c2ecf20Sopenharmony_ci		q->head = qlink;
688c2ecf20Sopenharmony_ci	else
698c2ecf20Sopenharmony_ci		q->tail->next = qlink;
708c2ecf20Sopenharmony_ci	q->tail = qlink;
718c2ecf20Sopenharmony_ci	qlink->next = NULL;
728c2ecf20Sopenharmony_ci	q->bytes += size;
738c2ecf20Sopenharmony_ci}
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_cistatic void qlist_move_all(struct qlist_head *from, struct qlist_head *to)
768c2ecf20Sopenharmony_ci{
778c2ecf20Sopenharmony_ci	if (unlikely(qlist_empty(from)))
788c2ecf20Sopenharmony_ci		return;
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	if (qlist_empty(to)) {
818c2ecf20Sopenharmony_ci		*to = *from;
828c2ecf20Sopenharmony_ci		qlist_init(from);
838c2ecf20Sopenharmony_ci		return;
848c2ecf20Sopenharmony_ci	}
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	to->tail->next = from->head;
878c2ecf20Sopenharmony_ci	to->tail = from->tail;
888c2ecf20Sopenharmony_ci	to->bytes += from->bytes;
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	qlist_init(from);
918c2ecf20Sopenharmony_ci}
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci#define QUARANTINE_PERCPU_SIZE (1 << 20)
948c2ecf20Sopenharmony_ci#define QUARANTINE_BATCHES \
958c2ecf20Sopenharmony_ci	(1024 > 4 * CONFIG_NR_CPUS ? 1024 : 4 * CONFIG_NR_CPUS)
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci/*
988c2ecf20Sopenharmony_ci * The object quarantine consists of per-cpu queues and a global queue,
998c2ecf20Sopenharmony_ci * guarded by quarantine_lock.
1008c2ecf20Sopenharmony_ci */
1018c2ecf20Sopenharmony_cistatic DEFINE_PER_CPU(struct qlist_head, cpu_quarantine);
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci/* Round-robin FIFO array of batches. */
1048c2ecf20Sopenharmony_cistatic struct qlist_head global_quarantine[QUARANTINE_BATCHES];
1058c2ecf20Sopenharmony_cistatic int quarantine_head;
1068c2ecf20Sopenharmony_cistatic int quarantine_tail;
1078c2ecf20Sopenharmony_ci/* Total size of all objects in global_quarantine across all batches. */
1088c2ecf20Sopenharmony_cistatic unsigned long quarantine_size;
1098c2ecf20Sopenharmony_cistatic DEFINE_RAW_SPINLOCK(quarantine_lock);
1108c2ecf20Sopenharmony_ciDEFINE_STATIC_SRCU(remove_cache_srcu);
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci/* Maximum size of the global queue. */
1138c2ecf20Sopenharmony_cistatic unsigned long quarantine_max_size;
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci/*
1168c2ecf20Sopenharmony_ci * Target size of a batch in global_quarantine.
1178c2ecf20Sopenharmony_ci * Usually equal to QUARANTINE_PERCPU_SIZE unless we have too much RAM.
1188c2ecf20Sopenharmony_ci */
1198c2ecf20Sopenharmony_cistatic unsigned long quarantine_batch_size;
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci/*
1228c2ecf20Sopenharmony_ci * The fraction of physical memory the quarantine is allowed to occupy.
1238c2ecf20Sopenharmony_ci * Quarantine doesn't support memory shrinker with SLAB allocator, so we keep
1248c2ecf20Sopenharmony_ci * the ratio low to avoid OOM.
1258c2ecf20Sopenharmony_ci */
1268c2ecf20Sopenharmony_ci#define QUARANTINE_FRACTION 32
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_cistatic struct kmem_cache *qlink_to_cache(struct qlist_node *qlink)
1298c2ecf20Sopenharmony_ci{
1308c2ecf20Sopenharmony_ci	return virt_to_head_page(qlink)->slab_cache;
1318c2ecf20Sopenharmony_ci}
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_cistatic void *qlink_to_object(struct qlist_node *qlink, struct kmem_cache *cache)
1348c2ecf20Sopenharmony_ci{
1358c2ecf20Sopenharmony_ci	struct kasan_free_meta *free_info =
1368c2ecf20Sopenharmony_ci		container_of(qlink, struct kasan_free_meta,
1378c2ecf20Sopenharmony_ci			     quarantine_link);
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	return ((void *)free_info) - cache->kasan_info.free_meta_offset;
1408c2ecf20Sopenharmony_ci}
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_cistatic void qlink_free(struct qlist_node *qlink, struct kmem_cache *cache)
1438c2ecf20Sopenharmony_ci{
1448c2ecf20Sopenharmony_ci	void *object = qlink_to_object(qlink, cache);
1458c2ecf20Sopenharmony_ci	unsigned long flags;
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	if (IS_ENABLED(CONFIG_SLAB))
1488c2ecf20Sopenharmony_ci		local_irq_save(flags);
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	*(u8 *)kasan_mem_to_shadow(object) = KASAN_KMALLOC_FREE;
1518c2ecf20Sopenharmony_ci	___cache_free(cache, object, _THIS_IP_);
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	if (IS_ENABLED(CONFIG_SLAB))
1548c2ecf20Sopenharmony_ci		local_irq_restore(flags);
1558c2ecf20Sopenharmony_ci}
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_cistatic void qlist_free_all(struct qlist_head *q, struct kmem_cache *cache)
1588c2ecf20Sopenharmony_ci{
1598c2ecf20Sopenharmony_ci	struct qlist_node *qlink;
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	if (unlikely(qlist_empty(q)))
1628c2ecf20Sopenharmony_ci		return;
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	qlink = q->head;
1658c2ecf20Sopenharmony_ci	while (qlink) {
1668c2ecf20Sopenharmony_ci		struct kmem_cache *obj_cache =
1678c2ecf20Sopenharmony_ci			cache ? cache :	qlink_to_cache(qlink);
1688c2ecf20Sopenharmony_ci		struct qlist_node *next = qlink->next;
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci		qlink_free(qlink, obj_cache);
1718c2ecf20Sopenharmony_ci		qlink = next;
1728c2ecf20Sopenharmony_ci	}
1738c2ecf20Sopenharmony_ci	qlist_init(q);
1748c2ecf20Sopenharmony_ci}
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_civoid quarantine_put(struct kasan_free_meta *info, struct kmem_cache *cache)
1778c2ecf20Sopenharmony_ci{
1788c2ecf20Sopenharmony_ci	unsigned long flags;
1798c2ecf20Sopenharmony_ci	struct qlist_head *q;
1808c2ecf20Sopenharmony_ci	struct qlist_head temp = QLIST_INIT;
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	/*
1838c2ecf20Sopenharmony_ci	 * Note: irq must be disabled until after we move the batch to the
1848c2ecf20Sopenharmony_ci	 * global quarantine. Otherwise quarantine_remove_cache() can miss
1858c2ecf20Sopenharmony_ci	 * some objects belonging to the cache if they are in our local temp
1868c2ecf20Sopenharmony_ci	 * list. quarantine_remove_cache() executes on_each_cpu() at the
1878c2ecf20Sopenharmony_ci	 * beginning which ensures that it either sees the objects in per-cpu
1888c2ecf20Sopenharmony_ci	 * lists or in the global quarantine.
1898c2ecf20Sopenharmony_ci	 */
1908c2ecf20Sopenharmony_ci	local_irq_save(flags);
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	q = this_cpu_ptr(&cpu_quarantine);
1938c2ecf20Sopenharmony_ci	if (q->offline) {
1948c2ecf20Sopenharmony_ci		local_irq_restore(flags);
1958c2ecf20Sopenharmony_ci		return;
1968c2ecf20Sopenharmony_ci	}
1978c2ecf20Sopenharmony_ci	qlist_put(q, &info->quarantine_link, cache->size);
1988c2ecf20Sopenharmony_ci	if (unlikely(q->bytes > QUARANTINE_PERCPU_SIZE)) {
1998c2ecf20Sopenharmony_ci		qlist_move_all(q, &temp);
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci		raw_spin_lock(&quarantine_lock);
2028c2ecf20Sopenharmony_ci		WRITE_ONCE(quarantine_size, quarantine_size + temp.bytes);
2038c2ecf20Sopenharmony_ci		qlist_move_all(&temp, &global_quarantine[quarantine_tail]);
2048c2ecf20Sopenharmony_ci		if (global_quarantine[quarantine_tail].bytes >=
2058c2ecf20Sopenharmony_ci				READ_ONCE(quarantine_batch_size)) {
2068c2ecf20Sopenharmony_ci			int new_tail;
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci			new_tail = quarantine_tail + 1;
2098c2ecf20Sopenharmony_ci			if (new_tail == QUARANTINE_BATCHES)
2108c2ecf20Sopenharmony_ci				new_tail = 0;
2118c2ecf20Sopenharmony_ci			if (new_tail != quarantine_head)
2128c2ecf20Sopenharmony_ci				quarantine_tail = new_tail;
2138c2ecf20Sopenharmony_ci		}
2148c2ecf20Sopenharmony_ci		raw_spin_unlock(&quarantine_lock);
2158c2ecf20Sopenharmony_ci	}
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci	local_irq_restore(flags);
2188c2ecf20Sopenharmony_ci}
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_civoid quarantine_reduce(void)
2218c2ecf20Sopenharmony_ci{
2228c2ecf20Sopenharmony_ci	size_t total_size, new_quarantine_size, percpu_quarantines;
2238c2ecf20Sopenharmony_ci	unsigned long flags;
2248c2ecf20Sopenharmony_ci	int srcu_idx;
2258c2ecf20Sopenharmony_ci	struct qlist_head to_free = QLIST_INIT;
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci	if (likely(READ_ONCE(quarantine_size) <=
2288c2ecf20Sopenharmony_ci		   READ_ONCE(quarantine_max_size)))
2298c2ecf20Sopenharmony_ci		return;
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci	/*
2328c2ecf20Sopenharmony_ci	 * srcu critical section ensures that quarantine_remove_cache()
2338c2ecf20Sopenharmony_ci	 * will not miss objects belonging to the cache while they are in our
2348c2ecf20Sopenharmony_ci	 * local to_free list. srcu is chosen because (1) it gives us private
2358c2ecf20Sopenharmony_ci	 * grace period domain that does not interfere with anything else,
2368c2ecf20Sopenharmony_ci	 * and (2) it allows synchronize_srcu() to return without waiting
2378c2ecf20Sopenharmony_ci	 * if there are no pending read critical sections (which is the
2388c2ecf20Sopenharmony_ci	 * expected case).
2398c2ecf20Sopenharmony_ci	 */
2408c2ecf20Sopenharmony_ci	srcu_idx = srcu_read_lock(&remove_cache_srcu);
2418c2ecf20Sopenharmony_ci	raw_spin_lock_irqsave(&quarantine_lock, flags);
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci	/*
2448c2ecf20Sopenharmony_ci	 * Update quarantine size in case of hotplug. Allocate a fraction of
2458c2ecf20Sopenharmony_ci	 * the installed memory to quarantine minus per-cpu queue limits.
2468c2ecf20Sopenharmony_ci	 */
2478c2ecf20Sopenharmony_ci	total_size = (totalram_pages() << PAGE_SHIFT) /
2488c2ecf20Sopenharmony_ci		QUARANTINE_FRACTION;
2498c2ecf20Sopenharmony_ci	percpu_quarantines = QUARANTINE_PERCPU_SIZE * num_online_cpus();
2508c2ecf20Sopenharmony_ci	new_quarantine_size = (total_size < percpu_quarantines) ?
2518c2ecf20Sopenharmony_ci		0 : total_size - percpu_quarantines;
2528c2ecf20Sopenharmony_ci	WRITE_ONCE(quarantine_max_size, new_quarantine_size);
2538c2ecf20Sopenharmony_ci	/* Aim at consuming at most 1/2 of slots in quarantine. */
2548c2ecf20Sopenharmony_ci	WRITE_ONCE(quarantine_batch_size, max((size_t)QUARANTINE_PERCPU_SIZE,
2558c2ecf20Sopenharmony_ci		2 * total_size / QUARANTINE_BATCHES));
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci	if (likely(quarantine_size > quarantine_max_size)) {
2588c2ecf20Sopenharmony_ci		qlist_move_all(&global_quarantine[quarantine_head], &to_free);
2598c2ecf20Sopenharmony_ci		WRITE_ONCE(quarantine_size, quarantine_size - to_free.bytes);
2608c2ecf20Sopenharmony_ci		quarantine_head++;
2618c2ecf20Sopenharmony_ci		if (quarantine_head == QUARANTINE_BATCHES)
2628c2ecf20Sopenharmony_ci			quarantine_head = 0;
2638c2ecf20Sopenharmony_ci	}
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci	raw_spin_unlock_irqrestore(&quarantine_lock, flags);
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci	qlist_free_all(&to_free, NULL);
2688c2ecf20Sopenharmony_ci	srcu_read_unlock(&remove_cache_srcu, srcu_idx);
2698c2ecf20Sopenharmony_ci}
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_cistatic void qlist_move_cache(struct qlist_head *from,
2728c2ecf20Sopenharmony_ci				   struct qlist_head *to,
2738c2ecf20Sopenharmony_ci				   struct kmem_cache *cache)
2748c2ecf20Sopenharmony_ci{
2758c2ecf20Sopenharmony_ci	struct qlist_node *curr;
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci	if (unlikely(qlist_empty(from)))
2788c2ecf20Sopenharmony_ci		return;
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci	curr = from->head;
2818c2ecf20Sopenharmony_ci	qlist_init(from);
2828c2ecf20Sopenharmony_ci	while (curr) {
2838c2ecf20Sopenharmony_ci		struct qlist_node *next = curr->next;
2848c2ecf20Sopenharmony_ci		struct kmem_cache *obj_cache = qlink_to_cache(curr);
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci		if (obj_cache == cache)
2878c2ecf20Sopenharmony_ci			qlist_put(to, curr, obj_cache->size);
2888c2ecf20Sopenharmony_ci		else
2898c2ecf20Sopenharmony_ci			qlist_put(from, curr, obj_cache->size);
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci		curr = next;
2928c2ecf20Sopenharmony_ci	}
2938c2ecf20Sopenharmony_ci}
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_cistatic void per_cpu_remove_cache(void *arg)
2968c2ecf20Sopenharmony_ci{
2978c2ecf20Sopenharmony_ci	struct kmem_cache *cache = arg;
2988c2ecf20Sopenharmony_ci	struct qlist_head to_free = QLIST_INIT;
2998c2ecf20Sopenharmony_ci	struct qlist_head *q;
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci	q = this_cpu_ptr(&cpu_quarantine);
3028c2ecf20Sopenharmony_ci	/*
3038c2ecf20Sopenharmony_ci	 * Ensure the ordering between the writing to q->offline and
3048c2ecf20Sopenharmony_ci	 * per_cpu_remove_cache.  Prevent cpu_quarantine from being corrupted
3058c2ecf20Sopenharmony_ci	 * by interrupt.
3068c2ecf20Sopenharmony_ci	 */
3078c2ecf20Sopenharmony_ci	if (READ_ONCE(q->offline))
3088c2ecf20Sopenharmony_ci		return;
3098c2ecf20Sopenharmony_ci	qlist_move_cache(q, &to_free, cache);
3108c2ecf20Sopenharmony_ci	qlist_free_all(&to_free, cache);
3118c2ecf20Sopenharmony_ci}
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci/* Free all quarantined objects belonging to cache. */
3148c2ecf20Sopenharmony_civoid quarantine_remove_cache(struct kmem_cache *cache)
3158c2ecf20Sopenharmony_ci{
3168c2ecf20Sopenharmony_ci	unsigned long flags, i;
3178c2ecf20Sopenharmony_ci	struct qlist_head to_free = QLIST_INIT;
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci	/*
3208c2ecf20Sopenharmony_ci	 * Must be careful to not miss any objects that are being moved from
3218c2ecf20Sopenharmony_ci	 * per-cpu list to the global quarantine in quarantine_put(),
3228c2ecf20Sopenharmony_ci	 * nor objects being freed in quarantine_reduce(). on_each_cpu()
3238c2ecf20Sopenharmony_ci	 * achieves the first goal, while synchronize_srcu() achieves the
3248c2ecf20Sopenharmony_ci	 * second.
3258c2ecf20Sopenharmony_ci	 */
3268c2ecf20Sopenharmony_ci	on_each_cpu(per_cpu_remove_cache, cache, 1);
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ci	raw_spin_lock_irqsave(&quarantine_lock, flags);
3298c2ecf20Sopenharmony_ci	for (i = 0; i < QUARANTINE_BATCHES; i++) {
3308c2ecf20Sopenharmony_ci		if (qlist_empty(&global_quarantine[i]))
3318c2ecf20Sopenharmony_ci			continue;
3328c2ecf20Sopenharmony_ci		qlist_move_cache(&global_quarantine[i], &to_free, cache);
3338c2ecf20Sopenharmony_ci		/* Scanning whole quarantine can take a while. */
3348c2ecf20Sopenharmony_ci		raw_spin_unlock_irqrestore(&quarantine_lock, flags);
3358c2ecf20Sopenharmony_ci		cond_resched();
3368c2ecf20Sopenharmony_ci		raw_spin_lock_irqsave(&quarantine_lock, flags);
3378c2ecf20Sopenharmony_ci	}
3388c2ecf20Sopenharmony_ci	raw_spin_unlock_irqrestore(&quarantine_lock, flags);
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci	qlist_free_all(&to_free, cache);
3418c2ecf20Sopenharmony_ci
3428c2ecf20Sopenharmony_ci	synchronize_srcu(&remove_cache_srcu);
3438c2ecf20Sopenharmony_ci}
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_cistatic int kasan_cpu_online(unsigned int cpu)
3468c2ecf20Sopenharmony_ci{
3478c2ecf20Sopenharmony_ci	this_cpu_ptr(&cpu_quarantine)->offline = false;
3488c2ecf20Sopenharmony_ci	return 0;
3498c2ecf20Sopenharmony_ci}
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_cistatic int kasan_cpu_offline(unsigned int cpu)
3528c2ecf20Sopenharmony_ci{
3538c2ecf20Sopenharmony_ci	struct qlist_head *q;
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci	q = this_cpu_ptr(&cpu_quarantine);
3568c2ecf20Sopenharmony_ci	/* Ensure the ordering between the writing to q->offline and
3578c2ecf20Sopenharmony_ci	 * qlist_free_all. Otherwise, cpu_quarantine may be corrupted
3588c2ecf20Sopenharmony_ci	 * by interrupt.
3598c2ecf20Sopenharmony_ci	 */
3608c2ecf20Sopenharmony_ci	WRITE_ONCE(q->offline, true);
3618c2ecf20Sopenharmony_ci	barrier();
3628c2ecf20Sopenharmony_ci	qlist_free_all(q, NULL);
3638c2ecf20Sopenharmony_ci	return 0;
3648c2ecf20Sopenharmony_ci}
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_cistatic int __init kasan_cpu_quarantine_init(void)
3678c2ecf20Sopenharmony_ci{
3688c2ecf20Sopenharmony_ci	int ret = 0;
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci	ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "mm/kasan:online",
3718c2ecf20Sopenharmony_ci				kasan_cpu_online, kasan_cpu_offline);
3728c2ecf20Sopenharmony_ci	if (ret < 0)
3738c2ecf20Sopenharmony_ci		pr_err("kasan cpu quarantine register failed [%d]\n", ret);
3748c2ecf20Sopenharmony_ci	return ret;
3758c2ecf20Sopenharmony_ci}
3768c2ecf20Sopenharmony_cilate_initcall(kasan_cpu_quarantine_init);
377