summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKent Overstreet <kmo@daterainc.com>2013-07-10 17:34:38 -0700
committerKent Overstreet <kmo@daterainc.com>2015-01-26 14:40:33 -0800
commitdd9b387f19a7c7e7cbbccf5e3d1ce3b4d6fde65a (patch)
tree7bdbd5a94cbfbb3d6f50cf3205ad67cc903d916c
parent455c6fdbd219161bd09b1165f11699d6d73de11c (diff)
Dynamic fault injection
Signed-off-by: Kent Overstreet <koverstreet@google.com>
-rw-r--r--include/asm-generic/vmlinux.lds.h4
-rw-r--r--include/linux/dynamic_fault.h101
-rw-r--r--lib/Kconfig.debug5
-rw-r--r--lib/Makefile2
-rw-r--r--lib/dynamic_fault.c705
5 files changed, 817 insertions, 0 deletions
diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h
index bc2121fa9132..08037a36a4c7 100644
--- a/include/asm-generic/vmlinux.lds.h
+++ b/include/asm-generic/vmlinux.lds.h
@@ -192,6 +192,10 @@
VMLINUX_SYMBOL(__start___verbose) = .; \
*(__verbose) \
VMLINUX_SYMBOL(__stop___verbose) = .; \
+ . = ALIGN(8); \
+ VMLINUX_SYMBOL(__start___faults) = .; \
+ *(__faults) \
+ VMLINUX_SYMBOL(__stop___faults) = .; \
LIKELY_PROFILE() \
BRANCH_PROFILE() \
TRACE_PRINTKS() \
diff --git a/include/linux/dynamic_fault.h b/include/linux/dynamic_fault.h
new file mode 100644
index 000000000000..c090547b39e5
--- /dev/null
+++ b/include/linux/dynamic_fault.h
@@ -0,0 +1,101 @@
+#ifndef _DYNAMIC_FAULT_H
+#define _DYNAMIC_FAULT_H
+
+#include <linux/bio.h>
+#include <linux/jump_label.h>
+#include <linux/slab.h>
+
+/*
+ * An instance of this structure is created in a special
+ * ELF section at every dynamic fault callsite. At runtime,
+ * the special section is treated as an array of these.
+ */
+struct _dfault {
+ const char *modname;
+ const char *function;
+ const char *filename;
+ unsigned int lineno:24;
+ /*
+ * The flags field controls the behaviour at the callsite.
+ * The bits here are changed dynamically when the user
+ * writes commands to <debugfs>/dynamic_debug/ddebug
+ */
+#define _DFAULT_ON (1<<0)
+#define _DFAULT_ONE_SHOT (1<<1)
+ unsigned int flags:8;
+ struct static_key enabled;
+} __attribute__((aligned(8)));
+
+
+#ifdef CONFIG_DYNAMIC_FAULT
+
+extern long long dynamic_fault_enabled;
+extern long long dynamic_fault_enabled2;
+
+int dfault_add_module(struct _dfault *tab, unsigned int n, const char *mod);
+int dfault_remove_module(char *mod_name);
+
+#define __dynamic_fault_enabled(df) \
+({ \
+ int __ret = 0; \
+ if (static_key_true(&(df).enabled)) { \
+ __ret = df.flags; \
+ df.flags &= ~_DFAULT_ONE_SHOT; \
+ } \
+ __ret; \
+})
+
+#define dynamic_fault() \
+({ \
+ static struct _dfault descriptor \
+ __used __attribute__((section("__faults"), aligned(8))) = { \
+ .modname = KBUILD_MODNAME, \
+ .function = __func__, \
+ .filename = __FILE__, \
+ .lineno = __LINE__, \
+ }; \
+ __dynamic_fault_enabled(descriptor); \
+})
+
+#define kmalloc(...) \
+ (dynamic_fault() ? NULL : kmalloc(__VA_ARGS__))
+#define kzalloc(...) \
+ (dynamic_fault() ? NULL : kzalloc(__VA_ARGS__))
+#define krealloc(...) \
+ (dynamic_fault() ? NULL : krealloc(__VA_ARGS__))
+
+#define __get_free_pages(...) \
+ (dynamic_fault() ? 0 : __get_free_pages(__VA_ARGS__))
+#define alloc_pages_node(...) \
+ (dynamic_fault() ? NULL : alloc_pages_node(__VA_ARGS__))
+#define alloc_pages_nodemask(...) \
+ (dynamic_fault() ? NULL : alloc_pages_nodemask(__VA_ARGS__))
+
+#define bio_alloc_bioset(gfp, ...) \
+ (!(gfp & __GFP_WAIT) && dynamic_fault() \
+ ? NULL : bio_alloc_bioset(gfp, __VA_ARGS__))
+
+#define bio_clone(bio, gfp) \
+ (!(gfp & __GFP_WAIT) && dynamic_fault() \
+ ? NULL : bio_clone(bio, gfp))
+
+#define bio_clone_bioset(bio, gfp, bs) \
+ (!(gfp & __GFP_WAIT) && dynamic_fault() \
+ ? NULL : bio_clone_bioset(bio, gfp, bs))
+
+#define bio_kmalloc(...) \
+ (dynamic_fault() ? NULL : bio_kmalloc(__VA_ARGS__))
+#define bio_clone_kmalloc(...) \
+ (dynamic_fault() ? NULL : bio_clone_kmalloc(__VA_ARGS__))
+#define bio_alloc_pages(...) \
+ (dynamic_fault() ? -ENOMEM : bio_alloc_pages(__VA_ARGS__))
+
+#else /* CONFIG_DYNAMIC_FAULT */
+
+#define dfault_add_module(tab, n, modname) 0
+#define dfault_remove_module(mod) 0
+#define dynamic_fault() 0
+
+#endif /* CONFIG_DYNAMIC_FAULT */
+
+#endif
diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
index a48abeac753f..80162b7d0d3c 100644
--- a/lib/Kconfig.debug
+++ b/lib/Kconfig.debug
@@ -1565,6 +1565,11 @@ config BUILD_DOCSRC
Say N if you are unsure.
+config DYNAMIC_FAULT
+ bool "Enable dynamic fault support"
+ default n
+ depends on DEBUG_FS
+
config DMA_API_DEBUG
bool "Enable debugging of DMA-API usage"
depends on HAVE_DMA_API_DEBUG
diff --git a/lib/Makefile b/lib/Makefile
index 48140e3ba73f..e7bb2306861b 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -113,6 +113,8 @@ obj-$(CONFIG_HAVE_ARCH_TRACEHOOK) += syscall.o
obj-$(CONFIG_DYNAMIC_DEBUG) += dynamic_debug.o
+obj-$(CONFIG_DYNAMIC_FAULT) += dynamic_fault.o
+
obj-$(CONFIG_NLATTR) += nlattr.o
obj-$(CONFIG_LRU_CACHE) += lru_cache.o
diff --git a/lib/dynamic_fault.c b/lib/dynamic_fault.c
new file mode 100644
index 000000000000..b8668a5d33d0
--- /dev/null
+++ b/lib/dynamic_fault.c
@@ -0,0 +1,705 @@
+/*
+ * lib/dynamic_fault.c
+ *
+ * make fault() calls runtime configurable based upon their
+ * source module.
+ *
+ * Copyright (C) 2011 Adam Berkan <aberkan@google.com>
+ * Based on dynamic_debug.c:
+ * Copyright (C) 2008 Jason Baron <jbaron@redhat.com>
+ * By Greg Banks <gnb@melbourne.sgi.com>
+ * Copyright (c) 2008 Silicon Graphics Inc. All Rights Reserved.
+ *
+ */
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/moduleparam.h>
+#include <linux/kallsyms.h>
+#include <linux/version.h>
+#include <linux/types.h>
+#include <linux/mutex.h>
+#include <linux/proc_fs.h>
+#include <linux/seq_file.h>
+#include <linux/list.h>
+#include <linux/sysctl.h>
+#include <linux/ctype.h>
+#include <linux/string.h>
+#include <linux/uaccess.h>
+#include <linux/dynamic_fault.h>
+#include <linux/debugfs.h>
+#include <linux/slab.h>
+
+extern struct _dfault __start___faults[];
+extern struct _dfault __stop___faults[];
+
+/* dynamic_fault_enabled, and dynamic_fault_enabled2 are bitmasks in which
+ * bit n is set to 1 if any modname hashes into the bucket n, 0 otherwise. They
+ * use independent hash functions, to reduce the chance of false positives.
+ */
+long long dynamic_fault_enabled;
+EXPORT_SYMBOL_GPL(dynamic_fault_enabled);
+long long dynamic_fault_enabled2;
+EXPORT_SYMBOL_GPL(dynamic_fault_enabled2);
+
+struct dfault_table {
+ struct list_head link;
+ char *mod_name;
+ unsigned int num_dfaults;
+ unsigned int num_enabled;
+ struct _dfault *dfaults;
+};
+
+struct dfault_query {
+ const char *filename;
+ const char *module;
+ const char *function;
+ unsigned int first_lineno, last_lineno;
+};
+
+struct dfault_iter {
+ struct dfault_table *table;
+ unsigned int idx;
+};
+
+static DEFINE_MUTEX(dfault_lock);
+static LIST_HEAD(dfault_tables);
+static int verbose;
+
+/* Return the last part of a pathname */
+static inline const char *basename(const char *path)
+{
+ const char *tail = strrchr(path, '/');
+ return tail ? tail+1 : path;
+}
+
+/* format a string into buf[] which describes the _dfault's flags */
+static char *dfault_describe_flags(struct _dfault *df, char *buf,
+ size_t maxlen)
+{
+ char *p = buf;
+
+ BUG_ON(maxlen < 4);
+ if (df->flags & _DFAULT_ON)
+ *p++ = 'f';
+ if (df->flags & _DFAULT_ONE_SHOT)
+ *p++ = 'o';
+ if (p == buf)
+ *p++ = '-';
+ *p = '\0';
+
+ return buf;
+}
+
+/*
+ * must be called with dfault_lock held
+ */
+
+/*
+ * Search the tables for _dfault's which match the given
+ * `query' and apply the `flags' and `mask' to them. Tells
+ * the user which dfault's were changed, or whether none
+ * were matched.
+ */
+static void dfault_change(const struct dfault_query *query,
+ unsigned int flags, unsigned int mask)
+{
+ int i;
+ struct dfault_table *dt;
+ unsigned int newflags;
+ unsigned int nfound = 0;
+ char flagbuf[8];
+
+ /* search for matching dfaults */
+ mutex_lock(&dfault_lock);
+ list_for_each_entry(dt, &dfault_tables, link) {
+
+ /* match against the module name */
+ if (query->module != NULL &&
+ strcmp(query->module, dt->mod_name))
+ continue;
+
+ for (i = 0 ; i < dt->num_dfaults ; i++) {
+ struct _dfault *df = &dt->dfaults[i];
+
+ /* match against the source filename */
+ if (query->filename != NULL &&
+ strcmp(query->filename, df->filename) &&
+ strcmp(query->filename, basename(df->filename)))
+ continue;
+
+ /* match against the function */
+ if (query->function != NULL &&
+ strcmp(query->function, df->function))
+ continue;
+
+ /* match against the line number range */
+ if (query->first_lineno &&
+ df->lineno < query->first_lineno)
+ continue;
+ if (query->last_lineno &&
+ df->lineno > query->last_lineno)
+ continue;
+
+ nfound++;
+
+ newflags = (df->flags & mask) | flags;
+ if (newflags == df->flags)
+ continue;
+
+ if (!newflags)
+ dt->num_enabled--;
+ else if (!df->flags)
+ dt->num_enabled++;
+
+ df->flags = newflags;
+ if (newflags)
+ static_key_slow_inc(&df->enabled);
+
+ if (verbose)
+ printk(KERN_INFO
+ "dfault: changed %s:%d [%s]%s %s\n",
+ df->filename, df->lineno,
+ dt->mod_name, df->function,
+ dfault_describe_flags(df, flagbuf,
+ sizeof(flagbuf)));
+ }
+ }
+ mutex_unlock(&dfault_lock);
+
+ if (!nfound && verbose)
+ printk(KERN_INFO "dfault: no matches for query\n");
+}
+
+/*
+ * Split the buffer `buf' into space-separated words.
+ * Handles simple " and ' quoting, i.e. without nested,
+ * embedded or escaped \". Return the number of words
+ * or <0 on error.
+ */
+static int dfault_tokenize(char *buf, char *words[], int maxwords)
+{
+ int nwords = 0;
+
+ while (*buf) {
+ char *end;
+
+ /* Skip leading whitespace */
+ buf = skip_spaces(buf);
+ if (!*buf)
+ break; /* oh, it was trailing whitespace */
+
+ /* Run `end' over a word, either whitespace separated or quoted
+ */
+ if (*buf == '"' || *buf == '\'') {
+ int quote = *buf++;
+ for (end = buf ; *end && *end != quote ; end++)
+ ;
+ if (!*end)
+ return -EINVAL; /* unclosed quote */
+ } else {
+ for (end = buf ; *end && !isspace(*end) ; end++)
+ ;
+ BUG_ON(end == buf);
+ }
+ /* Here `buf' is the start of the word, `end' is one past the
+ * end
+ */
+
+ if (nwords == maxwords)
+ return -EINVAL; /* ran out of words[] before bytes */
+ if (*end)
+ *end++ = '\0'; /* terminate the word */
+ words[nwords++] = buf;
+ buf = end;
+ }
+
+ if (verbose) {
+ int i;
+ printk(KERN_INFO "%s: split into words:", __func__);
+ for (i = 0 ; i < nwords ; i++)
+ printk(" \"%s\"", words[i]);
+ printk("\n");
+ }
+
+ return nwords;
+}
+
+/*
+ * Parse a single line number. Note that the empty string ""
+ * is treated as a special case and converted to zero, which
+ * is later treated as a "don't care" value.
+ */
+static inline int parse_lineno(const char *str, unsigned int *val)
+{
+ char *end = NULL;
+ BUG_ON(str == NULL);
+ if (*str == '\0') {
+ *val = 0;
+ return 0;
+ }
+ *val = simple_strtoul(str, &end, 10);
+ return end == NULL || end == str || *end != '\0' ? -EINVAL : 0;
+}
+
+/*
+ * Parse words[] as a dfault query specification, which is a series
+ * of (keyword, value) pairs chosen from these possibilities:
+ *
+ * func <function-name>
+ * file <full-pathname>
+ * file <base-filename>
+ * module <module-name>
+ * line <lineno>
+ * line <first-lineno>-<last-lineno> // where either may be empty
+ */
+static int dfault_parse_query(char *words[], int nwords,
+ struct dfault_query *query)
+{
+ unsigned int i;
+
+ /* check we have an even number of words */
+ if (nwords % 2 != 0)
+ return -EINVAL;
+ memset(query, 0, sizeof(*query));
+
+ for (i = 0 ; i < nwords ; i += 2) {
+ if (!strcmp(words[i], "func"))
+ query->function = words[i+1];
+ else if (!strcmp(words[i], "file"))
+ query->filename = words[i+1];
+ else if (!strcmp(words[i], "module"))
+ query->module = words[i+1];
+ else if (!strcmp(words[i], "line")) {
+ char *first = words[i+1];
+ char *last = strchr(first, '-');
+ if (last)
+ *last++ = '\0';
+ if (parse_lineno(first, &query->first_lineno) < 0)
+ return -EINVAL;
+ if (last != NULL) {
+ /* range <first>-<last> */
+ if (parse_lineno(last, &query->last_lineno) < 0)
+ return -EINVAL;
+ } else {
+ query->last_lineno = query->first_lineno;
+ }
+ } else {
+ if (verbose)
+ printk(KERN_ERR "%s: unknown keyword \"%s\"\n",
+ __func__, words[i]);
+ return -EINVAL;
+ }
+ }
+
+ if (verbose)
+ printk(KERN_INFO "%s: q->function=\"%s\" q->filename=\"%s\" "
+ "q->module=\"%s\" q->lineno=%u-%u\n",
+ __func__, query->function, query->filename,
+ query->module, query->first_lineno,
+ query->last_lineno);
+
+ return 0;
+}
+
+/*
+ * Parse `str' as a flags specification, format [-+=][p]+.
+ * Sets up *maskp and *flagsp to be used when changing the
+ * flags fields of matched _dfault's. Returns 0 on success
+ * or <0 on error.
+ */
+static int dfault_parse_flags(const char *str, unsigned int *flagsp,
+ unsigned int *maskp)
+{
+ unsigned flags = 0;
+ int op = '=';
+
+ switch (*str) {
+ case '+':
+ case '-':
+ case '=':
+ op = *str++;
+ break;
+ default:
+ return -EINVAL;
+ }
+ if (verbose)
+ printk(KERN_INFO "%s: op='%c', flag='%c'\n", __func__,
+ op, *str);
+
+ for ( ; *str ; ++str) {
+ switch (*str) {
+ case 'f':
+ flags |= _DFAULT_ON;
+ break;
+ case 'o':
+ flags |= _DFAULT_ONE_SHOT;
+ break;
+ default:
+ return -EINVAL;
+ }
+ }
+ if (flags == 0)
+ return -EINVAL;
+ if (verbose)
+ printk(KERN_INFO "%s: flags=0x%x\n", __func__, flags);
+
+ /* calculate final *flagsp, *maskp according to mask and op */
+ switch (op) {
+ case '=':
+ *maskp = 0;
+ *flagsp = flags;
+ break;
+ case '+':
+ *maskp = ~0U;
+ *flagsp = flags;
+ break;
+ case '-':
+ *maskp = ~flags;
+ *flagsp = 0;
+ break;
+ }
+ if (verbose)
+ printk(KERN_INFO "%s: *flagsp=0x%x *maskp=0x%x\n",
+ __func__, *flagsp, *maskp);
+ return 0;
+}
+
+/*
+ * File_ops->write method for <debugfs>/dynamic_fault/conrol. Gathers the
+ * command text from userspace, parses and executes it.
+ */
+static ssize_t dfault_proc_write(struct file *file, const char __user *ubuf,
+ size_t len, loff_t *offp)
+{
+ unsigned int flags = 0, mask = 0;
+ struct dfault_query query;
+#define MAXWORDS 9
+ int nwords;
+ char *words[MAXWORDS];
+ char tmpbuf[256];
+
+ if (len == 0)
+ return 0;
+ /* we don't check *offp -- multiple writes() are allowed */
+ if (len > sizeof(tmpbuf)-1)
+ return -E2BIG;
+ if (copy_from_user(tmpbuf, ubuf, len))
+ return -EFAULT;
+ tmpbuf[len] = '\0';
+ if (verbose)
+ printk(KERN_INFO "%s: read %d bytes from userspace\n",
+ __func__, (int)len);
+
+ nwords = dfault_tokenize(tmpbuf, words, MAXWORDS);
+ if (nwords < 0)
+ return -EINVAL;
+ if (dfault_parse_query(words, nwords-1, &query))
+ return -EINVAL;
+ if (dfault_parse_flags(words[nwords-1], &flags, &mask))
+ return -EINVAL;
+
+ /* actually go and implement the change */
+ dfault_change(&query, flags, mask);
+
+ *offp += len;
+ return len;
+}
+
+/*
+ * Set the iterator to point to the first _dfault object
+ * and return a pointer to that first object. Returns
+ * NULL if there are no _dfaults at all.
+ */
+static struct _dfault *dfault_iter_first(struct dfault_iter *iter)
+{
+ if (list_empty(&dfault_tables)) {
+ iter->table = NULL;
+ iter->idx = 0;
+ return NULL;
+ }
+ iter->table = list_entry(dfault_tables.next,
+ struct dfault_table, link);
+ iter->idx = 0;
+ return &iter->table->dfaults[iter->idx];
+}
+
+/*
+ * Advance the iterator to point to the next _dfault
+ * object from the one the iterator currently points at,
+ * and returns a pointer to the new _dfault. Returns
+ * NULL if the iterator has seen all the _dfaults.
+ */
+static struct _dfault *dfault_iter_next(struct dfault_iter *iter)
+{
+ if (iter->table == NULL)
+ return NULL;
+ if (++iter->idx == iter->table->num_dfaults) {
+ /* iterate to next table */
+ iter->idx = 0;
+ if (list_is_last(&iter->table->link, &dfault_tables)) {
+ iter->table = NULL;
+ return NULL;
+ }
+ iter->table = list_entry(iter->table->link.next,
+ struct dfault_table, link);
+ }
+ return &iter->table->dfaults[iter->idx];
+}
+
+/*
+ * Seq_ops start method. Called at the start of every
+ * read() call from userspace. Takes the dfault_lock and
+ * seeks the seq_file's iterator to the given position.
+ */
+static void *dfault_proc_start(struct seq_file *m, loff_t *pos)
+{
+ struct dfault_iter *iter = m->private;
+ struct _dfault *dp;
+ int n = *pos;
+
+ if (verbose)
+ printk(KERN_INFO "%s: called m=%p *pos=%lld\n",
+ __func__, m, (unsigned long long)*pos);
+
+ mutex_lock(&dfault_lock);
+
+ if (!n)
+ return SEQ_START_TOKEN;
+ if (n < 0)
+ return NULL;
+ dp = dfault_iter_first(iter);
+ while (dp != NULL && --n > 0)
+ dp = dfault_iter_next(iter);
+ return dp;
+}
+
+/*
+ * Seq_ops next method. Called several times within a read()
+ * call from userspace, with dfault_lock held. Walks to the
+ * next _dfault object with a special case for the header line.
+ */
+static void *dfault_proc_next(struct seq_file *m, void *p, loff_t *pos)
+{
+ struct dfault_iter *iter = m->private;
+ struct _dfault *dp;
+
+ if (verbose)
+ printk(KERN_INFO "%s: called m=%p p=%p *pos=%lld\n",
+ __func__, m, p, (unsigned long long)*pos);
+
+ if (p == SEQ_START_TOKEN)
+ dp = dfault_iter_first(iter);
+ else
+ dp = dfault_iter_next(iter);
+ ++*pos;
+ return dp;
+}
+
+/*
+ * Seq_ops show method. Called several times within a read()
+ * call from userspace, with dfault_lock held. Formats the
+ * current _dfault as a single human-readable line, with a
+ * special case for the header line.
+ */
+static int dfault_proc_show(struct seq_file *m, void *p)
+{
+ struct dfault_iter *iter = m->private;
+ struct _dfault *df = p;
+ char flagsbuf[8];
+
+ if (verbose)
+ printk(KERN_INFO "%s: called m=%p p=%p\n",
+ __func__, m, p);
+
+ if (p == SEQ_START_TOKEN) {
+ seq_puts(m,
+ "# filename:lineno [module]function flags format\n");
+ return 0;
+ }
+
+ seq_printf(m, "%s:%u [%s]%s %s \"",
+ df->filename, df->lineno,
+ iter->table->mod_name, df->function,
+ dfault_describe_flags(df, flagsbuf, sizeof(flagsbuf)));
+ seq_puts(m, "\"\n");
+
+ return 0;
+}
+
+/*
+ * Seq_ops stop method. Called at the end of each read()
+ * call from userspace. Drops dfault_lock.
+ */
+static void dfault_proc_stop(struct seq_file *m, void *p)
+{
+ if (verbose)
+ printk(KERN_INFO "%s: called m=%p p=%p\n",
+ __func__, m, p);
+ mutex_unlock(&dfault_lock);
+}
+
+static const struct seq_operations dfault_proc_seqops = {
+ .start = dfault_proc_start,
+ .next = dfault_proc_next,
+ .show = dfault_proc_show,
+ .stop = dfault_proc_stop
+};
+
+/*
+ * File_ops->open method for <debugfs>/dynamic_fault/control. Does the seq_file
+ * setup dance, and also creates an iterator to walk the _dfaults.
+ * Note that we create a seq_file always, even for O_WRONLY files
+ * where it's not needed, as doing so simplifies the ->release method.
+ */
+static int dfault_proc_open(struct inode *inode, struct file *file)
+{
+ struct dfault_iter *iter;
+ int err;
+
+ if (verbose)
+ printk(KERN_INFO "%s: called\n", __func__);
+
+ iter = kzalloc(sizeof(*iter), GFP_KERNEL);
+ if (iter == NULL)
+ return -ENOMEM;
+
+ err = seq_open(file, &dfault_proc_seqops);
+ if (err) {
+ kfree(iter);
+ return err;
+ }
+ ((struct seq_file *) file->private_data)->private = iter;
+ return 0;
+}
+
+static const struct file_operations dfault_proc_fops = {
+ .owner = THIS_MODULE,
+ .open = dfault_proc_open,
+ .read = seq_read,
+ .llseek = seq_lseek,
+ .release = seq_release_private,
+ .write = dfault_proc_write
+};
+
+/*
+ * Allocate a new dfault_table for the given module
+ * and add it to the global list.
+ */
+int dfault_add_module(struct _dfault *tab, unsigned int n,
+ const char *name)
+{
+ struct dfault_table *dt;
+ char *new_name;
+
+ dt = kzalloc(sizeof(*dt), GFP_KERNEL);
+ if (dt == NULL)
+ return -ENOMEM;
+ new_name = kstrdup(name, GFP_KERNEL);
+ if (new_name == NULL) {
+ kfree(dt);
+ return -ENOMEM;
+ }
+ dt->mod_name = new_name;
+ dt->num_dfaults = n;
+ dt->num_enabled = 0;
+ dt->dfaults = tab;
+
+ mutex_lock(&dfault_lock);
+ list_add_tail(&dt->link, &dfault_tables);
+ mutex_unlock(&dfault_lock);
+
+ if (verbose)
+ printk(KERN_INFO "%u debug prints in module %s\n",
+ n, dt->mod_name);
+ return 0;
+}
+EXPORT_SYMBOL_GPL(dfault_add_module);
+
+static void dfault_table_free(struct dfault_table *dt)
+{
+ list_del_init(&dt->link);
+ kfree(dt->mod_name);
+ kfree(dt);
+}
+
+/*
+ * Called in response to a module being unloaded. Removes
+ * any dfault_table's which point at the module.
+ */
+int dfault_remove_module(char *mod_name)
+{
+ struct dfault_table *dt, *nextdt;
+ int ret = -ENOENT;
+
+ if (verbose)
+ printk(KERN_INFO "%s: removing module \"%s\"\n",
+ __func__, mod_name);
+
+ mutex_lock(&dfault_lock);
+ list_for_each_entry_safe(dt, nextdt, &dfault_tables, link) {
+ if (!strcmp(dt->mod_name, mod_name)) {
+ dfault_table_free(dt);
+ ret = 0;
+ }
+ }
+ mutex_unlock(&dfault_lock);
+ return ret;
+}
+EXPORT_SYMBOL_GPL(dfault_remove_module);
+
+static void dfault_remove_all_tables(void)
+{
+ mutex_lock(&dfault_lock);
+ while (!list_empty(&dfault_tables)) {
+ struct dfault_table *dt = list_entry(dfault_tables.next,
+ struct dfault_table,
+ link);
+ dfault_table_free(dt);
+ }
+ mutex_unlock(&dfault_lock);
+}
+
+static int __init dynamic_fault_init(void)
+{
+ struct dentry *dir, *file;
+ struct _dfault *iter, *iter_start;
+ const char *modname = NULL;
+ int ret = 0;
+ int n = 0;
+
+ dir = debugfs_create_dir("dynamic_fault", NULL);
+ if (!dir)
+ return -ENOMEM;
+ file = debugfs_create_file("control", 0644, dir, NULL,
+ &dfault_proc_fops);
+ if (!file) {
+ debugfs_remove(dir);
+ return -ENOMEM;
+ }
+ if (__start___faults != __stop___faults) {
+ iter = __start___faults;
+ modname = iter->modname;
+ iter_start = iter;
+ for (; iter < __stop___faults; iter++) {
+ if (strcmp(modname, iter->modname)) {
+ ret = dfault_add_module(iter_start, n, modname);
+ if (ret)
+ goto out_free;
+ n = 0;
+ modname = iter->modname;
+ iter_start = iter;
+ }
+ n++;
+ }
+ ret = dfault_add_module(iter_start, n, modname);
+ }
+out_free:
+ if (ret) {
+ dfault_remove_all_tables();
+ debugfs_remove(dir);
+ debugfs_remove(file);
+ }
+ return 0;
+}
+module_init(dynamic_fault_init);