From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj2-f13.google.com (mail-pj2-f13.google.com [74.125.227.141]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 13373374A1F for ; Sun, 13 Sep 2026 02:43:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.227.141 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789267425; cv=none; b=X3/E/mLOb3JMQ7POm6v54lVrtYkgORM1OqYWOTtcgMQ6hJPfKyIuWfFoM23BhofZfUpz4bF7Ady5r3To1APBrfDo9K5kWi/WmtNLz/rwzmWM6/rQ+GkH0JS9k57mfZ+xvqr4J0NgAe6tmexd4Mvlhj3z0OLXcEB/b8d3Y6Ra+VI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789267425; c=relaxed/simple; bh=ijHICcSbs6CGQMcJleyByNbTrwLXVs12RNtJRFwhp98=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=oKMBuNbADftpFwsH2IwEmI2IjmcGtnncmtuz0mBK5oA60MwS6e9TmM3wBWflzB2kXLMx3P4DNQMdvA1jDaCZ6eiscxvPLyWRzbeuF5t4YD6ZR7rElM4qnZqHWMIC/I8bF5sYs7fezkwBCwI+h6p9WGOeLOlp9jJLHXgRvHZPY1k= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=BzX8q5nt; arc=none smtp.client-ip=74.125.227.141 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="BzX8q5nt" Received: by mail-pj2-f13.google.com with SMTP id 98e67ed59e1d1-396ccc09d65so969689a91.3 for ; Sat, 12 Sep 2026 19:43:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1789267423; x=1789872223; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=df+C7fWEBLA6Kz/oTAntnR6MQG40i5qNStgvucC+a8E=; b=BzX8q5ntJMZI2CpAPA8r/szb4g2Botwd4/zUb5zTKnfNJQSooIl4N9iL0cc09WMbmy W0FBvXPAg1g8c8iU+LpzI9U1jOaQY4CzomvTncu5aQebgYLnQhws7o7OoDYYZqdQ1nPy qNzB68TSEBEsaYhLiIK+nc80DKucw2/HAOBGPMyUP0J775hSuFMIXSE2KekhUohoJr2/ WvTGAjp8DxiGfZim/XPjjavoT3buiEV4fYyNtHBJEq5BEuNWjwSrHlk/i9MOp6UJj0GT OuJ+oYv/dV9ex8q1Kp0uneghDs73ll0Nk9yblAItLf5QV11QWNcQ0TqM/1B07MOToeLJ zsvQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1789267423; x=1789872223; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=df+C7fWEBLA6Kz/oTAntnR6MQG40i5qNStgvucC+a8E=; b=ZjW/tHpyHS9I8ILB/bTtQiM7t/6g4pUoi13Teli1kRHmIVV2Xw52P9wKLHYi6H/toT l+jFkWvOfDR/cKJHPLs7xjBX1/xAC4adg7ss3GaV3c30R8deZR9NDLmFdaY9HCfwsbtu 7Dif1Ib/BlWBTBXF1T38xA1SWMn5bwuMyxSvNHDfJwNasptuTR6dCXsm/W6AQQZb3E38 B1SIWAizGjiAj/6EDxsOersS6FnigSvHdZvTeDGJ+dUcxukpQUGP5w0yswpWGGGpOXWA nx3t19YGhp3Re7G38rJbTdTCdGE600My8giKizaM8FudRRmjLwhaJGlC6YfMnWxt6ckC ObLg== X-Gm-Message-State: AFuF++ltT7cy6i81dgQ4z1LWGZfz3/yJZt0jMDNtxD726NNJWAgOiNOZ nTJ6vj2AdPnmKSIxIjXim76dthPFKcp2IFWEtOd9t/9++T9INM3DHbOa X-Gm-Gg: AYBFou0JBnduo0YASQmJV5FYWKKpo5OnA0WpzVUNv7e03/jtSasNMgY74OaJvQiOxdu zeFyF1O+IpYCvr/gJdJRw8KdcEyZYEMTTNjtbeMvcn83YFhPnp0DCPaGz2496IEHOoP7lMYtPOD rIrFCrymjcxfWArfhUrapen2Vd9kSCmpjKuA7h5KGe/Lt3NBK6zOd1g0ll/RSyfL4l3OKCGACPi 6ZtLw+hckAHqqeAU5ysrI3P4sPTWTfwnzdJGjXOuT73ltmTP0pDfHUMhvIn/a4AeaInDh+CaBH0 U96z95k3mKQhHDv734WkMs+af7Z0z0Ln2i1oQZX2QBP+DoUyEmTKD9ijO+H1NDx8YoSuyb66OKj aewwiaKjUUuOraza1JLmDilGlSo0aS/NyuAYWhSN1Erd8P3GegdZ9hgJUYpfg3D7TyIjGjI8nND 23XBmILuPOpDHRZu3eheETfIh0jK1aBUkKRxEIiR/bMRYaWsz9DaGiNbvkZ0vWyufuiTag0OEBR oUSM/kgTc8nBVLArF8BxjnVj1JoK6hTq+6fPqvgPbrF0APPpgcaLztOsQGkrAxN+ZpgpQ== X-Received: by 2002:a17:90b:1d44:b0:38e:57a3:f218 with SMTP id 98e67ed59e1d1-39d9c234a8emr19768343a91.13.1789267423373; Sat, 12 Sep 2026 19:43:43 -0700 (PDT) Received: from localhost.localdomain ([240e:46d:2000:1e66:594b:84e9:394f:396d]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-39d994cc978sm12730756a91.9.2026.09.12.19.43.39 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Sat, 12 Sep 2026 19:43:43 -0700 (PDT) From: Yafang Shao To: jpoimboe@kernel.org, jikos@kernel.org, mbenes@suse.cz, pmladek@suse.com, joe.lawrence@redhat.com, song@kernel.org Cc: live-patching@vger.kernel.org, Yafang Shao Subject: [PATCH v9 9/9] selftests/livepatch: Add function test for provides/obsoletes Date: Sun, 13 Sep 2026 10:42:28 +0800 Message-ID: <20260913024228.72317-10-laoar.shao@gmail.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20260913024228.72317-1-laoar.shao@gmail.com> References: <20260913024228.72317-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: live-patching@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Livepatches with different provides ids must not modify the same function, unless they share the same provides id or the second livepatch obsoletes the first one. Add test scenarios that verify this rule on the combination of patched functions and provides ids. These tests are skipped on kernels that still support the legacy "replace" attribute. Suggested-by: Petr Mladek Signed-off-by: Yafang Shao --- .../livepatch/test-provides-obsoletes.sh | 188 ++++++++++++++++++ .../selftests/livepatch/test_modules/Makefile | 1 + .../test_modules/test_klp_meminfo_lp2.c | 1 + 3 files changed, 190 insertions(+) create mode 100644 tools/testing/selftests/livepatch/test_modules/test_klp_meminfo_lp2.c diff --git a/tools/testing/selftests/livepatch/test-provides-obsoletes.sh b/tools/testing/selftests/livepatch/test-provides-obsoletes.sh index f2d23914b7d3..4c635492b341 100755 --- a/tools/testing/selftests/livepatch/test-provides-obsoletes.sh +++ b/tools/testing/selftests/livepatch/test-provides-obsoletes.sh @@ -5,6 +5,7 @@ . $(dirname $0)/functions.sh MOD_MEMINFO=test_klp_meminfo_lp +MOD_MEMINFO2=test_klp_meminfo_lp2 MOD_CMDLINE=test_klp_cmdline_lp MOD_STATE=test_klp_state MOD_STATE2=test_klp_state2 @@ -452,4 +453,191 @@ $MOD_STATE: free_loglevel_state: freeing space for the stored console_loglevel livepatch: '$MOD_STATE': unpatching complete % rmmod $MOD_STATE" + +# - load two livepatches that modify the same function +# (meminfo_proc_show), both with provides=1. The second livepatch +# replaces the first one because of the same provides id, even +# though they share the patched function. +# - verify that the first livepatch is gone from sysfs (replaced) +# and that the second one is enabled +# - unload the replacing livepatch, then the replaced one + +start_test "same function, replaced by provides" + +load_lp $MOD_MEMINFO provides=1 + +grep 'live patched' /proc/meminfo > /dev/kmsg + +load_lp $MOD_MEMINFO2 provides=1 + +loop_until '[[ ! -e "$SYSFS_KLP_DIR/$MOD_MEMINFO" ]]' || + die "failed to replace $MOD_MEMINFO" + +grep 'live patched' /proc/meminfo > /dev/kmsg + +check_sysfs_value "$MOD_MEMINFO2" "provides" "1" + +disable_lp $MOD_MEMINFO2 +unload_lp $MOD_MEMINFO2 + +grep 'live patched' /proc/meminfo > /dev/kmsg + +unload_lp $MOD_MEMINFO + +check_result "% insmod test_modules/$MOD_MEMINFO.ko provides=1 +livepatch: enabling patch '$MOD_MEMINFO' +livepatch: '$MOD_MEMINFO': initializing patching transition +livepatch: '$MOD_MEMINFO': starting patching transition +livepatch: '$MOD_MEMINFO': completing patching transition +livepatch: '$MOD_MEMINFO': patching complete +$MOD_MEMINFO: this has been live patched +% insmod test_modules/$MOD_MEMINFO2.ko provides=1 +livepatch: enabling patch '$MOD_MEMINFO2' +livepatch: '$MOD_MEMINFO2': initializing patching transition +livepatch: '$MOD_MEMINFO2': starting patching transition +livepatch: '$MOD_MEMINFO2': completing patching transition +livepatch: '$MOD_MEMINFO2': patching complete +$MOD_MEMINFO2: this has been live patched +% echo 0 > $SYSFS_KLP_DIR/$MOD_MEMINFO2/enabled +livepatch: '$MOD_MEMINFO2': initializing unpatching transition +livepatch: '$MOD_MEMINFO2': starting unpatching transition +livepatch: '$MOD_MEMINFO2': completing unpatching transition +livepatch: '$MOD_MEMINFO2': unpatching complete +% rmmod $MOD_MEMINFO2 +% rmmod $MOD_MEMINFO" + + +# - load a livepatch that modifies meminfo_proc_show with provides=1, +# then try to load another livepatch that modifies the same +# function with a different provides id (provides=2). +# - since the provides ids differ, the second livepatch would not +# replace the first one; the same function can not be handled by +# two different provides groups in parallel, so the load must fail. +# - unload the first livepatch + +start_test "same function, can't be loaded in parallel" + +load_lp $MOD_MEMINFO provides=1 +load_failing_mod $MOD_MEMINFO2 provides=2 + +disable_lp $MOD_MEMINFO +unload_lp $MOD_MEMINFO + +check_result "% insmod test_modules/$MOD_MEMINFO.ko provides=1 +livepatch: enabling patch '$MOD_MEMINFO' +livepatch: '$MOD_MEMINFO': initializing patching transition +livepatch: '$MOD_MEMINFO': starting patching transition +livepatch: '$MOD_MEMINFO': completing patching transition +livepatch: '$MOD_MEMINFO': patching complete +% insmod test_modules/$MOD_MEMINFO2.ko provides=2 +livepatch: Livepatch patch ($MOD_MEMINFO2) is not compatible with the already installed livepatches. +insmod: ERROR: could not insert module test_modules/$MOD_MEMINFO2.ko: Invalid parameters +% echo 0 > $SYSFS_KLP_DIR/$MOD_MEMINFO/enabled +livepatch: '$MOD_MEMINFO': initializing unpatching transition +livepatch: '$MOD_MEMINFO': starting unpatching transition +livepatch: '$MOD_MEMINFO': completing unpatching transition +livepatch: '$MOD_MEMINFO': unpatching complete +% rmmod $MOD_MEMINFO" + + +# - load a livepatch that modifies meminfo_proc_show with provides=1, +# then another livepatch with provides=1 that modifies a different +# function (cmdline_proc_show). The second one replaces the first +# one because of the same provides id. +# - since the patched functions differ, the replacement does not need +# to restore the old function body; the transition only switches +# the ftrace nops, i.e. the atomic replace (nops) path. +# - after the replacement /proc/meminfo is no longer patched, while +# /proc/cmdline is patched +# - unload the replacing livepatch, then the replaced one + +start_test "different functions, atomic replace works (nops)" + +load_lp $MOD_MEMINFO provides=1 + +grep 'live patched' /proc/meminfo > /dev/kmsg + +load_lp $MOD_CMDLINE provides=1 + +grep 'live patched' /proc/meminfo > /dev/kmsg +grep 'live patched' /proc/cmdline > /dev/kmsg + +disable_lp $MOD_CMDLINE +unload_lp $MOD_CMDLINE +unload_lp $MOD_MEMINFO + +check_result "% insmod test_modules/$MOD_MEMINFO.ko provides=1 +livepatch: enabling patch '$MOD_MEMINFO' +livepatch: '$MOD_MEMINFO': initializing patching transition +livepatch: '$MOD_MEMINFO': starting patching transition +livepatch: '$MOD_MEMINFO': completing patching transition +livepatch: '$MOD_MEMINFO': patching complete +$MOD_MEMINFO: this has been live patched +% insmod test_modules/$MOD_CMDLINE.ko provides=1 +livepatch: enabling patch '$MOD_CMDLINE' +livepatch: '$MOD_CMDLINE': initializing patching transition +livepatch: '$MOD_CMDLINE': starting patching transition +livepatch: '$MOD_CMDLINE': completing patching transition +livepatch: '$MOD_CMDLINE': patching complete +$MOD_CMDLINE: this has been live patched +% echo 0 > $SYSFS_KLP_DIR/$MOD_CMDLINE/enabled +livepatch: '$MOD_CMDLINE': initializing unpatching transition +livepatch: '$MOD_CMDLINE': starting unpatching transition +livepatch: '$MOD_CMDLINE': completing unpatching transition +livepatch: '$MOD_CMDLINE': unpatching complete +% rmmod $MOD_CMDLINE +% rmmod $MOD_MEMINFO" + + +# - load two livepatches with different provides ids (1 and 2) that +# modify different functions (meminfo_proc_show and +# cmdline_proc_show). Neither of them replaces the other, so both +# livepatches must coexist. +# - verify that both /proc/meminfo and /proc/cmdline are patched +# - disable and unload both livepatches + +start_test "different functions, install in parallel" + +load_lp $MOD_MEMINFO provides=1 + +grep 'live patched' /proc/meminfo > /dev/kmsg + +load_lp $MOD_CMDLINE provides=2 + +grep 'live patched' /proc/meminfo > /dev/kmsg +grep 'live patched' /proc/cmdline > /dev/kmsg + +disable_lp $MOD_MEMINFO +unload_lp $MOD_MEMINFO +disable_lp $MOD_CMDLINE +unload_lp $MOD_CMDLINE + +check_result "% insmod test_modules/$MOD_MEMINFO.ko provides=1 +livepatch: enabling patch '$MOD_MEMINFO' +livepatch: '$MOD_MEMINFO': initializing patching transition +livepatch: '$MOD_MEMINFO': starting patching transition +livepatch: '$MOD_MEMINFO': completing patching transition +livepatch: '$MOD_MEMINFO': patching complete +$MOD_MEMINFO: this has been live patched +% insmod test_modules/$MOD_CMDLINE.ko provides=2 +livepatch: enabling patch '$MOD_CMDLINE' +livepatch: '$MOD_CMDLINE': initializing patching transition +livepatch: '$MOD_CMDLINE': starting patching transition +livepatch: '$MOD_CMDLINE': completing patching transition +livepatch: '$MOD_CMDLINE': patching complete +$MOD_MEMINFO: this has been live patched +$MOD_CMDLINE: this has been live patched +% echo 0 > $SYSFS_KLP_DIR/$MOD_MEMINFO/enabled +livepatch: '$MOD_MEMINFO': initializing unpatching transition +livepatch: '$MOD_MEMINFO': starting unpatching transition +livepatch: '$MOD_MEMINFO': completing unpatching transition +livepatch: '$MOD_MEMINFO': unpatching complete +% rmmod $MOD_MEMINFO +% echo 0 > $SYSFS_KLP_DIR/$MOD_CMDLINE/enabled +livepatch: '$MOD_CMDLINE': initializing unpatching transition +livepatch: '$MOD_CMDLINE': starting unpatching transition +livepatch: '$MOD_CMDLINE': completing unpatching transition +livepatch: '$MOD_CMDLINE': unpatching complete +% rmmod $MOD_CMDLINE" + exit 0 diff --git a/tools/testing/selftests/livepatch/test_modules/Makefile b/tools/testing/selftests/livepatch/test_modules/Makefile index 732e0003930b..0c17754997d2 100644 --- a/tools/testing/selftests/livepatch/test_modules/Makefile +++ b/tools/testing/selftests/livepatch/test_modules/Makefile @@ -2,6 +2,7 @@ TESTMODS_DIR := $(realpath $(dir $(abspath $(lastword $(MAKEFILE_LIST))))) KDIR ?= /lib/modules/$(shell uname -r)/build obj-m += test_klp_meminfo_lp.o \ + test_klp_meminfo_lp2.o \ test_klp_callbacks_busy.o \ test_klp_callbacks_demo.o \ test_klp_callbacks_demo2.o \ diff --git a/tools/testing/selftests/livepatch/test_modules/test_klp_meminfo_lp2.c b/tools/testing/selftests/livepatch/test_modules/test_klp_meminfo_lp2.c new file mode 100644 index 000000000000..c82827207f10 --- /dev/null +++ b/tools/testing/selftests/livepatch/test_modules/test_klp_meminfo_lp2.c @@ -0,0 +1 @@ +#include "test_klp_meminfo_lp.c" -- 2.52.0