[PATCH v2 1/3] livepatch: Add force sysfs attribute
From: Miroslav Benes
Date: Thu Aug 10 2017 - 06:48:31 EST
Add read-write force attribute to livepatch sysfs infrastructure. We can
use it later to force couple of events during a live patching process.
Be it a sending of a fake signal or forcing of the tasks' successful
conversion.
It does not make sense to use the force facility when there is no
transaction running (although there is no harm doing that). Therefore we
limit it only to situations when klp_transition_patch variable is set.
Normally, klp_mutex lock should be acquired, because the variable is
shared. However that would hold the action back unnecessarily because of
waiting for the lock, so we omit the lock here. The resulting race
window is harmless (using force when there is no transaction running).
Signed-off-by: Miroslav Benes <mbenes@xxxxxxx>
---
Documentation/ABI/testing/sysfs-kernel-livepatch | 12 +++++++
Documentation/livepatch/livepatch.txt | 7 ++++
kernel/livepatch/core.c | 43 ++++++++++++++++++++++++
3 files changed, 62 insertions(+)
diff --git a/Documentation/ABI/testing/sysfs-kernel-livepatch b/Documentation/ABI/testing/sysfs-kernel-livepatch
index d5d39748382f..b7a487ca8852 100644
--- a/Documentation/ABI/testing/sysfs-kernel-livepatch
+++ b/Documentation/ABI/testing/sysfs-kernel-livepatch
@@ -8,6 +8,18 @@ Contact: live-patching@xxxxxxxxxxxxxxx
The /sys/kernel/livepatch directory contains subdirectories for
each loaded live patch module.
+What: /sys/kernel/livepatch/force
+Date: Jul 2017
+KernelVersion: 4.14.0
+Contact: live-patching@xxxxxxxxxxxxxxx
+Description:
+ The attribute allows administrator to affect the course of an
+ existing transition.
+
+ Reading from the file returns all available operations.
+
+ Writing one of the strings to the file executes the operation.
+
What: /sys/kernel/livepatch/<patch>
Date: Nov 2014
KernelVersion: 3.19.0
diff --git a/Documentation/livepatch/livepatch.txt b/Documentation/livepatch/livepatch.txt
index ecdb18104ab0..9c9966be328d 100644
--- a/Documentation/livepatch/livepatch.txt
+++ b/Documentation/livepatch/livepatch.txt
@@ -178,6 +178,10 @@ transition, it shows -1. Any tasks which are blocking the transition
can be signaled with SIGSTOP and SIGCONT to force them to change their
patched state.
+Administrator can also affect a transition through /sys/kernel/livepatch/force
+attribute. Reading from the file returns all available operations. Writing one
+of the strings to the file executes the operation.
+
3.1 Adding consistency model support to new architectures
---------------------------------------------------------
@@ -435,6 +439,9 @@ Information about the registered patches can be found under
/sys/kernel/livepatch. The patches could be enabled and disabled
by writing there.
+/sys/kernel/livepatch/force attribute allows administrator to affect a patching
+operation.
+
See Documentation/ABI/testing/sysfs-kernel-livepatch for more details.
diff --git a/kernel/livepatch/core.c b/kernel/livepatch/core.c
index b9628e43c78f..79022b7eca2c 100644
--- a/kernel/livepatch/core.c
+++ b/kernel/livepatch/core.c
@@ -437,6 +437,7 @@ EXPORT_SYMBOL_GPL(klp_enable_patch);
* Sysfs Interface
*
* /sys/kernel/livepatch
+ * /sys/kernel/livepatch/force
* /sys/kernel/livepatch/<patch>
* /sys/kernel/livepatch/<patch>/enabled
* /sys/kernel/livepatch/<patch>/transition
@@ -444,6 +445,41 @@ EXPORT_SYMBOL_GPL(klp_enable_patch);
* /sys/kernel/livepatch/<patch>/<object>/<function,sympos>
*/
+/*
+ * NOTE: The following function are currently empty. The intention is to keep
+ * sysfs glue separate and thus make the review easier.
+ */
+static ssize_t force_show(struct kobject *kobj,
+ struct kobj_attribute *attr, char *buf)
+{
+ return sprintf(buf, "No operation is currently permitted.\n");
+}
+
+static ssize_t force_store(struct kobject *kobj, struct kobj_attribute *attr,
+ const char *buf, size_t count)
+{
+ /*
+ * klp_mutex lock is not grabbed here intentionally. It is not really
+ * needed. The race window is harmless and grabbing the lock would only
+ * hold the action back.
+ */
+ if (!klp_transition_patch) {
+ pr_info("no patching in progress, forced action ineffective\n");
+ return -EINVAL;
+ }
+
+ return -EINVAL;
+}
+
+static struct kobj_attribute force_kobj_attr = __ATTR_RW(force);
+static struct attribute *klp_attrs[] = {
+ &force_kobj_attr.attr,
+ NULL
+};
+static struct attribute_group klp_sysfs_group = {
+ .attrs = klp_attrs,
+};
+
static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr,
const char *buf, size_t count)
{
@@ -954,6 +990,13 @@ static int __init klp_init(void)
if (!klp_root_kobj)
return -ENOMEM;
+ ret = sysfs_create_group(klp_root_kobj, &klp_sysfs_group);
+ if (ret) {
+ pr_err("cannot create livepatch attributes in sysfs\n");
+ kobject_put(klp_root_kobj);
+ return ret;
+ }
+
return 0;
}
--
2.13.3