Skip to contentSkip to conversation

Livepatch shadow variables

PATCH Applied Joe Lawrence 5 participants 8 messages Aug 31, 2017 – Sep 25, 2017

Subsystems (2)
lore
Share & tools
lore

Joe Lawrence

author maintainer
#0 lore raw

PATCH v6 1/1 Livepatch shadow variables

This version cleans up a few minor tweaks requested by Josh and Miroslav.

v6:

- Convert an enum into a function argument flag
- Update code comment that still referenced updating existing shadow variables
- Doc: Add a closing ')' parenthesis character
- Doc: Add a blurb about callers providing mutual exclusion

Joe Lawrence (1):
livepatch: introduce shadow variable API

 Documentation/livepatch/shadow-vars.txt   | 192 +++++++++++++++++++++
 include/linux/livepatch.h                 |   8 +
 kernel/livepatch/Makefile                 |   2 +-
 kernel/livepatch/shadow.c                 | 277 ++++++++++++++++++++++++++++++
 samples/Kconfig                           |   5 +-
 samples/livepatch/Makefile                |   3 +
 samples/livepatch/livepatch-shadow-fix1.c | 173 +++++++++++++++++++
 samples/livepatch/livepatch-shadow-fix2.c | 168 ++++++++++++++++++
 samples/livepatch/livepatch-shadow-mod.c  | 224 ++++++++++++++++++++++++
 9 files changed, 1048 insertions(+), 4 deletions(-)
 create mode 100644 Documentation/livepatch/shadow-vars.txt
 create mode 100644 kernel/livepatch/shadow.c
 create mode 100644 samples/livepatch/livepatch-shadow-fix1.c
 create mode 100644 samples/livepatch/livepatch-shadow-fix2.c
 create mode 100644 samples/livepatch/livepatch-shadow-mod.c
Signature · 1 line
1.8.3.1
author maintainer
#1 lore raw

PATCH v6 1/1 livepatch: introduce shadow variable API

Add exported API for livepatch modules:

klp_shadow_get()
klp_shadow_alloc()
klp_shadow_get_or_alloc()
klp_shadow_free()
klp_shadow_free_all()

that implement "shadow" variables, which allow callers to associate new shadow fields to existing data structures. This is intended to be used by livepatch modules seeking to emulate additions to data structure definitions.

See Documentation/livepatch/shadow-vars.txt for a summary of the new shadow variable API, including a few common use cases.

See samples/livepatch/livepatch-shadow-* for example modules that demonstrate shadow variables.

---
 Documentation/livepatch/shadow-vars.txt   | 192 +++++++++++++++++++++
 include/linux/livepatch.h                 |   8 +
 kernel/livepatch/Makefile                 |   2 +-
 kernel/livepatch/shadow.c                 | 277 ++++++++++++++++++++++++++++++
 samples/Kconfig                           |   5 +-
 samples/livepatch/Makefile                |   3 +
 samples/livepatch/livepatch-shadow-fix1.c | 173 +++++++++++++++++++
 samples/livepatch/livepatch-shadow-fix2.c | 168 ++++++++++++++++++
 samples/livepatch/livepatch-shadow-mod.c  | 224 ++++++++++++++++++++++++
 9 files changed, 1048 insertions(+), 4 deletions(-)
 create mode 100644 Documentation/livepatch/shadow-vars.txt
 create mode 100644 kernel/livepatch/shadow.c
 create mode 100644 samples/livepatch/livepatch-shadow-fix1.c
 create mode 100644 samples/livepatch/livepatch-shadow-fix2.c
 create mode 100644 samples/livepatch/livepatch-shadow-mod.c
