Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp1020077rdb; Fri, 1 Dec 2023 05:13:43 -0800 (PST) X-Google-Smtp-Source: AGHT+IE65a8HHSvu5PZjiK3pk7ARJi6+8QaPzQihAMQ3ECAm98qEgL3woDwErZvk/pHjnGGqGOTE X-Received: by 2002:a05:6a20:7f98:b0:18c:8afc:cdd2 with SMTP id d24-20020a056a207f9800b0018c8afccdd2mr17926662pzj.17.1701436422832; Fri, 01 Dec 2023 05:13:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701436422; cv=none; d=google.com; s=arc-20160816; b=BNxXNDQe2Fi2OOm66CuY2nQHqdxfWDUNLRU6ihpW1VT0iHaJ8sI6t0/KPFcWThWVFQ nJHH/5FkS9yQiWvgoy+vabiPvH1NZYc1yCGp1YaN4HEX7EoSAR/fjHBE/38x6pMgnOZP hFvMjCS3+GZ82O2ticx30nHFvSozIaNKdQb4m19Ec06sB89AqwOi0Pv9yPxHXdnztyAH WPUdbN3CjQgavpLmVOxmbLAiWtv/r0ymrfmWipLoICXp/aAi37e2jqCnDIw0f399UrZM eEN5NPe6GeSufj6vDn3DlF6/GEJYyMIAwIXleiLpwKpSDtyf5umgy7wE9I57yFhylYbC GEaA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=GS3UdeIEvfluMor0FILZ2/arrUxfmrFE/SmJey4NwMg=; fh=P1YVqbFykfpvuBqS08LXL4aNVwxDIkcrPn4ZatQO2aM=; b=uO/l1sjIrfeCvbnBiWOqF0/9fnaCnprwv4aBgPrsNBxx3JIedLoG3kfuw1kB4JGCx7 zPM9YghwcNrVhjvLp96330q3T2tGF5ah8BQTjbp5kzzM0Tgf76GjUKEIsSEisPHrm9gr 1XCOqR1M2JMEDxInHPrLK9XA0/+EHFhkC5wAavLFACsQFems/L+17fNAPN07wkoZOGfc ouSkm49aIMsTg1nQ9sCxjxiXWp0PoxtWdapE9JVoMm1xaf/Qe74kBz0ELaUbLD22E7sp r9lGqFliCNu9iMiosI4J/Kil6oHYkRTCdQy6cJ6wJV/GtInZ6yAfCAb4mKZgXtBCXVJ8 xVcQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=suse.de Return-Path: Received: from agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id q9-20020a056a00150900b006cdaa195ea6si3368831pfu.69.2023.12.01.05.13.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Dec 2023 05:13:42 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=suse.de Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 242E580FF299; Fri, 1 Dec 2023 05:13:32 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1378950AbjLANNN (ORCPT + 99 others); Fri, 1 Dec 2023 08:13:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52444 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1378922AbjLANNL (ORCPT ); Fri, 1 Dec 2023 08:13:11 -0500 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CDC0D10F8; Fri, 1 Dec 2023 05:13:16 -0800 (PST) Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 9590C1FD70; Fri, 1 Dec 2023 13:13:13 +0000 (UTC) Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 18EF31369E; Fri, 1 Dec 2023 13:13:12 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id AWyXM+jbaWW5GAAAD6G6ig (envelope-from ); Fri, 01 Dec 2023 13:13:12 +0000 Date: Fri, 1 Dec 2023 10:13:10 -0300 From: Marcos Paulo de Souza To: Shuah Khan Cc: 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 , 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 Subject: Re: [PATCH v3 3/3] selftests: livepatch: Test livepatching a heavily called syscall Message-ID: References: <20231031-send-lp-kselftests-v3-0-2b1655c2605f@suse.com> <20231031-send-lp-kselftests-v3-3-2b1655c2605f@suse.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spamd-Bar: / X-Spam-Level: X-Rspamd-Server: rspamd1 Authentication-Results: smtp-out2.suse.de; dkim=none; spf=fail (smtp-out2.suse.de: domain of mpdesouza@suse.com does not designate 2a07:de40:b281:104:10:150:64:97 as permitted sender) smtp.mailfrom=mpdesouza@suse.com; dmarc=fail reason="No valid SPF, No valid DKIM" header.from=suse.de (policy=none) X-Rspamd-Queue-Id: 9590C1FD70 X-Spam-Score: 0.84 X-Spamd-Result: default: False [0.84 / 50.00]; RCVD_VIA_SMTP_AUTH(0.00)[]; R_SPF_FAIL(1.00)[-all]; ARC_NA(0.00)[]; FROM_HAS_DN(0.00)[]; TO_DN_SOME(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; BAYES_HAM(-3.00)[99.99%]; MIME_GOOD(-0.10)[text/plain]; MID_RHS_NOT_FQDN(0.50)[]; NEURAL_HAM_LONG(-0.07)[-0.068]; SPAMHAUS_XBL(0.00)[2a07:de40:b281:104:10:150:64:97:from]; RCVD_COUNT_THREE(0.00)[3]; MX_GOOD(-0.01)[]; NEURAL_HAM_SHORT(-0.18)[-0.892]; RCPT_COUNT_TWELVE(0.00)[19]; FORGED_SENDER(0.30)[mpdesouza@suse.de,mpdesouza@suse.com]; FUZZY_BLOCKED(0.00)[rspamd.com]; R_DKIM_NA(2.20)[]; MIME_TRACE(0.00)[0:+]; FROM_NEQ_ENVFROM(0.10)[mpdesouza@suse.de,mpdesouza@suse.com]; RCVD_TLS_ALL(0.00)[]; DMARC_POLICY_SOFTFAIL(0.10)[suse.de : No valid SPF, No valid DKIM,none] X-Spam-Status: No, score=-0.8 required=5.0 tests=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 agentk.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 (agentk.vger.email [0.0.0.0]); Fri, 01 Dec 2023 05:13:32 -0800 (PST) On Thu, Nov 30, 2023 at 04:24:26PM -0700, Shuah Khan wrote: > 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? Can you please elaborate? This new module use the same MODULE_ macros used by the current livepatch selftests. What do you mean by module name? Thanks in advance, Marcos > > thanks, > -- Shuah