cregit-Linux how code gets into the kernel

Release 4.11 mm/kasan/quarantine.c

Directory: mm/kasan
/*
 * KASAN quarantine.
 *
 * Author: Alexander Potapenko <glider@google.com>
 * Copyright (C) 2016 Google, Inc.
 *
 * Based on code by Dmitry Chernenkov.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 */

#include <linux/gfp.h>
#include <linux/hash.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/percpu.h>
#include <linux/printk.h>
#include <linux/shrinker.h>
#include <linux/slab.h>
#include <linux/srcu.h>
#include <linux/string.h>
#include <linux/types.h>

#include "../slab.h"
#include "kasan.h"

/* Data structure and operations for quarantine queues. */

/*
 * Each queue is a signle-linked list, which also stores the total size of
 * objects inside of it.
 */

struct qlist_head {
	
struct qlist_node *head;
	
struct qlist_node *tail;
	
size_t bytes;
};


#define QLIST_INIT { NULL, NULL, 0 }


static bool qlist_empty(struct qlist_head *q) { return !q->head; }

Contributors

PersonTokensPropCommitsCommitProp
Alexander Potapenko17100.00%1100.00%
Total17100.00%1100.00%


static void qlist_init(struct qlist_head *q) { q->head = q->tail = NULL; q->bytes = 0; }

Contributors

PersonTokensPropCommitsCommitProp
Alexander Potapenko27100.00%1100.00%
Total27100.00%1100.00%


static void qlist_put(struct qlist_head *q, struct qlist_node *qlink, size_t size) { if (unlikely(qlist_empty(q))) q->head = qlink; else q->tail->next = qlink; q->tail = qlink; qlink->next = NULL; q->bytes += size; }

Contributors

PersonTokensPropCommitsCommitProp
Alexander Potapenko62100.00%1100.00%
Total62100.00%1100.00%


static void qlist_move_all(struct qlist_head *from, struct qlist_head *to) { if (unlikely(qlist_empty(from))) return; if (qlist_empty(to)) { *to = *from; qlist_init(from); return; } to->tail->next = from->head; to->tail = from->tail; to->bytes += from->bytes; qlist_init(from); }

Contributors

PersonTokensPropCommitsCommitProp
Alexander Potapenko79100.00%1100.00%
Total79100.00%1100.00%

#define QUARANTINE_PERCPU_SIZE (1 << 20) #define QUARANTINE_BATCHES \ (1024 > 4 * CONFIG_NR_CPUS ? 1024 : 4 * CONFIG_NR_CPUS) /* * The object quarantine consists of per-cpu queues and a global queue, * guarded by quarantine_lock. */ static DEFINE_PER_CPU(struct qlist_head, cpu_quarantine); /* Round-robin FIFO array of batches. */ static struct qlist_head global_quarantine[QUARANTINE_BATCHES]; static int quarantine_head; static int quarantine_tail; /* Total size of all objects in global_quarantine across all batches. */ static unsigned long quarantine_size; static DEFINE_SPINLOCK(quarantine_lock); DEFINE_STATIC_SRCU(remove_cache_srcu); /* Maximum size of the global queue. */ static unsigned long quarantine_max_size; /* * Target size of a batch in global_quarantine. * Usually equal to QUARANTINE_PERCPU_SIZE unless we have too much RAM. */ static unsigned long quarantine_batch_size; /* * The fraction of physical memory the quarantine is allowed to occupy. * Quarantine doesn't support memory shrinker with SLAB allocator, so we keep * the ratio low to avoid OOM. */ #define QUARANTINE_FRACTION 32
static struct kmem_cache *qlink_to_cache(struct qlist_node *qlink) { return virt_to_head_page(qlink)->slab_cache; }

Contributors

PersonTokensPropCommitsCommitProp
Alexander Potapenko21100.00%1100.00%
Total21100.00%1100.00%


static void *qlink_to_object(struct qlist_node *qlink, struct kmem_cache *cache) { struct kasan_free_meta *free_info = container_of(qlink, struct kasan_free_meta, quarantine_link); return ((void *)free_info) - cache->kasan_info.free_meta_offset; }

