From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj2-f12.google.com (mail-pj2-f12.google.com [74.125.227.140]) (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 B1C774195A6 for ; Sat, 12 Sep 2026 08:40:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.227.140 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789202419; cv=none; b=LbRPJDWN573byazmz2nhptdtBPScrTAsGVxFl3aFn4AUWqrwal7PdCaADAl11W00aDCS1L28zyTqCJWYqmG0kL9o6lXKspwFAWPzZtgZM5IoAjRiuFj785ac7f1ACmgTdgLOzZS0yGyjpxjKwcschCoY1+KCuYhFHkfDLncg6ww= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789202419; c=relaxed/simple; bh=vVKv47cmfAuACjoqEhj5I+LzS+aoqmIZBNi2XYraPes=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=gjGlR7mgAy2j9osj8jSJu2J7Ld43950onFTEJDZSpENQk0raRljE427MIQThi7YgAtsF5WIsSPMqMiIa1DP1s83ndDHWm9NpzYEmZkHljbm2uLbbyeJaJTp+DD3lWg7IJq3KsIpDIFX/8z3M012M8LwblxlGHKC4rxGHdBVZc74= 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=SuRJoFSp; arc=none smtp.client-ip=74.125.227.140 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="SuRJoFSp" Received: by mail-pj2-f12.google.com with SMTP id 98e67ed59e1d1-396ccb65437so217122a91.3 for ; Sat, 12 Sep 2026 01:40:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1789202417; x=1789807217; 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=D62nAxto8FBPn6ofyV4nlMYglOuFu5fWBNRz3jiEv7M=; b=SuRJoFSpIRLQ4qAK7vXrgRvw1ZjJlXGd87wKCyWlcf3mCVn7nZQGlukWbPx7sz8UnU HUtXJNHtLY7JHXsaTqe7MgLhM/j3Z48gRgfO0WUsQA6tchB45JeCh42gsrVjMuYw8UMy WID73GfLw5paL3my0tyhEPrgxjEP/XWTiqraYb6vplu9lkrWRygNTc6WtMDVFCJD13t5 OiqrwpICbWRZO/9WYPTi4vn5t4BPi75O/mUfIF9BidamN2rFRV/qY12vZGf4ohxO5LY2 ckgk/0edon7NK/MG8l3g8BYms/GX6eQ1gjlep425d2F5zR0edBWIgHD55X0oEfQoqAKH vUiA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1789202417; x=1789807217; 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=D62nAxto8FBPn6ofyV4nlMYglOuFu5fWBNRz3jiEv7M=; b=SayhdGwEvYhKJbsirHBnTvPVb2pEneS2qT0RgIPk5gXqSLMkqMjFFO8BZzAvSK7Fkr r+XQD9oJLGQrLB/t2D79a5PND59NuchUoAWeF1CToTU4yVJaBU0AVi4yzzlT4y8ePgrF HAuCw5/hdEDTzFyLRsFgL4GyFg0cYz/fT0EjuyxpkAemZGLj0Ezij2FGM4InK+av/vkH jmV1cS44r3eFerJvGFIv0vbIHoXj+YS8Q+C4iCmCeC2c/UFJ7/IE8Xr7F9EkJMmaA0ns SXJVspo8WPAHUk3Lwx2Pvm1PVSxr3y18SQXHaScyVV3uTggy14p+BZGiCX5ja8qOrwdS Q16Q== X-Forwarded-Encrypted: i=1; AKwUvBxj7fDrq2rB/eGOAxwH0eiZhA8sxxMIL7gMFAaHL43vcNMafBR4q6yuxp3iWC/Iv29VnzHJJvFl0e4=@vger.kernel.org X-Gm-Message-State: AFuF++m2WsIurfZR9fldX6NxbAei/hcH+U57htRLkoYD+F7BVcUpUjw+ twrWQ8I+u964woaujr6oH8wcJWj1kvLFZX82Lzt0yCvbVrAxFWiAsMfJ X-Gm-Gg: AYBFou0U/qCWM7UpaICA4ZSNzy/Ts6O/MyGBEDsHrdoTMLGZm+HqS3tSb/6y3T7m3Jg MDhLO1uq3++BwNUZKMY80oUnd07eRYKR/kL8KF5j27O4zNAKxa5oQ8pxy+j56P9uhtuL91qYgUH ERQDRVbOg0d7IV5V2htGjeHpoD23FAAFhQ9nXKAQ2mLjQPlmHMxzuX7BrZX7tdLajVEaJWZ/9Dp lVh8eeu/Hm05pLjPIziAfqqhrPRpucnnQzbUg8t2qIHy8iUBofguKOa5iwfTmcWu1czxud7IL4Z 2zbae+CYhrqkp4zcVVuAOt4//a/gUQzMWLMR3WquEo4webNPrNt2HY3zDPMKAIJHdBpJW21/cqG SI/Ce+J7RpummdQAHygHSaNUwVUTsJLAKqwSQAFkJp+SfBY4L9xcHJz+tNloObFsfFfkqny8H4H 0LnFyJuMB2H68LJOAV0TugzzU/YCTdQo1OLtlf+t6upJxwEnx1FeEqxOL/YKv74MuS+H2iA4Qh6 cZYgk921NcpOVset4oRCiOSWTCSOyBCPTI6CnVS X-Received: by 2002:a17:90b:28cd:b0:38e:97f0:aa4b with SMTP id 98e67ed59e1d1-39dbc69b2c5mr3272923a91.13.1789202417155; Sat, 12 Sep 2026 01:40:17 -0700 (PDT) Received: from lipengfei28-ThinkStation-P368.mioffice.cn ([2408:8607:1b00:8:16a3:d08b:6ddb:dec6]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-39d95091214sm9790583a91.3.2026.09.12.01.40.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 12 Sep 2026 01:40:16 -0700 (PDT) From: Li Pengfei X-Google-Original-From: Li Pengfei To: rostedt@goodmis.org, mhiramat@kernel.org Cc: mathieu.desnoyers@efficios.com, mark.rutland@arm.com, corbet@lwn.net, skhan@linuxfoundation.org, lkp@intel.com, linux-trace-kernel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, zhangbo56@xiaomi.com, lipengfei28@xiaomi.com Subject: [RFC PATCH v7 10/10] selftests/ftrace: add a stackmap instance gating test Date: Sat, 12 Sep 2026 16:37:53 +0800 Message-Id: <20260912083753.3426176-11-lipengfei28@xiaomi.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260912083753.3426176-1-lipengfei28@xiaomi.com> References: <20260912083753.3426176-1-lipengfei28@xiaomi.com> Precedence: bulk X-Mailing-List: linux-doc@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Pengfei Li Verify both enforcement points for the global-instance-only stackmap option. Require options/stackmap and stack_map on the top-level instance. Read a shared-kernel random UUID from /proc/sys/kernel/random/uuid and combine it with ftracetest's $PID to create collision-resistant owned and foreign secondary-instance names, including across PID namespaces. Verify that neither the option nor any stack_map node appears in the owned instance. Also require writing stackmap through its aggregate trace_options file to fail and leave the option unset. Create the foreign instance as a sentinel. Remove the owned instance explicitly and verify the sentinel remains, proving that cleanup does not remove unrelated instances. Defer signal exit across each mkdir and ownership assignment so cleanup removes a directory only after this invocation has created it. Put mkdir in an if condition so ftracetest's errexit mode cannot bypass return-code handling, the explicit failure path, or ownership bookkeeping. Track both directories separately and remove only those created by this test. Keep the auxiliary stack_map_stat and stack_map_bin nodes optional on the global side. Signed-off-by: Pengfei Li --- .../test.d/ftrace/stackmap-instance-gate.tc | 114 ++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 tools/testing/selftests/ftrace/test.d/ftrace/stackmap-instance-gate.tc diff --git a/tools/testing/selftests/ftrace/test.d/ftrace/stackmap-instance-gate.tc b/tools/testing/selftests/ftrace/test.d/ftrace/stackmap-instance-gate.tc new file mode 100644 index 000000000000..818dbb7bfeb8 --- /dev/null +++ b/tools/testing/selftests/ftrace/test.d/ftrace/stackmap-instance-gate.tc @@ -0,0 +1,114 @@ +#!/bin/sh +# SPDX-License-Identifier: GPL-2.0 +# description: ftrace - stackmap option is gated to the top-level trace instance +# requires: stack_map options/stackmap instances + +# The 'stackmap' option is added to TOP_LEVEL_TRACE_FLAGS, matching the +# convention used for global-only options like 'printk' and 'record-cmd'. +# Verify that: +# 1. The global instance exposes options/stackmap and the required +# stack_map node. stack_map_stat and stack_map_bin are auxiliary and +# may be absent if their tracefs creation failed. +# 2. A newly created secondary instance under instances/ does NOT expose +# options/stackmap or any stack_map* nodes. + +fail() { + echo "FAIL: $1" + exit_fail +} + +instance_token=$(cat /proc/sys/kernel/random/uuid 2>/dev/null) || { + echo "could not obtain a collision-resistant instance token" + exit_unresolved +} +[ -n "$instance_token" ] || exit_unresolved +owned_instance=stackmap_gate_owned_${PID}_${instance_token} +owned_dir=instances/$owned_instance +foreign_instance=stackmap_gate_foreign_${PID}_${instance_token} +foreign_dir=instances/$foreign_instance +owned_created=0 +foreign_created=0 + +remove_owned() { + if [ "$owned_created" -eq 1 ]; then + rmdir "$owned_dir" || return 1 + owned_created=0 + fi +} + +cleanup() { + if [ "$owned_created" -eq 1 ]; then + rmdir "$owned_dir" 2>/dev/null || : + fi + if [ "$foreign_created" -eq 1 ]; then + rmdir "$foreign_dir" 2>/dev/null || : + fi +} +trap cleanup EXIT +trap 'exit 1' HUP INT TERM + +# 1. Global instance must expose the option and required map node +test -e options/stackmap || fail "options/stackmap missing on global instance" +test -e stack_map || fail "stack_map missing on global instance" + +# 2. Create a foreign sentinel and an owned secondary instance. Defer signal +# exit across each mkdir and ownership assignment: cleanup must remove a path +# only after this invocation has successfully created it. +interrupted=0 +trap 'interrupted=1' HUP INT TERM +create_rc=0 +if mkdir "$foreign_dir"; then + foreign_created=1 +else + create_rc=$? +fi +trap 'exit 1' HUP INT TERM +[ "$interrupted" -eq 0 ] || exit 1 +[ "$create_rc" -eq 0 ] || fail "could not create foreign sentinel instance" + +interrupted=0 +trap 'interrupted=1' HUP INT TERM +create_rc=0 +if mkdir "$owned_dir"; then + owned_created=1 +else + create_rc=$? +fi +trap 'exit 1' HUP INT TERM +[ "$interrupted" -eq 0 ] || exit 1 +[ "$create_rc" -eq 0 ] || fail "could not create secondary instance" + +if [ -e "$owned_dir/options/stackmap" ]; then + fail "secondary instance unexpectedly exposes options/stackmap" +fi + +for f in stack_map stack_map_stat stack_map_bin; do + if [ -e "$owned_dir/$f" ]; then + fail "secondary instance unexpectedly has $f" + fi +done + +# 3. The aggregate trace_options file still reaches set_tracer_flag(), +# so writing 'stackmap' there must be rejected on a secondary +# instance. Otherwise the bit could appear set in trace_options +# while the hot path silently falls back to a full stack trace +# (tr->stackmap == NULL). +if echo stackmap > "$owned_dir/trace_options" 2>/dev/null; then + fail "secondary instance accepted 'echo stackmap > trace_options'" +fi +if grep -qw stackmap "$owned_dir/trace_options"; then + fail "secondary instance trace_options reports stackmap as set" +fi + +remove_owned || fail "could not remove owned secondary instance" +if [ -e "$owned_dir" ]; then + fail "owned secondary instance still exists after removal" +fi +if [ ! -d "$foreign_dir" ]; then + fail "owned cleanup removed foreign sentinel instance" +fi +rmdir "$foreign_dir" || fail "could not remove foreign sentinel instance" +foreign_created=0 + +echo "stackmap option gating to top-level instance works" +exit 0 -- 2.34.1