Documentation/livepatch/shadow-vars.txt new file +192 -0
new file mode 100644
index 000000000000..e3ffc4301bfa
@@ -0,0 +1,192 @@
+================
+Shadow Variables
+================
+
+Shadow variables are a simple way for livepatch modules to associate
+additional "shadow" data with existing data structures.  Shadow data is
+allocated separately from parent data structures, which are left
+unmodified.  The shadow variable API described in this document is used
+to allocate/attach and detach/release shadow variables to their parents.
+
+The implementation introduces a global, in-kernel hashtable that
+associates pointers to parent objects and a numeric identifier of the
+shadow data.  The numeric identifier is a simple enumeration that may be
+used to describe shadow variable version, class or type, etc.  More
+specifically, the parent pointer serves as the hashtable key while the
+numeric id subsequently filters hashtable queries.  Multiple shadow
+variables may attach to the same parent object, but their numeric
+identifier distinguishes between them.
+
+
+1. Brief API summary
+====================
+
+(See the full API usage docbook notes in livepatch/shadow.c.)
+
+A hashtable references all shadow variables.  These references are
+stored and retrieved through a <obj, id> pair.
+
+* The klp_shadow variable data structure encapsulates both tracking
+meta-data and shadow-data:
+  - meta-data
+    - obj - pointer to parent object
+    - id - data identifier
+  - data[] - storage for shadow data
+
+It is important to note that the klp_shadow_alloc() and
+klp_shadow_get_or_alloc() calls, described below, store a *copy* of the
+data that the functions are provided.  Callers should provide whatever
+mutual exclusion is required of the shadow data.
+
+* klp_shadow_get() - retrieve a shadow variable data pointer
+  - search hashtable for <obj, id> pair
+
+* klp_shadow_alloc() - allocate and add a new shadow variable
+  - search hashtable for <obj, id> pair
+  - if exists
+    - WARN and return NULL
+  - if <obj, id> doesn't already exist
+    - allocate a new shadow variable
+    - copy data into the new shadow variable
+    - add <obj, id> to the global hashtable
+
+* klp_shadow_get_or_alloc() - get existing or alloc a new shadow variable
+  - search hashtable for <obj, id> pair
+  - if exists
+    - return existing shadow variable
+  - if <obj, id> doesn't already exist
+    - allocate a new shadow variable
+    - copy data into the new shadow variable
+    - add <obj, id> pair to the global hashtable
+
+* klp_shadow_free() - detach and free a <obj, id> shadow variable
+  - find and remove a <obj, id> reference from global hashtable
+    - if found, free shadow variable
+
+* klp_shadow_free_all() - detach and free all <*, id> shadow variables
+  - find and remove any <*, id> references from global hashtable
+    - if found, free shadow variable
+
+
+2. Use cases
+============
+
+(See the example shadow variable livepatch modules in samples/livepatch/
+for full working demonstrations.)
+
+For the following use-case examples, consider commit 1d147bfa6429
+("mac80211: fix AP powersave TX vs.  wakeup race"), which added a
+spinlock to net/mac80211/sta_info.h :: struct sta_info.  Each use-case
+example can be considered a stand-alone livepatch implementation of this
+fix.
+
+
+Matching parent's lifecycle
+---------------------------
+
+If parent data structures are frequently created and destroyed, it may
+be easiest to align their shadow variables lifetimes to the same
+allocation and release functions.  In this case, the parent data
+structure is typically allocated, initialized, then registered in some
+manner.  Shadow variable allocation and setup can then be considered
+part of the parent's initialization and should be completed before the
+parent "goes live" (ie, any shadow variable get-API requests are made
+for this <obj, id> pair.)
+
+For commit 1d147bfa6429, when a parent sta_info structure is allocated,
+allocate a shadow copy of the ps_lock pointer, then initialize it:
+
+#define PS_LOCK 1
+struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
+				const u8 *addr, gfp_t gfp)
+{
+	struct sta_info *sta;
+	spinlock_t *ps_lock;
+
+	/* Parent structure is created */
+	sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
+
+	/* Attach a corresponding shadow variable, then initialize it */
+	ps_lock = klp_shadow_alloc(sta, PS_LOCK, NULL, sizeof(ps_lock), gfp);
+	if (!ps_lock)
+		goto shadow_fail;
+	spin_lock_init(ps_lock);
+	...
+
+When requiring a ps_lock, query the shadow variable API to retrieve one
+for a specific struct sta_info:
+
+void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
+{
+	spinlock_t *ps_lock;
+
+	/* sync with ieee80211_tx_h_unicast_ps_buf */
+	ps_lock = klp_shadow_get(sta, PS_LOCK);
+	if (ps_lock)
+		spin_lock(ps_lock);
+	...
+
+When the parent sta_info structure is freed, first free the shadow
+variable:
+
+void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
+{
+	klp_shadow_free(sta, PS_LOCK);
+	kfree(sta);
+	...
+
+
+In-flight parent objects
+------------------------
+
+Sometimes it may not be convenient or possible to allocate shadow
+variables alongside their parent objects.  Or a livepatch fix may
+require shadow varibles to only a subset of parent object instances.  In
+these cases, the klp_shadow_get_or_alloc() call can be used to attach
+shadow variables to parents already in-flight.
+
+For commit 1d147bfa6429, a good spot to allocate a shadow spinlock is
+inside ieee80211_sta_ps_deliver_wakeup():
+
+#define PS_LOCK 1
+void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
+{
+	DEFINE_SPINLOCK(ps_lock_fallback);
+	spinlock_t *ps_lock;
+
+	/* sync with ieee80211_tx_h_unicast_ps_buf */
+	ps_lock = klp_shadow_get_or_alloc(sta, PS_LOCK,
+			&ps_lock_fallback, sizeof(ps_lock_fallback),
+			GFP_ATOMIC);
+	if (ps_lock)
+		spin_lock(ps_lock);
+	...
+
+This usage will create a shadow variable, only if needed, otherwise it
+will use one that was already created for this <obj, id> pair.
+
+Like the previous use-case, the shadow spinlock needs to be cleaned up.
+A shadow variable can be freed just before its parent object is freed,
+or even when the shadow variable itself is no longer required.
+
+
+Other use-cases
+---------------
+
+Shadow variables can also be used as a flag indicating that a data
+structure was allocated by new, livepatched code.  In this case, it
+doesn't matter what data value the shadow variable holds, its existence
+suggests how to handle the parent object.
+
+
+3. References
+=============
+
+* https://github.com/dynup/kpatch
+The livepatch implementation is based on the kpatch version of shadow
+variables.
+
+* http://files.mkgnu.net/files/dynamos/doc/papers/dynamos_eurosys_07.pdf
+Dynamic and Adaptive Updates of Non-Quiescent Subsystems in Commodity
+Operating System Kernels (Kritis Makris, Kyung Dong Ryu 2007) presented
+a datatype update technique called "shadow data structures".
include/linux/livepatch.h +8 -0
index 194991ef9347..d08eddc00497 100644
@@ -164,6 +164,14 @@ static inline bool klp_have_reliable_stack(void)
 	       IS_ENABLED(CONFIG_HAVE_RELIABLE_STACKTRACE);
 }
 
+void *klp_shadow_get(void *obj, unsigned long id);
+void *klp_shadow_alloc(void *obj, unsigned long id, void *data,
+		       size_t size, gfp_t gfp_flags);
+void *klp_shadow_get_or_alloc(void *obj, unsigned long id, void *data,
+			      size_t size, gfp_t gfp_flags);
+void klp_shadow_free(void *obj, unsigned long id);
+void klp_shadow_free_all(unsigned long id);
+
 #else /* !CONFIG_LIVEPATCH */
 
 static inline int klp_module_coming(struct module *mod) { return 0; }
kernel/livepatch/Makefile +1 -1
index 2b8bdb1925da..b36ceda6488e 100644
@@ -1,3 +1,3 @@
 obj-$(CONFIG_LIVEPATCH) += livepatch.o
 
-livepatch-objs := core.o patch.o transition.o
+livepatch-objs := core.o patch.o shadow.o transition.o
kernel/livepatch/shadow.c new file +277 -0

277 lines not on this page · raw message

samples/Kconfig +2 -3
index 9cb63188d3ef..c332a3b9de05 100644
@@ -71,11 +71,10 @@ config SAMPLE_RPMSG_CLIENT
 	  the rpmsg bus.
 
 config SAMPLE_LIVEPATCH
-	tristate "Build live patching sample -- loadable modules only"
+	tristate "Build live patching samples -- loadable modules only"
 	depends on LIVEPATCH && m
 	help
-	  Builds a sample live patch that replaces the procfs handler
-	  for /proc/cmdline to print "this has been live patched".
+	  Build sample live patch demonstrations.
 
 config SAMPLE_CONFIGFS
 	tristate "Build configfs patching sample -- loadable modules only"
samples/livepatch/Makefile +3 -0
index 10319d7ea0b1..539e81d433cd 100644
@@ -1 +1,4 @@
 obj-$(CONFIG_SAMPLE_LIVEPATCH) += livepatch-sample.o
+obj-$(CONFIG_SAMPLE_LIVEPATCH) += livepatch-shadow-mod.o
+obj-$(CONFIG_SAMPLE_LIVEPATCH) += livepatch-shadow-fix1.o
+obj-$(CONFIG_SAMPLE_LIVEPATCH) += livepatch-shadow-fix2.o
samples/livepatch/livepatch-shadow-fix1.c new file +173 -0

173 lines not on this page · raw message

samples/livepatch/livepatch-shadow-fix2.c new file +168 -0

168 lines not on this page · raw message

samples/livepatch/livepatch-shadow-mod.c new file +224 -0

224 lines not on this page · raw message

Signature · 1 line
1.8.3.1
Acked-by on v6 1/1
#2 lore raw

Re: [PATCH v6] livepatch: introduce shadow variable API

kernel/livepatch/shadow.c line 123

Quoted 8 lines · Joe Lawrence
kernel/livepatch/shadow.c new file +8 -0
+void *__klp_shadow_get_or_alloc(void *obj, unsigned long id, void *data,
+		       size_t size, gfp_t gfp_flags, bool warn_on_exist)
+{
+	struct klp_shadow *new_shadow;
+	void *shadow_data;
+	unsigned long flags;
+
+	/* Check if the shadow variable if <obj, id> already exists */

Grammar fix: either

s/if the shadow variable//

or

s/if <obj, id>//

Otherwise:

Signature · 1 line
Josh
author maintainer
#3 lore raw

Re: [PATCH v6] livepatch: introduce shadow variable API

Quoted 14 lines · Josh Poimboeuf

kernel/livepatch/shadow.c line 123

Quoted 8 lines · Joe Lawrence
kernel/livepatch/shadow.c new file +8 -0
+void *__klp_shadow_get_or_alloc(void *obj, unsigned long id, void *data,
+		       size_t size, gfp_t gfp_flags, bool warn_on_exist)
+{
+	struct klp_shadow *new_shadow;
+	void *shadow_data;
+	unsigned long flags;
+
+	/* Check if the shadow variable if <obj, id> already exists */

Grammar fix: either

s/if the shadow variable//

Quoted 8 lines

or

s/if <obj, id>//

Otherwise:

If this is the only outstanding nit, can this be fixed up on merge or does it need a new patch spin?

Thanks,

-- Joe

#4 lore raw

Re: [PATCH v6] livepatch: introduce shadow variable API

Quoted 18 lines · Joe Lawrence
Quoted 18 lines · Joe Lawrence

kernel/livepatch/shadow.c line 123

Quoted 8 lines
kernel/livepatch/shadow.c new file +8 -0
+void *__klp_shadow_get_or_alloc(void *obj, unsigned long id, void *data,
+		       size_t size, gfp_t gfp_flags, bool warn_on_exist)
+{
+	struct klp_shadow *new_shadow;
+	void *shadow_data;
+	unsigned long flags;
+
+	/* Check if the shadow variable if <obj, id> already exists */

Grammar fix: either

s/if the shadow variable//

or

s/if <obj, id>//

Quoted 7 lines
Quoted 4 lines

Otherwise:

If this is the only outstanding nit, can this be fixed up on merge or does it need a new patch spin?

No need to resend just because of this, I'll fix this up manually once applying.

Thanks,

Signature · 2 lines
Jiri Kosina
SUSE Labs
maintainer Acked-by on v6 1/1
#5 lore raw

Re: [PATCH v6] livepatch: introduce shadow variable API

In reply to Joe Lawrence
Quoted 13 lines · Joe Lawrence

Add exported API for livepatch modules:

klp_shadow_get()
klp_shadow_alloc()
klp_shadow_get_or_alloc()
klp_shadow_free()
klp_shadow_free_all()

that implement "shadow" variables, which allow callers to associate new shadow fields to existing data structures. This is intended to be used by livepatch modules seeking to emulate additions to data structure definitions.

Quoted 7 lines

See Documentation/livepatch/shadow-vars.txt for a summary of the new shadow variable API, including a few common use cases.

See samples/livepatch/livepatch-shadow-* for example modules that demonstrate shadow variables.

I'd wait for Petr's review.

Thanks,
Miroslav

Applied
#6 lore raw
In reply to Joe Lawrence
Quoted 5 lines · Joe Lawrence

This version cleans up a few minor tweaks requested by Josh and Miroslav.

v6:

Quoted 8 lines

- Convert an enum into a function argument flag
- Update code comment that still referenced updating existing shadow variables
- Doc: Add a closing ')' parenthesis character
- Doc: Add a blurb about callers providing mutual exclusion

Joe Lawrence (1):
livepatch: introduce shadow variable API

Applied to for-4.15/shadow-variables, so please send any followup fixes on top of that branch.

Thanks!

Signature · 2 lines
Jiri Kosina
SUSE Labs
maintainer Reviewed-by
#7 lore raw
Quoted 13 lines · Jiri Kosina
Quoted 11 lines · Joe Lawrence

This version cleans up a few minor tweaks requested by Josh and Miroslav.

v6:

- Convert an enum into a function argument flag
- Update code comment that still referenced updating existing shadow variables
- Doc: Add a closing ')' parenthesis character
- Doc: Add a blurb about callers providing mutual exclusion

Quoted 5 lines
Quoted 2 lines

Joe Lawrence (1):
livepatch: introduce shadow variable API

Applied to for-4.15/shadow-variables, so please send any followup fixes on top of that branch.

JFYI, v6 in for-4.15/shadow-variables branch looks great to me.

I have only sent 2 small fixes for the documentation in a separate patch. Feel free to squash it into the original patch if you want. Also feel free to use for the original patch:

Best Regards,
Petr