Blame SOURCES/0100-kpatch-wait-for-module-ref-counts-on-unload.patch

707f0e
From cdee6bd650a35075515d4fe2bb67657811c9640c Mon Sep 17 00:00:00 2001
707f0e
From: Joe Lawrence <joe.lawrence@redhat.com>
707f0e
Date: Mon, 16 Nov 2020 15:21:59 -0500
707f0e
Subject: [PATCH] kpatch: wait for module ref counts on unload
707f0e
707f0e
There exists a very small timing window in which "kpatch unload" gets to
707f0e
its "rmmod" step before the kpatch-patch module's reference count has
707f0e
cleared and the "rmmod" fails.
707f0e
707f0e
This is only a transient problem, but we can adopt code from upstream
707f0e
livepatch kselftests which wait for the module refcounts to settle
707f0e
before moving onto "rmmod".
707f0e
707f0e
A small wrinkle is that this is not supported by the older kpatch.ko
707f0e
core.  The price for circumventing the activeness safety check via
707f0e
KPATCH_FORCE_UNSAFE is that it must leave the kpatch patch modules in
707f0e
place (see e1890e627a9b ("prevent rmmod of forced modules")).
707f0e
707f0e
Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
707f0e
---
707f0e
 kpatch/kpatch | 40 ++++++++++++++++++++++++++++++++++++++--
707f0e
 1 file changed, 38 insertions(+), 2 deletions(-)
707f0e
707f0e
diff --git a/kpatch/kpatch b/kpatch/kpatch
707f0e
index bca8f41..b35b742 100755
707f0e
--- a/kpatch/kpatch
707f0e
+++ b/kpatch/kpatch
707f0e
@@ -28,6 +28,7 @@ SCRIPTDIR="$(readlink -f "$(dirname "$(type -p "$0")")")"
707f0e
 VERSION="0.9.2"
707f0e
 POST_ENABLE_WAIT=15	# seconds
707f0e
 POST_SIGNAL_WAIT=60	# seconds
707f0e
+MODULE_REF_WAIT=15	# seconds
707f0e
 
707f0e
 # How many times to try loading the patch if activeness safety check fails.
707f0e
 MAX_LOAD_ATTEMPTS=5
707f0e
@@ -125,6 +126,10 @@ find_core_module() {
707f0e
 	return 1
707f0e
 }
707f0e
 
707f0e
+kpatch_core_loaded() {
707f0e
+	grep -q -e "T kpatch_register" /proc/kallsyms
707f0e
+}
707f0e
+
707f0e
 core_loaded () {
707f0e
 	grep -q -e "T klp_enable_patch" -e "T kpatch_register" /proc/kallsyms
707f0e
 }
707f0e
@@ -265,6 +270,31 @@ wait_for_patch_transition() {
707f0e
 	return 1
707f0e
 }
707f0e
 
707f0e
+module_ref_count() {
707f0e
+	local modname="$1"
707f0e
+	[[ $(cat "/sys/module/$modname/refcnt" 2>/dev/null) != "0" ]]
707f0e
+}
707f0e
+
707f0e
+wait_for_zero_module_ref_count() {
707f0e
+	local modname="$1"
707f0e
+	local i=0
707f0e
+
707f0e
+	# We can't rely on a zero refcount with kpatch.ko as it
707f0e
+	# implements KPATCH_FORCE_UNSAFE with an additional reference on
707f0e
+	# kpatch-patch modules to avoid potential crashes.
707f0e
+	kpatch_core_loaded && return 0
707f0e
+
707f0e
+	module_ref_count "$modname" || return 0
707f0e
+
707f0e
+	echo "waiting (up to $MODULE_REF_WAIT seconds) for module refcount..."
707f0e
+	for (( i=0; i
707f0e
+		module_ref_count "$modname" || return 0
707f0e
+		sleep 1s
707f0e
+	done
707f0e
+
707f0e
+	return 1
707f0e
+}
707f0e
+
707f0e
 load_module () {
707f0e
 	local module="$1"
707f0e
 
707f0e
@@ -381,10 +411,16 @@ disable_patch_strict () {
707f0e
 }
707f0e
 
707f0e
 remove_module () {
707f0e
-	echo "unloading patch module: $1"
707f0e
+	local modname="$1"
707f0e
+
707f0e
+	if ! wait_for_zero_module_ref_count "$modname"; then
707f0e
+		die "failed to unload module $modname (refcnt)"
707f0e
+	fi
707f0e
+
707f0e
+	echo "unloading patch module: $modname"
707f0e
 	# ignore any error here because rmmod can fail if the module used
707f0e
 	# KPATCH_FORCE_UNSAFE.
707f0e
-	rmmod "$1" 2> /dev/null || return 0
707f0e
+	rmmod "$modname" 2> /dev/null || return 0
707f0e
 }
707f0e
 
707f0e
 unload_module () {
707f0e
-- 
707f0e
2.25.4
707f0e