1 // SPDX-License-Identifier: GPL-2.0
5 * Author: Alexander Potapenko <glider@google.com>
6 * Copyright (C) 2016 Google, Inc.
8 * Based on code by Dmitry Chernenkov.
11 #include <linux/gfp.h>
12 #include <linux/hash.h>
13 #include <linux/kernel.h>
15 #include <linux/percpu.h>
16 #include <linux/printk.h>
17 #include <linux/shrinker.h>
18 #include <linux/slab.h>
19 #include <linux/srcu.h>
20 #include <linux/string.h>
21 #include <linux/types.h>
22 #include <linux/cpuhotplug.h>
27 /* Data structure and operations for quarantine queues. */
30 * Each queue is a signle-linked list, which also stores the total size of
31 * objects inside of it.
34 struct qlist_node
*head
;
35 struct qlist_node
*tail
;
40 #define QLIST_INIT { NULL, NULL, 0 }
42 static bool qlist_empty(struct qlist_head
*q
)
47 static void qlist_init(struct qlist_head
*q
)
49 q
->head
= q
->tail
= NULL
;
53 static void qlist_put(struct qlist_head
*q
, struct qlist_node
*qlink
,
56 if (unlikely(qlist_empty(q
)))
59 q
->tail
->next
= qlink
;
65 static void qlist_move_all(struct qlist_head
*from
, struct qlist_head
*to
)
67 if (unlikely(qlist_empty(from
)))
70 if (qlist_empty(to
)) {
76 to
->tail
->next
= from
->head
;
77 to
->tail
= from
->tail
;
78 to
->bytes
+= from
->bytes
;
83 #define QUARANTINE_PERCPU_SIZE (1 << 20)
84 #define QUARANTINE_BATCHES \
85 (1024 > 4 * CONFIG_NR_CPUS ? 1024 : 4 * CONFIG_NR_CPUS)
88 * The object quarantine consists of per-cpu queues and a global queue,
89 * guarded by quarantine_lock.
91 static DEFINE_PER_CPU(struct qlist_head
, cpu_quarantine
);
93 /* Round-robin FIFO array of batches. */
94 static struct qlist_head global_quarantine
[QUARANTINE_BATCHES
];
95 static int quarantine_head
;
96 static int quarantine_tail
;
97 /* Total size of all objects in global_quarantine across all batches. */
98 static unsigned long quarantine_size
;
99 static DEFINE_RAW_SPINLOCK(quarantine_lock
);
100 DEFINE_STATIC_SRCU(remove_cache_srcu
);
102 /* Maximum size of the global queue. */
103 static unsigned long quarantine_max_size
;
106 * Target size of a batch in global_quarantine.
107 * Usually equal to QUARANTINE_PERCPU_SIZE unless we have too much RAM.
109 static unsigned long quarantine_batch_size
;
112 * The fraction of physical memory the quarantine is allowed to occupy.
113 * Quarantine doesn't support memory shrinker with SLAB allocator, so we keep
114 * the ratio low to avoid OOM.
116 #define QUARANTINE_FRACTION 32
118 static struct kmem_cache
*qlink_to_cache(struct qlist_node
*qlink
)
120 return virt_to_head_page(qlink
)->slab_cache
;
123 static void *qlink_to_object(struct qlist_node
*qlink
, struct kmem_cache
*cache
)
125 struct kasan_free_meta
*free_info
=
126 container_of(qlink
, struct kasan_free_meta
,
129 return ((void *)free_info
) - cache
->kasan_info
.free_meta_offset
;
132 static void qlink_free(struct qlist_node
*qlink
, struct kmem_cache
*cache
)
134 void *object
= qlink_to_object(qlink
, cache
);
137 if (IS_ENABLED(CONFIG_SLAB
))
138 local_irq_save(flags
);
141 * As the object now gets freed from the quaratine, assume that its
142 * free track is no longer valid.
144 *(u8
*)kasan_mem_to_shadow(object
) = KASAN_KMALLOC_FREE
;
146 ___cache_free(cache
, object
, _THIS_IP_
);
148 if (IS_ENABLED(CONFIG_SLAB
))
149 local_irq_restore(flags
);
152 static void qlist_free_all(struct qlist_head
*q
, struct kmem_cache
*cache
)
154 struct qlist_node
*qlink
;
156 if (unlikely(qlist_empty(q
)))
161 struct kmem_cache
*obj_cache
=
162 cache
? cache
: qlink_to_cache(qlink
);
163 struct qlist_node
*next
= qlink
->next
;
165 qlink_free(qlink
, obj_cache
);
171 bool quarantine_put(struct kmem_cache
*cache
, void *object
)
174 struct qlist_head
*q
;
175 struct qlist_head temp
= QLIST_INIT
;
176 struct kasan_free_meta
*meta
= kasan_get_free_meta(cache
, object
);
179 * If there's no metadata for this object, don't put it into
186 * Note: irq must be disabled until after we move the batch to the
187 * global quarantine. Otherwise quarantine_remove_cache() can miss
188 * some objects belonging to the cache if they are in our local temp
189 * list. quarantine_remove_cache() executes on_each_cpu() at the
190 * beginning which ensures that it either sees the objects in per-cpu
191 * lists or in the global quarantine.
193 local_irq_save(flags
);
195 q
= this_cpu_ptr(&cpu_quarantine
);
197 local_irq_restore(flags
);
200 qlist_put(q
, &meta
->quarantine_link
, cache
->size
);
201 if (unlikely(q
->bytes
> QUARANTINE_PERCPU_SIZE
)) {
202 qlist_move_all(q
, &temp
);
204 raw_spin_lock(&quarantine_lock
);
205 WRITE_ONCE(quarantine_size
, quarantine_size
+ temp
.bytes
);
206 qlist_move_all(&temp
, &global_quarantine
[quarantine_tail
]);
207 if (global_quarantine
[quarantine_tail
].bytes
>=
208 READ_ONCE(quarantine_batch_size
)) {
211 new_tail
= quarantine_tail
+ 1;
212 if (new_tail
== QUARANTINE_BATCHES
)
214 if (new_tail
!= quarantine_head
)
215 quarantine_tail
= new_tail
;
217 raw_spin_unlock(&quarantine_lock
);
220 local_irq_restore(flags
);
225 void quarantine_reduce(void)
227 size_t total_size
, new_quarantine_size
, percpu_quarantines
;
230 struct qlist_head to_free
= QLIST_INIT
;
232 if (likely(READ_ONCE(quarantine_size
) <=
233 READ_ONCE(quarantine_max_size
)))
237 * srcu critical section ensures that quarantine_remove_cache()
238 * will not miss objects belonging to the cache while they are in our
239 * local to_free list. srcu is chosen because (1) it gives us private
240 * grace period domain that does not interfere with anything else,
241 * and (2) it allows synchronize_srcu() to return without waiting
242 * if there are no pending read critical sections (which is the
245 srcu_idx
= srcu_read_lock(&remove_cache_srcu
);
246 raw_spin_lock_irqsave(&quarantine_lock
, flags
);
249 * Update quarantine size in case of hotplug. Allocate a fraction of
250 * the installed memory to quarantine minus per-cpu queue limits.
252 total_size
= (totalram_pages() << PAGE_SHIFT
) /
254 percpu_quarantines
= QUARANTINE_PERCPU_SIZE
* num_online_cpus();
255 new_quarantine_size
= (total_size
< percpu_quarantines
) ?
256 0 : total_size
- percpu_quarantines
;
257 WRITE_ONCE(quarantine_max_size
, new_quarantine_size
);
258 /* Aim at consuming at most 1/2 of slots in quarantine. */
259 WRITE_ONCE(quarantine_batch_size
, max((size_t)QUARANTINE_PERCPU_SIZE
,
260 2 * total_size
/ QUARANTINE_BATCHES
));
262 if (likely(quarantine_size
> quarantine_max_size
)) {
263 qlist_move_all(&global_quarantine
[quarantine_head
], &to_free
);
264 WRITE_ONCE(quarantine_size
, quarantine_size
- to_free
.bytes
);
266 if (quarantine_head
== QUARANTINE_BATCHES
)
270 raw_spin_unlock_irqrestore(&quarantine_lock
, flags
);
272 qlist_free_all(&to_free
, NULL
);
273 srcu_read_unlock(&remove_cache_srcu
, srcu_idx
);
276 static void qlist_move_cache(struct qlist_head
*from
,
277 struct qlist_head
*to
,
278 struct kmem_cache
*cache
)
280 struct qlist_node
*curr
;
282 if (unlikely(qlist_empty(from
)))
288 struct qlist_node
*next
= curr
->next
;
289 struct kmem_cache
*obj_cache
= qlink_to_cache(curr
);
291 if (obj_cache
== cache
)
292 qlist_put(to
, curr
, obj_cache
->size
);
294 qlist_put(from
, curr
, obj_cache
->size
);
300 static void per_cpu_remove_cache(void *arg
)
302 struct kmem_cache
*cache
= arg
;
303 struct qlist_head to_free
= QLIST_INIT
;
304 struct qlist_head
*q
;
306 q
= this_cpu_ptr(&cpu_quarantine
);
307 qlist_move_cache(q
, &to_free
, cache
);
308 qlist_free_all(&to_free
, cache
);
311 /* Free all quarantined objects belonging to cache. */
312 void quarantine_remove_cache(struct kmem_cache
*cache
)
314 unsigned long flags
, i
;
315 struct qlist_head to_free
= QLIST_INIT
;
318 * Must be careful to not miss any objects that are being moved from
319 * per-cpu list to the global quarantine in quarantine_put(),
320 * nor objects being freed in quarantine_reduce(). on_each_cpu()
321 * achieves the first goal, while synchronize_srcu() achieves the
324 on_each_cpu(per_cpu_remove_cache
, cache
, 1);
326 raw_spin_lock_irqsave(&quarantine_lock
, flags
);
327 for (i
= 0; i
< QUARANTINE_BATCHES
; i
++) {
328 if (qlist_empty(&global_quarantine
[i
]))
330 qlist_move_cache(&global_quarantine
[i
], &to_free
, cache
);
331 /* Scanning whole quarantine can take a while. */
332 raw_spin_unlock_irqrestore(&quarantine_lock
, flags
);
334 raw_spin_lock_irqsave(&quarantine_lock
, flags
);
336 raw_spin_unlock_irqrestore(&quarantine_lock
, flags
);
338 qlist_free_all(&to_free
, cache
);
340 synchronize_srcu(&remove_cache_srcu
);
343 static int kasan_cpu_online(unsigned int cpu
)
345 this_cpu_ptr(&cpu_quarantine
)->offline
= false;
349 static int kasan_cpu_offline(unsigned int cpu
)
351 struct qlist_head
*q
;
353 q
= this_cpu_ptr(&cpu_quarantine
);
354 /* Ensure the ordering between the writing to q->offline and
355 * qlist_free_all. Otherwise, cpu_quarantine may be corrupted
358 WRITE_ONCE(q
->offline
, true);
360 qlist_free_all(q
, NULL
);
364 static int __init
kasan_cpu_quarantine_init(void)
368 ret
= cpuhp_setup_state(CPUHP_AP_ONLINE_DYN
, "mm/kasan:online",
369 kasan_cpu_online
, kasan_cpu_offline
);
371 pr_err("kasan cpu quarantine register failed [%d]\n", ret
);
374 late_initcall(kasan_cpu_quarantine_init
);