Contributors

PersonTokensPropCommitsCommitProp
Alexander Potapenko47100.00%1100.00%
Total47100.00%1100.00%


static void qlink_free(struct qlist_node *qlink, struct kmem_cache *cache) { void *object = qlink_to_object(qlink, cache); unsigned long flags; if (IS_ENABLED(CONFIG_SLAB)) local_irq_save(flags); ___cache_free(cache, object, _THIS_IP_); if (IS_ENABLED(CONFIG_SLAB)) local_irq_restore(flags); }

Contributors

PersonTokensPropCommitsCommitProp
Alexander Potapenko5078.12%150.00%
Andrey Ryabinin1421.88%150.00%
Total64100.00%2100.00%


static void qlist_free_all(struct qlist_head *q, struct kmem_cache *cache) { struct qlist_node *qlink; if (unlikely(qlist_empty(q))) return; qlink = q->head; while (qlink) { struct kmem_cache *obj_cache = cache ? cache : qlink_to_cache(qlink); struct qlist_node *next = qlink->next; qlink_free(qlink, obj_cache); qlink = next; } qlist_init(q); }

Contributors

PersonTokensPropCommitsCommitProp
Alexander Potapenko83100.00%1100.00%
Total83100.00%1100.00%


void quarantine_put(struct kasan_free_meta *info, struct kmem_cache *cache) { unsigned long flags; struct qlist_head *q; struct qlist_head temp = QLIST_INIT; /* * Note: irq must be disabled until after we move the batch to the * global quarantine. Otherwise quarantine_remove_cache() can miss * some objects belonging to the cache if they are in our local temp * list. quarantine_remove_cache() executes on_each_cpu() at the * beginning which ensures that it either sees the objects in per-cpu * lists or in the global quarantine. */ local_irq_save(flags); q = this_cpu_ptr(&cpu_quarantine); qlist_put(q, &info->quarantine_link, cache->size); if (unlikely(q->bytes > QUARANTINE_PERCPU_SIZE)) { qlist_move_all(q, &temp); spin_lock(&quarantine_lock); WRITE_ONCE(quarantine_size, quarantine_size + temp.bytes); qlist_move_all(&temp, &global_quarantine[quarantine_tail]); if (global_quarantine[quarantine_tail].bytes >= READ_ONCE(quarantine_batch_size)) { int new_tail; new_tail = quarantine_tail + 1; if (new_tail == QUARANTINE_BATCHES) new_tail = 0; if (new_tail != quarantine_head) quarantine_tail = new_tail; } spin_unlock(&quarantine_lock); } local_irq_restore(flags); }

Contributors

PersonTokensPropCommitsCommitProp
Alexander Potapenko9658.54%133.33%
Dmitriy Vyukov6841.46%266.67%
Total164100.00%3100.00%


void quarantine_reduce(void) { size_t total_size, new_quarantine_size, percpu_quarantines; unsigned long flags; int srcu_idx; struct qlist_head to_free = QLIST_INIT; if (likely(READ_ONCE(quarantine_size) <= READ_ONCE(quarantine_max_size))) return; /* * srcu critical section ensures that quarantine_remove_cache() * will not miss objects belonging to the cache while they are in our * local to_free list. srcu is chosen because (1) it gives us private * grace period domain that does not interfere with anything else, * and (2) it allows synchronize_srcu() to return without waiting * if there are no pending read critical sections (which is the * expected case). */ srcu_idx = srcu_read_lock(&remove_cache_srcu); spin_lock_irqsave(&quarantine_lock, flags); /* * Update quarantine size in case of hotplug. Allocate a fraction of * the installed memory to quarantine minus per-cpu queue limits. */ total_size = (READ_ONCE(totalram_pages) << PAGE_SHIFT) / QUARANTINE_FRACTION; percpu_quarantines = QUARANTINE_PERCPU_SIZE * num_online_cpus(); new_quarantine_size = (total_size < percpu_quarantines) ? 0 : total_size - percpu_quarantines; WRITE_ONCE(quarantine_max_size, new_quarantine_size); /* Aim at consuming at most 1/2 of slots in quarantine. */ WRITE_ONCE(quarantine_batch_size, max((size_t)QUARANTINE_PERCPU_SIZE, 2 * total_size / QUARANTINE_BATCHES)); if (likely(quarantine_size > quarantine_max_size)) { qlist_move_all(&global_quarantine[quarantine_head], &to_free); WRITE_ONCE(quarantine_size, quarantine_size - to_free.bytes); quarantine_head++; if (quarantine_head == QUARANTINE_BATCHES) quarantine_head = 0; } spin_unlock_irqrestore(&quarantine_lock, flags); qlist_free_all(&to_free, NULL); srcu_read_unlock(&remove_cache_srcu, srcu_idx); }

