* [RFC PATCH v3 01/19] skc: Add supplemental kernel cmdline support
From: Masami Hiramatsu @ 2019-08-26 3:15 UTC (permalink / raw)
To: Steven Rostedt, Frank Rowand
Cc: Ingo Molnar, Namhyung Kim, Tim Bird, Jiri Olsa,
Arnaldo Carvalho de Melo, Tom Zanussi, Rob Herring, Andrew Morton,
Thomas Gleixner, Greg Kroah-Hartman, Alexey Dobriyan,
Jonathan Corbet, Linus Torvalds, linux-doc, linux-fsdevel,
linux-kernel
In-Reply-To: <156678933823.21459.4100380582025186209.stgit@devnote2>
Supplemental kernel command line (SKC) allows admin to pass a
tree-structured supplemental kernel commandline file (SKC file)
when boot up kernel. This expands the kernel command line in
efficient way.
SKC file will contain some key-value commands, e.g.
key.word = value1;
another.key.word = value2;
It can fold same keys with braces, also you can write array
data. For example,
key {
word1 {
setting1 = data;
setting2;
}
word2.array = "val1", "val2";
}
User can access these key-value pair and tree structure via
SKC APIs.
Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
---
MAINTAINERS | 6
arch/Kconfig | 9 +
include/linux/skc.h | 205 +++++++++++++++
lib/Kconfig | 3
lib/Makefile | 2
lib/skc.c | 694 +++++++++++++++++++++++++++++++++++++++++++++++++++
6 files changed, 919 insertions(+)
create mode 100644 include/linux/skc.h
create mode 100644 lib/skc.c
diff --git a/MAINTAINERS b/MAINTAINERS
index 035ffc1e16a3..67590c0e37c5 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -15362,6 +15362,12 @@ W: http://www.stlinux.com
S: Supported
F: drivers/net/ethernet/stmicro/stmmac/
+SUPPLEMENTAL KERNEL CMDLINE
+M: Masami Hiramatsu <mhiramat@kernel.org>
+S: Maintained
+F: lib/skc.c
+F: include/linux/skc.h
+
SUN3/3X
M: Sam Creasey <sammy@sammy.net>
W: http://sammy.net/sun3/
diff --git a/arch/Kconfig b/arch/Kconfig
index a7b57dd42c26..14d709ef0396 100644
--- a/arch/Kconfig
+++ b/arch/Kconfig
@@ -24,6 +24,15 @@ config HAVE_IMA_KEXEC
config HOTPLUG_SMT
bool
+config SKC
+ bool "Supplemental kernel cmdline"
+ select LIBSKC
+ default y
+ help
+ Supplemental kernel command line allows system admin to pass a
+ text file as complemental extension of kernel cmdline when boot.
+ The bootloader needs to support loading the SKC file.
+
config OPROFILE
tristate "OProfile system profiling"
depends on PROFILING
diff --git a/include/linux/skc.h b/include/linux/skc.h
new file mode 100644
index 000000000000..71e485ce947f
--- /dev/null
+++ b/include/linux/skc.h
@@ -0,0 +1,205 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _LINUX_SKC_H
+#define _LINUX_SKC_H
+/*
+ * Supplemental Kernel Command Line
+ * Copyright (C) 2019 Linaro Ltd.
+ * Author: Masami Hiramatsu <mhiramat@kernel.org>
+ */
+
+#include <linux/kernel.h>
+#include <linux/types.h>
+
+/* SKC tree node */
+struct skc_node {
+ u16 next;
+ u16 child;
+ u16 parent;
+ u16 data;
+} __attribute__ ((__packed__));
+
+#define SKC_KEY 0
+#define SKC_VALUE (1 << 15)
+/* Maximum size of supplemental kernel cmdline is 32KB - 1 */
+#define SKC_DATA_MAX (SKC_VALUE - 1)
+
+#define SKC_NODE_MAX 512
+#define SKC_KEYLEN_MAX 256
+#define SKC_DEPTH_MAX 16
+
+/* Node tree access raw APIs */
+struct skc_node * __init skc_root_node(void);
+int __init skc_node_index(struct skc_node *node);
+struct skc_node * __init skc_node_get_parent(struct skc_node *node);
+struct skc_node * __init skc_node_get_child(struct skc_node *node);
+struct skc_node * __init skc_node_get_next(struct skc_node *node);
+const char * __init skc_node_get_data(struct skc_node *node);
+
+/**
+ * skc_node_is_value() - Test the node is a value node
+ * @node: An SKC node.
+ *
+ * Test the @node is a value node and return true if a value node, false if not.
+ */
+static inline __init bool skc_node_is_value(struct skc_node *node)
+{
+ return !!(node->data & SKC_VALUE);
+}
+
+/**
+ * skc_node_is_key() - Test the node is a key node
+ * @node: An SKC node.
+ *
+ * Test the @node is a key node and return true if a key node, false if not.
+ */
+static inline __init bool skc_node_is_key(struct skc_node *node)
+{
+ return !(node->data & SKC_VALUE);
+}
+
+/**
+ * skc_node_is_array() - Test the node is an arraied value node
+ * @node: An SKC node.
+ *
+ * Test the @node is an arraied value node.
+ */
+static inline __init bool skc_node_is_array(struct skc_node *node)
+{
+ return skc_node_is_value(node) && node->next != 0;
+}
+
+/**
+ * skc_node_is_leaf() - Test the node is a leaf key node
+ * @node: An SKC node.
+ *
+ * Test the @node is a leaf key node which is a key node and has a value node
+ * or no child. Returns true if it is a leaf node, or false if not.
+ */
+static inline __init bool skc_node_is_leaf(struct skc_node *node)
+{
+ return skc_node_is_key(node) &&
+ (!node->child || skc_node_is_value(skc_node_get_child(node)));
+}
+
+/* Tree-based key-value access APIs */
+struct skc_node * __init skc_node_find_child(struct skc_node *parent,
+ const char *key);
+
+const char * __init skc_node_find_value(struct skc_node *parent,
+ const char *key,
+ struct skc_node **vnode);
+
+struct skc_node * __init skc_node_find_next_leaf(struct skc_node *root,
+ struct skc_node *leaf);
+
+const char * __init skc_node_find_next_key_value(struct skc_node *root,
+ struct skc_node **leaf);
+
+/**
+ * skc_find_value() - Find a value which matches the key
+ * @key: Search key
+ * @vnode: A container pointer of SKC value node.
+ *
+ * Search a value whose key matches @key from whole of SKC tree and return
+ * the value if found. Found value node is stored in *@vnode.
+ * Note that this can return 0-length string and store NULL in *@vnode for
+ * key-only (non-value) entry.
+ */
+static inline const char * __init
+skc_find_value(const char *key, struct skc_node **vnode)
+{
+ return skc_node_find_value(NULL, key, vnode);
+}
+
+/**
+ * skc_find_node() - Find a node which matches the key
+ * @key: Search key
+ * @value: A container pointer of SKC value node.
+ *
+ * Search a (key) node whose key matches @key from whole of SKC tree and
+ * return the node if found. If not found, returns NULL.
+ */
+static inline struct skc_node * __init skc_find_node(const char *key)
+{
+ return skc_node_find_child(NULL, key);
+}
+
+/**
+ * skc_array_for_each_value() - Iterate value nodes on an array
+ * @anode: An SKC arraied value node
+ * @value: A value
+ *
+ * Iterate array value nodes and values starts from @anode. This is expected to
+ * be used with skc_find_value() and skc_node_find_value(), so that user can
+ * process each array entry node.
+ */
+#define skc_array_for_each_value(anode, value) \
+ for (value = skc_node_get_data(anode); anode != NULL ; \
+ anode = skc_node_get_next(anode), \
+ value = anode ? skc_node_get_data(anode) : NULL)
+
+/**
+ * skc_node_for_each_child() - Iterate child nodes
+ * @parent: An SKC node.
+ * @child: Iterated SKC node.
+ *
+ * Iterate child nodes of @parent. Each child nodes are stored to @child.
+ */
+#define skc_node_for_each_child(parent, child) \
+ for (child = skc_node_get_child(parent); child != NULL ; \
+ child = skc_node_get_next(child))
+
+/**
+ * skc_node_for_each_array_value() - Iterate array entries of geven key
+ * @node: An SKC node.
+ * @key: A key string searched under @node
+ * @anode: Iterated SKC node of array entry.
+ * @value: Iterated value of array entry.
+ *
+ * Iterate array entries of given @key under @node. Each array entry node
+ * is stroed to @anode and @value. If the @node doesn't have @key node,
+ * it does nothing.
+ * Note that even if the found key node has only one value (not array)
+ * this executes block once. Hoever, if the found key node has no value
+ * (key-only node), this does nothing. So don't use this for testing the
+ * key-value pair existence.
+ */
+#define skc_node_for_each_array_value(node, key, anode, value) \
+ for (value = skc_node_find_value(node, key, &anode); value != NULL; \
+ anode = skc_node_get_next(anode), \
+ value = anode ? skc_node_get_data(anode) : NULL)
+
+/**
+ * skc_node_for_each_key_value() - Iterate key-value pairs under a node
+ * @node: An SKC node.
+ * @knode: Iterated key node
+ * @value: Iterated value string
+ *
+ * Iterate key-value pairs under @node. Each key node and value string are
+ * stored in @knode and @value respectively.
+ */
+#define skc_node_for_each_key_value(node, knode, value) \
+ for (knode = NULL, value = skc_node_find_next_key_value(node, &knode);\
+ knode != NULL; value = skc_node_find_next_key_value(node, &knode))
+
+/**
+ * skc_for_each_key_value() - Iterate key-value pairs
+ * @knode: Iterated key node
+ * @value: Iterated value string
+ *
+ * Iterate key-value pairs in whole SKC tree. Each key node and value string
+ * are stored in @knode and @value respectively.
+ */
+#define skc_for_each_key_value(knode, value) \
+ skc_node_for_each_key_value(NULL, knode, value)
+
+/* Compose complete key */
+int __init skc_node_compose_key(struct skc_node *node, char *buf, size_t size);
+
+/* SKC node initializer */
+int __init skc_init(char *buf);
+
+/* Debug dump functions */
+void __init skc_debug_dump(void);
+
+#endif
diff --git a/lib/Kconfig b/lib/Kconfig
index f33d66fc0e86..97a53f400a7c 100644
--- a/lib/Kconfig
+++ b/lib/Kconfig
@@ -568,6 +568,9 @@ config DIMLIB
config LIBFDT
bool
+config LIBSKC
+ bool
+
config OID_REGISTRY
tristate
help
diff --git a/lib/Makefile b/lib/Makefile
index 29c02a924973..52132d0d8ff2 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -229,6 +229,8 @@ $(foreach file, $(libfdt_files), \
$(eval CFLAGS_$(file) = -I $(srctree)/scripts/dtc/libfdt))
lib-$(CONFIG_LIBFDT) += $(libfdt_files)
+lib-$(CONFIG_LIBSKC) += skc.o
+
obj-$(CONFIG_RBTREE_TEST) += rbtree_test.o
obj-$(CONFIG_INTERVAL_TREE_TEST) += interval_tree_test.o
diff --git a/lib/skc.c b/lib/skc.c
new file mode 100644
index 000000000000..bbcc81724ec4
--- /dev/null
+++ b/lib/skc.c
@@ -0,0 +1,694 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Supplemental Kernel Commandline
+ * Masami Hiramatsu <mhiramat@kernel.org>
+ */
+
+#define pr_fmt(fmt) "skc: " fmt
+
+#include <linux/bug.h>
+#include <linux/ctype.h>
+#include <linux/errno.h>
+#include <linux/kernel.h>
+#include <linux/printk.h>
+#include <linux/skc.h>
+#include <linux/string.h>
+
+/*
+ * Supplemental Kernel Commandline (SKC) is given as tree-structured ascii
+ * text of key-value pairs on memory.
+ * skc_parse() parses the text to build a simple tree. Each tree node is
+ * simply a key word or a value. A key node may have a next key node or/and
+ * a child node (both key and value). A value node may have a next value
+ * node (for array).
+ */
+
+static struct skc_node skc_nodes[SKC_NODE_MAX] __initdata;
+static int skc_node_num __initdata;
+static char *skc_data __initdata;
+static size_t skc_data_size __initdata;
+static struct skc_node *last_parent __initdata;
+
+static int __init skc_parse_error(const char *msg, const char *p)
+{
+ int line = 0, col = 0;
+ int i, pos = p - skc_data;
+
+ for (i = 0; i < pos; i++) {
+ if (skc_data[i] == '\n') {
+ line++;
+ col = pos - i;
+ }
+ }
+ pr_err("Parse error at line %d, col %d: %s\n", line + 1, col + 1, msg);
+ return -EINVAL;
+}
+
+/**
+ * skc_root_node() - Get the root node of supplemental kernel cmdline
+ *
+ * Return the address of root node of supplemental kernel cmdline. If the
+ * supplemental kernel cmdline is not initiized, return NULL.
+ */
+struct skc_node * __init skc_root_node(void)
+{
+ if (unlikely(!skc_data))
+ return NULL;
+
+ return skc_nodes;
+}
+
+/**
+ * skc_node_index() - Get the index of SKC node
+ * @node: A target node of getting index.
+ *
+ * Return the index number of @node in SKC node list.
+ */
+int __init skc_node_index(struct skc_node *node)
+{
+ return node - &skc_nodes[0];
+}
+
+/**
+ * skc_node_get_parent() - Get the parent SKC node
+ * @node: An SKC node.
+ *
+ * Return the parent node of @node. If the node is top node of the tree,
+ * return NULL.
+ */
+struct skc_node * __init skc_node_get_parent(struct skc_node *node)
+{
+ return node->parent == SKC_NODE_MAX ? NULL : &skc_nodes[node->parent];
+}
+
+/**
+ * skc_node_get_child() - Get the child SKC node
+ * @node: An SKC node.
+ *
+ * Return the first child node of @node. If the node has no child, return
+ * NULL.
+ */
+struct skc_node * __init skc_node_get_child(struct skc_node *node)
+{
+ return node->child ? &skc_nodes[node->child] : NULL;
+}
+
+/**
+ * skc_node_get_next() - Get the next sibling SKC node
+ * @node: An SKC node.
+ *
+ * Return the NEXT sibling node of @node. If the node has no next sibling,
+ * return NULL. Note that even if this returns NULL, it doesn't mean @node
+ * has no siblings. (You also has to check whether the parent's child node
+ * is @node or not.)
+ */
+struct skc_node * __init skc_node_get_next(struct skc_node *node)
+{
+ return node->next ? &skc_nodes[node->next] : NULL;
+}
+
+/**
+ * skc_node_get_data() - Get the data of SKC node
+ * @node: An SKC node.
+ *
+ * Return the data (which is always a null terminated string) of @node.
+ * If the node has invalid data, warn and return NULL.
+ */
+const char * __init skc_node_get_data(struct skc_node *node)
+{
+ int offset = node->data & ~SKC_VALUE;
+
+ if (WARN_ON(offset >= skc_data_size))
+ return NULL;
+
+ return skc_data + offset;
+}
+
+static bool __init
+skc_node_match_prefix(struct skc_node *node, const char **prefix)
+{
+ const char *p = skc_node_get_data(node);
+ int len = strlen(p);
+
+ if (strncmp(*prefix, p, len))
+ return false;
+
+ p = *prefix + len;
+ if (*p == '.')
+ p++;
+ else if (*p != '\0')
+ return false;
+ *prefix = p;
+
+ return true;
+}
+
+/**
+ * skc_node_find_child() - Find a child node which matches given key
+ * @parent: An SKC node.
+ * @key: A key string.
+ *
+ * Search a node under @parent which matches @key. The @key can contain
+ * several words jointed with '.'. If @parent is NULL, this searches the
+ * node from whole tree. Return NULL if no node is matched.
+ */
+struct skc_node * __init
+skc_node_find_child(struct skc_node *parent, const char *key)
+{
+ struct skc_node *node;
+
+ if (parent)
+ node = skc_node_get_child(parent);
+ else
+ node = skc_root_node();
+
+ while (node && skc_node_is_key(node)) {
+ if (!skc_node_match_prefix(node, &key))
+ node = skc_node_get_next(node);
+ else if (*key != '\0')
+ node = skc_node_get_child(node);
+ else
+ break;
+ }
+
+ return node;
+}
+
+/**
+ * skc_node_find_value() - Find a value node which matches given key
+ * @parent: An SKC node.
+ * @key: A key string.
+ * @vnode: A container pointer of found SKC node.
+ *
+ * Search a value node under @parent whose (parent) key node matches @key,
+ * store it in *@vnode, and returns the value string.
+ * The @key can contain several words jointed with '.'. If @parent is NULL,
+ * this searches the node from whole tree. Return the value string if a
+ * matched key found, return NULL if no node is matched.
+ * Note that this returns 0-length string and stores NULL in *@vnode if the
+ * key has no value. And also it will return the value of the first entry if
+ * the value is an array.
+ */
+const char * __init
+skc_node_find_value(struct skc_node *parent, const char *key,
+ struct skc_node **vnode)
+{
+ struct skc_node *node = skc_node_find_child(parent, key);
+
+ if (!node || !skc_node_is_key(node))
+ return NULL;
+
+ node = skc_node_get_child(node);
+ if (node && !skc_node_is_value(node))
+ return NULL;
+
+ if (vnode)
+ *vnode = node;
+
+ return node ? skc_node_get_data(node) : "";
+}
+
+/**
+ * skc_node_compose_key() - Compose key string of the SKC node
+ * @node: An SKC node.
+ * @buf: A buffer to store the key.
+ * @size: The size of the @buf.
+ *
+ * Compose the full-length key of the @node into @buf. Returns the total
+ * length of the key stored in @buf. Or returns -EINVAL if @node is NULL,
+ * and -ERANGE if the key depth is deeper than max depth.
+ */
+int __init skc_node_compose_key(struct skc_node *node, char *buf, size_t size)
+{
+ u16 keys[SKC_DEPTH_MAX];
+ int depth = 0, ret = 0, total = 0;
+
+ if (!node)
+ return -EINVAL;
+
+ if (skc_node_is_value(node))
+ node = skc_node_get_parent(node);
+
+ while (node) {
+ keys[depth++] = skc_node_index(node);
+ if (depth == SKC_DEPTH_MAX)
+ return -ERANGE;
+ node = skc_node_get_parent(node);
+ }
+
+ while (--depth >= 0) {
+ node = skc_nodes + keys[depth];
+ ret = snprintf(buf, size, "%s%s", skc_node_get_data(node),
+ depth ? "." : "");
+ if (ret < 0)
+ return ret;
+ if (ret > size) {
+ size = 0;
+ } else {
+ size -= ret;
+ buf += ret;
+ }
+ total += ret;
+ }
+
+ return total;
+}
+
+/**
+ * skc_node_find_next_leaf() - Find the next leaf node under given node
+ * @root: An SKC root node
+ * @node: An SKC node which starts from.
+ *
+ * Search the next leaf node (which means the terminal key node) of @node
+ * under @root node (including @root node itself).
+ * Return the next node or NULL if next leaf node is not found.
+ */
+struct skc_node * __init skc_node_find_next_leaf(struct skc_node *root,
+ struct skc_node *node)
+{
+ if (unlikely(!skc_data))
+ return NULL;
+
+ if (!node) { /* First try */
+ node = root;
+ if (!node)
+ node = skc_nodes;
+ } else {
+ if (node == root) /* @root was a leaf, no child node. */
+ return NULL;
+
+ while (!node->next) {
+ node = skc_node_get_parent(node);
+ if (node == root)
+ return NULL;
+ /* User passed a node which is not uder parent */
+ if (WARN_ON(!node))
+ return NULL;
+ }
+ node = skc_node_get_next(node);
+ }
+
+ while (node && !skc_node_is_leaf(node))
+ node = skc_node_get_child(node);
+
+ return node;
+}
+
+/**
+ * skc_node_find_next_key_value() - Find the next key-value pair nodes
+ * @root: An SKC root node
+ * @leaf: A container pointer of SKC node which starts from.
+ *
+ * Search the next leaf node (which means the terminal key node) of *@leaf
+ * under @root node. Returns the value and update *@leaf if next leaf node
+ * is found, or NULL if no next leaf node is found.
+ * Note that this returns 0-length string if the key has no value, or
+ * the value of the first entry if the value is an array.
+ */
+const char * __init skc_node_find_next_key_value(struct skc_node *root,
+ struct skc_node **leaf)
+{
+ /* tip must be passed */
+ if (WARN_ON(!leaf))
+ return NULL;
+
+ *leaf = skc_node_find_next_leaf(root, *leaf);
+ if (!*leaf)
+ return NULL;
+ if ((*leaf)->child)
+ return skc_node_get_data(skc_node_get_child(*leaf));
+ else
+ return ""; /* No value key */
+}
+
+/* SKC parse and tree build */
+
+static struct skc_node * __init skc_add_node(char *data, u32 flag)
+{
+ struct skc_node *node;
+ unsigned long offset;
+
+ if (skc_node_num == SKC_NODE_MAX)
+ return NULL;
+
+ node = &skc_nodes[skc_node_num++];
+ offset = data - skc_data;
+ node->data = (u16)offset;
+ if (WARN_ON(offset >= SKC_DATA_MAX))
+ return NULL;
+ node->data |= flag;
+ node->child = 0;
+ node->next = 0;
+
+ return node;
+}
+
+static inline __init struct skc_node *skc_last_sibling(struct skc_node *node)
+{
+ while (node->next)
+ node = skc_node_get_next(node);
+
+ return node;
+}
+
+static struct skc_node * __init skc_add_sibling(char *data, u32 flag)
+{
+ struct skc_node *sib, *node = skc_add_node(data, flag);
+
+ if (node) {
+ if (!last_parent) {
+ node->parent = SKC_NODE_MAX;
+ sib = skc_last_sibling(skc_nodes);
+ sib->next = skc_node_index(node);
+ } else {
+ node->parent = skc_node_index(last_parent);
+ if (!last_parent->child) {
+ last_parent->child = skc_node_index(node);
+ } else {
+ sib = skc_node_get_child(last_parent);
+ sib = skc_last_sibling(sib);
+ sib->next = skc_node_index(node);
+ }
+ }
+ }
+
+ return node;
+}
+
+static inline __init struct skc_node *skc_add_child(char *data, u32 flag)
+{
+ struct skc_node *node = skc_add_sibling(data, flag);
+
+ if (node)
+ last_parent = node;
+
+ return node;
+}
+
+static inline __init bool skc_valid_keyword(char *key)
+{
+ if (key[0] == '\0')
+ return false;
+
+ while (isalnum(*key) || *key == '-' || *key == '_')
+ key++;
+
+ return *key == '\0';
+}
+
+static inline __init char *find_ending_quote(char *p)
+{
+ do {
+ p = strchr(p + 1, '"');
+ if (!p)
+ break;
+ } while (*(p - 1) == '\\');
+
+ return p;
+}
+
+/* Return delimiter or error, no node added */
+static int __init __skc_parse_value(char **__v, char **__n)
+{
+ char *p, *v = *__v;
+ int c;
+
+ v = skip_spaces(v);
+ if (*v == '"') {
+ v++;
+ p = find_ending_quote(v);
+ if (!p)
+ return skc_parse_error("No closing quotation", v);
+ *p++ = '\0';
+ p = skip_spaces(p);
+ if (*p != ',' && *p != ';')
+ return skc_parse_error("No delimiter for value", v);
+ c = *p;
+ *p++ = '\0';
+ } else {
+ p = strpbrk(v, ",;");
+ if (!p)
+ return skc_parse_error("No delimiter for value", v);
+ c = *p;
+ *p++ = '\0';
+ v = strim(v);
+ }
+ *__v = v;
+ *__n = p;
+
+ return c;
+}
+
+static int __init skc_parse_array(char **__v)
+{
+ struct skc_node *node;
+ char *next;
+ int c = 0;
+
+ do {
+ c = __skc_parse_value(__v, &next);
+ if (c < 0)
+ return c;
+
+ node = skc_add_sibling(*__v, SKC_VALUE);
+ if (!node)
+ return -ENOMEM;
+ *__v = next;
+ } while (c != ';');
+ node->next = 0;
+
+ return 0;
+}
+
+static inline __init
+struct skc_node *find_match_node(struct skc_node *node, char *k)
+{
+ while (node) {
+ if (!strcmp(skc_node_get_data(node), k))
+ break;
+ node = skc_node_get_next(node);
+ }
+ return node;
+}
+
+static int __init __skc_add_key(char *k)
+{
+ struct skc_node *node;
+
+ if (!skc_valid_keyword(k))
+ return skc_parse_error("Invalid keyword", k);
+
+ if (unlikely(skc_node_num == 0))
+ goto add_node;
+
+ if (!last_parent) /* the first level */
+ node = find_match_node(skc_nodes, k);
+ else
+ node = find_match_node(skc_node_get_child(last_parent), k);
+
+ if (node)
+ last_parent = node;
+ else {
+add_node:
+ node = skc_add_child(k, SKC_KEY);
+ if (!node)
+ return -ENOMEM;
+ }
+ return 0;
+}
+
+static int __init __skc_parse_keys(char *k)
+{
+ char *p;
+ int ret;
+
+ k = strim(k);
+ while ((p = strchr(k, '.'))) {
+ *p++ = '\0';
+ ret = __skc_add_key(k);
+ if (ret)
+ return ret;
+ k = p;
+ }
+
+ return __skc_add_key(k);
+}
+
+static int __init skc_parse_kv(char **k, char *v)
+{
+ struct skc_node *prev_parent = last_parent;
+ struct skc_node *node;
+ char *next;
+ int c, ret;
+
+ ret = __skc_parse_keys(*k);
+ if (ret)
+ return ret;
+
+ c = __skc_parse_value(&v, &next);
+ if (c < 0)
+ return c;
+
+ node = skc_add_sibling(v, SKC_VALUE);
+ if (!node)
+ return -ENOMEM;
+
+ if (c == ',') { /* Array */
+ ret = skc_parse_array(&next);
+ if (ret < 0)
+ return ret;
+ }
+
+ last_parent = prev_parent;
+
+ *k = next;
+
+ return 0;
+}
+
+static int __init skc_parse_key(char **k, char *n)
+{
+ struct skc_node *prev_parent = last_parent;
+ int ret;
+
+ *k = strim(*k);
+ if (**k != '\0') {
+ ret = __skc_parse_keys(*k);
+ if (ret)
+ return ret;
+ last_parent = prev_parent;
+ }
+
+ *k = n;
+
+ return 0;
+}
+
+static int __init skc_open_brace(char **k, char *n)
+{
+ int ret;
+
+ ret = __skc_parse_keys(*k);
+ if (ret)
+ return ret;
+
+ /* Mark the last key as open brace */
+ last_parent->next = SKC_NODE_MAX;
+
+ *k = n;
+
+ return 0;
+}
+
+static int __init skc_close_brace(char **k, char *n)
+{
+ struct skc_node *node;
+
+ *k = strim(*k);
+ if (**k != '\0')
+ return skc_parse_error("Unexpected key, maybe forgot ;?", *k);
+
+ if (!last_parent || last_parent->next != SKC_NODE_MAX)
+ return skc_parse_error("Unexpected closing brace", *k);
+
+ node = last_parent;
+ node->next = 0;
+ do {
+ node = skc_node_get_parent(node);
+ } while (node && node->next != SKC_NODE_MAX);
+ last_parent = node;
+
+ *k = n;
+
+ return 0;
+}
+
+static int __init skc_verify_tree(void)
+{
+ int i;
+
+ for (i = 0; i < skc_node_num; i++) {
+ if (skc_nodes[i].next > skc_node_num) {
+ return skc_parse_error("No closing brace",
+ skc_node_get_data(skc_nodes + i));
+ }
+ }
+
+ return 0;
+}
+
+/**
+ * skc_init() - Parse given SKC file and build SKC internal tree
+ * @buf: Supplemental kernel cmdline text
+ *
+ * This parses the supplemental kernel cmdline text in @buf. @buf must be a
+ * null terminated string and smaller than SKC_DATA_MAX.
+ * Return 0 if succeeded, or -errno if there is any error.
+ */
+int __init skc_init(char *buf)
+{
+ char *p, *q;
+ int ret, c;
+
+ if (skc_data)
+ return -EBUSY;
+
+ ret = strlen(buf);
+ if (ret > SKC_DATA_MAX - 1 || ret == 0)
+ return -ERANGE;
+
+ skc_data = buf;
+ skc_data_size = ret + 1;
+
+ p = buf;
+ do {
+ q = strpbrk(p, "{}=;");
+ if (!q)
+ break;
+ c = *q;
+ *q++ = '\0';
+ switch (c) {
+ case '=':
+ ret = skc_parse_kv(&p, q);
+ break;
+ case '{':
+ ret = skc_open_brace(&p, q);
+ break;
+ case ';':
+ ret = skc_parse_key(&p, q);
+ break;
+ case '}':
+ ret = skc_close_brace(&p, q);
+ break;
+ }
+ } while (ret == 0);
+
+ if (ret < 0)
+ return ret;
+
+ if (!q) {
+ p = skip_spaces(p);
+ if (*p != '\0')
+ return skc_parse_error("No delimiter", p);
+ }
+
+ return skc_verify_tree();
+}
+
+/**
+ * skc_debug_dump() - Dump current SKC node list
+ *
+ * Dump the current SKC node list on printk buffer for debug.
+ */
+void __init skc_debug_dump(void)
+{
+ int i;
+
+ for (i = 0; i < skc_node_num; i++) {
+ pr_debug("[%d] %s (%s) .next=%d, .child=%d .parent=%d\n", i,
+ skc_node_get_data(skc_nodes + i),
+ skc_node_is_value(skc_nodes + i) ? "value" : "key",
+ skc_nodes[i].next, skc_nodes[i].child,
+ skc_nodes[i].parent);
+ }
+}
^ permalink raw reply related
* [RFC PATCH v3 02/19] skc: Add /proc/sup_cmdline to show SKC key-value list
From: Masami Hiramatsu @ 2019-08-26 3:16 UTC (permalink / raw)
To: Steven Rostedt, Frank Rowand
Cc: Ingo Molnar, Namhyung Kim, Tim Bird, Jiri Olsa,
Arnaldo Carvalho de Melo, Tom Zanussi, Rob Herring, Andrew Morton,
Thomas Gleixner, Greg Kroah-Hartman, Alexey Dobriyan,
Jonathan Corbet, Linus Torvalds, linux-doc, linux-fsdevel,
linux-kernel
In-Reply-To: <156678933823.21459.4100380582025186209.stgit@devnote2>
Add /proc/sup_cmdline which shows the list of key-value pairs
in SKC data. Since after boot, all SKC data and tree are
removed, this interface just keep a copy of key-value
pairs in text.
Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
---
MAINTAINERS | 1
fs/proc/Makefile | 1
fs/proc/sup_cmdline.c | 106 +++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 108 insertions(+)
create mode 100644 fs/proc/sup_cmdline.c
diff --git a/MAINTAINERS b/MAINTAINERS
index 67590c0e37c5..10dd38311d96 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -15366,6 +15366,7 @@ SUPPLEMENTAL KERNEL CMDLINE
M: Masami Hiramatsu <mhiramat@kernel.org>
S: Maintained
F: lib/skc.c
+F: fs/proc/sup_cmdline.c
F: include/linux/skc.h
SUN3/3X
diff --git a/fs/proc/Makefile b/fs/proc/Makefile
index ead487e80510..a5d018f9422c 100644
--- a/fs/proc/Makefile
+++ b/fs/proc/Makefile
@@ -33,3 +33,4 @@ proc-$(CONFIG_PROC_KCORE) += kcore.o
proc-$(CONFIG_PROC_VMCORE) += vmcore.o
proc-$(CONFIG_PRINTK) += kmsg.o
proc-$(CONFIG_PROC_PAGE_MONITOR) += page.o
+proc-$(CONFIG_SKC) += sup_cmdline.o
diff --git a/fs/proc/sup_cmdline.c b/fs/proc/sup_cmdline.c
new file mode 100644
index 000000000000..97bc40f0c9dd
--- /dev/null
+++ b/fs/proc/sup_cmdline.c
@@ -0,0 +1,106 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * /proc/sup_cmdline - Supplemental kernel command line
+ */
+#include <linux/fs.h>
+#include <linux/init.h>
+#include <linux/printk.h>
+#include <linux/proc_fs.h>
+#include <linux/seq_file.h>
+#include <linux/skc.h>
+#include <linux/slab.h>
+
+static char *saved_sup_cmdline;
+
+static int skc_proc_show(struct seq_file *m, void *v)
+{
+ if (saved_sup_cmdline)
+ seq_puts(m, saved_sup_cmdline);
+ else
+ seq_putc(m, '\n');
+ return 0;
+}
+
+static int __init update_snprintf(char **dstp, size_t *sizep,
+ const char *fmt, ...)
+{
+ va_list args;
+ int ret;
+
+ va_start(args, fmt);
+ ret = vsnprintf(*dstp, *sizep, fmt, args);
+ va_end(args);
+
+ if (*sizep && ret > 0) {
+ *sizep -= ret;
+ *dstp += ret;
+ }
+
+ return ret;
+}
+
+/* Return the needed total length if @size is 0 */
+static int __init copy_skc_key_value_list(char *dst, size_t size)
+{
+ struct skc_node *leaf, *vnode;
+ const char *val;
+ int len = 0, ret = 0;
+ char *key;
+
+ key = kzalloc(SKC_KEYLEN_MAX, GFP_KERNEL);
+
+ skc_for_each_key_value(leaf, val) {
+ ret = skc_node_compose_key(leaf, key, SKC_KEYLEN_MAX);
+ if (ret < 0)
+ break;
+ ret = update_snprintf(&dst, &size, "%s = ", key);
+ if (ret < 0)
+ break;
+ len += ret;
+ vnode = skc_node_get_child(leaf);
+ if (vnode && skc_node_is_array(vnode)) {
+ skc_array_for_each_value(vnode, val) {
+ ret = update_snprintf(&dst, &size, "\"%s\"%s",
+ val, vnode->next ? ", " : ";\n");
+ if (ret < 0)
+ goto out;
+ len += ret;
+ }
+ } else {
+ ret = update_snprintf(&dst, &size, "\"%s\";\n", val);
+ if (ret < 0)
+ break;
+ len += ret;
+ }
+ }
+out:
+ kfree(key);
+
+ return ret < 0 ? ret : len;
+}
+
+static int __init proc_skc_init(void)
+{
+ int len;
+
+ len = copy_skc_key_value_list(NULL, 0);
+ if (len < 0)
+ return len;
+
+ if (len > 0) {
+ saved_sup_cmdline = kzalloc(len + 1, GFP_KERNEL);
+ if (!saved_sup_cmdline)
+ return -ENOMEM;
+
+ len = copy_skc_key_value_list(saved_sup_cmdline, len + 1);
+ if (len < 0) {
+ kfree(saved_sup_cmdline);
+ return len;
+ }
+ }
+
+ proc_create_single("sup_cmdline", 0, NULL, skc_proc_show);
+
+ return 0;
+}
+fs_initcall(proc_skc_init);
^ permalink raw reply related
* [RFC PATCH v3 03/19] skc: Add a boot setup routine from cmdline
From: Masami Hiramatsu @ 2019-08-26 3:16 UTC (permalink / raw)
To: Steven Rostedt, Frank Rowand
Cc: Ingo Molnar, Namhyung Kim, Tim Bird, Jiri Olsa,
Arnaldo Carvalho de Melo, Tom Zanussi, Rob Herring, Andrew Morton,
Thomas Gleixner, Greg Kroah-Hartman, Alexey Dobriyan,
Jonathan Corbet, Linus Torvalds, linux-doc, linux-fsdevel,
linux-kernel
In-Reply-To: <156678933823.21459.4100380582025186209.stgit@devnote2>
Add a boot setup routine from cmdline option "skc=ADDR,SIZE".
Bootloader has to setup this cmdline option when it loads
the skc file on memory. The ADDR must be a physical address.
Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
---
Documentation/admin-guide/kernel-parameters.txt | 5 ++
init/main.c | 54 +++++++++++++++++++++++
2 files changed, 59 insertions(+)
diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index 47d981a86e2f..9a955b1bd1bf 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -4273,6 +4273,11 @@
simeth= [IA-64]
simscsi=
+ skc=paddr,size [SKC]
+ Pass the physical memory address and size of loaded
+ supplemental kernel cmdline (SKC) text. This will
+ be treated by bootloader which loads the SKC file.
+
slram= [HW,MTD]
slab_nomerge [MM]
diff --git a/init/main.c b/init/main.c
index 96f8d5af52d6..d06eb3d63b77 100644
--- a/init/main.c
+++ b/init/main.c
@@ -36,6 +36,7 @@
#include <linux/kernel_stat.h>
#include <linux/start_kernel.h>
#include <linux/security.h>
+#include <linux/skc.h>
#include <linux/smp.h>
#include <linux/profile.h>
#include <linux/rcupdate.h>
@@ -245,6 +246,58 @@ static int __init loglevel(char *str)
early_param("loglevel", loglevel);
+#ifdef CONFIG_SKC
+__initdata unsigned long initial_skc;
+__initdata unsigned long initial_skc_len;
+
+static int __init save_skc_address(char *str)
+{
+ char *p;
+
+ p = strchr(str, ',');
+ if (!p)
+ return -EINVAL;
+ *p++ = '\0';
+ /* First options should be physical address - int is not enough */
+ if (kstrtoul(str, 0, &initial_skc) < 0)
+ return -EINVAL;
+ if (kstrtoul(p, 0, &initial_skc_len) < 0)
+ return -EINVAL;
+ if (initial_skc_len > SKC_DATA_MAX)
+ return -E2BIG;
+ /* Reserve it for protection */
+ memblock_reserve(initial_skc, initial_skc_len);
+
+ return 0;
+}
+early_param("skc", save_skc_address);
+
+static void __init setup_skc(void)
+{
+ u32 size;
+ char *data, *copy;
+
+ if (!initial_skc)
+ return;
+
+ data = early_memremap(initial_skc, initial_skc_len);
+ size = initial_skc_len + 1;
+
+ copy = memblock_alloc(size, SMP_CACHE_BYTES);
+ if (!copy) {
+ pr_err("Failed to allocate memory for structured kernel cmdline\n");
+ goto end;
+ }
+ memcpy(copy, data, initial_skc_len);
+ copy[size - 1] = '\0';
+
+ skc_init(copy);
+end:
+ early_memunmap(data, initial_skc_len);
+}
+#else
+#define setup_skc() do { } while (0)
+#endif
/* Change NUL term back to "=", to make "param" the whole string. */
static int __init repair_env_string(char *param, char *val,
const char *unused, void *arg)
@@ -596,6 +649,7 @@ asmlinkage __visible void __init start_kernel(void)
setup_arch(&command_line);
mm_init_cpumask(&init_mm);
setup_command_line(command_line);
+ setup_skc();
setup_nr_cpu_ids();
setup_per_cpu_areas();
smp_prepare_boot_cpu(); /* arch-specific boot-cpu hooks */
^ permalink raw reply related
* [RFC PATCH v3 04/19] Documentation: skc: Add a doc for supplemental kernel cmdline
From: Masami Hiramatsu @ 2019-08-26 3:16 UTC (permalink / raw)
To: Steven Rostedt, Frank Rowand
Cc: Ingo Molnar, Namhyung Kim, Tim Bird, Jiri Olsa,
Arnaldo Carvalho de Melo, Tom Zanussi, Rob Herring, Andrew Morton,
Thomas Gleixner, Greg Kroah-Hartman, Alexey Dobriyan,
Jonathan Corbet, Linus Torvalds, linux-doc, linux-fsdevel,
linux-kernel
In-Reply-To: <156678933823.21459.4100380582025186209.stgit@devnote2>
Add a documentation for supplemental kernel cmdline under
admin-guide, since it is including the syntax of SKC file.
Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
---
Documentation/admin-guide/index.rst | 1
Documentation/admin-guide/kernel-parameters.txt | 1
Documentation/admin-guide/skc.rst | 123 +++++++++++++++++++++++
MAINTAINERS | 1
4 files changed, 126 insertions(+)
create mode 100644 Documentation/admin-guide/skc.rst
diff --git a/Documentation/admin-guide/index.rst b/Documentation/admin-guide/index.rst
index 33feab2f4084..44b4cb61003a 100644
--- a/Documentation/admin-guide/index.rst
+++ b/Documentation/admin-guide/index.rst
@@ -106,6 +106,7 @@ configure specific aspects of kernel behavior to your liking.
rtc
svga
video-output
+ skc
.. only:: subproject and html
diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index 9a955b1bd1bf..334ce59de23a 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -4277,6 +4277,7 @@
Pass the physical memory address and size of loaded
supplemental kernel cmdline (SKC) text. This will
be treated by bootloader which loads the SKC file.
+ See Documentation/admin-guide/skc.rst.
slram= [HW,MTD]
diff --git a/Documentation/admin-guide/skc.rst b/Documentation/admin-guide/skc.rst
new file mode 100644
index 000000000000..dc6f7ba8e1d7
--- /dev/null
+++ b/Documentation/admin-guide/skc.rst
@@ -0,0 +1,123 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+================================
+Supplemental Kernel Commandline
+================================
+
+:Author: Masami Hiramatsu <mhiramat@kernel.org>
+
+Overview
+========
+
+Supplemental Kernel Commandline (SKC) is expanding current kernel cmdline
+to support additional key-value data when boot the kernel in efficient way.
+This allows adoministrators to pass a tree-structured key-value text file
+(SKC file) via bootloaders.
+
+SKC File Syntax
+===============
+
+SKC basic syntax is simple. Each key consists of dot-connected-words, and
+key and value are connected by "=". The value has to be terminated by semi-
+colon (";"). For array value, array entries are separated by comma (",").
+
+KEY[.WORD[...]] = VALUE[, VALUE2[...]];
+
+Each key word only contains alphabet, number, dash ("-") or underscore ("_").
+If a value need to contain the delimiters, you can use double-quotes to
+quote it. A double quote in VALUE can be escaped by backslash. There can
+be a key which doesn't have value or has an empty value. Those keys are
+used for checking the key exists or not (like a boolean).
+
+Tree Structure
+--------------
+
+SKC allows user to merge partially same word keys by brace. For example,
+
+foo.bar.baz = value1;
+foo.bar.qux.quux = value2;
+
+These can be written also in
+
+foo.bar {
+ baz = value1;
+ qux.quux = value2;
+}
+
+In both style, same key words are automatically merged when parsing it
+at boot time. So you can append similar trees or key-values.
+
+SKC File Limitation
+===================
+
+Currently the maximum SKC file size is 32KB and the total key-words (not
+key-value entries) must be under 512 nodes.
+
+/proc/sup_cmdline
+=================
+
+/proc/sup_cmdline is the user-space interface of supplemental kernel
+cmdline. Unlike /proc/cmdline, this file shows the key-value style list.
+Each key-value pair is shown in each line with following style.
+
+KEY[.WORDS...] = "[VALUE]"[,"VALUE2"...];
+
+How to Pass at Boot
+===================
+
+SKC file is passed to kernel via memory, so the boot loader must support
+loading SKC file. After loading the SKC file on memory, the boot loader
+has to add "skc=PADDR,SIZE" argument to kernel cmdline, where the PADDR
+is the physical address of the memory block and SIZE is the size of SKC
+file.
+
+SKC APIs
+========
+
+User can query or loop on key-value pairs, also it is possible to find
+a root (prefix) key node and find key-values under that node.
+
+If you have a key string, you can query the value directly with the key
+using skc_find_value(). If you want to know what keys exist in the SKC
+tree, you can use skc_for_each_key_value() to iterate key-value pairs.
+Note that you need to use skc_array_for_each_value() for accessing
+each arraies value, e.g.
+
+::
+
+ vnode = NULL;
+ skc_find_value("key.word", &vnode);
+ if (vnode && skc_node_is_array(vnode))
+ skc_array_for_each_value(vnode, value) {
+ printk("%s ", value);
+ }
+
+If you want to focus on keys which has a prefix string, you can use
+skc_find_node() to find a node which prefix key words, and iterate
+keys under the prefix node with skc_node_for_each_key_value().
+
+But the most typical usage is to get the named value under prefix
+or get the named array under prefix as below.
+
+::
+
+ root = skc_find_node("key.prefix");
+ value = skc_node_find_value(root, "option", &vnode);
+ ...
+ skc_node_for_each_array_value(root, "array-option", value, anode) {
+ ...
+ }
+
+This accesses a value of "key.prefix.option" and an array of
+"key.prefix.array-option".
+
+Locking is not needed, since after initialized, SKC becomes readonly.
+All data and keys must be copied if you need to modify it.
+
+
+Functions and structures
+========================
+
+.. kernel-doc:: include/linux/skc.h
+.. kernel-doc:: lib/skc.c
+
diff --git a/MAINTAINERS b/MAINTAINERS
index 10dd38311d96..9c0b97643515 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -15368,6 +15368,7 @@ S: Maintained
F: lib/skc.c
F: fs/proc/sup_cmdline.c
F: include/linux/skc.h
+F: Documentation/admin-guide/skc.rst
SUN3/3X
M: Sam Creasey <sammy@sammy.net>
^ permalink raw reply related
* [RFC PATCH v3 05/19] tracing: Apply soft-disabled and filter to tracepoints printk
From: Masami Hiramatsu @ 2019-08-26 3:16 UTC (permalink / raw)
To: Steven Rostedt, Frank Rowand
Cc: Ingo Molnar, Namhyung Kim, Tim Bird, Jiri Olsa,
Arnaldo Carvalho de Melo, Tom Zanussi, Rob Herring, Andrew Morton,
Thomas Gleixner, Greg Kroah-Hartman, Alexey Dobriyan,
Jonathan Corbet, Linus Torvalds, linux-doc, linux-fsdevel,
linux-kernel
In-Reply-To: <156678933823.21459.4100380582025186209.stgit@devnote2>
Apply soft-disabled and the filter rule of the trace events to
the printk output of tracepoints (a.k.a. tp_printk kernel parameter)
as same as trace buffer output.
Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
---
kernel/trace/trace.c | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
index 525a97fbbc60..ead3a9faa584 100644
--- a/kernel/trace/trace.c
+++ b/kernel/trace/trace.c
@@ -2502,6 +2502,7 @@ static DEFINE_MUTEX(tracepoint_printk_mutex);
static void output_printk(struct trace_event_buffer *fbuffer)
{
struct trace_event_call *event_call;
+ struct trace_event_file *file;
struct trace_event *event;
unsigned long flags;
struct trace_iterator *iter = tracepoint_print_iter;
@@ -2515,6 +2516,12 @@ static void output_printk(struct trace_event_buffer *fbuffer)
!event_call->event.funcs->trace)
return;
+ file = fbuffer->trace_file;
+ if (test_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags) ||
+ (unlikely(file->flags & EVENT_FILE_FL_FILTERED) &&
+ !filter_match_preds(file->filter, fbuffer->entry)))
+ return;
+
event = &fbuffer->trace_file->event_call->event;
spin_lock_irqsave(&tracepoint_iter_lock, flags);
^ permalink raw reply related
* [RFC PATCH v3 06/19] tracing: kprobes: Output kprobe event to printk buffer
From: Masami Hiramatsu @ 2019-08-26 3:16 UTC (permalink / raw)
To: Steven Rostedt, Frank Rowand
Cc: Ingo Molnar, Namhyung Kim, Tim Bird, Jiri Olsa,
Arnaldo Carvalho de Melo, Tom Zanussi, Rob Herring, Andrew Morton,
Thomas Gleixner, Greg Kroah-Hartman, Alexey Dobriyan,
Jonathan Corbet, Linus Torvalds, linux-doc, linux-fsdevel,
linux-kernel
In-Reply-To: <156678933823.21459.4100380582025186209.stgit@devnote2>
Since kprobe-events use event_trigger_unlock_commit_regs() directly,
that events doesn't show up in printk buffer if "tp_printk" is set.
Use trace_event_buffer_commit() in kprobe events so that it can
invoke output_printk() as same as other trace events.
Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
---
include/linux/trace_events.h | 1 +
kernel/trace/trace.c | 4 +--
kernel/trace/trace_events.c | 1 +
kernel/trace/trace_kprobe.c | 57 +++++++++++++++++++++---------------------
4 files changed, 32 insertions(+), 31 deletions(-)
diff --git a/include/linux/trace_events.h b/include/linux/trace_events.h
index 5150436783e8..8912ccdb3d4b 100644
--- a/include/linux/trace_events.h
+++ b/include/linux/trace_events.h
@@ -211,6 +211,7 @@ struct trace_event_buffer {
void *entry;
unsigned long flags;
int pc;
+ struct pt_regs *regs;
};
void *trace_event_buffer_reserve(struct trace_event_buffer *fbuffer,
diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
index ead3a9faa584..605faf584164 100644
--- a/kernel/trace/trace.c
+++ b/kernel/trace/trace.c
@@ -2572,9 +2572,9 @@ void trace_event_buffer_commit(struct trace_event_buffer *fbuffer)
if (static_key_false(&tracepoint_printk_key.key))
output_printk(fbuffer);
- event_trigger_unlock_commit(fbuffer->trace_file, fbuffer->buffer,
+ event_trigger_unlock_commit_regs(fbuffer->trace_file, fbuffer->buffer,
fbuffer->event, fbuffer->entry,
- fbuffer->flags, fbuffer->pc);
+ fbuffer->flags, fbuffer->pc, fbuffer->regs);
}
EXPORT_SYMBOL_GPL(trace_event_buffer_commit);
diff --git a/kernel/trace/trace_events.c b/kernel/trace/trace_events.c
index c7506bc81b75..22cf08bd2317 100644
--- a/kernel/trace/trace_events.c
+++ b/kernel/trace/trace_events.c
@@ -271,6 +271,7 @@ void *trace_event_buffer_reserve(struct trace_event_buffer *fbuffer,
if (!fbuffer->event)
return NULL;
+ fbuffer->regs = NULL;
fbuffer->entry = ring_buffer_event_data(fbuffer->event);
return fbuffer->entry;
}
diff --git a/kernel/trace/trace_kprobe.c b/kernel/trace/trace_kprobe.c
index 9d483ad9bb6c..6c5145525f90 100644
--- a/kernel/trace/trace_kprobe.c
+++ b/kernel/trace/trace_kprobe.c
@@ -988,10 +988,8 @@ __kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs,
struct trace_event_file *trace_file)
{
struct kprobe_trace_entry_head *entry;
- struct ring_buffer_event *event;
- struct ring_buffer *buffer;
- int size, dsize, pc;
- unsigned long irq_flags;
+ struct trace_event_buffer fbuffer;
+ int dsize;
struct trace_event_call *call = trace_probe_event_call(&tk->tp);
WARN_ON(call != trace_file->event_call);
@@ -999,24 +997,26 @@ __kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs,
if (trace_trigger_soft_disabled(trace_file))
return;
- local_save_flags(irq_flags);
- pc = preempt_count();
+ local_save_flags(fbuffer.flags);
+ fbuffer.pc = preempt_count();
+ fbuffer.trace_file = trace_file;
dsize = __get_data_size(&tk->tp, regs);
- size = sizeof(*entry) + tk->tp.size + dsize;
- event = trace_event_buffer_lock_reserve(&buffer, trace_file,
- call->event.type,
- size, irq_flags, pc);
- if (!event)
+ fbuffer.event =
+ trace_event_buffer_lock_reserve(&fbuffer.buffer, trace_file,
+ call->event.type,
+ sizeof(*entry) + tk->tp.size + dsize,
+ fbuffer.flags, fbuffer.pc);
+ if (!fbuffer.event)
return;
- entry = ring_buffer_event_data(event);
+ fbuffer.regs = regs;
+ entry = fbuffer.entry = ring_buffer_event_data(fbuffer.event);
entry->ip = (unsigned long)tk->rp.kp.addr;
store_trace_args(&entry[1], &tk->tp, regs, sizeof(*entry), dsize);
- event_trigger_unlock_commit_regs(trace_file, buffer, event,
- entry, irq_flags, pc, regs);
+ trace_event_buffer_commit(&fbuffer);
}
static void
@@ -1036,10 +1036,8 @@ __kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
struct trace_event_file *trace_file)
{
struct kretprobe_trace_entry_head *entry;
- struct ring_buffer_event *event;
- struct ring_buffer *buffer;
- int size, pc, dsize;
- unsigned long irq_flags;
+ struct trace_event_buffer fbuffer;
+ int dsize;
struct trace_event_call *call = trace_probe_event_call(&tk->tp);
WARN_ON(call != trace_file->event_call);
@@ -1047,25 +1045,26 @@ __kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
if (trace_trigger_soft_disabled(trace_file))
return;
- local_save_flags(irq_flags);
- pc = preempt_count();
+ local_save_flags(fbuffer.flags);
+ fbuffer.pc = preempt_count();
+ fbuffer.trace_file = trace_file;
dsize = __get_data_size(&tk->tp, regs);
- size = sizeof(*entry) + tk->tp.size + dsize;
-
- event = trace_event_buffer_lock_reserve(&buffer, trace_file,
- call->event.type,
- size, irq_flags, pc);
- if (!event)
+ fbuffer.event =
+ trace_event_buffer_lock_reserve(&fbuffer.buffer, trace_file,
+ call->event.type,
+ sizeof(*entry) + tk->tp.size + dsize,
+ fbuffer.flags, fbuffer.pc);
+ if (!fbuffer.event)
return;
- entry = ring_buffer_event_data(event);
+ fbuffer.regs = regs;
+ entry = fbuffer.entry = ring_buffer_event_data(fbuffer.event);
entry->func = (unsigned long)tk->rp.kp.addr;
entry->ret_ip = (unsigned long)ri->ret_addr;
store_trace_args(&entry[1], &tk->tp, regs, sizeof(*entry), dsize);
- event_trigger_unlock_commit_regs(trace_file, buffer, event,
- entry, irq_flags, pc, regs);
+ trace_event_buffer_commit(&fbuffer);
}
static void
^ permalink raw reply related
* [RFC PATCH v3 07/19] tracing: Expose EXPORT_SYMBOL_GPL symbol
From: Masami Hiramatsu @ 2019-08-26 3:17 UTC (permalink / raw)
To: Steven Rostedt, Frank Rowand
Cc: Ingo Molnar, Namhyung Kim, Tim Bird, Jiri Olsa,
Arnaldo Carvalho de Melo, Tom Zanussi, Rob Herring, Andrew Morton,
Thomas Gleixner, Greg Kroah-Hartman, Alexey Dobriyan,
Jonathan Corbet, Linus Torvalds, linux-doc, linux-fsdevel,
linux-kernel
In-Reply-To: <156678933823.21459.4100380582025186209.stgit@devnote2>
Since ftrace_set_clr_event is already exported by EXPORT_SYMBOL_GPL,
it should not be static.
Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
---
kernel/trace/trace_events.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/kernel/trace/trace_events.c b/kernel/trace/trace_events.c
index 22cf08bd2317..c2d38048edae 100644
--- a/kernel/trace/trace_events.c
+++ b/kernel/trace/trace_events.c
@@ -788,7 +788,7 @@ static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
return ret;
}
-static int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
+int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
{
char *event = NULL, *sub = NULL, *match;
int ret;
^ permalink raw reply related
* [RFC PATCH v3 08/19] tracing: kprobes: Register to dynevent earlier stage
From: Masami Hiramatsu @ 2019-08-26 3:17 UTC (permalink / raw)
To: Steven Rostedt, Frank Rowand
Cc: Ingo Molnar, Namhyung Kim, Tim Bird, Jiri Olsa,
Arnaldo Carvalho de Melo, Tom Zanussi, Rob Herring, Andrew Morton,
Thomas Gleixner, Greg Kroah-Hartman, Alexey Dobriyan,
Jonathan Corbet, Linus Torvalds, linux-doc, linux-fsdevel,
linux-kernel
In-Reply-To: <156678933823.21459.4100380582025186209.stgit@devnote2>
Register kprobe event to dynevent in subsys_initcall level.
This will allow kernel to register new kprobe events in
fs_initcall level via trace_run_command.
Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
---
kernel/trace/trace_kprobe.c | 19 +++++++++++++++----
1 file changed, 15 insertions(+), 4 deletions(-)
diff --git a/kernel/trace/trace_kprobe.c b/kernel/trace/trace_kprobe.c
index 6c5145525f90..5135c07b6557 100644
--- a/kernel/trace/trace_kprobe.c
+++ b/kernel/trace/trace_kprobe.c
@@ -1484,11 +1484,12 @@ static __init void setup_boot_kprobe_events(void)
enable_boot_kprobe_events();
}
-/* Make a tracefs interface for controlling probe points */
-static __init int init_kprobe_trace(void)
+/*
+ * Register dynevent at subsys_initcall. This allows kernel to setup kprobe
+ * events in fs_initcall without tracefs.
+ */
+static __init int init_kprobe_trace_early(void)
{
- struct dentry *d_tracer;
- struct dentry *entry;
int ret;
ret = dyn_event_register(&trace_kprobe_ops);
@@ -1498,6 +1499,16 @@ static __init int init_kprobe_trace(void)
if (register_module_notifier(&trace_kprobe_module_nb))
return -EINVAL;
+ return 0;
+}
+subsys_initcall(init_kprobe_trace_early);
+
+/* Make a tracefs interface for controlling probe points */
+static __init int init_kprobe_trace(void)
+{
+ struct dentry *d_tracer;
+ struct dentry *entry;
+
d_tracer = tracing_init_dentry();
if (IS_ERR(d_tracer))
return 0;
^ permalink raw reply related
* [RFC PATCH v3 09/19] tracing: Accept different type for synthetic event fields
From: Masami Hiramatsu @ 2019-08-26 3:17 UTC (permalink / raw)
To: Steven Rostedt, Frank Rowand
Cc: Ingo Molnar, Namhyung Kim, Tim Bird, Jiri Olsa,
Arnaldo Carvalho de Melo, Tom Zanussi, Rob Herring, Andrew Morton,
Thomas Gleixner, Greg Kroah-Hartman, Alexey Dobriyan,
Jonathan Corbet, Linus Torvalds, linux-doc, linux-fsdevel,
linux-kernel
In-Reply-To: <156678933823.21459.4100380582025186209.stgit@devnote2>
Make the synthetic event accepts a different type field to record.
However, the size and signed flag must be same.
Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
---
kernel/trace/trace_events_hist.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
diff --git a/kernel/trace/trace_events_hist.c b/kernel/trace/trace_events_hist.c
index ca6b0dff60c5..a7f447195143 100644
--- a/kernel/trace/trace_events_hist.c
+++ b/kernel/trace/trace_events_hist.c
@@ -4063,8 +4063,11 @@ static int check_synth_field(struct synth_event *event,
field = event->fields[field_pos];
- if (strcmp(field->type, hist_field->type) != 0)
- return -EINVAL;
+ if (strcmp(field->type, hist_field->type) != 0) {
+ if (field->size != hist_field->size ||
+ field->is_signed != hist_field->is_signed)
+ return -EINVAL;
+ }
return 0;
}
^ permalink raw reply related
* [RFC PATCH v3 10/19] tracing: Add NULL trace-array check in print_synth_event()
From: Masami Hiramatsu @ 2019-08-26 3:17 UTC (permalink / raw)
To: Steven Rostedt, Frank Rowand
Cc: Ingo Molnar, Namhyung Kim, Tim Bird, Jiri Olsa,
Arnaldo Carvalho de Melo, Tom Zanussi, Rob Herring, Andrew Morton,
Thomas Gleixner, Greg Kroah-Hartman, Alexey Dobriyan,
Jonathan Corbet, Linus Torvalds, linux-doc, linux-fsdevel,
linux-kernel
In-Reply-To: <156678933823.21459.4100380582025186209.stgit@devnote2>
Add NULL trace-array check in print_synth_event(), because
if we enable tp_printk option, iter->tr can be NULL.
Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
---
kernel/trace/trace_events_hist.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/kernel/trace/trace_events_hist.c b/kernel/trace/trace_events_hist.c
index a7f447195143..db973928e580 100644
--- a/kernel/trace/trace_events_hist.c
+++ b/kernel/trace/trace_events_hist.c
@@ -822,7 +822,7 @@ static enum print_line_t print_synth_event(struct trace_iterator *iter,
fmt = synth_field_fmt(se->fields[i]->type);
/* parameter types */
- if (tr->trace_flags & TRACE_ITER_VERBOSE)
+ if (tr && tr->trace_flags & TRACE_ITER_VERBOSE)
trace_seq_printf(s, "%s ", fmt);
snprintf(print_fmt, sizeof(print_fmt), "%%s=%s%%s", fmt);
^ permalink raw reply related
* [RFC PATCH v3 11/19] tracing/boot: Add boot-time tracing by supplemental kernel cmdline
From: Masami Hiramatsu @ 2019-08-26 3:17 UTC (permalink / raw)
To: Steven Rostedt, Frank Rowand
Cc: Ingo Molnar, Namhyung Kim, Tim Bird, Jiri Olsa,
Arnaldo Carvalho de Melo, Tom Zanussi, Rob Herring, Andrew Morton,
Thomas Gleixner, Greg Kroah-Hartman, Alexey Dobriyan,
Jonathan Corbet, Linus Torvalds, linux-doc, linux-fsdevel,
linux-kernel
In-Reply-To: <156678933823.21459.4100380582025186209.stgit@devnote2>
Setup tracing options by supplemental kernel cmdline (skc) in addition
to kernel parameters. In this patch, add following commands support
- ftrace.options = OPT1[,OPT2...];
Enable given ftrace options.
- ftrace.trace_clock = CLOCK;
Set given CLOCK to ftrace's trace_clock.
- ftrace.dump_on_oops [= MODE];
Dump ftrace on Oops. If MODE = 1 or omitted, dump trace buffer
on all CPUs. If MODE = 2, dump a buffer on a CPU which kicks Oops.
- ftrace.traceoff_on_warning;
Stop tracing if WARN_ON() occurs.
- ftrace.tp_printk;
Output trace-event data on printk buffer too.
- ftrace.buffer_size = SIZE;
Configure ftrace buffer size to SIZE. You can use "KB" or "MB"
for that SIZE.
- ftrace.alloc_snapshot;
Allocate snapshot buffer.
- ftrace.events = EVENT[, EVENT2...];
Enable given events on boot. You can use a wild card in EVENT.
- ftrace.tracer = TRACER;
Set TRACER to current tracer on boot. (e.g. function)
Since the kernel parameter is limited length, sometimes there is
no space to setup the tracing options. This will read the tracing
options from skc "ftrace" prefix and setup tracers at boot.
Note that this is not replacing the kernel parameters, because
this skc based setting is later than that. If you want to
trace earlier boot events, you still need kernel parameters.
Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
---
kernel/trace/Kconfig | 9 +++
kernel/trace/Makefile | 1
kernel/trace/trace.c | 38 ++++++++----
kernel/trace/trace_boot.c | 137 +++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 172 insertions(+), 13 deletions(-)
create mode 100644 kernel/trace/trace_boot.c
diff --git a/kernel/trace/Kconfig b/kernel/trace/Kconfig
index 98da8998c25c..0f831adb4e4a 100644
--- a/kernel/trace/Kconfig
+++ b/kernel/trace/Kconfig
@@ -797,6 +797,15 @@ config GCOV_PROFILE_FTRACE
Note that on a kernel compiled with this config, ftrace will
run significantly slower.
+config BOOTTIME_TRACING
+ bool "Boot-time Tracing support"
+ depends on SKC && TRACING
+ default y
+ help
+ Enable developer to setup ftrace subsystem via supplemental
+ kernel cmdline at boot time for debugging (tracing) driver
+ initialization and boot process.
+
endif # FTRACE
endif # TRACING_SUPPORT
diff --git a/kernel/trace/Makefile b/kernel/trace/Makefile
index c2b2148bb1d2..f9d3c2c72fb5 100644
--- a/kernel/trace/Makefile
+++ b/kernel/trace/Makefile
@@ -82,6 +82,7 @@ endif
obj-$(CONFIG_DYNAMIC_EVENTS) += trace_dynevent.o
obj-$(CONFIG_PROBE_EVENTS) += trace_probe.o
obj-$(CONFIG_UPROBE_EVENTS) += trace_uprobe.o
+obj-$(CONFIG_BOOTTIME_TRACING) += trace_boot.o
obj-$(CONFIG_TRACEPOINT_BENCHMARK) += trace_benchmark.o
diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
index 605faf584164..69400a87e48f 100644
--- a/kernel/trace/trace.c
+++ b/kernel/trace/trace.c
@@ -158,7 +158,7 @@ union trace_eval_map_item {
static union trace_eval_map_item *trace_eval_maps;
#endif /* CONFIG_TRACE_EVAL_MAP_FILE */
-static int tracing_set_tracer(struct trace_array *tr, const char *buf);
+int tracing_set_tracer(struct trace_array *tr, const char *buf);
static void ftrace_trace_userstack(struct ring_buffer *buffer,
unsigned long flags, int pc);
@@ -178,6 +178,11 @@ static int __init set_cmdline_ftrace(char *str)
}
__setup("ftrace=", set_cmdline_ftrace);
+void __init trace_init_dump_on_oops(int mode)
+{
+ ftrace_dump_on_oops = mode;
+}
+
static int __init set_ftrace_dump_on_oops(char *str)
{
if (*str++ != '=' || !*str) {
@@ -4614,7 +4619,7 @@ int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled)
return 0;
}
-static int trace_set_options(struct trace_array *tr, char *option)
+int trace_set_options(struct trace_array *tr, char *option)
{
char *cmp;
int neg = 0;
@@ -5505,8 +5510,8 @@ static int __tracing_resize_ring_buffer(struct trace_array *tr,
return ret;
}
-static ssize_t tracing_resize_ring_buffer(struct trace_array *tr,
- unsigned long size, int cpu_id)
+ssize_t tracing_resize_ring_buffer(struct trace_array *tr,
+ unsigned long size, int cpu_id)
{
int ret = size;
@@ -5585,7 +5590,7 @@ static void add_tracer_options(struct trace_array *tr, struct tracer *t)
create_trace_option_files(tr, t);
}
-static int tracing_set_tracer(struct trace_array *tr, const char *buf)
+int tracing_set_tracer(struct trace_array *tr, const char *buf)
{
struct tracer *t;
#ifdef CONFIG_TRACER_MAX_TRACE
@@ -9170,16 +9175,23 @@ __init static int tracer_alloc_buffers(void)
return ret;
}
+void __init trace_init_tracepoint_printk(void)
+{
+ tracepoint_printk = 1;
+
+ tracepoint_print_iter =
+ kmalloc(sizeof(*tracepoint_print_iter), GFP_KERNEL);
+ if (WARN_ON(!tracepoint_print_iter))
+ tracepoint_printk = 0;
+ else
+ static_key_enable(&tracepoint_printk_key.key);
+}
+
void __init early_trace_init(void)
{
- if (tracepoint_printk) {
- tracepoint_print_iter =
- kmalloc(sizeof(*tracepoint_print_iter), GFP_KERNEL);
- if (WARN_ON(!tracepoint_print_iter))
- tracepoint_printk = 0;
- else
- static_key_enable(&tracepoint_printk_key.key);
- }
+ if (tracepoint_printk)
+ trace_init_tracepoint_printk();
+
tracer_alloc_buffers();
}
diff --git a/kernel/trace/trace_boot.c b/kernel/trace/trace_boot.c
new file mode 100644
index 000000000000..cc5e81368065
--- /dev/null
+++ b/kernel/trace/trace_boot.c
@@ -0,0 +1,137 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * trace_boot.c
+ * Tracing kernel boot-time
+ */
+
+#define pr_fmt(fmt) "trace_boot: " fmt
+
+#include <linux/ftrace.h>
+#include <linux/init.h>
+#include <linux/skc.h>
+
+#include "trace.h"
+
+#define MAX_BUF_LEN 256
+
+extern int trace_set_options(struct trace_array *tr, char *option);
+extern enum ftrace_dump_mode ftrace_dump_on_oops;
+extern int __disable_trace_on_warning;
+extern int tracing_set_tracer(struct trace_array *tr, const char *buf);
+extern void __init trace_init_tracepoint_printk(void);
+extern ssize_t tracing_resize_ring_buffer(struct trace_array *tr,
+ unsigned long size, int cpu_id);
+
+static void __init
+trace_boot_set_ftrace_options(struct trace_array *tr, struct skc_node *node)
+{
+ struct skc_node *anode;
+ const char *p;
+ char buf[MAX_BUF_LEN];
+ unsigned long v = 0;
+ int err;
+
+ /* Common ftrace options */
+ skc_node_for_each_array_value(node, "options", anode, p) {
+ if (strlcpy(buf, p, ARRAY_SIZE(buf)) >= ARRAY_SIZE(buf)) {
+ pr_err("String is too long: %s\n", p);
+ continue;
+ }
+
+ if (trace_set_options(tr, buf) < 0)
+ pr_err("Failed to set option: %s\n", buf);
+ }
+
+ p = skc_node_find_value(node, "trace_clock", NULL);
+ if (p && *p != '\0') {
+ if (tracing_set_clock(tr, p) < 0)
+ pr_err("Failed to set trace clock: %s\n", p);
+ }
+
+ /* Command line boot options */
+ p = skc_node_find_value(node, "dump_on_oops", NULL);
+ if (p) {
+ err = kstrtoul(p, 0, &v);
+ if (err || v == 1)
+ ftrace_dump_on_oops = DUMP_ALL;
+ else if (!err && v == 2)
+ ftrace_dump_on_oops = DUMP_ORIG;
+ }
+
+ if (skc_node_find_value(node, "traceoff_on_warning", NULL))
+ __disable_trace_on_warning = 1;
+
+ if (skc_node_find_value(node, "tp_printk", NULL))
+ trace_init_tracepoint_printk();
+
+ p = skc_node_find_value(node, "buffer_size", NULL);
+ if (p && *p != '\0') {
+ v = memparse(p, NULL);
+ if (v < PAGE_SIZE)
+ pr_err("Buffer size is too small: %s\n", p);
+ if (tracing_resize_ring_buffer(tr, v, RING_BUFFER_ALL_CPUS) < 0)
+ pr_err("Failed to resize trace buffer to %s\n", p);
+ }
+
+ if (skc_node_find_value(node, "alloc_snapshot", NULL))
+ if (tracing_alloc_snapshot() < 0)
+ pr_err("Failed to allocate snapshot buffer\n");
+}
+
+#ifdef CONFIG_EVENT_TRACING
+extern int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set);
+
+static void __init
+trace_boot_enable_events(struct trace_array *tr, struct skc_node *node)
+{
+ struct skc_node *anode;
+ char buf[MAX_BUF_LEN];
+ const char *p;
+
+ skc_node_for_each_array_value(node, "events", anode, p) {
+ if (strlcpy(buf, p, ARRAY_SIZE(buf)) >= ARRAY_SIZE(buf)) {
+ pr_err("String is too long: %s\n", p);
+ continue;
+ }
+
+ if (ftrace_set_clr_event(tr, buf, 1) < 0)
+ pr_err("Failed to enable event: %s\n", p);
+ }
+}
+#else
+#define trace_boot_enable_events(tr, node) do {} while (0)
+#endif
+
+static void __init
+trace_boot_enable_tracer(struct trace_array *tr, struct skc_node *node)
+{
+ const char *p;
+
+ p = skc_node_find_value(node, "tracer", NULL);
+ if (p && *p != '\0') {
+ if (tracing_set_tracer(tr, p) < 0)
+ pr_err("Failed to set given tracer: %s\n", p);
+ }
+}
+
+static int __init trace_boot_init(void)
+{
+ struct skc_node *trace_node;
+ struct trace_array *tr;
+
+ trace_node = skc_find_node("ftrace");
+ if (!trace_node)
+ return 0;
+
+ tr = top_trace_array();
+ if (!tr)
+ return 0;
+
+ trace_boot_set_ftrace_options(tr, trace_node);
+ trace_boot_enable_events(tr, trace_node);
+ trace_boot_enable_tracer(tr, trace_node);
+
+ return 0;
+}
+
+fs_initcall(trace_boot_init);
^ permalink raw reply related
* [RFC PATCH v3 12/19] tracing/boot: Add per-event settings
From: Masami Hiramatsu @ 2019-08-26 3:18 UTC (permalink / raw)
To: Steven Rostedt, Frank Rowand
Cc: Ingo Molnar, Namhyung Kim, Tim Bird, Jiri Olsa,
Arnaldo Carvalho de Melo, Tom Zanussi, Rob Herring, Andrew Morton,
Thomas Gleixner, Greg Kroah-Hartman, Alexey Dobriyan,
Jonathan Corbet, Linus Torvalds, linux-doc, linux-fsdevel,
linux-kernel
In-Reply-To: <156678933823.21459.4100380582025186209.stgit@devnote2>
Add per-event settings for boottime tracing. User can set filter,
actions and enable on each event on boot. The event entries are
under ftrace.event.GROUP.EVENT node (note that the option key
includes event's group name and event name.) This supports below
commands.
- ftrace.event.GROUP.EVENT.enable;
Enables GROUP:EVENT tracing.
- ftrace.event.GROUP.EVENT.filter = FILTER;
Set FILTER rule to the GROUP:EVENT.
- ftrace.event.GROUP.EVENT.actions = ACTION[, ACTION2...];
Set ACTIONs to the GROUP:EVENT.
For example,
ftrace.event.sched.sched_process_exec {
filter = "pid < 128";
enable;
}
this will enable tracing "sched:sched_process_exec" event
with "pid < 128" filter.
Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
---
kernel/trace/trace_boot.c | 60 +++++++++++++++++++++++++++++++++++
kernel/trace/trace_events_trigger.c | 2 +
2 files changed, 61 insertions(+), 1 deletion(-)
diff --git a/kernel/trace/trace_boot.c b/kernel/trace/trace_boot.c
index cc5e81368065..cfd628c16761 100644
--- a/kernel/trace/trace_boot.c
+++ b/kernel/trace/trace_boot.c
@@ -80,6 +80,7 @@ trace_boot_set_ftrace_options(struct trace_array *tr, struct skc_node *node)
#ifdef CONFIG_EVENT_TRACING
extern int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set);
+extern int trigger_process_regex(struct trace_event_file *file, char *buff);
static void __init
trace_boot_enable_events(struct trace_array *tr, struct skc_node *node)
@@ -98,8 +99,66 @@ trace_boot_enable_events(struct trace_array *tr, struct skc_node *node)
pr_err("Failed to enable event: %s\n", p);
}
}
+
+static void __init
+trace_boot_init_one_event(struct trace_array *tr, struct skc_node *gnode,
+ struct skc_node *enode)
+{
+ struct trace_event_file *file;
+ struct skc_node *anode;
+ char buf[MAX_BUF_LEN];
+ const char *p, *group, *event;
+
+ group = skc_node_get_data(gnode);
+ event = skc_node_get_data(enode);
+
+ mutex_lock(&event_mutex);
+ file = find_event_file(tr, group, event);
+ if (!file) {
+ pr_err("Failed to find event: %s:%s\n", group, event);
+ goto out;
+ }
+
+ p = skc_node_find_value(enode, "filter", NULL);
+ if (p && *p != '\0') {
+ if (strlcpy(buf, p, ARRAY_SIZE(buf)) >= ARRAY_SIZE(buf))
+ pr_err("filter string is too long: %s\n", p);
+ else if (apply_event_filter(file, buf) < 0)
+ pr_err("Failed to apply filter: %s\n", buf);
+ }
+
+ skc_node_for_each_array_value(enode, "actions", anode, p) {
+ if (strlcpy(buf, p, ARRAY_SIZE(buf)) >= ARRAY_SIZE(buf))
+ pr_err("action string is too long: %s\n", p);
+ else if (trigger_process_regex(file, buf) < 0)
+ pr_err("Failed to apply an action: %s\n", buf);
+ }
+
+ if (skc_node_find_value(enode, "enable", NULL)) {
+ if (trace_event_enable_disable(file, 1, 0) < 0)
+ pr_err("Failed to enable event node: %s:%s\n",
+ group, event);
+ }
+out:
+ mutex_unlock(&event_mutex);
+}
+
+static void __init
+trace_boot_init_events(struct trace_array *tr, struct skc_node *node)
+{
+ struct skc_node *gnode, *enode;
+
+ node = skc_node_find_child(node, "event");
+ if (!node)
+ return;
+ /* per-event key starts with "event.GROUP.EVENT" */
+ skc_node_for_each_child(node, gnode)
+ skc_node_for_each_child(gnode, enode)
+ trace_boot_init_one_event(tr, gnode, enode);
+}
#else
#define trace_boot_enable_events(tr, node) do {} while (0)
+#define trace_boot_init_events(tr, node) do {} while (0)
#endif
static void __init
@@ -128,6 +187,7 @@ static int __init trace_boot_init(void)
return 0;
trace_boot_set_ftrace_options(tr, trace_node);
+ trace_boot_init_events(tr, trace_node);
trace_boot_enable_events(tr, trace_node);
trace_boot_enable_tracer(tr, trace_node);
diff --git a/kernel/trace/trace_events_trigger.c b/kernel/trace/trace_events_trigger.c
index 2a2912cb4533..74a19c18219f 100644
--- a/kernel/trace/trace_events_trigger.c
+++ b/kernel/trace/trace_events_trigger.c
@@ -208,7 +208,7 @@ static int event_trigger_regex_open(struct inode *inode, struct file *file)
return ret;
}
-static int trigger_process_regex(struct trace_event_file *file, char *buff)
+int trigger_process_regex(struct trace_event_file *file, char *buff)
{
char *command, *next = buff;
struct event_command *p;
^ permalink raw reply related
* [RFC PATCH v3 13/19] tracing/boot Add kprobe event support
From: Masami Hiramatsu @ 2019-08-26 3:18 UTC (permalink / raw)
To: Steven Rostedt, Frank Rowand
Cc: Ingo Molnar, Namhyung Kim, Tim Bird, Jiri Olsa,
Arnaldo Carvalho de Melo, Tom Zanussi, Rob Herring, Andrew Morton,
Thomas Gleixner, Greg Kroah-Hartman, Alexey Dobriyan,
Jonathan Corbet, Linus Torvalds, linux-doc, linux-fsdevel,
linux-kernel
In-Reply-To: <156678933823.21459.4100380582025186209.stgit@devnote2>
Add kprobe event support on event node. If the group name of event
is "kprobes", boottime trace defines new probe event according
to "probes" values.
- ftrace.event.kprobes.EVENT.probes = PROBE[, PROBE2...];
Defines new kprobe event based on PROBEs. It is able to define
multiple probes on one event, but those must have same type of
arguments.
For example,
ftrace.events.kprobes.myevent {
probes = "vfs_read $arg1 $arg2";
enable;
}
This will add kprobes:myevent on vfs_read with the 1st and the 2nd
arguments.
Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
---
kernel/trace/trace_boot.c | 46 +++++++++++++++++++++++++++++++++++++++++++
kernel/trace/trace_kprobe.c | 5 +++++
2 files changed, 51 insertions(+)
diff --git a/kernel/trace/trace_boot.c b/kernel/trace/trace_boot.c
index cfd628c16761..40c89c7ceee0 100644
--- a/kernel/trace/trace_boot.c
+++ b/kernel/trace/trace_boot.c
@@ -100,6 +100,48 @@ trace_boot_enable_events(struct trace_array *tr, struct skc_node *node)
}
}
+#ifdef CONFIG_KPROBE_EVENTS
+extern int trace_kprobe_run_command(const char *command);
+
+static int __init
+trace_boot_add_kprobe_event(struct skc_node *node, const char *event)
+{
+ struct skc_node *anode;
+ char buf[MAX_BUF_LEN];
+ const char *val;
+ char *p;
+ int len;
+
+ len = snprintf(buf, ARRAY_SIZE(buf) - 1, "p:kprobes/%s ", event);
+ if (len >= ARRAY_SIZE(buf)) {
+ pr_err("Event name is too long: %s\n", event);
+ return -E2BIG;
+ }
+ p = buf + len;
+ len = ARRAY_SIZE(buf) - len;
+
+ skc_node_for_each_array_value(node, "probes", anode, val) {
+ if (strlcpy(p, val, len) >= len) {
+ pr_err("Probe definition is too long: %s\n", val);
+ return -E2BIG;
+ }
+ if (trace_kprobe_run_command(buf) < 0) {
+ pr_err("Failed to add probe: %s\n", buf);
+ return -EINVAL;
+ }
+ }
+
+ return 0;
+}
+#else
+static inline int __init
+trace_boot_add_kprobe_event(struct skc_node *node, const char *event)
+{
+ pr_err("Kprobe event is not supported.\n");
+ return -ENOTSUPP;
+}
+#endif
+
static void __init
trace_boot_init_one_event(struct trace_array *tr, struct skc_node *gnode,
struct skc_node *enode)
@@ -112,6 +154,10 @@ trace_boot_init_one_event(struct trace_array *tr, struct skc_node *gnode,
group = skc_node_get_data(gnode);
event = skc_node_get_data(enode);
+ if (!strcmp(group, "kprobes"))
+ if (trace_boot_add_kprobe_event(enode, event) < 0)
+ return;
+
mutex_lock(&event_mutex);
file = find_event_file(tr, group, event);
if (!file) {
diff --git a/kernel/trace/trace_kprobe.c b/kernel/trace/trace_kprobe.c
index 5135c07b6557..03ce60928c18 100644
--- a/kernel/trace/trace_kprobe.c
+++ b/kernel/trace/trace_kprobe.c
@@ -728,6 +728,11 @@ static int create_or_delete_trace_kprobe(int argc, char **argv)
return ret == -ECANCELED ? -EINVAL : ret;
}
+int trace_kprobe_run_command(const char *command)
+{
+ return trace_run_command(command, create_or_delete_trace_kprobe);
+}
+
static int trace_kprobe_release(struct dyn_event *ev)
{
struct trace_kprobe *tk = to_trace_kprobe(ev);
^ permalink raw reply related
* [RFC PATCH v3 14/19] tracing/boot: Add synthetic event support
From: Masami Hiramatsu @ 2019-08-26 3:18 UTC (permalink / raw)
To: Steven Rostedt, Frank Rowand
Cc: Ingo Molnar, Namhyung Kim, Tim Bird, Jiri Olsa,
Arnaldo Carvalho de Melo, Tom Zanussi, Rob Herring, Andrew Morton,
Thomas Gleixner, Greg Kroah-Hartman, Alexey Dobriyan,
Jonathan Corbet, Linus Torvalds, linux-doc, linux-fsdevel,
linux-kernel
In-Reply-To: <156678933823.21459.4100380582025186209.stgit@devnote2>
Add synthetic event node support. The synthetic event is a kind of
event node, but the group name is "synthetic".
- ftrace.event.synthetic.EVENT.fields = FIELD[, FIELD2...];
Defines new synthetic event with FIELDs. Each field should be
"type varname".
The synthetic node requires "fields" string arraies, which defines
the fields as same as tracing/synth_events interface.
Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
---
kernel/trace/trace_boot.c | 47 ++++++++++++++++++++++++++++++++++++++
kernel/trace/trace_events_hist.c | 5 ++++
2 files changed, 52 insertions(+)
diff --git a/kernel/trace/trace_boot.c b/kernel/trace/trace_boot.c
index 40c89c7ceee0..2e9fddff660f 100644
--- a/kernel/trace/trace_boot.c
+++ b/kernel/trace/trace_boot.c
@@ -142,6 +142,50 @@ trace_boot_add_kprobe_event(struct skc_node *node, const char *event)
}
#endif
+#ifdef CONFIG_HIST_TRIGGERS
+extern int synth_event_run_command(const char *command);
+
+static int __init
+trace_boot_add_synth_event(struct skc_node *node, const char *event)
+{
+ struct skc_node *anode;
+ char buf[MAX_BUF_LEN], *q;
+ const char *p;
+ int len, delta, ret;
+
+ len = ARRAY_SIZE(buf);
+ delta = snprintf(buf, len, "%s", event);
+ if (delta >= len) {
+ pr_err("Event name is too long: %s\n", event);
+ return -E2BIG;
+ }
+ len -= delta; q = buf + delta;
+
+ skc_node_for_each_array_value(node, "fields", anode, p) {
+ delta = snprintf(q, len, " %s;", p);
+ if (delta >= len) {
+ pr_err("fields string is too long: %s\n", p);
+ return -E2BIG;
+ }
+ len -= delta; q += delta;
+ }
+
+ ret = synth_event_run_command(buf);
+ if (ret < 0)
+ pr_err("Failed to add synthetic event: %s\n", buf);
+
+
+ return ret;
+}
+#else
+static inline int __init
+trace_boot_add_synth_event(struct skc_node *node, const char *event)
+{
+ pr_err("Synthetic event is not supported.\n");
+ return -ENOTSUPP;
+}
+#endif
+
static void __init
trace_boot_init_one_event(struct trace_array *tr, struct skc_node *gnode,
struct skc_node *enode)
@@ -157,6 +201,9 @@ trace_boot_init_one_event(struct trace_array *tr, struct skc_node *gnode,
if (!strcmp(group, "kprobes"))
if (trace_boot_add_kprobe_event(enode, event) < 0)
return;
+ if (!strcmp(group, "synthetic"))
+ if (trace_boot_add_synth_event(enode, event) < 0)
+ return;
mutex_lock(&event_mutex);
file = find_event_file(tr, group, event);
diff --git a/kernel/trace/trace_events_hist.c b/kernel/trace/trace_events_hist.c
index db973928e580..e7f5d0a353e2 100644
--- a/kernel/trace/trace_events_hist.c
+++ b/kernel/trace/trace_events_hist.c
@@ -1343,6 +1343,11 @@ static int create_or_delete_synth_event(int argc, char **argv)
return ret == -ECANCELED ? -EINVAL : ret;
}
+int synth_event_run_command(const char *command)
+{
+ return trace_run_command(command, create_or_delete_synth_event);
+}
+
static int synth_event_create(int argc, const char **argv)
{
const char *name = argv[0];
^ permalink raw reply related
* [RFC PATCH v3 15/19] tracing/boot: Add instance node support
From: Masami Hiramatsu @ 2019-08-26 3:18 UTC (permalink / raw)
To: Steven Rostedt, Frank Rowand
Cc: Ingo Molnar, Namhyung Kim, Tim Bird, Jiri Olsa,
Arnaldo Carvalho de Melo, Tom Zanussi, Rob Herring, Andrew Morton,
Thomas Gleixner, Greg Kroah-Hartman, Alexey Dobriyan,
Jonathan Corbet, Linus Torvalds, linux-doc, linux-fsdevel,
linux-kernel
In-Reply-To: <156678933823.21459.4100380582025186209.stgit@devnote2>
Add instance node support to boottime tracing. User can set
some options and event nodes under instance node.
- ftrace.instance.INSTANCE[...];
Add new INSTANCE instance. Some options and event nodes
are acceptable for instance node.
Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
---
kernel/trace/trace_boot.c | 72 ++++++++++++++++++++++++++++++++++++---------
1 file changed, 57 insertions(+), 15 deletions(-)
diff --git a/kernel/trace/trace_boot.c b/kernel/trace/trace_boot.c
index 2e9fddff660f..74cffecd12a4 100644
--- a/kernel/trace/trace_boot.c
+++ b/kernel/trace/trace_boot.c
@@ -21,15 +21,15 @@ extern int tracing_set_tracer(struct trace_array *tr, const char *buf);
extern void __init trace_init_tracepoint_printk(void);
extern ssize_t tracing_resize_ring_buffer(struct trace_array *tr,
unsigned long size, int cpu_id);
+extern struct trace_array *trace_array_create(const char *name);
static void __init
-trace_boot_set_ftrace_options(struct trace_array *tr, struct skc_node *node)
+trace_boot_set_instance_options(struct trace_array *tr, struct skc_node *node)
{
struct skc_node *anode;
const char *p;
char buf[MAX_BUF_LEN];
unsigned long v = 0;
- int err;
/* Common ftrace options */
skc_node_for_each_array_value(node, "options", anode, p) {
@@ -48,6 +48,23 @@ trace_boot_set_ftrace_options(struct trace_array *tr, struct skc_node *node)
pr_err("Failed to set trace clock: %s\n", p);
}
+ p = skc_node_find_value(node, "buffer_size", NULL);
+ if (p && *p != '\0') {
+ v = memparse(p, NULL);
+ if (v < PAGE_SIZE)
+ pr_err("Buffer size is too small: %s\n", p);
+ if (tracing_resize_ring_buffer(tr, v, RING_BUFFER_ALL_CPUS) < 0)
+ pr_err("Failed to resize trace buffer to %s\n", p);
+ }
+}
+
+static void __init
+trace_boot_set_global_options(struct trace_array *tr, struct skc_node *node)
+{
+ unsigned long v = 0;
+ const char *p;
+ int err;
+
/* Command line boot options */
p = skc_node_find_value(node, "dump_on_oops", NULL);
if (p) {
@@ -64,15 +81,6 @@ trace_boot_set_ftrace_options(struct trace_array *tr, struct skc_node *node)
if (skc_node_find_value(node, "tp_printk", NULL))
trace_init_tracepoint_printk();
- p = skc_node_find_value(node, "buffer_size", NULL);
- if (p && *p != '\0') {
- v = memparse(p, NULL);
- if (v < PAGE_SIZE)
- pr_err("Buffer size is too small: %s\n", p);
- if (tracing_resize_ring_buffer(tr, v, RING_BUFFER_ALL_CPUS) < 0)
- pr_err("Failed to resize trace buffer to %s\n", p);
- }
-
if (skc_node_find_value(node, "alloc_snapshot", NULL))
if (tracing_alloc_snapshot() < 0)
pr_err("Failed to allocate snapshot buffer\n");
@@ -266,6 +274,40 @@ trace_boot_enable_tracer(struct trace_array *tr, struct skc_node *node)
}
}
+static void __init
+trace_boot_init_one_instance(struct trace_array *tr, struct skc_node *node)
+{
+ trace_boot_set_instance_options(tr, node);
+ trace_boot_init_events(tr, node);
+ trace_boot_enable_events(tr, node);
+ trace_boot_enable_tracer(tr, node);
+}
+
+static void __init
+trace_boot_init_instances(struct skc_node *node)
+{
+ struct skc_node *inode;
+ struct trace_array *tr;
+ const char *p;
+
+ node = skc_node_find_child(node, "instance");
+ if (!node)
+ return;
+
+ skc_node_for_each_child(node, inode) {
+ p = skc_node_get_data(inode);
+ if (!p || *p == '\0')
+ continue;
+
+ tr = trace_array_create(p);
+ if (IS_ERR(tr)) {
+ pr_err("Failed to create instance %s\n", p);
+ continue;
+ }
+ trace_boot_init_one_instance(tr, inode);
+ }
+}
+
static int __init trace_boot_init(void)
{
struct skc_node *trace_node;
@@ -279,10 +321,10 @@ static int __init trace_boot_init(void)
if (!tr)
return 0;
- trace_boot_set_ftrace_options(tr, trace_node);
- trace_boot_init_events(tr, trace_node);
- trace_boot_enable_events(tr, trace_node);
- trace_boot_enable_tracer(tr, trace_node);
+ trace_boot_set_global_options(tr, trace_node);
+ /* Global trace array is also one instance */
+ trace_boot_init_one_instance(tr, trace_node);
+ trace_boot_init_instances(trace_node);
return 0;
}
^ permalink raw reply related
* [RFC PATCH v3 16/19] tracing/boot: Add cpu_mask option support
From: Masami Hiramatsu @ 2019-08-26 3:18 UTC (permalink / raw)
To: Steven Rostedt, Frank Rowand
Cc: Ingo Molnar, Namhyung Kim, Tim Bird, Jiri Olsa,
Arnaldo Carvalho de Melo, Tom Zanussi, Rob Herring, Andrew Morton,
Thomas Gleixner, Greg Kroah-Hartman, Alexey Dobriyan,
Jonathan Corbet, Linus Torvalds, linux-doc, linux-fsdevel,
linux-kernel
In-Reply-To: <156678933823.21459.4100380582025186209.stgit@devnote2>
Add ftrace.cpumask option support for setting trace
cpumask.
- ftrace.[instance.INSTANCE.]cpumask = CPUMASK;
Set the trace cpumask. Note that the CPUMASK should be a string
which <tracefs>/tracing_cpumask can accepts.
Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
---
kernel/trace/trace.c | 41 ++++++++++++++++++++++++++++-------------
kernel/trace/trace_boot.c | 14 ++++++++++++++
2 files changed, 42 insertions(+), 13 deletions(-)
diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
index 69400a87e48f..bfe513e472d2 100644
--- a/kernel/trace/trace.c
+++ b/kernel/trace/trace.c
@@ -4437,20 +4437,12 @@ tracing_cpumask_read(struct file *filp, char __user *ubuf,
return count;
}
-static ssize_t
-tracing_cpumask_write(struct file *filp, const char __user *ubuf,
- size_t count, loff_t *ppos)
+int tracing_set_cpumask(struct trace_array *tr, cpumask_var_t tracing_cpumask_new)
{
- struct trace_array *tr = file_inode(filp)->i_private;
- cpumask_var_t tracing_cpumask_new;
- int err, cpu;
-
- if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
- return -ENOMEM;
+ int cpu;
- err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
- if (err)
- goto err_unlock;
+ if (!tr)
+ return -EINVAL;
local_irq_disable();
arch_spin_lock(&tr->max_lock);
@@ -4474,11 +4466,34 @@ tracing_cpumask_write(struct file *filp, const char __user *ubuf,
local_irq_enable();
cpumask_copy(tr->tracing_cpumask, tracing_cpumask_new);
+
+ return 0;
+}
+
+static ssize_t
+tracing_cpumask_write(struct file *filp, const char __user *ubuf,
+ size_t count, loff_t *ppos)
+{
+ struct trace_array *tr = file_inode(filp)->i_private;
+ cpumask_var_t tracing_cpumask_new;
+ int err;
+
+ if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
+ return -ENOMEM;
+
+ err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
+ if (err)
+ goto err_free;
+
+ err = tracing_set_cpumask(tr, tracing_cpumask_new);
+ if (err)
+ goto err_free;
+
free_cpumask_var(tracing_cpumask_new);
return count;
-err_unlock:
+err_free:
free_cpumask_var(tracing_cpumask_new);
return err;
diff --git a/kernel/trace/trace_boot.c b/kernel/trace/trace_boot.c
index 74cffecd12a4..755a2040aebf 100644
--- a/kernel/trace/trace_boot.c
+++ b/kernel/trace/trace_boot.c
@@ -22,6 +22,8 @@ extern void __init trace_init_tracepoint_printk(void);
extern ssize_t tracing_resize_ring_buffer(struct trace_array *tr,
unsigned long size, int cpu_id);
extern struct trace_array *trace_array_create(const char *name);
+extern int tracing_set_cpumask(struct trace_array *tr,
+ cpumask_var_t tracing_cpumask_new);
static void __init
trace_boot_set_instance_options(struct trace_array *tr, struct skc_node *node)
@@ -56,6 +58,18 @@ trace_boot_set_instance_options(struct trace_array *tr, struct skc_node *node)
if (tracing_resize_ring_buffer(tr, v, RING_BUFFER_ALL_CPUS) < 0)
pr_err("Failed to resize trace buffer to %s\n", p);
}
+
+ p = skc_node_find_value(node, "cpumask", NULL);
+ if (p && *p != '\0') {
+ cpumask_var_t new_mask;
+
+ if (alloc_cpumask_var(&new_mask, GFP_KERNEL)) {
+ if (cpumask_parse(p, new_mask) < 0 ||
+ tracing_set_cpumask(tr, new_mask) < 0)
+ pr_err("Failed to set new CPU mask %s\n", p);
+ free_cpumask_var(new_mask);
+ }
+ }
}
static void __init
^ permalink raw reply related
* [RFC PATCH v3 17/19] tracing/boot: Add function tracer filter options
From: Masami Hiramatsu @ 2019-08-26 3:18 UTC (permalink / raw)
To: Steven Rostedt, Frank Rowand
Cc: Ingo Molnar, Namhyung Kim, Tim Bird, Jiri Olsa,
Arnaldo Carvalho de Melo, Tom Zanussi, Rob Herring, Andrew Morton,
Thomas Gleixner, Greg Kroah-Hartman, Alexey Dobriyan,
Jonathan Corbet, Linus Torvalds, linux-doc, linux-fsdevel,
linux-kernel
In-Reply-To: <156678933823.21459.4100380582025186209.stgit@devnote2>
Add below function-tracer filter options to boottime tracer.
- ftrace.[instance.INSTANCE.]ftrace.filters
This will take an array of tracing function filter rules
- ftrace.[instance.INSTANCE.]ftrace.notraces
This will take an array of NON-tracing function filter rules
Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
---
kernel/trace/trace_boot.c | 40 ++++++++++++++++++++++++++++++++++++++++
1 file changed, 40 insertions(+)
diff --git a/kernel/trace/trace_boot.c b/kernel/trace/trace_boot.c
index 755a2040aebf..942ca8d3fcc8 100644
--- a/kernel/trace/trace_boot.c
+++ b/kernel/trace/trace_boot.c
@@ -276,11 +276,51 @@ trace_boot_init_events(struct trace_array *tr, struct skc_node *node)
#define trace_boot_init_events(tr, node) do {} while (0)
#endif
+#ifdef CONFIG_FUNCTION_TRACER
+extern bool ftrace_filter_param __initdata;
+extern int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
+ int len, int reset);
+extern int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
+ int len, int reset);
+static void __init
+trace_boot_set_ftrace_filter(struct trace_array *tr, struct skc_node *node)
+{
+ struct skc_node *anode;
+ const char *p;
+ char *q;
+
+ skc_node_for_each_array_value(node, "ftrace.filters", anode, p) {
+ q = kstrdup(p, GFP_KERNEL);
+ if (!q)
+ return;
+ if (ftrace_set_filter(tr->ops, q, strlen(q), 0) < 0)
+ pr_err("Failed to add %s to ftrace filter\n", p);
+ else
+ ftrace_filter_param = true;
+ kfree(q);
+ }
+ skc_node_for_each_array_value(node, "ftrace.notraces", anode, p) {
+ q = kstrdup(p, GFP_KERNEL);
+ if (!q)
+ return;
+ if (ftrace_set_notrace(tr->ops, q, strlen(q), 0) < 0)
+ pr_err("Failed to add %s to ftrace filter\n", p);
+ else
+ ftrace_filter_param = true;
+ kfree(q);
+ }
+}
+#else
+#define trace_boot_set_ftrace_filter(tr, node) do {} while (0)
+#endif
+
static void __init
trace_boot_enable_tracer(struct trace_array *tr, struct skc_node *node)
{
const char *p;
+ trace_boot_set_ftrace_filter(tr, node);
+
p = skc_node_find_value(node, "tracer", NULL);
if (p && *p != '\0') {
if (tracing_set_tracer(tr, p) < 0)
^ permalink raw reply related
* [RFC PATCH v3 18/19] tracing/boot: Add function-graph tracer options
From: Masami Hiramatsu @ 2019-08-26 3:19 UTC (permalink / raw)
To: Steven Rostedt, Frank Rowand
Cc: Ingo Molnar, Namhyung Kim, Tim Bird, Jiri Olsa,
Arnaldo Carvalho de Melo, Tom Zanussi, Rob Herring, Andrew Morton,
Thomas Gleixner, Greg Kroah-Hartman, Alexey Dobriyan,
Jonathan Corbet, Linus Torvalds, linux-doc, linux-fsdevel,
linux-kernel
In-Reply-To: <156678933823.21459.4100380582025186209.stgit@devnote2>
Add following function-graph tracer related options
- ftrace.fgraph.filters = FILTER[, FILTER2...];
Add fgraph tracing function filters.
- ftrace.fgraph.notraces = FILTER[, FILTER2...];
Add fgraph non tracing function filters.
- ftrace.fgraph.max_depth = MAX_DEPTH;
Set MAX_DEPTH to maximum depth of fgraph tracer.
Note that these properties are available on ftrace global node
only, because these filters are globally applied.
Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
---
kernel/trace/ftrace.c | 85 +++++++++++++++++++++++++++++----------------
kernel/trace/trace_boot.c | 71 ++++++++++++++++++++++++++++++++++++++
2 files changed, 126 insertions(+), 30 deletions(-)
diff --git a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c
index eca34503f178..e016ce12e60a 100644
--- a/kernel/trace/ftrace.c
+++ b/kernel/trace/ftrace.c
@@ -1241,7 +1241,7 @@ static void clear_ftrace_mod_list(struct list_head *head)
mutex_unlock(&ftrace_lock);
}
-static void free_ftrace_hash(struct ftrace_hash *hash)
+void free_ftrace_hash(struct ftrace_hash *hash)
{
if (!hash || hash == EMPTY_HASH)
return;
@@ -4897,7 +4897,7 @@ __setup("ftrace_filter=", set_ftrace_filter);
#ifdef CONFIG_FUNCTION_GRAPH_TRACER
static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
-static int ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer);
+int ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer);
static int __init set_graph_function(char *str)
{
@@ -5226,6 +5226,26 @@ __ftrace_graph_open(struct inode *inode, struct file *file,
return ret;
}
+struct ftrace_hash *ftrace_graph_copy_hash(bool enable)
+{
+ struct ftrace_hash *hash;
+
+ mutex_lock(&graph_lock);
+
+ if (enable)
+ hash = rcu_dereference_protected(ftrace_graph_hash,
+ lockdep_is_held(&graph_lock));
+ else
+ hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
+ lockdep_is_held(&graph_lock));
+
+ hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
+
+ mutex_unlock(&graph_lock);
+
+ return hash;
+}
+
static int
ftrace_graph_open(struct inode *inode, struct file *file)
{
@@ -5282,11 +5302,40 @@ ftrace_graph_notrace_open(struct inode *inode, struct file *file)
return ret;
}
+int ftrace_graph_apply_hash(struct ftrace_hash *hash, bool enable)
+{
+ struct ftrace_hash *old_hash, *new_hash;
+
+ new_hash = __ftrace_hash_move(hash);
+ if (!new_hash)
+ return -ENOMEM;
+
+ mutex_lock(&graph_lock);
+
+ if (enable) {
+ old_hash = rcu_dereference_protected(ftrace_graph_hash,
+ lockdep_is_held(&graph_lock));
+ rcu_assign_pointer(ftrace_graph_hash, new_hash);
+ } else {
+ old_hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
+ lockdep_is_held(&graph_lock));
+ rcu_assign_pointer(ftrace_graph_notrace_hash, new_hash);
+ }
+
+ mutex_unlock(&graph_lock);
+
+ /* Wait till all users are no longer using the old hash */
+ synchronize_rcu();
+
+ free_ftrace_hash(old_hash);
+
+ return 0;
+}
+
static int
ftrace_graph_release(struct inode *inode, struct file *file)
{
struct ftrace_graph_data *fgd;
- struct ftrace_hash *old_hash, *new_hash;
struct trace_parser *parser;
int ret = 0;
@@ -5311,41 +5360,17 @@ ftrace_graph_release(struct inode *inode, struct file *file)
trace_parser_put(parser);
- new_hash = __ftrace_hash_move(fgd->new_hash);
- if (!new_hash) {
- ret = -ENOMEM;
- goto out;
- }
-
- mutex_lock(&graph_lock);
-
- if (fgd->type == GRAPH_FILTER_FUNCTION) {
- old_hash = rcu_dereference_protected(ftrace_graph_hash,
- lockdep_is_held(&graph_lock));
- rcu_assign_pointer(ftrace_graph_hash, new_hash);
- } else {
- old_hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
- lockdep_is_held(&graph_lock));
- rcu_assign_pointer(ftrace_graph_notrace_hash, new_hash);
- }
-
- mutex_unlock(&graph_lock);
-
- /* Wait till all users are no longer using the old hash */
- synchronize_rcu();
-
- free_ftrace_hash(old_hash);
+ ret = ftrace_graph_apply_hash(fgd->new_hash,
+ fgd->type == GRAPH_FILTER_FUNCTION);
}
- out:
free_ftrace_hash(fgd->new_hash);
kfree(fgd);
return ret;
}
-static int
-ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer)
+int ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer)
{
struct ftrace_glob func_g;
struct dyn_ftrace *rec;
diff --git a/kernel/trace/trace_boot.c b/kernel/trace/trace_boot.c
index 942ca8d3fcc8..b32688032d36 100644
--- a/kernel/trace/trace_boot.c
+++ b/kernel/trace/trace_boot.c
@@ -72,6 +72,75 @@ trace_boot_set_instance_options(struct trace_array *tr, struct skc_node *node)
}
}
+#ifdef CONFIG_FUNCTION_GRAPH_TRACER
+extern unsigned int fgraph_max_depth;
+extern struct ftrace_hash *ftrace_graph_copy_hash(bool enable);
+extern int ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer);
+extern int ftrace_graph_apply_hash(struct ftrace_hash *hash, bool enable);
+extern void free_ftrace_hash(struct ftrace_hash *hash);
+
+static void __init
+trace_boot_set_fgraph_filter(struct skc_node *node, const char *option,
+ bool enable)
+{
+ struct ftrace_hash *hash;
+ struct skc_node *anode;
+ const char *p;
+ char *q;
+ int err;
+ bool updated = false;
+
+ hash = ftrace_graph_copy_hash(enable);
+ if (!hash) {
+ pr_err("Failed to copy fgraph hash\n");
+ return;
+ }
+ skc_node_for_each_array_value(node, option, anode, p) {
+ q = kstrdup(p, GFP_KERNEL);
+ if (!q)
+ goto free_hash;
+ err = ftrace_graph_set_hash(hash, q);
+ kfree(q);
+ if (err)
+ pr_err("Failed to add %s: %s\n", option, p);
+ else
+ updated = true;
+ }
+ if (!updated)
+ goto free_hash;
+
+ if (ftrace_graph_apply_hash(hash, enable) < 0)
+ pr_err("Failed to apply new fgraph hash\n");
+ else {
+ /* If succeeded to apply new hash, old hash is released */
+ return;
+ }
+
+free_hash:
+ free_ftrace_hash(hash);
+}
+
+static void __init
+trace_boot_set_fgraph_options(struct skc_node *node)
+{
+ const char *p;
+ unsigned long v;
+
+ node = skc_node_find_child(node, "fgraph");
+ if (!node)
+ return;
+
+ trace_boot_set_fgraph_filter(node, "filters", true);
+ trace_boot_set_fgraph_filter(node, "notraces", false);
+
+ p = skc_node_find_value(node, "max_depth", NULL);
+ if (p && *p != '\0' && !kstrtoul(p, 0, &v))
+ fgraph_max_depth = (unsigned int)v;
+}
+#else
+#define trace_boot_set_fgraph_options(node) do {} while (0)
+#endif
+
static void __init
trace_boot_set_global_options(struct trace_array *tr, struct skc_node *node)
{
@@ -98,6 +167,8 @@ trace_boot_set_global_options(struct trace_array *tr, struct skc_node *node)
if (skc_node_find_value(node, "alloc_snapshot", NULL))
if (tracing_alloc_snapshot() < 0)
pr_err("Failed to allocate snapshot buffer\n");
+
+ trace_boot_set_fgraph_options(node);
}
#ifdef CONFIG_EVENT_TRACING
^ permalink raw reply related
* [RFC PATCH v3 19/19] Documentation: tracing: Add boot-time tracing document
From: Masami Hiramatsu @ 2019-08-26 3:19 UTC (permalink / raw)
To: Steven Rostedt, Frank Rowand
Cc: Ingo Molnar, Namhyung Kim, Tim Bird, Jiri Olsa,
Arnaldo Carvalho de Melo, Tom Zanussi, Rob Herring, Andrew Morton,
Thomas Gleixner, Greg Kroah-Hartman, Alexey Dobriyan,
Jonathan Corbet, Linus Torvalds, linux-doc, linux-fsdevel,
linux-kernel
In-Reply-To: <156678933823.21459.4100380582025186209.stgit@devnote2>
Add a documentation about boot-time tracing options for
SKC file.
Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
---
Documentation/trace/boottime-trace.rst | 185 ++++++++++++++++++++++++++++++++
1 file changed, 185 insertions(+)
create mode 100644 Documentation/trace/boottime-trace.rst
diff --git a/Documentation/trace/boottime-trace.rst b/Documentation/trace/boottime-trace.rst
new file mode 100644
index 000000000000..fc074afd014e
--- /dev/null
+++ b/Documentation/trace/boottime-trace.rst
@@ -0,0 +1,185 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+=================
+Boot-time tracing
+=================
+
+:Author: Masami Hiramatsu <mhiramat@kernel.org>
+
+Overview
+========
+
+Boot-time tracing allows users to trace boot-time process including
+device initialization with full features of ftrace including per-event
+filter and actions, histograms, kprobe-events and synthetic-events,
+and trace instances.
+Since kernel cmdline is not enough to control these complex features,
+this uses supplemental kernel cmdline (SKC) to describe tracing
+feature programming.
+
+Options in Supplemental Kernel Cmdline
+======================================
+
+Here is the list of available options list for boot time tracing in
+supplemental kenrel cmdline file [1]_. All options are under "ftrace."
+prefix to isolate from other subsystems.
+
+.. [1] See Documentation/admin-guide/skc.rst for details.
+
+Ftrace Global Options
+---------------------
+
+These options are only for global ftrace node since these are globally
+applied.
+
+ftrace.tp_printk;
+ Output trace-event data on printk buffer too.
+
+ftrace.dump_on_oops [= MODE];
+ Dump ftrace on Oops. If MODE = 1 or omitted, dump trace buffer
+ on all CPUs. If MODE = 2, dump a buffer on a CPU which kicks Oops.
+
+ftrace.traceoff_on_warning;
+ Stop tracing if WARN_ON() occurs.
+
+ftrace.fgraph.filters = FILTER[, FILTER2...];
+ Add fgraph tracing function filters.
+
+ftrace.fgraph.notraces = FILTER[, FILTER2...];
+ Add fgraph non tracing function filters.
+
+ftrace.fgraph.max_depth = MAX_DEPTH;
+ Set MAX_DEPTH to maximum depth of fgraph tracer.
+
+
+Ftrace Per-instance Options
+---------------------------
+
+These options can be used for each instance including global ftrace node.
+
+ftrace.[instance.INSTANCE.]options = OPT1[, OPT2[...]];
+ Enable given ftrace options.
+
+ftrace.[instance.INSTANCE.]trace_clock = CLOCK;
+ Set given CLOCK to ftrace's trace_clock.
+
+ftrace.[instance.INSTANCE.]buffer_size = SIZE;
+ Configure ftrace buffer size to SIZE. You can use "KB" or "MB"
+ for that SIZE.
+
+ftrace.[instance.INSTANCE.]alloc_snapshot;
+ Allocate snapshot buffer.
+
+ftrace.[instance.INSTANCE.]events = EVENT[, EVENT2[...]];
+ Enable given events on boot. You can use a wild card in EVENT.
+
+ftrace.[instance.INSTANCE.]tracer = TRACER;
+ Set TRACER to current tracer on boot. (e.g. function)
+
+ftrace.[instance.INSTANCE.]ftrace.filters
+ This will take an array of tracing function filter rules
+
+ftrace.[instance.INSTANCE.]ftrace.notraces
+ This will take an array of NON-tracing function filter rules
+
+
+Ftrace Per-Event Options
+------------------------
+
+These options are setting per-event options.
+
+ftrace.[instance.INSTANCE.]event.GROUP.EVENT.enable;
+ Enables GROUP:EVENT tracing.
+
+ftrace.[instance.INSTANCE.]event.GROUP.EVENT.filter = FILTER;
+ Set FILTER rule to the GROUP:EVENT.
+
+ftrace.[instance.INSTANCE.]event.GROUP.EVENT.actions = ACTION[, ACTION2[...]];
+ Set ACTIONs to the GROUP:EVENT.
+
+ftrace.[instance.INSTANCE.]event.kprobes.EVENT.probes = PROBE[, PROBE2[...]];
+ Defines new kprobe event based on PROBEs. It is able to define
+ multiple probes on one event, but those must have same type of
+ arguments. This option is available only for the event which
+ group name is "kprobes".
+
+ftrace.[instance.INSTANCE.]event.synthetic.EVENT.fields = FIELD[, FIELD2[...]];
+ Defines new synthetic event with FIELDs. Each field should be
+ "type varname".
+
+Note that kprobe and synthetic event definitions can be written under
+instance node, but those are also visible from other instances. So please
+take care for event name conflict.
+
+Examples
+========
+
+For example, to add filter and actions for each event, define kprobe
+events, and synthetic events with histogram, write SKC like below.
+
+::
+
+ ftrace.event {
+ task.task_newtask {
+ filter = "pid < 128";
+ enable;
+ }
+ kprobes.vfs_read {
+ probes = "vfs_read $arg1 $arg2";
+ filter = "common_pid < 200";
+ enable;
+ }
+ synthetic.initcall_latency {
+ fields = "unsigned long func", "u64 lat";
+ actions = "hist:keys=func.sym,lat:vals=lat:sort=lat";
+ }
+ initcall.initcall_start {
+ actions = "hist:keys=func:ts0=common_timestamp.usecs";
+ }
+ initcall.initcall_finish {
+ actions = "hist:keys=func:lat=common_timestamp.usecs-$ts0:onmatch(initcall.initcall_start).initcall_latency(func,$lat)";
+ }
+ }
+
+Also, boottime tracing supports "instance" node, which allows us to run
+several tracers for different purpose at once. For example, one tracer
+is for tracing functions in module alpha, and others tracing module beta,
+you can write as below.
+
+::
+
+ ftrace.instance {
+ foo {
+ tracer = "function";
+ ftrace-filters = "*:mod:alpha";
+ }
+ bar {
+ tracer = "function";
+ ftrace-filters = "*:mod:beta";
+ }
+ }
+
+The instance node also accepts event nodes so that each instance
+can customize its event tracing.
+
+This boot-time trace also supports ftrace kernel parameters.
+For example, following kernel parameters
+
+::
+
+ trace_options=sym-addr trace_event=initcall:* tp_printk trace_buf_size=1M ftrace=function ftrace_filter="vfs*"
+
+This can be written in SKC like below.
+
+::
+
+ ftrace {
+ options = sym-addr;
+ events = "initcall:*";
+ tp-printk;
+ buffer-size = 1MB;
+ ftrace-filters = "vfs*";
+ }
+
+However, since the initialization timing is different, if you need
+to trace very early boot, please use normal kernel parameters.
^ permalink raw reply related
* Re: [PATCH v1 1/2] vsprintf: introduce %dE for error constants
From: Sergey Senozhatsky @ 2019-08-26 5:55 UTC (permalink / raw)
To: Andrew Morton
Cc: Uwe Kleine-König, Jonathan Corbet, linux-doc, linux-kernel,
linux-gpio, Linus Walleij, Bartosz Golaszewski, Petr Mladek,
Sergey Senozhatsky, Steven Rostedt
In-Reply-To: <20190824165829.7d330367992c62dab87f6652@linux-foundation.org>
On (08/24/19 16:58), Andrew Morton wrote:
> On Sun, 25 Aug 2019 01:37:23 +0200 Uwe Kleine-König <uwe@kleine-koenig.org> wrote:
>
> > pr_info("probing failed (%dE)\n", ret);
> >
> > expands to
> >
> > probing failed (EIO)
> >
> > if ret holds -EIO (or EIO). This introduces an array of error codes. If
> > the error code is missing, %dE falls back to %d and so prints the plain
> > number.
>
> Huh. I'm surprised we don't already have this. Seems that this will
> be applicable in a lot of places? Although we shouldn't go blindly
> converting everything in sight - that would risk breaking userspace
> which parses kernel strings.
>
> Is it really necessary to handle the positive errnos? Does much kernel
> code actually do that (apart from kernel code which is buggy)?
Good point.
POSIX functions on error usually return -1 (negative value) and set errno
(positive value). Positive errno value can be passed to strerror() or
strerror_r() that decode that value and return a human readable
representation. E.g. strerr(9) returns "Bad file descriptor".
We don't have errno. Instead, and I may be wrong on this, kernel functions
are expected to return negative error codes. A very quick grep shows that
there are, however, patterns like "return positive errno".
E.g. drivers/xen/xenbus/xenbus_xs.c: get_error()
return EINVAL;
But this EINVAL eventually becomes negative
err = get_error(ret);
return ERR_PTR(-err);
or net/bluetooth/lib.c: bt_to_errno(). But, once again, bt_to_errno()
return value eventually becomes negative:
err = -bt_to_errno(hdev->req_result);
So errstr() probably can handle only negative values. And, may be,
I'd rename errstr() to strerror(); just because there is a well known
function, which "translates" errnos.
Unlike strerror(), errstr() just returns a macro name. Example:
"Request failed: EJUKEBOX"
EJUKEBOX does not tell me anything. A quick way to find out what does
EJUKEBOX stand for is to grep include/linux/errno.h
#define EJUKEBOX 528 /* Request initiated, but will not complete before timeout */
One still has to grep; either for 528 or for EJUKEBOX. I think that it
might be simpler, however, to grep for EJUKEBOX, because one can grep
the source code immediately, while in case of 528 one has to map 528
to the corresponding macro first and then grep the source code for EJUKEBOX.
Overall %dE looks interesting.
-ss
^ permalink raw reply
* Re: [PATCH 03/11] asm-generic: add generic dwarf definition
From: Peter Zijlstra @ 2019-08-26 7:42 UTC (permalink / raw)
To: Changbin Du
Cc: Steven Rostedt, Ingo Molnar, Jonathan Corbet, Jessica Yu,
Thomas Gleixner, x86, linux-doc, linux-kernel, linux-arm-kernel,
linux-mips, linux-parisc, linuxppc-dev, linux-riscv, linux-s390,
linux-sh, sparclinux, linux-arch, linux-kbuild
In-Reply-To: <20190825132330.5015-4-changbin.du@gmail.com>
On Sun, Aug 25, 2019 at 09:23:22PM +0800, Changbin Du wrote:
> Add generic DWARF constant definitions. We will use it later.
>
> Signed-off-by: Changbin Du <changbin.du@gmail.com>
> ---
> include/asm-generic/dwarf.h | 199 ++++++++++++++++++++++++++++++++++++
> 1 file changed, 199 insertions(+)
> create mode 100644 include/asm-generic/dwarf.h
>
> diff --git a/include/asm-generic/dwarf.h b/include/asm-generic/dwarf.h
> new file mode 100644
> index 000000000000..c705633c2a8f
> --- /dev/null
> +++ b/include/asm-generic/dwarf.h
> @@ -0,0 +1,199 @@
> +/* SPDX-License-Identifier: GPL-2.0
> + *
> + * Architecture independent definitions of DWARF.
> + *
> + * Copyright (C) 2019 Changbin Du <changbin.du@gmail.com>
You're claiming copyright on dwarf definitions? ;-)
I'm thinking only Oracle was daft enough to think stuff like that was
copyrightable.
Also; I think it would be very good to not use/depend on DWARF for this.
You really don't need all of DWARF; I'm thikning you only need a few
types; for location we already have regs_get_kernel_argument() which
has all the logic to find the n-th argument.
^ permalink raw reply
* Re: [PATCH 05/11] ftrace: create memcache for hash entries
From: Peter Zijlstra @ 2019-08-26 7:44 UTC (permalink / raw)
To: Changbin Du
Cc: Steven Rostedt, Ingo Molnar, Jonathan Corbet, Jessica Yu,
Thomas Gleixner, x86, linux-doc, linux-kernel, linux-arm-kernel,
linux-mips, linux-parisc, linuxppc-dev, linux-riscv, linux-s390,
linux-sh, sparclinux, linux-arch, linux-kbuild
In-Reply-To: <20190825132330.5015-6-changbin.du@gmail.com>
On Sun, Aug 25, 2019 at 09:23:24PM +0800, Changbin Du wrote:
> When CONFIG_FTRACE_FUNC_PROTOTYPE is enabled, thousands of
> ftrace_func_entry instances are created. So create a dedicated
> memcache to enhance performance.
>
> Signed-off-by: Changbin Du <changbin.du@gmail.com>
> ---
> kernel/trace/ftrace.c | 17 ++++++++++++++++-
> 1 file changed, 16 insertions(+), 1 deletion(-)
>
> diff --git a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c
> index a314f0768b2c..cfcb8dad93ea 100644
> --- a/kernel/trace/ftrace.c
> +++ b/kernel/trace/ftrace.c
> @@ -94,6 +94,8 @@ struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
> /* What to set function_trace_op to */
> static struct ftrace_ops *set_function_trace_op;
>
> +struct kmem_cache *hash_entry_cache;
> +
> static bool ftrace_pids_enabled(struct ftrace_ops *ops)
> {
> struct trace_array *tr;
> @@ -1169,7 +1171,7 @@ static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip,
> {
> struct ftrace_func_entry *entry;
>
> - entry = kmalloc(sizeof(*entry), GFP_KERNEL);
> + entry = kmem_cache_alloc(hash_entry_cache, GFP_KERNEL);
> if (!entry)
> return -ENOMEM;
>
> @@ -6153,6 +6155,15 @@ void __init ftrace_init(void)
> if (ret)
> goto failed;
>
> + hash_entry_cache = kmem_cache_create("ftrace-hash",
> + sizeof(struct ftrace_func_entry),
> + sizeof(struct ftrace_func_entry),
> + 0, NULL);
> + if (!hash_entry_cache) {
> + pr_err("failed to create ftrace hash entry cache\n");
> + goto failed;
> + }
Wait what; you already have then in the binary image, now you're
allocating extra memory for each of them?
Did you look at what ORC does? Is the binary search really not fast
enough?
^ permalink raw reply
* Re: [PATCH v2 1/1] counter: cros_ec: Add synchronization sensor
From: Jonathan Cameron @ 2019-08-26 8:56 UTC (permalink / raw)
To: Fabien Lahoudere
Cc: gwendal, egranata, kernel, William Breathitt Gray,
Jonathan Corbet, Benson Leung, Enric Balletbo i Serra,
Guenter Roeck, Hartmut Knaack, Lars-Peter Clausen,
Peter Meerwald-Stadler, Lee Jones, Mauro Carvalho Chehab,
David S. Miller, Greg Kroah-Hartman, Nicolas Ferre, Nick Vaccaro,
linux-iio, linux-doc, linux-kernel
In-Reply-To: <d985a8a811996148e8cda78b9fe47bb87b884b56.1566563833.git.fabien.lahoudere@collabora.com>
On Fri, 23 Aug 2019 14:41:27 +0200
Fabien Lahoudere <fabien.lahoudere@collabora.com> wrote:
> From: Gwendal Grignou <gwendal@chromium.org>
>
> EC returns a counter when there is an event on camera vsync.
> This patch comes from chromeos kernel 4.4
>
> Signed-off-by: Gwendal Grignou <gwendal@chromium.org>
> Signed-off-by: Fabien Lahoudere <fabien.lahoudere@collabora.com>
>
> CROS EC sync sensor was originally designed as an IIO device.
> Now that the counter subsystem will replace IIO_COUNTER, we
> have to implement a new way to get sync count.
I'm curious. What is this counter used for?
This combined counter and iio driver isn't something we would normally
want to support. What is the reasoning behind doing both interfaces?
>
> Signed-off-by: Fabien Lahoudere <fabien.lahoudere@collabora.com>
> ---
> Documentation/driver-api/generic-counter.rst | 3 +
> MAINTAINERS | 7 +
> drivers/counter/Kconfig | 9 +
> drivers/counter/Makefile | 1 +
> drivers/counter/counter.c | 2 +
> drivers/counter/cros_ec_sensors_sync.c | 208 ++++++++++++++++++
> .../cros_ec_sensors/cros_ec_sensors_core.c | 1 +
> drivers/mfd/cros_ec_dev.c | 3 +
> include/linux/counter.h | 1 +
> 9 files changed, 235 insertions(+)
> create mode 100644 drivers/counter/cros_ec_sensors_sync.c
>
> diff --git a/Documentation/driver-api/generic-counter.rst b/Documentation/driver-api/generic-counter.rst
> index 8382f01a53e3..beb80714ac8b 100644
> --- a/Documentation/driver-api/generic-counter.rst
> +++ b/Documentation/driver-api/generic-counter.rst
> @@ -44,6 +44,9 @@ Counter interface provides the following available count data types:
> * COUNT_POSITION:
> Unsigned integer value representing position.
>
> +* COUNT_TALLY:
> + Unsigned integer value representing tally.
> +
> A Count has a count function mode which represents the update behavior
> for the count data. The Generic Counter interface provides the following
> available count function modes:
> diff --git a/MAINTAINERS b/MAINTAINERS
> index e60f5c361969..83bd291d103e 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -3902,6 +3902,13 @@ R: Guenter Roeck <groeck@chromium.org>
> F: Documentation/devicetree/bindings/sound/google,cros-ec-codec.txt
> F: sound/soc/codecs/cros_ec_codec.*
>
> +CHROMEOS EC COUNTER DRIVER
> +M: Fabien Lahoudere <fabien.lahoudere@collabora.com>
> +M: William Breathitt Gray <vilhelm.gray@gmail.com>
> +L: linux-iio@vger.kernel.org
> +S: Maintained
> +F: drivers/counter/cros_ec_sensors_sync.c
> +
> CIRRUS LOGIC AUDIO CODEC DRIVERS
> M: Brian Austin <brian.austin@cirrus.com>
> M: Paul Handrigan <Paul.Handrigan@cirrus.com>
> diff --git a/drivers/counter/Kconfig b/drivers/counter/Kconfig
> index 2967d0a9ff91..22287f5715e5 100644
> --- a/drivers/counter/Kconfig
> +++ b/drivers/counter/Kconfig
> @@ -59,4 +59,13 @@ config FTM_QUADDEC
> To compile this driver as a module, choose M here: the
> module will be called ftm-quaddec.
>
> +config IIO_CROS_EC_SENSORS_SYNC
> + tristate "ChromeOS EC Counter Sensors"
> + depends on IIO_CROS_EC_SENSORS_CORE && IIO
> + help
> + Module to handle synchronisation sensors presented by the ChromeOS EC
> + Sensor hub.
> + Synchronisation sensors are counter sensors triggered when events
> + occurs from other subsystems.
> +
> endif # COUNTER
> diff --git a/drivers/counter/Makefile b/drivers/counter/Makefile
> index 40d35522937d..6fe4c98a446f 100644
> --- a/drivers/counter/Makefile
> +++ b/drivers/counter/Makefile
> @@ -9,3 +9,4 @@ obj-$(CONFIG_104_QUAD_8) += 104-quad-8.o
> obj-$(CONFIG_STM32_TIMER_CNT) += stm32-timer-cnt.o
> obj-$(CONFIG_STM32_LPTIMER_CNT) += stm32-lptimer-cnt.o
> obj-$(CONFIG_FTM_QUADDEC) += ftm-quaddec.o
> +obj-$(CONFIG_IIO_CROS_EC_SENSORS_SYNC) += cros_ec_sensors_sync.o
> diff --git a/drivers/counter/counter.c b/drivers/counter/counter.c
> index 106bc7180cd8..53525b109094 100644
> --- a/drivers/counter/counter.c
> +++ b/drivers/counter/counter.c
> @@ -261,6 +261,7 @@ void counter_count_read_value_set(struct counter_count_read_value *const val,
> {
> switch (type) {
> case COUNTER_COUNT_POSITION:
> + case COUNTER_COUNT_TALLY:
> val->len = sprintf(val->buf, "%lu\n", *(unsigned long *)data);
> break;
> default:
> @@ -290,6 +291,7 @@ int counter_count_write_value_get(void *const data,
>
> switch (type) {
> case COUNTER_COUNT_POSITION:
> + case COUNTER_COUNT_TALLY:
> err = kstrtoul(val->buf, 0, data);
> if (err)
> return err;
> diff --git a/drivers/counter/cros_ec_sensors_sync.c b/drivers/counter/cros_ec_sensors_sync.c
> new file mode 100644
> index 000000000000..b6f5e2c6da9f
> --- /dev/null
> +++ b/drivers/counter/cros_ec_sensors_sync.c
> @@ -0,0 +1,208 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * Driver of counter incremented after events on interrupt line in EC.
> + *
> + * Copyright 2018 Google, Inc
> + */
> +
> +#include <linux/device.h>
> +#include <linux/counter.h>
> +#include <linux/iio/common/cros_ec_sensors_core.h>
> +#include <linux/iio/triggered_buffer.h>
> +#include <linux/kernel.h>
> +#include <linux/mfd/cros_ec.h>
> +#include <linux/module.h>
> +#include <linux/platform_device.h>
> +
> +#define DRV_NAME "cros-ec-sync"
> +
> +/*
> + * One channel for counter, the other for timestamp.
> + */
> +#define MAX_CHANNELS (1)
> +
> +/* State data for ec_sensors iio driver. */
> +struct cros_ec_sensors_sync_state {
> + /* Shared by all sensors */
> + struct cros_ec_sensors_core_state core;
> + struct counter_device counter;
> + struct iio_chan_spec channels[MAX_CHANNELS];
> +};
> +
> +static int cros_ec_sensors_sync_read(struct iio_dev *indio_dev,
> + struct iio_chan_spec const *chan,
> + int *val, int *val2, long mask)
> +{
> + struct cros_ec_sensors_sync_state *st = iio_priv(indio_dev);
> + u16 data;
> + int ret;
> +
> + mutex_lock(&st->core.cmd_lock);
> + switch (mask) {
> + case IIO_CHAN_INFO_RAW:
> + ret = cros_ec_sensors_read_cmd(indio_dev, BIT(0), &data);
> + if (ret < 0)
> + break;
> + ret = IIO_VAL_INT;
> + *val = data;
> + break;
> + default:
> + ret = cros_ec_sensors_core_read(&st->core, chan, val, val2,
> + mask);
> + break;
> + }
> + mutex_unlock(&st->core.cmd_lock);
> + return ret;
> +}
> +
> +static struct iio_info cros_ec_sensors_sync_info = {
> + .read_raw = &cros_ec_sensors_sync_read,
> + .read_avail = &cros_ec_sensors_core_read_avail,
> +};
> +
> +static struct counter_count cros_ec_sync_counts = {
> + .id = 0,
> + .name = "Cros EC sync counter",
> +};
> +
> +static int cros_ec_sync_cnt_read(struct counter_device *counter,
> + struct counter_count *count,
> + struct counter_count_read_value *val)
> +{
> + s16 cnt;
> + int ret;
> + struct iio_dev *indio_dev = counter->priv;
> + struct cros_ec_sensors_sync_state *const st = iio_priv(indio_dev);
> + unsigned long data;
> +
> + mutex_lock(&st->core.cmd_lock);
> + ret = cros_ec_sensors_read_cmd(indio_dev, BIT(0), &cnt);
> + mutex_unlock(&st->core.cmd_lock);
> + if (ret != 0) {
> + dev_warn(&indio_dev->dev, "Unable to read sensor data\n");
> + return ret;
> + }
> +
> + data = (unsigned long) cnt;
> + counter_count_read_value_set(val, COUNTER_COUNT_TALLY, &data);
> +
> + return 0;
> +}
> +
> +static const struct counter_ops cros_ec_sync_cnt_ops = {
> + .count_read = cros_ec_sync_cnt_read,
> +};
> +
> +static char *cros_ec_loc[] = {
> + [MOTIONSENSE_LOC_BASE] = "base",
> + [MOTIONSENSE_LOC_LID] = "lid",
> + [MOTIONSENSE_LOC_CAMERA] = "camera",
> + [MOTIONSENSE_LOC_MAX] = "unknown",
> +};
> +
> +static ssize_t cros_ec_sync_id(struct counter_device *counter,
> + void *private, char *buf)
> +{
> + struct iio_dev *indio_dev = counter->priv;
> + struct cros_ec_sensors_sync_state *const st = iio_priv(indio_dev);
> +
> + return snprintf(buf, PAGE_SIZE, "%d\n", st->core.param.info.sensor_num);
> +}
> +
> +static ssize_t cros_ec_sync_loc(struct counter_device *counter,
> + void *private, char *buf)
> +{
> + struct iio_dev *indio_dev = counter->priv;
> + struct cros_ec_sensors_sync_state *const st = iio_priv(indio_dev);
> +
> + return snprintf(buf, PAGE_SIZE, "%s\n", cros_ec_loc[st->core.loc]);
> +}
> +
> +static struct counter_device_ext cros_ec_sync_cnt_ext[] = {
> + {
> + .name = "id",
> + .read = cros_ec_sync_id
> + },
> + {
> + .name = "location",
> + .read = cros_ec_sync_loc
> + },
> +};
> +
> +static int cros_ec_sensors_sync_probe(struct platform_device *pdev)
> +{
> + struct cros_ec_sensors_sync_state *state;
> + struct device *dev = &pdev->dev;
> + struct iio_chan_spec *channel;
> + struct iio_dev *indio_dev;
> + int ret;
> +
> + indio_dev = devm_iio_device_alloc(dev, sizeof(*state));
> + if (!indio_dev)
> + return -ENOMEM;
> +
> + ret = cros_ec_sensors_core_init(pdev, indio_dev, true);
> + if (ret)
> + return ret;
> +
> + indio_dev->info = &cros_ec_sensors_sync_info;
> + state = iio_priv(indio_dev);
> +
> + if (state->core.type != MOTIONSENSE_TYPE_SYNC)
> + return -EINVAL;
> +
> + /* Initialize IIO device */
> + channel = state->channels;
> + channel->type = IIO_TIMESTAMP;
> + channel->channel = -1;
> + channel->scan_index = 1;
> + channel->scan_type.sign = 's';
> + channel->scan_type.realbits = 64;
> + channel->scan_type.storagebits = 64;
> +
> + indio_dev->channels = state->channels;
> + indio_dev->num_channels = MAX_CHANNELS;
> +
> + state->core.read_ec_sensors_data = cros_ec_sensors_read_cmd;
> +
> + ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
> + cros_ec_sensors_capture, NULL);
> + if (ret)
> + return ret;
> +
> + ret = devm_iio_device_register(dev, indio_dev);
Hmm. Wasn't expecting to see that here if it's a counter device.
> + if (ret)
> + return ret;
> +
> + /* Initialize counter device */
> + state->counter.name = dev_name(&pdev->dev);
> + state->counter.parent = &pdev->dev;
> + state->counter.counts = &cros_ec_sync_counts;
> + state->counter.num_counts = 1;
> + state->counter.priv = indio_dev;
> + state->counter.ops = &cros_ec_sync_cnt_ops;
> + state->counter.ext = cros_ec_sync_cnt_ext;
> + state->counter.num_ext = ARRAY_SIZE(cros_ec_sync_cnt_ext);
> +
> + return devm_counter_register(&pdev->dev, &state->counter);
> +}
> +
> +static const struct platform_device_id cros_ec_sensors_sync_ids[] = {
> + { .name = DRV_NAME, },
> + { }
> +};
> +MODULE_DEVICE_TABLE(platform, cros_ec_sensors_sync_ids);
> +
> +static struct platform_driver cros_ec_sensors_sync_platform_driver = {
> + .driver = {
> + .name = DRV_NAME,
> + .pm = &cros_ec_sensors_pm_ops,
> + },
> + .probe = cros_ec_sensors_sync_probe,
> + .id_table = cros_ec_sensors_sync_ids,
> +};
> +module_platform_driver(cros_ec_sensors_sync_platform_driver);
> +
> +MODULE_DESCRIPTION("ChromeOS EC synchronisation sensor driver");
> +MODULE_ALIAS("platform:" DRV_NAME);
> +MODULE_LICENSE("GPL v2");
> diff --git a/drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c b/drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c
> index 805652250960..2bf183425eaf 100644
> --- a/drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c
> +++ b/drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c
> @@ -22,6 +22,7 @@
> static char *cros_ec_loc[] = {
> [MOTIONSENSE_LOC_BASE] = "base",
> [MOTIONSENSE_LOC_LID] = "lid",
> + [MOTIONSENSE_LOC_CAMERA] = "camera",
> [MOTIONSENSE_LOC_MAX] = "unknown",
> };
>
> diff --git a/drivers/mfd/cros_ec_dev.c b/drivers/mfd/cros_ec_dev.c
> index 41dccced5026..1c5c2c38af88 100644
> --- a/drivers/mfd/cros_ec_dev.c
> +++ b/drivers/mfd/cros_ec_dev.c
> @@ -332,6 +332,9 @@ static void cros_ec_sensors_register(struct cros_ec_dev *ec)
> case MOTIONSENSE_TYPE_ACTIVITY:
> sensor_cells[id].name = "cros-ec-activity";
> break;
> + case MOTIONSENSE_TYPE_SYNC:
> + sensor_cells[id].name = "cros-ec-sync";
> + break;
> default:
> dev_warn(ec->dev, "unknown type %d\n", resp->info.type);
> continue;
> diff --git a/include/linux/counter.h b/include/linux/counter.h
> index a061cdcdef7c..1198e675306f 100644
> --- a/include/linux/counter.h
> +++ b/include/linux/counter.h
> @@ -488,6 +488,7 @@ enum counter_signal_value_type {
>
> enum counter_count_value_type {
> COUNTER_COUNT_POSITION = 0,
> + COUNTER_COUNT_TALLY
> };
>
> void counter_signal_read_value_set(struct counter_signal_read_value *const val,
^ permalink raw reply
* FAILED: patch "[PATCH] x86/CPU/AMD: Clear RDRAND CPUID bit on AMD family 15h/16h" failed to apply to 4.4-stable tree
From: gregkh @ 2019-08-26 8:59 UTC (permalink / raw)
To: thomas.lendacky, akpm, andrew.cooper3, bp, corbet, hpa, jgross,
jpoimboe, keescook, linux-doc, linux-pm, mingo, natechancellor,
pavel, pbonzini, rjw, stable, tglx, x86, yu.c.chen
Cc: stable
The patch below does not apply to the 4.4-stable tree.
If someone wants it applied there, or to any other stable or longterm
tree, then please email the backport, including the original git commit
id to <stable@vger.kernel.org>.
thanks,
greg k-h
------------------ original commit in Linus's tree ------------------
From c49a0a80137c7ca7d6ced4c812c9e07a949f6f24 Mon Sep 17 00:00:00 2001
From: Tom Lendacky <thomas.lendacky@amd.com>
Date: Mon, 19 Aug 2019 15:52:35 +0000
Subject: [PATCH] x86/CPU/AMD: Clear RDRAND CPUID bit on AMD family 15h/16h
There have been reports of RDRAND issues after resuming from suspend on
some AMD family 15h and family 16h systems. This issue stems from a BIOS
not performing the proper steps during resume to ensure RDRAND continues
to function properly.
RDRAND support is indicated by CPUID Fn00000001_ECX[30]. This bit can be
reset by clearing MSR C001_1004[62]. Any software that checks for RDRAND
support using CPUID, including the kernel, will believe that RDRAND is
not supported.
Update the CPU initialization to clear the RDRAND CPUID bit for any family
15h and 16h processor that supports RDRAND. If it is known that the family
15h or family 16h system does not have an RDRAND resume issue or that the
system will not be placed in suspend, the "rdrand=force" kernel parameter
can be used to stop the clearing of the RDRAND CPUID bit.
Additionally, update the suspend and resume path to save and restore the
MSR C001_1004 value to ensure that the RDRAND CPUID setting remains in
place after resuming from suspend.
Note, that clearing the RDRAND CPUID bit does not prevent a processor
that normally supports the RDRAND instruction from executing it. So any
code that determined the support based on family and model won't #UD.
Signed-off-by: Tom Lendacky <thomas.lendacky@amd.com>
Signed-off-by: Borislav Petkov <bp@suse.de>
Cc: Andrew Cooper <andrew.cooper3@citrix.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Chen Yu <yu.c.chen@intel.com>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: Juergen Gross <jgross@suse.com>
Cc: Kees Cook <keescook@chromium.org>
Cc: "linux-doc@vger.kernel.org" <linux-doc@vger.kernel.org>
Cc: "linux-pm@vger.kernel.org" <linux-pm@vger.kernel.org>
Cc: Nathan Chancellor <natechancellor@gmail.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Pavel Machek <pavel@ucw.cz>
Cc: "Rafael J. Wysocki" <rjw@rjwysocki.net>
Cc: <stable@vger.kernel.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: "x86@kernel.org" <x86@kernel.org>
Link: https://lkml.kernel.org/r/7543af91666f491547bd86cebb1e17c66824ab9f.1566229943.git.thomas.lendacky@amd.com
diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index 47d981a86e2f..4c1971960afa 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -4090,6 +4090,13 @@
Run specified binary instead of /init from the ramdisk,
used for early userspace startup. See initrd.
+ rdrand= [X86]
+ force - Override the decision by the kernel to hide the
+ advertisement of RDRAND support (this affects
+ certain AMD processors because of buggy BIOS
+ support, specifically around the suspend/resume
+ path).
+
rdt= [HW,X86,RDT]
Turn on/off individual RDT features. List is:
cmt, mbmtotal, mbmlocal, l3cat, l3cdp, l2cat, l2cdp,
diff --git a/arch/x86/include/asm/msr-index.h b/arch/x86/include/asm/msr-index.h
index 6b4fc2788078..271d837d69a8 100644
--- a/arch/x86/include/asm/msr-index.h
+++ b/arch/x86/include/asm/msr-index.h
@@ -381,6 +381,7 @@
#define MSR_AMD64_PATCH_LEVEL 0x0000008b
#define MSR_AMD64_TSC_RATIO 0xc0000104
#define MSR_AMD64_NB_CFG 0xc001001f
+#define MSR_AMD64_CPUID_FN_1 0xc0011004
#define MSR_AMD64_PATCH_LOADER 0xc0010020
#define MSR_AMD64_OSVW_ID_LENGTH 0xc0010140
#define MSR_AMD64_OSVW_STATUS 0xc0010141
diff --git a/arch/x86/kernel/cpu/amd.c b/arch/x86/kernel/cpu/amd.c
index 8d4e50428b68..68c363c341bf 100644
--- a/arch/x86/kernel/cpu/amd.c
+++ b/arch/x86/kernel/cpu/amd.c
@@ -804,6 +804,64 @@ static void init_amd_ln(struct cpuinfo_x86 *c)
msr_set_bit(MSR_AMD64_DE_CFG, 31);
}
+static bool rdrand_force;
+
+static int __init rdrand_cmdline(char *str)
+{
+ if (!str)
+ return -EINVAL;
+
+ if (!strcmp(str, "force"))
+ rdrand_force = true;
+ else
+ return -EINVAL;
+
+ return 0;
+}
+early_param("rdrand", rdrand_cmdline);
+
+static void clear_rdrand_cpuid_bit(struct cpuinfo_x86 *c)
+{
+ /*
+ * Saving of the MSR used to hide the RDRAND support during
+ * suspend/resume is done by arch/x86/power/cpu.c, which is
+ * dependent on CONFIG_PM_SLEEP.
+ */
+ if (!IS_ENABLED(CONFIG_PM_SLEEP))
+ return;
+
+ /*
+ * The nordrand option can clear X86_FEATURE_RDRAND, so check for
+ * RDRAND support using the CPUID function directly.
+ */
+ if (!(cpuid_ecx(1) & BIT(30)) || rdrand_force)
+ return;
+
+ msr_clear_bit(MSR_AMD64_CPUID_FN_1, 62);
+
+ /*
+ * Verify that the CPUID change has occurred in case the kernel is
+ * running virtualized and the hypervisor doesn't support the MSR.
+ */
+ if (cpuid_ecx(1) & BIT(30)) {
+ pr_info_once("BIOS may not properly restore RDRAND after suspend, but hypervisor does not support hiding RDRAND via CPUID.\n");
+ return;
+ }
+
+ clear_cpu_cap(c, X86_FEATURE_RDRAND);
+ pr_info_once("BIOS may not properly restore RDRAND after suspend, hiding RDRAND via CPUID. Use rdrand=force to reenable.\n");
+}
+
+static void init_amd_jg(struct cpuinfo_x86 *c)
+{
+ /*
+ * Some BIOS implementations do not restore proper RDRAND support
+ * across suspend and resume. Check on whether to hide the RDRAND
+ * instruction support via CPUID.
+ */
+ clear_rdrand_cpuid_bit(c);
+}
+
static void init_amd_bd(struct cpuinfo_x86 *c)
{
u64 value;
@@ -818,6 +876,13 @@ static void init_amd_bd(struct cpuinfo_x86 *c)
wrmsrl_safe(MSR_F15H_IC_CFG, value);
}
}
+
+ /*
+ * Some BIOS implementations do not restore proper RDRAND support
+ * across suspend and resume. Check on whether to hide the RDRAND
+ * instruction support via CPUID.
+ */
+ clear_rdrand_cpuid_bit(c);
}
static void init_amd_zn(struct cpuinfo_x86 *c)
@@ -860,6 +925,7 @@ static void init_amd(struct cpuinfo_x86 *c)
case 0x10: init_amd_gh(c); break;
case 0x12: init_amd_ln(c); break;
case 0x15: init_amd_bd(c); break;
+ case 0x16: init_amd_jg(c); break;
case 0x17: init_amd_zn(c); break;
}
diff --git a/arch/x86/power/cpu.c b/arch/x86/power/cpu.c
index 24b079e94bc2..c9ef6a7a4a1a 100644
--- a/arch/x86/power/cpu.c
+++ b/arch/x86/power/cpu.c
@@ -12,6 +12,7 @@
#include <linux/smp.h>
#include <linux/perf_event.h>
#include <linux/tboot.h>
+#include <linux/dmi.h>
#include <asm/pgtable.h>
#include <asm/proto.h>
@@ -23,7 +24,7 @@
#include <asm/debugreg.h>
#include <asm/cpu.h>
#include <asm/mmu_context.h>
-#include <linux/dmi.h>
+#include <asm/cpu_device_id.h>
#ifdef CONFIG_X86_32
__visible unsigned long saved_context_ebx;
@@ -397,15 +398,14 @@ static int __init bsp_pm_check_init(void)
core_initcall(bsp_pm_check_init);
-static int msr_init_context(const u32 *msr_id, const int total_num)
+static int msr_build_context(const u32 *msr_id, const int num)
{
- int i = 0;
+ struct saved_msrs *saved_msrs = &saved_context.saved_msrs;
struct saved_msr *msr_array;
+ int total_num;
+ int i, j;
- if (saved_context.saved_msrs.array || saved_context.saved_msrs.num > 0) {
- pr_err("x86/pm: MSR quirk already applied, please check your DMI match table.\n");
- return -EINVAL;
- }
+ total_num = saved_msrs->num + num;
msr_array = kmalloc_array(total_num, sizeof(struct saved_msr), GFP_KERNEL);
if (!msr_array) {
@@ -413,19 +413,30 @@ static int msr_init_context(const u32 *msr_id, const int total_num)
return -ENOMEM;
}
- for (i = 0; i < total_num; i++) {
- msr_array[i].info.msr_no = msr_id[i];
+ if (saved_msrs->array) {
+ /*
+ * Multiple callbacks can invoke this function, so copy any
+ * MSR save requests from previous invocations.
+ */
+ memcpy(msr_array, saved_msrs->array,
+ sizeof(struct saved_msr) * saved_msrs->num);
+
+ kfree(saved_msrs->array);
+ }
+
+ for (i = saved_msrs->num, j = 0; i < total_num; i++, j++) {
+ msr_array[i].info.msr_no = msr_id[j];
msr_array[i].valid = false;
msr_array[i].info.reg.q = 0;
}
- saved_context.saved_msrs.num = total_num;
- saved_context.saved_msrs.array = msr_array;
+ saved_msrs->num = total_num;
+ saved_msrs->array = msr_array;
return 0;
}
/*
- * The following section is a quirk framework for problematic BIOSen:
+ * The following sections are a quirk framework for problematic BIOSen:
* Sometimes MSRs are modified by the BIOSen after suspended to
* RAM, this might cause unexpected behavior after wakeup.
* Thus we save/restore these specified MSRs across suspend/resume
@@ -440,7 +451,7 @@ static int msr_initialize_bdw(const struct dmi_system_id *d)
u32 bdw_msr_id[] = { MSR_IA32_THERM_CONTROL };
pr_info("x86/pm: %s detected, MSR saving is needed during suspending.\n", d->ident);
- return msr_init_context(bdw_msr_id, ARRAY_SIZE(bdw_msr_id));
+ return msr_build_context(bdw_msr_id, ARRAY_SIZE(bdw_msr_id));
}
static const struct dmi_system_id msr_save_dmi_table[] = {
@@ -455,9 +466,58 @@ static const struct dmi_system_id msr_save_dmi_table[] = {
{}
};
+static int msr_save_cpuid_features(const struct x86_cpu_id *c)
+{
+ u32 cpuid_msr_id[] = {
+ MSR_AMD64_CPUID_FN_1,
+ };
+
+ pr_info("x86/pm: family %#hx cpu detected, MSR saving is needed during suspending.\n",
+ c->family);
+
+ return msr_build_context(cpuid_msr_id, ARRAY_SIZE(cpuid_msr_id));
+}
+
+static const struct x86_cpu_id msr_save_cpu_table[] = {
+ {
+ .vendor = X86_VENDOR_AMD,
+ .family = 0x15,
+ .model = X86_MODEL_ANY,
+ .feature = X86_FEATURE_ANY,
+ .driver_data = (kernel_ulong_t)msr_save_cpuid_features,
+ },
+ {
+ .vendor = X86_VENDOR_AMD,
+ .family = 0x16,
+ .model = X86_MODEL_ANY,
+ .feature = X86_FEATURE_ANY,
+ .driver_data = (kernel_ulong_t)msr_save_cpuid_features,
+ },
+ {}
+};
+
+typedef int (*pm_cpu_match_t)(const struct x86_cpu_id *);
+static int pm_cpu_check(const struct x86_cpu_id *c)
+{
+ const struct x86_cpu_id *m;
+ int ret = 0;
+
+ m = x86_match_cpu(msr_save_cpu_table);
+ if (m) {
+ pm_cpu_match_t fn;
+
+ fn = (pm_cpu_match_t)m->driver_data;
+ ret = fn(m);
+ }
+
+ return ret;
+}
+
static int pm_check_save_msr(void)
{
dmi_check_system(msr_save_dmi_table);
+ pm_cpu_check(msr_save_cpu_table);
+
return 0;
}
^ permalink raw reply related
* FAILED: patch "[PATCH] x86/CPU/AMD: Clear RDRAND CPUID bit on AMD family 15h/16h" failed to apply to 4.9-stable tree
From: gregkh @ 2019-08-26 8:59 UTC (permalink / raw)
To: thomas.lendacky, akpm, andrew.cooper3, bp, corbet, hpa, jgross,
jpoimboe, keescook, linux-doc, linux-pm, mingo, natechancellor,
pavel, pbonzini, rjw, stable, tglx, x86, yu.c.chen
Cc: stable
The patch below does not apply to the 4.9-stable tree.
If someone wants it applied there, or to any other stable or longterm
tree, then please email the backport, including the original git commit
id to <stable@vger.kernel.org>.
thanks,
greg k-h
------------------ original commit in Linus's tree ------------------
From c49a0a80137c7ca7d6ced4c812c9e07a949f6f24 Mon Sep 17 00:00:00 2001
From: Tom Lendacky <thomas.lendacky@amd.com>
Date: Mon, 19 Aug 2019 15:52:35 +0000
Subject: [PATCH] x86/CPU/AMD: Clear RDRAND CPUID bit on AMD family 15h/16h
There have been reports of RDRAND issues after resuming from suspend on
some AMD family 15h and family 16h systems. This issue stems from a BIOS
not performing the proper steps during resume to ensure RDRAND continues
to function properly.
RDRAND support is indicated by CPUID Fn00000001_ECX[30]. This bit can be
reset by clearing MSR C001_1004[62]. Any software that checks for RDRAND
support using CPUID, including the kernel, will believe that RDRAND is
not supported.
Update the CPU initialization to clear the RDRAND CPUID bit for any family
15h and 16h processor that supports RDRAND. If it is known that the family
15h or family 16h system does not have an RDRAND resume issue or that the
system will not be placed in suspend, the "rdrand=force" kernel parameter
can be used to stop the clearing of the RDRAND CPUID bit.
Additionally, update the suspend and resume path to save and restore the
MSR C001_1004 value to ensure that the RDRAND CPUID setting remains in
place after resuming from suspend.
Note, that clearing the RDRAND CPUID bit does not prevent a processor
that normally supports the RDRAND instruction from executing it. So any
code that determined the support based on family and model won't #UD.
Signed-off-by: Tom Lendacky <thomas.lendacky@amd.com>
Signed-off-by: Borislav Petkov <bp@suse.de>
Cc: Andrew Cooper <andrew.cooper3@citrix.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Chen Yu <yu.c.chen@intel.com>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: Juergen Gross <jgross@suse.com>
Cc: Kees Cook <keescook@chromium.org>
Cc: "linux-doc@vger.kernel.org" <linux-doc@vger.kernel.org>
Cc: "linux-pm@vger.kernel.org" <linux-pm@vger.kernel.org>
Cc: Nathan Chancellor <natechancellor@gmail.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Pavel Machek <pavel@ucw.cz>
Cc: "Rafael J. Wysocki" <rjw@rjwysocki.net>
Cc: <stable@vger.kernel.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: "x86@kernel.org" <x86@kernel.org>
Link: https://lkml.kernel.org/r/7543af91666f491547bd86cebb1e17c66824ab9f.1566229943.git.thomas.lendacky@amd.com
diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index 47d981a86e2f..4c1971960afa 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -4090,6 +4090,13 @@
Run specified binary instead of /init from the ramdisk,
used for early userspace startup. See initrd.
+ rdrand= [X86]
+ force - Override the decision by the kernel to hide the
+ advertisement of RDRAND support (this affects
+ certain AMD processors because of buggy BIOS
+ support, specifically around the suspend/resume
+ path).
+
rdt= [HW,X86,RDT]
Turn on/off individual RDT features. List is:
cmt, mbmtotal, mbmlocal, l3cat, l3cdp, l2cat, l2cdp,
diff --git a/arch/x86/include/asm/msr-index.h b/arch/x86/include/asm/msr-index.h
index 6b4fc2788078..271d837d69a8 100644
--- a/arch/x86/include/asm/msr-index.h
+++ b/arch/x86/include/asm/msr-index.h
@@ -381,6 +381,7 @@
#define MSR_AMD64_PATCH_LEVEL 0x0000008b
#define MSR_AMD64_TSC_RATIO 0xc0000104
#define MSR_AMD64_NB_CFG 0xc001001f
+#define MSR_AMD64_CPUID_FN_1 0xc0011004
#define MSR_AMD64_PATCH_LOADER 0xc0010020
#define MSR_AMD64_OSVW_ID_LENGTH 0xc0010140
#define MSR_AMD64_OSVW_STATUS 0xc0010141
diff --git a/arch/x86/kernel/cpu/amd.c b/arch/x86/kernel/cpu/amd.c
index 8d4e50428b68..68c363c341bf 100644
--- a/arch/x86/kernel/cpu/amd.c
+++ b/arch/x86/kernel/cpu/amd.c
@@ -804,6 +804,64 @@ static void init_amd_ln(struct cpuinfo_x86 *c)
msr_set_bit(MSR_AMD64_DE_CFG, 31);
}
+static bool rdrand_force;
+
+static int __init rdrand_cmdline(char *str)
+{
+ if (!str)
+ return -EINVAL;
+
+ if (!strcmp(str, "force"))
+ rdrand_force = true;
+ else
+ return -EINVAL;
+
+ return 0;
+}
+early_param("rdrand", rdrand_cmdline);
+
+static void clear_rdrand_cpuid_bit(struct cpuinfo_x86 *c)
+{
+ /*
+ * Saving of the MSR used to hide the RDRAND support during
+ * suspend/resume is done by arch/x86/power/cpu.c, which is
+ * dependent on CONFIG_PM_SLEEP.
+ */
+ if (!IS_ENABLED(CONFIG_PM_SLEEP))
+ return;
+
+ /*
+ * The nordrand option can clear X86_FEATURE_RDRAND, so check for
+ * RDRAND support using the CPUID function directly.
+ */
+ if (!(cpuid_ecx(1) & BIT(30)) || rdrand_force)
+ return;
+
+ msr_clear_bit(MSR_AMD64_CPUID_FN_1, 62);
+
+ /*
+ * Verify that the CPUID change has occurred in case the kernel is
+ * running virtualized and the hypervisor doesn't support the MSR.
+ */
+ if (cpuid_ecx(1) & BIT(30)) {
+ pr_info_once("BIOS may not properly restore RDRAND after suspend, but hypervisor does not support hiding RDRAND via CPUID.\n");
+ return;
+ }
+
+ clear_cpu_cap(c, X86_FEATURE_RDRAND);
+ pr_info_once("BIOS may not properly restore RDRAND after suspend, hiding RDRAND via CPUID. Use rdrand=force to reenable.\n");
+}
+
+static void init_amd_jg(struct cpuinfo_x86 *c)
+{
+ /*
+ * Some BIOS implementations do not restore proper RDRAND support
+ * across suspend and resume. Check on whether to hide the RDRAND
+ * instruction support via CPUID.
+ */
+ clear_rdrand_cpuid_bit(c);
+}
+
static void init_amd_bd(struct cpuinfo_x86 *c)
{
u64 value;
@@ -818,6 +876,13 @@ static void init_amd_bd(struct cpuinfo_x86 *c)
wrmsrl_safe(MSR_F15H_IC_CFG, value);
}
}
+
+ /*
+ * Some BIOS implementations do not restore proper RDRAND support
+ * across suspend and resume. Check on whether to hide the RDRAND
+ * instruction support via CPUID.
+ */
+ clear_rdrand_cpuid_bit(c);
}
static void init_amd_zn(struct cpuinfo_x86 *c)
@@ -860,6 +925,7 @@ static void init_amd(struct cpuinfo_x86 *c)
case 0x10: init_amd_gh(c); break;
case 0x12: init_amd_ln(c); break;
case 0x15: init_amd_bd(c); break;
+ case 0x16: init_amd_jg(c); break;
case 0x17: init_amd_zn(c); break;
}
diff --git a/arch/x86/power/cpu.c b/arch/x86/power/cpu.c
index 24b079e94bc2..c9ef6a7a4a1a 100644
--- a/arch/x86/power/cpu.c
+++ b/arch/x86/power/cpu.c
@@ -12,6 +12,7 @@
#include <linux/smp.h>
#include <linux/perf_event.h>
#include <linux/tboot.h>
+#include <linux/dmi.h>
#include <asm/pgtable.h>
#include <asm/proto.h>
@@ -23,7 +24,7 @@
#include <asm/debugreg.h>
#include <asm/cpu.h>
#include <asm/mmu_context.h>
-#include <linux/dmi.h>
+#include <asm/cpu_device_id.h>
#ifdef CONFIG_X86_32
__visible unsigned long saved_context_ebx;
@@ -397,15 +398,14 @@ static int __init bsp_pm_check_init(void)
core_initcall(bsp_pm_check_init);
-static int msr_init_context(const u32 *msr_id, const int total_num)
+static int msr_build_context(const u32 *msr_id, const int num)
{
- int i = 0;
+ struct saved_msrs *saved_msrs = &saved_context.saved_msrs;
struct saved_msr *msr_array;
+ int total_num;
+ int i, j;
- if (saved_context.saved_msrs.array || saved_context.saved_msrs.num > 0) {
- pr_err("x86/pm: MSR quirk already applied, please check your DMI match table.\n");
- return -EINVAL;
- }
+ total_num = saved_msrs->num + num;
msr_array = kmalloc_array(total_num, sizeof(struct saved_msr), GFP_KERNEL);
if (!msr_array) {
@@ -413,19 +413,30 @@ static int msr_init_context(const u32 *msr_id, const int total_num)
return -ENOMEM;
}
- for (i = 0; i < total_num; i++) {
- msr_array[i].info.msr_no = msr_id[i];
+ if (saved_msrs->array) {
+ /*
+ * Multiple callbacks can invoke this function, so copy any
+ * MSR save requests from previous invocations.
+ */
+ memcpy(msr_array, saved_msrs->array,
+ sizeof(struct saved_msr) * saved_msrs->num);
+
+ kfree(saved_msrs->array);
+ }
+
+ for (i = saved_msrs->num, j = 0; i < total_num; i++, j++) {
+ msr_array[i].info.msr_no = msr_id[j];
msr_array[i].valid = false;
msr_array[i].info.reg.q = 0;
}
- saved_context.saved_msrs.num = total_num;
- saved_context.saved_msrs.array = msr_array;
+ saved_msrs->num = total_num;
+ saved_msrs->array = msr_array;
return 0;
}
/*
- * The following section is a quirk framework for problematic BIOSen:
+ * The following sections are a quirk framework for problematic BIOSen:
* Sometimes MSRs are modified by the BIOSen after suspended to
* RAM, this might cause unexpected behavior after wakeup.
* Thus we save/restore these specified MSRs across suspend/resume
@@ -440,7 +451,7 @@ static int msr_initialize_bdw(const struct dmi_system_id *d)
u32 bdw_msr_id[] = { MSR_IA32_THERM_CONTROL };
pr_info("x86/pm: %s detected, MSR saving is needed during suspending.\n", d->ident);
- return msr_init_context(bdw_msr_id, ARRAY_SIZE(bdw_msr_id));
+ return msr_build_context(bdw_msr_id, ARRAY_SIZE(bdw_msr_id));
}
static const struct dmi_system_id msr_save_dmi_table[] = {
@@ -455,9 +466,58 @@ static const struct dmi_system_id msr_save_dmi_table[] = {
{}
};
+static int msr_save_cpuid_features(const struct x86_cpu_id *c)
+{
+ u32 cpuid_msr_id[] = {
+ MSR_AMD64_CPUID_FN_1,
+ };
+
+ pr_info("x86/pm: family %#hx cpu detected, MSR saving is needed during suspending.\n",
+ c->family);
+
+ return msr_build_context(cpuid_msr_id, ARRAY_SIZE(cpuid_msr_id));
+}
+
+static const struct x86_cpu_id msr_save_cpu_table[] = {
+ {
+ .vendor = X86_VENDOR_AMD,
+ .family = 0x15,
+ .model = X86_MODEL_ANY,
+ .feature = X86_FEATURE_ANY,
+ .driver_data = (kernel_ulong_t)msr_save_cpuid_features,
+ },
+ {
+ .vendor = X86_VENDOR_AMD,
+ .family = 0x16,
+ .model = X86_MODEL_ANY,
+ .feature = X86_FEATURE_ANY,
+ .driver_data = (kernel_ulong_t)msr_save_cpuid_features,
+ },
+ {}
+};
+
+typedef int (*pm_cpu_match_t)(const struct x86_cpu_id *);
+static int pm_cpu_check(const struct x86_cpu_id *c)
+{
+ const struct x86_cpu_id *m;
+ int ret = 0;
+
+ m = x86_match_cpu(msr_save_cpu_table);
+ if (m) {
+ pm_cpu_match_t fn;
+
+ fn = (pm_cpu_match_t)m->driver_data;
+ ret = fn(m);
+ }
+
+ return ret;
+}
+
static int pm_check_save_msr(void)
{
dmi_check_system(msr_save_dmi_table);
+ pm_cpu_check(msr_save_cpu_table);
+
return 0;
}
^ permalink raw reply related
page: next (older) | prev (newer) | latest
- recent:[subjects (threaded)|topics (new)|topics (active)]
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox