Skip to contentSkip to conversation

params: Add a per cpu module param type

PATCH In review Andi Kleen 2 participants 3 messages Aug 19, 2014 Module support

lore
Share & tools
lore

Andi Kleen

author
#0 lore raw

PATCH 1/2 params: Add a per cpu module param type

From: Andi Kleen <[email protected]>

This is mainly useful for simple statistic counters. Essentially read-only, writing only clears.

---
 include/linux/moduleparam.h |  4 ++++
 kernel/params.c             | 28 ++++++++++++++++++++++++++++
 2 files changed, 32 insertions(+)
include/linux/moduleparam.h +4 -0
index 494f99e..d019ad2 100644
@@ -408,6 +408,10 @@ extern int param_set_bint(const char *val, const struct kernel_param *kp);
 #define param_get_bint param_get_int
 #define param_check_bint param_check_int
 
+/* A per cpu read-only unsigned integer. Useful for counters. */
+extern struct kernel_param_ops param_ops_percpu_uint;
+#define param_check_percpu_uint(name, p) param_check_uint
+
 /**
  * module_param_array - a parameter which is an array of some type
  * @name: the name of the array variable
kernel/params.c +28 -0
index 34f5270..700badd 100644
@@ -499,6 +499,34 @@ struct kernel_param_ops param_ops_string = {
 };
 EXPORT_SYMBOL(param_ops_string);
 
+/* Per CPU read only module param. */
+
+static int param_percpu_uint_set(const char *val, const struct kernel_param *kp)
+{
+	int cpu;
+
+	/* just clear on any write */
+	for_each_possible_cpu(cpu)
+		*per_cpu_ptr((unsigned * __percpu)(kp->arg), cpu) = 0;
+	return 0;
+}
+
+static int param_percpu_uint_get(char *buffer, const struct kernel_param *kp)
+{
+	int cpu;
+	unsigned count = 0;
+
+	for_each_possible_cpu(cpu)
+		count += *per_cpu_ptr((unsigned * __percpu)(kp->arg), cpu);
+	return sprintf(buffer, "%u", count);
+}
+
+struct kernel_param_ops param_ops_percpu_uint = {
+	.set = param_percpu_uint_set,
+	.get = param_percpu_uint_get,
+};
+EXPORT_SYMBOL(param_ops_percpu_uint);
+
 /* sysfs output in /sys/modules/XYZ/parameters/ */
 #define to_module_attr(n) container_of(n, struct module_attribute, attr)
 #define to_module_kobject(n) container_of(n, struct module_kobject, kobj)
Signature · 1 line
1.9.3
author
#1 lore raw

PATCH 2/2 params: Add static key module param

From: Andi Kleen <[email protected]>

Add a module param type for static keys. Useful for time critical flags.

The basic jump_label interface is racy for boolean settings, because checking and changing is not atomic, so we use an additional lock

Open: should use deferred keys? But as it's only root changeable changes should be very rare. Would need some changes to jump labels.

---
 include/linux/moduleparam.h |  6 ++++++
 kernel/params.c             | 35 +++++++++++++++++++++++++++++++++++
 2 files changed, 41 insertions(+)
include/linux/moduleparam.h +6 -0
index d019ad2..30cdad8 100644
@@ -412,6 +412,12 @@ extern int param_set_bint(const char *val, const struct kernel_param *kp);
 extern struct kernel_param_ops param_ops_percpu_uint;
 #define param_check_percpu_uint(name, p) param_check_uint
 
+/* Static key module params. Useful for time critical flags. */
+struct static_key;
+extern struct kernel_param_ops param_ops_static_key;
+#define param_check_static_key(name, p) \
+	__param_check(name, p, struct static_key)
+
 /**
  * module_param_array - a parameter which is an array of some type
  * @name: the name of the array variable
kernel/params.c +35 -0
index 700badd..d176ae8 100644
@@ -23,6 +23,7 @@
 #include <linux/err.h>
 #include <linux/slab.h>
 #include <linux/ctype.h>
+#include <linux/jump_label.h>
 
 /* Protects all parameters, and incidentally kmalloced_param list. */
 static DEFINE_MUTEX(param_lock);
@@ -527,6 +528,40 @@ struct kernel_param_ops param_ops_percpu_uint = {
 };
 EXPORT_SYMBOL(param_ops_percpu_uint);
 
+/* Static key module params. Like a bool, but allows patching the jumps. */
+
+/* Noone else should change */
+static int param_static_key_set(const char *val, const struct kernel_param *kp)
+{
+	static DEFINE_SPINLOCK(lock);
+	bool l;
+	struct static_key *key = (struct static_key *)(kp->arg);
+	int ret = strtobool(val, &l);
+
+	if (ret < 0)
+		return ret;
+	/* Work around racy static key interface */
+	spin_lock(&lock);
+	if (l && !atomic_read(&key->enabled))
+		static_key_slow_inc(key);
+	if (!l && atomic_read(&key->enabled))
+		static_key_slow_dec(key);
+	spin_unlock(&lock);
+	return ret;
+}
+
+static int param_static_key_get(char *buffer, const struct kernel_param *kp)
+{
+	struct static_key *key = kp->arg;
+	return sprintf(buffer, "%u", atomic_read(&key->enabled) > 0);
+}
+
+struct kernel_param_ops param_ops_static_key = {
+	.set = param_static_key_set,
+	.get = param_static_key_get,
+};
+EXPORT_SYMBOL(param_ops_static_key);
+
 /* sysfs output in /sys/modules/XYZ/parameters/ */
 #define to_module_attr(n) container_of(n, struct module_attribute, attr)
 #define to_module_kobject(n) container_of(n, struct module_kobject, kobj)
Signature · 1 line
1.9.3
#2 lore raw
In reply to Andi Kleen
Quoted 4 lines · Andi Kleen

From: Andi Kleen <[email protected]>

This is mainly useful for simple statistic counters. Essentially read-only, writing only clears.

Nice...

Quoted 1 line

+#define param_check_percpu_uint(name, p) param_check_uint

This is wrong; will it even compile? It should also do the __percpu annotation so hopefully sparse will catch any misuses, eg:

#define param_check_percpu_uint(name, p) \
__param_check(name, p, __percpu unsigned int)

The rest looks good, but I'll need a user :)

Cheers,
Rusty.