Contributors

PersonTokensPropCommitsCommitProp
Alexander Potapenko11459.07%360.00%
Dmitriy Vyukov7940.93%240.00%
Total193100.00%5100.00%


static void qlist_move_cache(struct qlist_head *from, struct qlist_head *to, struct kmem_cache *cache) { struct qlist_node *curr; if (unlikely(qlist_empty(from))) return; curr = from->head; qlist_init(from); while (curr) { struct qlist_node *next = curr->next; struct kmem_cache *obj_cache = qlink_to_cache(curr); if (obj_cache == cache) qlist_put(to, curr, obj_cache->size); else qlist_put(from, curr, obj_cache->size); curr = next; } }

Contributors

PersonTokensPropCommitsCommitProp
Alexander Potapenko8782.08%150.00%
JoonSoo Kim1917.92%150.00%
Total106100.00%2100.00%


static void per_cpu_remove_cache(void *arg) { struct kmem_cache *cache = arg; struct qlist_head to_free = QLIST_INIT; struct qlist_head *q; q = this_cpu_ptr(&cpu_quarantine); qlist_move_cache(q, &to_free, cache); qlist_free_all(&to_free, cache); }

Contributors

PersonTokensPropCommitsCommitProp
Alexander Potapenko54100.00%1100.00%
Total54100.00%1100.00%

/* Free all quarantined objects belonging to cache. */
void quarantine_remove_cache(struct kmem_cache *cache) { unsigned long flags, i; struct qlist_head to_free = QLIST_INIT; /* * Must be careful to not miss any objects that are being moved from * per-cpu list to the global quarantine in quarantine_put(), * nor objects being freed in quarantine_reduce(). on_each_cpu() * achieves the first goal, while synchronize_srcu() achieves the * second. */ on_each_cpu(per_cpu_remove_cache, cache, 1); spin_lock_irqsave(&quarantine_lock, flags); for (i = 0; i < QUARANTINE_BATCHES; i++) { if (qlist_empty(&global_quarantine[i])) continue; qlist_move_cache(&global_quarantine[i], &to_free, cache); /* Scanning whole quarantine can take a while. */ spin_unlock_irqrestore(&quarantine_lock, flags); cond_resched(); spin_lock_irqsave(&quarantine_lock, flags); } spin_unlock_irqrestore(&quarantine_lock, flags); qlist_free_all(&to_free, cache); synchronize_srcu(&remove_cache_srcu); }

Contributors

PersonTokensPropCommitsCommitProp
Alexander Potapenko6452.03%125.00%
Dmitriy Vyukov5947.97%375.00%
Total123100.00%4100.00%


Overall Contributors

PersonTokensPropCommitsCommitProp
Alexander Potapenko89376.00%333.33%
Dmitriy Vyukov24821.11%333.33%
JoonSoo Kim191.62%111.11%
Andrey Ryabinin141.19%111.11%
Greg Thelen10.09%111.11%
Total1175100.00%9100.00%
Directory: mm/kasan
Information contained on this website is for historical information purposes only and does not indicate or represent copyright ownership.
Created with cregit.