From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f46.google.com (mail-wr1-f46.google.com [209.85.221.46]) (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 9E72B14B94B for ; Wed, 15 Jan 2025 15:31:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.46 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736955118; cv=none; b=ocOedCkA2Y54oRIS4uvXZfMfBw0fC1VOHb73GP3IHofUaAkF0ruwx6kEcfT0OBiujZ0dYhYUG1WOzCHHAWe7PLlc4pEagrIIsxJfbI9aYipxlZ/VkxdPgPsxnSNFE71vdDuGQ4UmNGZBo2IWOVkoVW1jcMiuJdQBmn8PnPkyCvw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736955118; c=relaxed/simple; bh=jXF89xKBnh+AD5qH+eCYzKNmJC1ZXvrRrlsJfnRr8Rg=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=CizqNd3zj2lXxUGai4EyWyF9z0v2zbFXCFfAOFAbPwPBJSubRACjuHzm9oL5x/SdyOUovRt2KqbNWzoJIKx/Mr7dCdLN44Jh0CTHd6c2cM4ttpcvDDt7OkVOSRZUuJ/7SdJpnF7Y2ULJecHD4uoYP6y1Yp7EyXwHxUrymJg4yxg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com; spf=pass smtp.mailfrom=suse.com; dkim=pass (2048-bit key) header.d=suse.com header.i=@suse.com header.b=JWQ618aX; arc=none smtp.client-ip=209.85.221.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=suse.com header.i=@suse.com header.b="JWQ618aX" Received: by mail-wr1-f46.google.com with SMTP id ffacd0b85a97d-385d7b4da2bso5795579f8f.1 for ; Wed, 15 Jan 2025 07:31:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1736955114; x=1737559914; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=1vqnrUKIh63ENHovv3lz9LwYLlI+FHwNt6f3V45tzuM=; b=JWQ618aXAEspyOpvM8Q3paIWkEp5rYxuwHvdxzyYKmaYfSrhqN+YD/ri7Iulg0bgeJ QUc7GTX5wqpYJbBMYL2k/n16FEhfutLg1NcJiLzY95/T82g1IFUImlxaU5I+uQpMz6Hf An/BmGG11a/zaOEm/Koi8u9dFGDx68MYQNTsC0qEkBvtRkLKHpBhzyMvpampADg77lEh MLgw8SW4d4j4HXSafbvAm94pdfc/0n9YBcXq6JbAKab4wP2yEyrjP/xjGibzTkYxqniY gfWNmQDr5Jey2KWnk4H7oQhoqg2a0xDQ9dMIebJMUrozFFNvLrg7UiIjp7MlOD0+3uaq iglQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736955114; x=1737559914; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=1vqnrUKIh63ENHovv3lz9LwYLlI+FHwNt6f3V45tzuM=; b=VGp60T3lJKzVVT12rEVqqLzlB+gHFbFTLuCN5XqgXnQKwrdv3hDBZUVB3nmuT7otqq jW7bXH9VaCDlASpe5t6NGvIuWbTu3GUuhesWQ2zWuQcqgNsBIpprfFG7qHkk7nbLaO5E zfOlW61xqeEIuQMCdGidAksAVlto1O+PREj21dCu3RuDloqYTZk6DUPQdyB7Kk2j9qfJ 3oc3YML0+0fWkwPaEQ4Kuf8JZuTYt15hS2o6Y9vPifTtFVP5RckgBkBKu4guXep1nbXY hEMUrG114TxWm3n6jugRfEVd+eHcZ9Z6Ye+ekcFNML4yO7nuGkHIpxJJDn0w9kkhOMXW rCJw== X-Forwarded-Encrypted: i=1; AJvYcCXROg76AUA2bO35IB2dj/g3B6hGdYPSslaOpu4EKND7assW7yCwdZQSNWNj2ra0wJbSoHtwOAe9otEIBKc=@vger.kernel.org X-Gm-Message-State: AOJu0Yz+rPxZx1gc1QhPctNHYgyOy3KRu2jyUzlpfxufPlzHAvPcKWfn JiVRN4C18kjGDsm293oA1Iyw0wqmHyRt3ChsLJ24nTO15OPMlBR4PJmJYIIUdz9+9mbOSp+9ct0 9 X-Gm-Gg: ASbGncsU5FsVTH4iBuDB8u7jkTsvid1ShCcVKICUHcSz291JzKejgaZfdOz7YWBZykw 0IsXbbslIutC6ehVoxicb6mwQcSn/zGt3nRshZiG5VoldTI9vv7LyMM18WyA+NHry5cCAKt4IY3 gkTNLCqqmix2HYqsMxGh9kvzZ5rbzRemmTH3/QdKGCydUqBTZeNEodZuLYEtXhZbNXcW83Zve6D gMJcijD8IPJ9kO3i3kKArgvzy0pRlrlathJIzPS+lWAB39zPIuxccFilg== X-Google-Smtp-Source: AGHT+IG3kHQwZtuC4Ysp3cANtbOTE9JmK3wljiHmMzPB4y66FwZUu7l+6M3B3NnDSAQBGnYagFcg4A== X-Received: by 2002:a05:6000:712:b0:385:ee3f:5cbf with SMTP id ffacd0b85a97d-38a87305369mr24469573f8f.20.1736955113748; Wed, 15 Jan 2025 07:31:53 -0800 (PST) Received: from pathway.suse.cz ([176.114.240.50]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38a8e383df8sm18236107f8f.38.2025.01.15.07.31.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Jan 2025 07:31:53 -0800 (PST) Date: Wed, 15 Jan 2025 16:31:51 +0100 From: Petr Mladek To: Filipe Xavier Cc: Marcos Paulo de Souza , Josh Poimboeuf , Jiri Kosina , Miroslav Benes , Joe Lawrence , Shuah Khan , live-patching@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, Felipe Xavier Subject: Re: [PATCH v2] selftests: livepatch: test if ftrace can trace a livepatched function Message-ID: References: <20250111-ftrace-selftest-livepatch-v2-1-9f4ff90f251a@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20250111-ftrace-selftest-livepatch-v2-1-9f4ff90f251a@gmail.com> Hi Filipe, first, thanks for the test. It is good. I just suggest a lot of clean up ;-) On Sat 2025-01-11 15:42:27, Filipe Xavier wrote: > This new test makes sure that ftrace can trace a > function that was introduced by a livepatch. > --- > Changes in v2: > - functions.sh: added reset tracing on push and pop_config. > - test-ftrace.sh: enabled tracing_on before test init. > - nitpick: added double quotations on filenames and fixed some wording. > - Link to v1: https://lore.kernel.org/r/20250102-ftrace-selftest-livepatch-v1-1-84880baefc1b@gmail.com > --- > tools/testing/selftests/livepatch/functions.sh | 14 ++++++++++ > tools/testing/selftests/livepatch/test-ftrace.sh | 33 ++++++++++++++++++++++++ > 2 files changed, 47 insertions(+) > > diff --git a/tools/testing/selftests/livepatch/functions.sh b/tools/testing/selftests/livepatch/functions.sh > index e5d06fb402335d85959bafe099087effc6ddce12..e6c13514002dae5f8d7461f90b8241ab43024ea4 100644 > --- a/tools/testing/selftests/livepatch/functions.sh > +++ b/tools/testing/selftests/livepatch/functions.sh > @@ -62,6 +62,9 @@ function push_config() { > awk -F'[: ]' '{print "file " $1 " line " $2 " " $4}') > FTRACE_ENABLED=$(sysctl --values kernel.ftrace_enabled) > KPROBE_ENABLED=$(cat "$SYSFS_KPROBES_DIR/enabled") > + TRACING_ON=$(cat "$SYSFS_DEBUG_DIR/tracing/tracing_on") The $SYSFS_DEBUG_DIR/tracing directory is used many times. It would make sense to define: SYSFS_TRACING_DIR="$SYSFS_DEBUG_DIR/tracing" and use: TRACING_ON=$(cat "$SYSFS_TRACING_DIR/tracing_on") > + CURRENT_TRACER=$(cat "$SYSFS_DEBUG_DIR/tracing/current_tracer") > + FTRACE_FILTER=$(cat "$SYSFS_DEBUG_DIR/tracing/set_ftrace_filter") > } > > function pop_config() { > @@ -74,6 +77,17 @@ function pop_config() { > if [[ -n "$KPROBE_ENABLED" ]]; then > echo "$KPROBE_ENABLED" > "$SYSFS_KPROBES_DIR/enabled" > fi > + if [[ -n "$TRACING_ON" ]]; then > + echo "$TRACING_ON" > "$SYSFS_DEBUG_DIR/tracing/tracing_on" > + fi > + if [[ -n "$CURRENT_TRACER" ]]; then > + echo "$CURRENT_TRACER" > "$SYSFS_DEBUG_DIR/tracing/current_tracer" > + fi > + if [[ "$FTRACE_FILTER" == *"#"* ]]; then > + echo > "$SYSFS_DEBUG_DIR/tracing/set_ftrace_filter" My understanding is that this is handling the situation when the filter is: #### all functions enabled #### I was curious about the pattern *"#"*. The quotation looked pretty non-standard to me. It made me to read the bash documentation and the rules for extressions in [[ ]] looked quite complicated. I wonder if we could make this more for "dummies". The following seems to work: if [[ -n "$FTRACE_FILTER" ]]; then echo "$FTRACE_FILTER" | sed -e "/#### all functions enabled ####/d" > "$SYSFS_DEBUG_DIR/tracing/set_ftrace_filter" fi Note that the string "#### all functions enabled ####" is a very stable thing. It has never been modified since it was introduced by the commit 0c75a3ed633419d75d ("ftrace: state that all functions are enabled in set_ftrace_filter") in Feb 2009. The string is used also in ftrace selftests, see tools/testing/selftests/ftrace/test.d/ftrace/func_set_ftrace_file.tc > + elif [[ -n "$FTRACE_FILTER" ]]; then > + echo "$FTRACE_FILTER" > "$SYSFS_DEBUG_DIR/tracing/set_ftrace_filter" > + fi > } > > function set_dynamic_debug() { > diff --git a/tools/testing/selftests/livepatch/test-ftrace.sh b/tools/testing/selftests/livepatch/test-ftrace.sh > index fe14f248913acbec46fb6c0fec38a2fc84209d39..66af5d726c52e48e5177804e182b4ff31784d5ac 100755 > --- a/tools/testing/selftests/livepatch/test-ftrace.sh > +++ b/tools/testing/selftests/livepatch/test-ftrace.sh > @@ -61,4 +61,37 @@ livepatch: '$MOD_LIVEPATCH': unpatching complete > % rmmod $MOD_LIVEPATCH" > > > +# - verify livepatch can load > +# - check if traces have a patched function > +# - unload livepatch and reset trace > + > +start_test "trace livepatched function and check that the live patch remains in effect" > + > +TRACE_FILE="$SYSFS_DEBUG_DIR/tracing/trace" > +FUNCTION_NAME="livepatch_cmdline_proc_show" > + > +load_lp $MOD_LIVEPATCH > + > +echo 1 > "$SYSFS_DEBUG_DIR/tracing/tracing_on" > +echo $FUNCTION_NAME > "$SYSFS_DEBUG_DIR/tracing/set_ftrace_filter" The above mentioned ftrace selftest shows that the behavior gets more complicated when "traceoff" is used: 1. The "traceoff" entries are not cleared by the above command. 2. The "traceoff" entries might cause that the tracing will stop before we finish this test. > +echo "function" > "$SYSFS_DEBUG_DIR/tracing/current_tracer" > +echo "" > "$TRACE_FILE" I suggest to change the ordering and make sure that nothing is running before we start: # Start with the clean up echo 0 > "$SYSFS_TRACING_DIR/tracing_on" echo '!*:traceoff' > $SYSFS_TRACING_DIR/set_ftrace_filter echo "" > "$SYSFS_TRACING_DIR/current_tracer" echo "" > $SYSFS_TRACING_DIR/trace # Setup and start our tracing echo "function" > "$SYSFS_TRACING_DIR/current_tracer" echo $FUNCTION_NAME > "$SYSFS_TRACING_DIR/set_ftrace_filter" echo 1 > "$SYSFS_TRACING_DIR/tracing_on" I suggest to this into helper functions which might be used by more tests: # cleanup_tracing() - stop and clean up function tracing function cleanup_tracing() { echo 0 > "$SYSFS_TRACING_DIR/tracing_on" echo '!*:traceoff' > $SYSFS_TRACING_DIR/set_ftrace_filter echo "" > "$SYSFS_TRACING_DIR/current_tracer" echo "" > $SYSFS_TRACING_DIR/trace } # trace_function(function) - start tracing of a function # function - to be traced function function trace_function() { local function="$1"; shift cleanup_tracing echo "function" > "$SYSFS_TRACING_DIR/current_tracer" echo "$functions" > $SYSFS_TRACING_DIR/set_ftrace_filter echo 1 > "$SYSFS_TRACING_DIR/tracing_on" } # check_traced_function(function) - check whether function appeared in trace log # function - to be traced function function check_trace_function() { local function="$1"; shift if ! grep -q "$function" $SYSFS_TRACING_DIR/trace" ; then die "Function ($function) did not appear in the trace" fi } As Marcos suggested, it would make sense to add the above mentioned helper functions in a separate patch. > + > +if [[ "$(cat /proc/cmdline)" != "$MOD_LIVEPATCH: this has been live patched" ]] ; then > + echo -e "FAIL\n\n" > + die "livepatch kselftest(s) failed" > +fi > + > +grep -q $FUNCTION_NAME "$TRACE_FILE" > +FOUND=$? We could replace this with the above proposed check_traced_function "$FUNCTION_NAME" Also I would personally stop the tracing after this sub-test finishes. We might use the cleanup_tracing function here. cleanup_tracing > + > +disable_lp $MOD_LIVEPATCH > +unload_lp $MOD_LIVEPATCH > + > +if [ "$FOUND" -eq 1 ]; then > + echo -e "FAIL\n\n" > + die "livepatch kselftest(s) failed" > +fi As mentioned by Marcos, we should also print "ok" on success. I would personally use the "check_result" function here. It could catch non-expected entries in dmesg. > + > exit 0 Best Regards, Petr