Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp673323rdb; Thu, 30 Nov 2023 15:24:56 -0800 (PST) X-Google-Smtp-Source: AGHT+IF3kxOZ7OntMmjxadrfMOgB7Tvl+Z2MGpOZlgIkzpq9FS/qv0I7mEHEKYYYfIa8auGITgON X-Received: by 2002:a17:90b:4f43:b0:285:bc16:611d with SMTP id pj3-20020a17090b4f4300b00285bc16611dmr14975285pjb.8.1701386696118; Thu, 30 Nov 2023 15:24:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701386696; cv=none; d=google.com; s=arc-20160816; b=OKT5faOToqI7K3p/AtO/1fStKp6CyxKOhGW18PpxLux5CYVPOSLuiWQK8pehAXZTxc F422+9EX3oOMhTTe9I6g02Oue0Xo5jrnjjcn58vjKhcM2BATZ5jPRM4953GC/qrZjONH YSZ4R/GjLkHyUMnVWcLq1LMwCXtsXUOrSE0aJ0GOBjH8XsuQcc4u/TGPyacFMZ+1dkkQ gOf8SXY7SCaRSQHr2tenVkXZxbE4VM/DspMJqhj+/bqHJUnUhi3fHbfPOZs/+OmY/4CQ YQMd0Kkw3wHOCzNKIPbaOqDh/tJ09zgx8GzJX4NTBZlfeEA5Wg7vznZlhZD4WlEfRN5w Y5+g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=8BaziYlHsY9fMqNNZOEE/jx4yQID7G/xy+alsqAMnPY=; fh=sJ72nPiBe5O6cCmOZohsIKPPmgyJ8MjTBIv8cyc33+Q=; b=Tg3JlLGaH3uPiDryF8tnwFo9LAiIRy1XWEKGainMqoc88v8B8jqSpPkIFf90qMiTw+ YNtYkdvg7TQOi2eGH+WHBOgNLlYehewmJjA4KDPBdVUwWDkQVSNaHDN40gE7ZRYr9YI6 3CTddQXYruhn8G3MXvBTlzsMYbZVthFk6t3e/JWLf842BF2ybe8C5I55EblekrnOhHV+ AYPp5WFIy6wkn5aEXRGq4ivK/tvjG5I9xpxtYoXQLdZWLs/BmRBtPlkL9PUFkHIFmP3l mz/M3V8QRL43sNU6XGhnmIwgMdJXJqvHLcSqzTnZsQlO2bycCI71/cAfLB5tSRmdi9Ef +Qww== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=google header.b=e8SLiguR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id ca16-20020a17090af31000b002764fc15dd3si2331928pjb.37.2023.11.30.15.24.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Nov 2023 15:24:56 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=google header.b=e8SLiguR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id C556E8026EE0; Thu, 30 Nov 2023 15:24:44 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377261AbjK3XYY (ORCPT + 99 others); Thu, 30 Nov 2023 18:24:24 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44590 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377251AbjK3XYX (ORCPT ); Thu, 30 Nov 2023 18:24:23 -0500 Received: from mail-il1-x130.google.com (mail-il1-x130.google.com [IPv6:2607:f8b0:4864:20::130]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 23C061703 for ; Thu, 30 Nov 2023 15:24:29 -0800 (PST) Received: by mail-il1-x130.google.com with SMTP id e9e14a558f8ab-35cfd975a53so1339075ab.1 for ; Thu, 30 Nov 2023 15:24:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=google; t=1701386668; x=1701991468; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=8BaziYlHsY9fMqNNZOEE/jx4yQID7G/xy+alsqAMnPY=; b=e8SLiguRBDemfWgnCWd1bixt5DKGZ0C1Mp24gYI0InNqvCyLfV6HxhSst5MxHwwXiJ Suq1zoFRyik5Yp/Rk7ayt4GwnWcwbRfKmL6SKXToVHkwWfrrt4UZ2FFTqr0jgF8Cm1iG kdYE4r5FF3ht2ou/dttP9j6qYBHPS3bJQeHg0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701386668; x=1701991468; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=8BaziYlHsY9fMqNNZOEE/jx4yQID7G/xy+alsqAMnPY=; b=o3nlx0tn/fPpwEt2tjdeaarYCjmeunEbsD5AnYG0h0mwZ0gxQCeCIjbKkYz2OZ5/PM 0QFQ50pq9i0VucJRDfA5gbXKgB1m2FGMbdZ1XG4YvpCW2FZHEDSZIHqVDelF27iJmOmS CIx6GTqi8dfmL3F+sIinV8sqdqRZAhUr0B8DaOJVpcKSSIBKz6zP5Def79NomoKzyj3a merWykTJqfnGbexpyy9wdSIzfma/qJGe4oCGAgp2Kr9oi+AUBksZ1G8BbA/WDm8L/Gvx V9y1SaEo+KDO2RYAHY/q2JIkjIkXERH07hmT04pGcAP5oSs6hcQhRTrjhTAhfj+XWZ4P 6PnA== X-Gm-Message-State: AOJu0Yz69ABDGpf04TnbsHAV13qmaAK7obddi7dbmsKsR86mX4i6Lj3k Q3BZeqs5dlZhULOsTTNPTvrkOg== X-Received: by 2002:a6b:660f:0:b0:7b3:58c4:b894 with SMTP id a15-20020a6b660f000000b007b358c4b894mr20751057ioc.1.1701386668362; Thu, 30 Nov 2023 15:24:28 -0800 (PST) Received: from [192.168.1.128] ([38.175.170.29]) by smtp.gmail.com with ESMTPSA id f19-20020a02b793000000b00451b5feb80fsm550629jam.8.2023.11.30.15.24.27 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 30 Nov 2023 15:24:27 -0800 (PST) Message-ID: Date: Thu, 30 Nov 2023 16:24:26 -0700 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v3 3/3] selftests: livepatch: Test livepatching a heavily called syscall Content-Language: en-US To: Marcos Paulo de Souza , Shuah Khan , Jonathan Corbet , Heiko Carstens , Vasily Gorbik , Alexander Gordeev , Christian Borntraeger , Sven Schnelle , Josh Poimboeuf , Jiri Kosina , Miroslav Benes , Petr Mladek , Joe Lawrence Cc: linux-kselftest@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-s390@vger.kernel.org, live-patching@vger.kernel.org, Shuah Khan References: <20231031-send-lp-kselftests-v3-0-2b1655c2605f@suse.com> <20231031-send-lp-kselftests-v3-3-2b1655c2605f@suse.com> From: Shuah Khan In-Reply-To: <20231031-send-lp-kselftests-v3-3-2b1655c2605f@suse.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Thu, 30 Nov 2023 15:24:45 -0800 (PST) On 10/31/23 15:10, Marcos Paulo de Souza wrote: > The test proves that a syscall can be livepatched. It is interesting > because syscalls are called a tricky way. Also the process gets > livepatched either when sleeping in the userspace or when entering > or leaving the kernel space. > > The livepatch is a bit tricky: > 1. The syscall function name is architecture specific. Also > ARCH_HAS_SYSCALL_WRAPPER must be taken in account. > > 2. The syscall must stay working the same way for other processes > on the system. It is solved by decrementing a counter only > for PIDs of the test processes. It means that the test processes > has to call the livepatched syscall at least once. > > The test creates one userspace process per online cpu. The processes > are calling getpid in a busy loop. The intention is to create random > locations when the livepatch gets enabled. Nothing is guarantted. > The magic is in the randomness. > > Signed-off-by: Marcos Paulo de Souza > --- > tools/testing/selftests/livepatch/Makefile | 4 +- > tools/testing/selftests/livepatch/test-syscall.sh | 53 ++++++++++ > .../selftests/livepatch/test_klp-call_getpid.c | 44 ++++++++ > .../selftests/livepatch/test_modules/Makefile | 3 +- > .../livepatch/test_modules/test_klp_syscall.c | 116 +++++++++++++++++++++ > 5 files changed, 218 insertions(+), 2 deletions(-) > > diff --git a/tools/testing/selftests/livepatch/Makefile b/tools/testing/selftests/livepatch/Makefile > index 119e2bbebe5d..35418a4790be 100644 > --- a/tools/testing/selftests/livepatch/Makefile > +++ b/tools/testing/selftests/livepatch/Makefile > @@ -1,5 +1,6 @@ > # SPDX-License-Identifier: GPL-2.0 > > +TEST_GEN_FILES := test_klp-call_getpid > TEST_GEN_MODS_DIR := test_modules > TEST_PROGS_EXTENDED := functions.sh > TEST_PROGS := \ > @@ -8,7 +9,8 @@ TEST_PROGS := \ > test-shadow-vars.sh \ > test-state.sh \ > test-ftrace.sh \ > - test-sysfs.sh > + test-sysfs.sh \ > + test-syscall.sh > > TEST_FILES := settings > > diff --git a/tools/testing/selftests/livepatch/test-syscall.sh b/tools/testing/selftests/livepatch/test-syscall.sh > new file mode 100755 > index 000000000000..b76a881d4013 > --- /dev/null > +++ b/tools/testing/selftests/livepatch/test-syscall.sh > @@ -0,0 +1,53 @@ > +#!/bin/bash > +# SPDX-License-Identifier: GPL-2.0 > +# Copyright (C) 2023 SUSE > +# Author: Marcos Paulo de Souza > + > +. $(dirname $0)/functions.sh > + > +MOD_SYSCALL=test_klp_syscall > + > +setup_config > + > +# - Start _NRPROC processes calling getpid and load a livepatch to patch the > +# getpid syscall. Check if all the processes transitioned to the livepatched > +# state. > + > +start_test "patch getpid syscall while being heavily hammered" > + > +for i in $(seq 1 $(getconf _NPROCESSORS_ONLN)); do > + ./test_klp-call_getpid & > + pids[$i]="$!" > +done > + > +pid_list=$(echo ${pids[@]} | tr ' ' ',') > +load_lp $MOD_SYSCALL klp_pids=$pid_list > + > +# wait for all tasks to transition to patched state > +loop_until 'grep -q '^0$' /sys/kernel/test_klp_syscall/npids' > + > +pending_pids=$(cat /sys/kernel/test_klp_syscall/npids) > +log "$MOD_SYSCALL: Remaining not livepatched processes: $pending_pids" > + > +for pid in ${pids[@]}; do > + kill $pid || true > +done > + > +disable_lp $MOD_SYSCALL > +unload_lp $MOD_SYSCALL > + > +check_result "% insmod test_modules/$MOD_SYSCALL.ko klp_pids=$pid_list > +livepatch: enabling patch '$MOD_SYSCALL' > +livepatch: '$MOD_SYSCALL': initializing patching transition > +livepatch: '$MOD_SYSCALL': starting patching transition > +livepatch: '$MOD_SYSCALL': completing patching transition > +livepatch: '$MOD_SYSCALL': patching complete > +$MOD_SYSCALL: Remaining not livepatched processes: 0 > +% echo 0 > /sys/kernel/livepatch/$MOD_SYSCALL/enabled > +livepatch: '$MOD_SYSCALL': initializing unpatching transition > +livepatch: '$MOD_SYSCALL': starting unpatching transition > +livepatch: '$MOD_SYSCALL': completing unpatching transition > +livepatch: '$MOD_SYSCALL': unpatching complete > +% rmmod $MOD_SYSCALL" > + > +exit 0 > diff --git a/tools/testing/selftests/livepatch/test_klp-call_getpid.c b/tools/testing/selftests/livepatch/test_klp-call_getpid.c > new file mode 100644 > index 000000000000..ce321a2d7308 > --- /dev/null > +++ b/tools/testing/selftests/livepatch/test_klp-call_getpid.c > @@ -0,0 +1,44 @@ > +// SPDX-License-Identifier: GPL-2.0 > +/* > + * Copyright (C) 2023 SUSE > + * Authors: Libor Pechacek > + * Marcos Paulo de Souza > + */ > + > +#include > +#include > +#include > +#include > +#include > + > +static int stop; > +static int sig_int; > + > +void hup_handler(int signum) > +{ > + stop = 1; > +} > + > +void int_handler(int signum) > +{ > + stop = 1; > + sig_int = 1; > +} > + > +int main(int argc, char *argv[]) > +{ > + long count = 0; > + > + signal(SIGHUP, &hup_handler); > + signal(SIGINT, &int_handler); > + > + while (!stop) { > + (void)syscall(SYS_getpid); > + count++; > + } > + > + if (sig_int) > + printf("%ld iterations done\n", count); > + > + return 0; > +} > diff --git a/tools/testing/selftests/livepatch/test_modules/Makefile b/tools/testing/selftests/livepatch/test_modules/Makefile > index 6f7c2103d27d..f5e880269bff 100644 > --- a/tools/testing/selftests/livepatch/test_modules/Makefile > +++ b/tools/testing/selftests/livepatch/test_modules/Makefile > @@ -10,7 +10,8 @@ obj-m += test_klp_atomic_replace.o \ > test_klp_state.o \ > test_klp_state2.o \ > test_klp_state3.o \ > - test_klp_shadow_vars.o > + test_klp_shadow_vars.o \ > + test_klp_syscall.o > > modules: > $(Q)$(MAKE) -C $(KDIR) modules KBUILD_EXTMOD=$(TESTMODS_DIR) > diff --git a/tools/testing/selftests/livepatch/test_modules/test_klp_syscall.c b/tools/testing/selftests/livepatch/test_modules/test_klp_syscall.c > new file mode 100644 > index 000000000000..619496cc3481 > --- /dev/null > +++ b/tools/testing/selftests/livepatch/test_modules/test_klp_syscall.c > @@ -0,0 +1,116 @@ > +// SPDX-License-Identifier: GPL-2.0 > +/* > + * Copyright (C) 2017-2023 SUSE > + * Authors: Libor Pechacek > + * Nicolai Stange > + * Marcos Paulo de Souza > + */ > + > +#include > +#include > +#include > +#include > +#include > + > +#if defined(__x86_64__) > +#define FN_PREFIX __x64_ > +#elif defined(__s390x__) > +#define FN_PREFIX __s390x_ > +#elif defined(__aarch64__) > +#define FN_PREFIX __arm64_ > +#else > +/* powerpc does not select ARCH_HAS_SYSCALL_WRAPPER */ > +#define FN_PREFIX > +#endif > + > +/* Protects klp_pids */ > +static DEFINE_MUTEX(kpid_mutex); > + > +static unsigned int npids, npids_pending; > +static int klp_pids[NR_CPUS]; > +module_param_array(klp_pids, int, &npids_pending, 0); > +MODULE_PARM_DESC(klp_pids, "Array of pids to be transitioned to livepatched state."); > + > +static ssize_t npids_show(struct kobject *kobj, struct kobj_attribute *attr, > + char *buf) > +{ > + return sprintf(buf, "%u\n", npids_pending); > +} > + > +static struct kobj_attribute klp_attr = __ATTR_RO(npids); > +static struct kobject *klp_kobj; > + > +asmlinkage long lp_sys_getpid(void) > +{ > + int i; > + > + mutex_lock(&kpid_mutex); > + if (npids_pending > 0) { > + for (i = 0; i < npids; i++) { > + if (current->pid == klp_pids[i]) { > + klp_pids[i] = 0; > + npids_pending--; > + break; > + } > + } > + } > + mutex_unlock(&kpid_mutex); > + > + return task_tgid_vnr(current); > +} > + > +static struct klp_func vmlinux_funcs[] = { > + { > + .old_name = __stringify(FN_PREFIX) "sys_getpid", > + .new_func = lp_sys_getpid, > + }, {} > +}; > + > +static struct klp_object objs[] = { > + { > + /* name being NULL means vmlinux */ > + .funcs = vmlinux_funcs, > + }, {} > +}; > + > +static struct klp_patch patch = { > + .mod = THIS_MODULE, > + .objs = objs, > +}; > + > +static int livepatch_init(void) > +{ > + int ret; > + > + klp_kobj = kobject_create_and_add("test_klp_syscall", kernel_kobj); > + if (!klp_kobj) > + return -ENOMEM; > + > + ret = sysfs_create_file(klp_kobj, &klp_attr.attr); > + if (ret) { > + kobject_put(klp_kobj); > + return ret; > + } > + > + /* > + * Save the number pids to transition to livepatched state before the > + * number of pending pids is decremented. > + */ > + npids = npids_pending; > + > + return klp_enable_patch(&patch); > +} > + > +static void livepatch_exit(void) > +{ > + kobject_put(klp_kobj); > +} > + > +module_init(livepatch_init); > +module_exit(livepatch_exit); > +MODULE_LICENSE("GPL"); > +MODULE_INFO(livepatch, "Y"); > +MODULE_AUTHOR("Libor Pechacek "); > +MODULE_AUTHOR("Nicolai Stange "); > +MODULE_AUTHOR("Marcos Paulo de Souza "); > +MODULE_DESCRIPTION("Livepatch test: syscall transition"); Missing module name? Is there a reason to not name this module? thanks, -- Shuah