4 * Author: Alexander Potapenko <glider@google.com>
5 * Copyright (C) 2016 Google, Inc.
7 * Based on code by Dmitry Chernenkov.
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * version 2 as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
20 #include <linux/gfp.h>
21 #include <linux/hash.h>
22 #include <linux/kernel.h>
24 #include <linux/percpu.h>
25 #include <linux/printk.h>
26 #include <linux/shrinker.h>
27 #include <linux/slab.h>
28 #include <linux/string.h>
29 #include <linux/types.h>
34 /* Data structure and operations for quarantine queues. */
37 * Each queue is a signle-linked list, which also stores the total size of
38 * objects inside of it.
41 struct qlist_node
*head
;
42 struct qlist_node
*tail
;
46 #define QLIST_INIT { NULL, NULL, 0 }
48 static bool qlist_empty(struct qlist_head
*q
)
53 static void qlist_init(struct qlist_head
*q
)
55 q
->head
= q
->tail
= NULL
;
59 static void qlist_put(struct qlist_head
*q
, struct qlist_node
*qlink
,
62 if (unlikely(qlist_empty(q
)))
65 q
->tail
->next
= qlink
;
71 static void qlist_move_all(struct qlist_head
*from
, struct qlist_head
*to
)
73 if (unlikely(qlist_empty(from
)))
76 if (qlist_empty(to
)) {
82 to
->tail
->next
= from
->head
;
83 to
->tail
= from
->tail
;
84 to
->bytes
+= from
->bytes
;
89 static void qlist_move(struct qlist_head
*from
, struct qlist_node
*last
,
90 struct qlist_head
*to
, size_t size
)
92 if (unlikely(last
== from
->tail
)) {
93 qlist_move_all(from
, to
);
97 to
->head
= from
->head
;
99 to
->tail
->next
= from
->head
;
101 from
->head
= last
->next
;
109 * The object quarantine consists of per-cpu queues and a global queue,
110 * guarded by quarantine_lock.
112 static DEFINE_PER_CPU(struct qlist_head
, cpu_quarantine
);
114 static struct qlist_head global_quarantine
;
115 static DEFINE_SPINLOCK(quarantine_lock
);
117 /* Maximum size of the global queue. */
118 static unsigned long quarantine_size
;
121 * The fraction of physical memory the quarantine is allowed to occupy.
122 * Quarantine doesn't support memory shrinker with SLAB allocator, so we keep
123 * the ratio low to avoid OOM.
125 #define QUARANTINE_FRACTION 32
127 #define QUARANTINE_LOW_SIZE (READ_ONCE(quarantine_size) * 3 / 4)
128 #define QUARANTINE_PERCPU_SIZE (1 << 20)
130 static struct kmem_cache
*qlink_to_cache(struct qlist_node
*qlink
)
132 return virt_to_head_page(qlink
)->slab_cache
;
135 static void *qlink_to_object(struct qlist_node
*qlink
, struct kmem_cache
*cache
)
137 struct kasan_free_meta
*free_info
=
138 container_of(qlink
, struct kasan_free_meta
,
141 return ((void *)free_info
) - cache
->kasan_info
.free_meta_offset
;
144 static void qlink_free(struct qlist_node
*qlink
, struct kmem_cache
*cache
)
146 void *object
= qlink_to_object(qlink
, cache
);
147 struct kasan_alloc_meta
*alloc_info
= get_alloc_info(cache
, object
);
150 local_irq_save(flags
);
151 alloc_info
->state
= KASAN_STATE_FREE
;
152 ___cache_free(cache
, object
, _THIS_IP_
);
153 local_irq_restore(flags
);
156 static void qlist_free_all(struct qlist_head
*q
, struct kmem_cache
*cache
)
158 struct qlist_node
*qlink
;
160 if (unlikely(qlist_empty(q
)))
165 struct kmem_cache
*obj_cache
=
166 cache
? cache
: qlink_to_cache(qlink
);
167 struct qlist_node
*next
= qlink
->next
;
169 qlink_free(qlink
, obj_cache
);
175 void quarantine_put(struct kasan_free_meta
*info
, struct kmem_cache
*cache
)
178 struct qlist_head
*q
;
179 struct qlist_head temp
= QLIST_INIT
;
181 local_irq_save(flags
);
183 q
= this_cpu_ptr(&cpu_quarantine
);
184 qlist_put(q
, &info
->quarantine_link
, cache
->size
);
185 if (unlikely(q
->bytes
> QUARANTINE_PERCPU_SIZE
))
186 qlist_move_all(q
, &temp
);
188 local_irq_restore(flags
);
190 if (unlikely(!qlist_empty(&temp
))) {
191 spin_lock_irqsave(&quarantine_lock
, flags
);
192 qlist_move_all(&temp
, &global_quarantine
);
193 spin_unlock_irqrestore(&quarantine_lock
, flags
);
197 void quarantine_reduce(void)
199 size_t new_quarantine_size
;
201 struct qlist_head to_free
= QLIST_INIT
;
202 size_t size_to_free
= 0;
203 struct qlist_node
*last
;
205 if (likely(READ_ONCE(global_quarantine
.bytes
) <=
206 READ_ONCE(quarantine_size
)))
209 spin_lock_irqsave(&quarantine_lock
, flags
);
212 * Update quarantine size in case of hotplug. Allocate a fraction of
213 * the installed memory to quarantine minus per-cpu queue limits.
215 new_quarantine_size
= (READ_ONCE(totalram_pages
) << PAGE_SHIFT
) /
217 new_quarantine_size
-= QUARANTINE_PERCPU_SIZE
* num_online_cpus();
218 WRITE_ONCE(quarantine_size
, new_quarantine_size
);
220 last
= global_quarantine
.head
;
222 struct kmem_cache
*cache
= qlink_to_cache(last
);
224 size_to_free
+= cache
->size
;
225 if (!last
->next
|| size_to_free
>
226 global_quarantine
.bytes
- QUARANTINE_LOW_SIZE
)
230 qlist_move(&global_quarantine
, last
, &to_free
, size_to_free
);
232 spin_unlock_irqrestore(&quarantine_lock
, flags
);
234 qlist_free_all(&to_free
, NULL
);
237 static void qlist_move_cache(struct qlist_head
*from
,
238 struct qlist_head
*to
,
239 struct kmem_cache
*cache
)
241 struct qlist_node
*curr
;
243 if (unlikely(qlist_empty(from
)))
249 struct qlist_node
*next
= curr
->next
;
250 struct kmem_cache
*obj_cache
= qlink_to_cache(curr
);
252 if (obj_cache
== cache
)
253 qlist_put(to
, curr
, obj_cache
->size
);
255 qlist_put(from
, curr
, obj_cache
->size
);
261 static void per_cpu_remove_cache(void *arg
)
263 struct kmem_cache
*cache
= arg
;
264 struct qlist_head to_free
= QLIST_INIT
;
265 struct qlist_head
*q
;
267 q
= this_cpu_ptr(&cpu_quarantine
);
268 qlist_move_cache(q
, &to_free
, cache
);
269 qlist_free_all(&to_free
, cache
);
272 void quarantine_remove_cache(struct kmem_cache
*cache
)
275 struct qlist_head to_free
= QLIST_INIT
;
277 on_each_cpu(per_cpu_remove_cache
, cache
, 1);
279 spin_lock_irqsave(&quarantine_lock
, flags
);
280 qlist_move_cache(&global_quarantine
, &to_free
, cache
);
281 spin_unlock_irqrestore(&quarantine_lock
, flags
);
283 qlist_free_all(&to_free
, cache
);