kasan: simplify quarantine_put call site
[platform/kernel/linux-rpi.git] / mm / kasan / quarantine.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * KASAN quarantine.
4  *
5  * Author: Alexander Potapenko <glider@google.com>
6  * Copyright (C) 2016 Google, Inc.
7  *
8  * Based on code by Dmitry Chernenkov.
9  */
10
11 #include <linux/gfp.h>
12 #include <linux/hash.h>
13 #include <linux/kernel.h>
14 #include <linux/mm.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>
23
24 #include "../slab.h"
25 #include "kasan.h"
26
27 /* Data structure and operations for quarantine queues. */
28
29 /*
30  * Each queue is a signle-linked list, which also stores the total size of
31  * objects inside of it.
32  */
33 struct qlist_head {
34         struct qlist_node *head;
35         struct qlist_node *tail;
36         size_t bytes;
37         bool offline;
38 };
39
40 #define QLIST_INIT { NULL, NULL, 0 }
41
42 static bool qlist_empty(struct qlist_head *q)
43 {
44         return !q->head;
45 }
46
47 static void qlist_init(struct qlist_head *q)
48 {
49         q->head = q->tail = NULL;
50         q->bytes = 0;
51 }
52
53 static void qlist_put(struct qlist_head *q, struct qlist_node *qlink,
54                 size_t size)
55 {
56         if (unlikely(qlist_empty(q)))
57                 q->head = qlink;
58         else
59                 q->tail->next = qlink;
60         q->tail = qlink;
61         qlink->next = NULL;
62         q->bytes += size;
63 }
64
65 static void qlist_move_all(struct qlist_head *from, struct qlist_head *to)
66 {
67         if (unlikely(qlist_empty(from)))
68                 return;
69
70         if (qlist_empty(to)) {
71                 *to = *from;
72                 qlist_init(from);
73                 return;
74         }
75
76         to->tail->next = from->head;
77         to->tail = from->tail;
78         to->bytes += from->bytes;
79
80         qlist_init(from);
81 }
82
83 #define QUARANTINE_PERCPU_SIZE (1 << 20)
84 #define QUARANTINE_BATCHES \
85         (1024 > 4 * CONFIG_NR_CPUS ? 1024 : 4 * CONFIG_NR_CPUS)
86
87 /*
88  * The object quarantine consists of per-cpu queues and a global queue,
89  * guarded by quarantine_lock.
90  */
91 static DEFINE_PER_CPU(struct qlist_head, cpu_quarantine);
92
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);
101
102 /* Maximum size of the global queue. */
103 static unsigned long quarantine_max_size;
104
105 /*
106  * Target size of a batch in global_quarantine.
107  * Usually equal to QUARANTINE_PERCPU_SIZE unless we have too much RAM.
108  */
109 static unsigned long quarantine_batch_size;
110
111 /*
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.
115  */
116 #define QUARANTINE_FRACTION 32
117
118 static struct kmem_cache *qlink_to_cache(struct qlist_node *qlink)
119 {
120         return virt_to_head_page(qlink)->slab_cache;
121 }
122
123 static void *qlink_to_object(struct qlist_node *qlink, struct kmem_cache *cache)
124 {
125         struct kasan_free_meta *free_info =
126                 container_of(qlink, struct kasan_free_meta,
127                              quarantine_link);
128
129         return ((void *)free_info) - cache->kasan_info.free_meta_offset;
130 }
131
132 static void qlink_free(struct qlist_node *qlink, struct kmem_cache *cache)
133 {
134         void *object = qlink_to_object(qlink, cache);
135         unsigned long flags;
136
137         if (IS_ENABLED(CONFIG_SLAB))
138                 local_irq_save(flags);
139
140         *(u8 *)kasan_mem_to_shadow(object) = KASAN_KMALLOC_FREE;
141         ___cache_free(cache, object, _THIS_IP_);
142
143         if (IS_ENABLED(CONFIG_SLAB))
144                 local_irq_restore(flags);
145 }
146
147 static void qlist_free_all(struct qlist_head *q, struct kmem_cache *cache)
148 {
149         struct qlist_node *qlink;
150
151         if (unlikely(qlist_empty(q)))
152                 return;
153
154         qlink = q->head;
155         while (qlink) {
156                 struct kmem_cache *obj_cache =
157                         cache ? cache : qlink_to_cache(qlink);
158                 struct qlist_node *next = qlink->next;
159
160                 qlink_free(qlink, obj_cache);
161                 qlink = next;
162         }
163         qlist_init(q);
164 }
165
166 void quarantine_put(struct kmem_cache *cache, void *object)
167 {
168         unsigned long flags;
169         struct qlist_head *q;
170         struct qlist_head temp = QLIST_INIT;
171         struct kasan_free_meta *info = get_free_info(cache, object);
172
173         /*
174          * Note: irq must be disabled until after we move the batch to the
175          * global quarantine. Otherwise quarantine_remove_cache() can miss
176          * some objects belonging to the cache if they are in our local temp
177          * list. quarantine_remove_cache() executes on_each_cpu() at the
178          * beginning which ensures that it either sees the objects in per-cpu
179          * lists or in the global quarantine.
180          */
181         local_irq_save(flags);
182
183         q = this_cpu_ptr(&cpu_quarantine);
184         if (q->offline) {
185                 local_irq_restore(flags);
186                 return;
187         }
188         qlist_put(q, &info->quarantine_link, cache->size);
189         if (unlikely(q->bytes > QUARANTINE_PERCPU_SIZE)) {
190                 qlist_move_all(q, &temp);
191
192                 raw_spin_lock(&quarantine_lock);
193                 WRITE_ONCE(quarantine_size, quarantine_size + temp.bytes);
194                 qlist_move_all(&temp, &global_quarantine[quarantine_tail]);
195                 if (global_quarantine[quarantine_tail].bytes >=
196                                 READ_ONCE(quarantine_batch_size)) {
197                         int new_tail;
198
199                         new_tail = quarantine_tail + 1;
200                         if (new_tail == QUARANTINE_BATCHES)
201                                 new_tail = 0;
202                         if (new_tail != quarantine_head)
203                                 quarantine_tail = new_tail;
204                 }
205                 raw_spin_unlock(&quarantine_lock);
206         }
207
208         local_irq_restore(flags);
209 }
210
211 void quarantine_reduce(void)
212 {
213         size_t total_size, new_quarantine_size, percpu_quarantines;
214         unsigned long flags;
215         int srcu_idx;
216         struct qlist_head to_free = QLIST_INIT;
217
218         if (likely(READ_ONCE(quarantine_size) <=
219                    READ_ONCE(quarantine_max_size)))
220                 return;
221
222         /*
223          * srcu critical section ensures that quarantine_remove_cache()
224          * will not miss objects belonging to the cache while they are in our
225          * local to_free list. srcu is chosen because (1) it gives us private
226          * grace period domain that does not interfere with anything else,
227          * and (2) it allows synchronize_srcu() to return without waiting
228          * if there are no pending read critical sections (which is the
229          * expected case).
230          */
231         srcu_idx = srcu_read_lock(&remove_cache_srcu);
232         raw_spin_lock_irqsave(&quarantine_lock, flags);
233
234         /*
235          * Update quarantine size in case of hotplug. Allocate a fraction of
236          * the installed memory to quarantine minus per-cpu queue limits.
237          */
238         total_size = (totalram_pages() << PAGE_SHIFT) /
239                 QUARANTINE_FRACTION;
240         percpu_quarantines = QUARANTINE_PERCPU_SIZE * num_online_cpus();
241         new_quarantine_size = (total_size < percpu_quarantines) ?
242                 0 : total_size - percpu_quarantines;
243         WRITE_ONCE(quarantine_max_size, new_quarantine_size);
244         /* Aim at consuming at most 1/2 of slots in quarantine. */
245         WRITE_ONCE(quarantine_batch_size, max((size_t)QUARANTINE_PERCPU_SIZE,
246                 2 * total_size / QUARANTINE_BATCHES));
247
248         if (likely(quarantine_size > quarantine_max_size)) {
249                 qlist_move_all(&global_quarantine[quarantine_head], &to_free);
250                 WRITE_ONCE(quarantine_size, quarantine_size - to_free.bytes);
251                 quarantine_head++;
252                 if (quarantine_head == QUARANTINE_BATCHES)
253                         quarantine_head = 0;
254         }
255
256         raw_spin_unlock_irqrestore(&quarantine_lock, flags);
257
258         qlist_free_all(&to_free, NULL);
259         srcu_read_unlock(&remove_cache_srcu, srcu_idx);
260 }
261
262 static void qlist_move_cache(struct qlist_head *from,
263                                    struct qlist_head *to,
264                                    struct kmem_cache *cache)
265 {
266         struct qlist_node *curr;
267
268         if (unlikely(qlist_empty(from)))
269                 return;
270
271         curr = from->head;
272         qlist_init(from);
273         while (curr) {
274                 struct qlist_node *next = curr->next;
275                 struct kmem_cache *obj_cache = qlink_to_cache(curr);
276
277                 if (obj_cache == cache)
278                         qlist_put(to, curr, obj_cache->size);
279                 else
280                         qlist_put(from, curr, obj_cache->size);
281
282                 curr = next;
283         }
284 }
285
286 static void per_cpu_remove_cache(void *arg)
287 {
288         struct kmem_cache *cache = arg;
289         struct qlist_head to_free = QLIST_INIT;
290         struct qlist_head *q;
291
292         q = this_cpu_ptr(&cpu_quarantine);
293         qlist_move_cache(q, &to_free, cache);
294         qlist_free_all(&to_free, cache);
295 }
296
297 /* Free all quarantined objects belonging to cache. */
298 void quarantine_remove_cache(struct kmem_cache *cache)
299 {
300         unsigned long flags, i;
301         struct qlist_head to_free = QLIST_INIT;
302
303         /*
304          * Must be careful to not miss any objects that are being moved from
305          * per-cpu list to the global quarantine in quarantine_put(),
306          * nor objects being freed in quarantine_reduce(). on_each_cpu()
307          * achieves the first goal, while synchronize_srcu() achieves the
308          * second.
309          */
310         on_each_cpu(per_cpu_remove_cache, cache, 1);
311
312         raw_spin_lock_irqsave(&quarantine_lock, flags);
313         for (i = 0; i < QUARANTINE_BATCHES; i++) {
314                 if (qlist_empty(&global_quarantine[i]))
315                         continue;
316                 qlist_move_cache(&global_quarantine[i], &to_free, cache);
317                 /* Scanning whole quarantine can take a while. */
318                 raw_spin_unlock_irqrestore(&quarantine_lock, flags);
319                 cond_resched();
320                 raw_spin_lock_irqsave(&quarantine_lock, flags);
321         }
322         raw_spin_unlock_irqrestore(&quarantine_lock, flags);
323
324         qlist_free_all(&to_free, cache);
325
326         synchronize_srcu(&remove_cache_srcu);
327 }
328
329 static int kasan_cpu_online(unsigned int cpu)
330 {
331         this_cpu_ptr(&cpu_quarantine)->offline = false;
332         return 0;
333 }
334
335 static int kasan_cpu_offline(unsigned int cpu)
336 {
337         struct qlist_head *q;
338
339         q = this_cpu_ptr(&cpu_quarantine);
340         /* Ensure the ordering between the writing to q->offline and
341          * qlist_free_all. Otherwise, cpu_quarantine may be corrupted
342          * by interrupt.
343          */
344         WRITE_ONCE(q->offline, true);
345         barrier();
346         qlist_free_all(q, NULL);
347         return 0;
348 }
349
350 static int __init kasan_cpu_quarantine_init(void)
351 {
352         int ret = 0;
353
354         ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "mm/kasan:online",
355                                 kasan_cpu_online, kasan_cpu_offline);
356         if (ret < 0)
357                 pr_err("kasan cpu quarantine register failed [%d]\n", ret);
358         return ret;
359 }
360 late_initcall(kasan_cpu_quarantine_init);