Message ID | 20240202220459.527138-15-namhyung@kernel.org |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel+bounces-50647-ouuuleilei=gmail.com@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:9bc1:b0:106:209c:c626 with SMTP id op1csp725040dyc; Fri, 2 Feb 2024 14:09:35 -0800 (PST) X-Google-Smtp-Source: AGHT+IF0ktLKm9gpuL4+rk18mdgofIlCLGSsRjDepVaxeMK3q7KArKxSkosHmCmxPHghdJKl5ukw X-Received: by 2002:a05:6359:a381:b0:178:72b5:cf41 with SMTP id ky1-20020a056359a38100b0017872b5cf41mr9703301rwc.7.1706911775316; Fri, 02 Feb 2024 14:09:35 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706911775; cv=pass; d=google.com; s=arc-20160816; b=jCRbl/yQItTSJqJ1SZiXv9ZfASKdEsm0pXsch+gsflTYIyySAULzcw24knmfae7u0n mxc8vsvQXuSuTNY/Eg0Ga9oeirljdDkJO3RolNlwZTiww1K4JJRFV48RNsYYpX8pyGZh QZZE49+qU61bRqRS7jn5TxpYGTaDMxfZjOEPEcZAPltO+yDY2p2KOKbFdONVyKZxPfAV GVV5kD7Dxc5XZ8Zw+HMAFOI8BL2nqyH8ExJVHaPhILCszJr8GFuYNtGukd3901g1TlFW r/XQx4WFzYR4pv+79DePNWOZfFlgmIlurg4fEP3S1LYQNTRfBgq6nOJ6VVoY1QSLZFOJ +/hw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=cyHEIG9bO6Abl7AHq3yfEYap5kTaynXteQfdMiMve6c=; fh=NerT2IPcodvohhlBftBIAHK6hfURN/QTmp1h76CaZ/M=; b=psilKqpFTc6b+FFjxZosSx2m3O0XO/m16fL49xpkn1wkhuU3YpwiNzKMgB51T1dzSh rXHzDJOfh9+8Zu+1smiaZt0FjCCS4eKC4KP0w1U0Tw7Tltv0pH9LKrOfTJo7/IfEt/Pa f7Hxg9QoioLIyfoyEYE0+oA6L4omscDVmZ8hjRrw3cj6QNfKesoJrElg6/58hXBxNkLU l9S089t0mUGv4ZNpESw9R3IdaHcLKA8XjqcmScEPQC6KhW/FdHB4W896pW+0A2Y/7bS2 /3528ZauvdJBYNClgnteCb3OcGezZ9zWY4AXDjJrawxtW4E96XBjIqXXLwMgcRN4Jdrp DmDQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Rj1bKvcP; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-50647-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50647-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Forwarded-Encrypted: i=1; AJvYcCUpw6lotQHXi0EApMa4qDV97rRLkD8tQBpHxRsBvtIESI+gnFRqjCdl9zBEsnFTRC54A85dFHqNGK2wG3ApLwKM9JsSBA== Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id q36-20020a635c24000000b005d8e379746fsi2246151pgb.469.2024.02.02.14.09.35 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 14:09:35 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-50647-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Rj1bKvcP; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-50647-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50647-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 0F8902837EA for <ouuuleilei@gmail.com>; Fri, 2 Feb 2024 22:09:20 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A9905134CEF; Fri, 2 Feb 2024 22:05:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Rj1bKvcP" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C9F74132498; Fri, 2 Feb 2024 22:05:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706911510; cv=none; b=B9XujTkVVcfeRZe5ZHM7RsuuemsGBPfw+KIBb3CQIO8rZKNPs3cwAUmEWjr0Azb4/0s1UpJj1tb6lMGCQtDcpWOIhYm92pEkWSbGGQ/dUdSPjz0zPEZXdbVRP5VpGrytQd3gMqtdFIANhsTgiiBEVk6ovxxzJzgBti2K+9lZ0m4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706911510; c=relaxed/simple; bh=u+3b408czl7+mH8GeTAKvju9aWQhRxzD0hyW0Dr5iWE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=oBJLpojbZbIoZ0Yc0ozK85KovEb4V7Y2NodPbDy0GB/dg19qkvKotcaEyrDBVhdaVhLs22L+l2eGE5/fsmFYryndUzVnioigkno4bVKnQqug5tNFsHKM+xJStAjgn8THs6XlOdrOVA49LXXWGnaO4YWP0mZxZA+DAI6qAcpMW08= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Rj1bKvcP; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id EFFC0C433C7; Fri, 2 Feb 2024 22:05:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1706911510; bh=u+3b408czl7+mH8GeTAKvju9aWQhRxzD0hyW0Dr5iWE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Rj1bKvcPX27x+4M1i+jYh7SuVlGttIBX9A5CC5LblU9hEvwGe2Od18VdDnFcbjFRk no3J1Xy5wkzm48aOCRzIo3KckxAdIxPqejNlRdZhTiAnV05sFv513YXo8N4sRXvNkc yc+SzlRdzjfBfA34VHkqBEWe0P6toRYi1gqtprXwEoJKSgtxfYXYe9gyyVK/u8gkcp zHn4niC9G0eo5JBaPs1Ib35wfDrhZjU769cjkQL+edF94vJBNMNwaBOdaKde9+v+ia L9d4L64ku4JADxIXdeXwwFnhW5HsYdcM9eaVLLWJxQwBESMMTkM1ZcHhL0gYCx2a1d fzw9QfMK+Ysdw== From: Namhyung Kim <namhyung@kernel.org> To: Arnaldo Carvalho de Melo <acme@kernel.org>, Ian Rogers <irogers@google.com> Cc: Jiri Olsa <jolsa@kernel.org>, Adrian Hunter <adrian.hunter@intel.com>, Peter Zijlstra <peterz@infradead.org>, Ingo Molnar <mingo@kernel.org>, LKML <linux-kernel@vger.kernel.org>, linux-perf-users@vger.kernel.org, Linus Torvalds <torvalds@linux-foundation.org>, Stephane Eranian <eranian@google.com>, Masami Hiramatsu <mhiramat@kernel.org>, linux-toolchains@vger.kernel.org, linux-trace-devel@vger.kernel.org Subject: [PATCH 14/14] perf annotate-data: Add stack canary type Date: Fri, 2 Feb 2024 14:04:59 -0800 Message-ID: <20240202220459.527138-15-namhyung@kernel.org> X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog In-Reply-To: <20240202220459.527138-1-namhyung@kernel.org> References: <20240202220459.527138-1-namhyung@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: <linux-kernel.vger.kernel.org> List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org> List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789826721881884851 X-GMAIL-MSGID: 1789826721881884851 |
Series |
perf tools: Remaining bits of data type profiling (v5)
|
|
Commit Message
Namhyung Kim
Feb. 2, 2024, 10:04 p.m. UTC
When the stack protector is enabled, compiler would generate code to
check stack overflow with a special value called 'stack carary' at
runtime. On x86_64, GCC hard-codes the stack canary as %gs:40.
While there's a definition of fixed_percpu_data in asm/processor.h,
it seems that the header is not included everywhere and many places
it cannot find the type info. As it's in the well-known location (at
%gs:40), let's add a pseudo stack canary type to handle it specially.
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
tools/perf/util/annotate-data.h | 1 +
tools/perf/util/annotate.c | 24 ++++++++++++++++++++++++
2 files changed, 25 insertions(+)
Comments
On Fri, Feb 2, 2024 at 2:05 PM Namhyung Kim <namhyung@kernel.org> wrote: > > When the stack protector is enabled, compiler would generate code to > check stack overflow with a special value called 'stack carary' at > runtime. On x86_64, GCC hard-codes the stack canary as %gs:40. > > While there's a definition of fixed_percpu_data in asm/processor.h, > it seems that the header is not included everywhere and many places > it cannot find the type info. As it's in the well-known location (at > %gs:40), let's add a pseudo stack canary type to handle it specially. I wonder if cases like this can be handled by debug info rather than special cases in the tool. Special cases are fine too, but are potentially less portable. Thanks, Ian > Signed-off-by: Namhyung Kim <namhyung@kernel.org> > --- > tools/perf/util/annotate-data.h | 1 + > tools/perf/util/annotate.c | 24 ++++++++++++++++++++++++ > 2 files changed, 25 insertions(+) > > diff --git a/tools/perf/util/annotate-data.h b/tools/perf/util/annotate-data.h > index 0bfef29fa52c..e293980eb11b 100644 > --- a/tools/perf/util/annotate-data.h > +++ b/tools/perf/util/annotate-data.h > @@ -77,6 +77,7 @@ struct annotated_data_type { > > extern struct annotated_data_type unknown_type; > extern struct annotated_data_type stackop_type; > +extern struct annotated_data_type canary_type; > > /** > * struct data_loc_info - Data location information > diff --git a/tools/perf/util/annotate.c b/tools/perf/util/annotate.c > index 5f3136f57c62..f2683dadf3cf 100644 > --- a/tools/perf/util/annotate.c > +++ b/tools/perf/util/annotate.c > @@ -116,6 +116,13 @@ struct annotated_data_type stackop_type = { > }, > }; > > +struct annotated_data_type canary_type = { > + .self = { > + .type_name = (char *)"(stack canary)", > + .children = LIST_HEAD_INIT(canary_type.self.children), > + }, > +}; > + > static int arch__grow_instructions(struct arch *arch) > { > struct ins *new_instructions; > @@ -3764,6 +3771,17 @@ static bool is_stack_operation(struct arch *arch, struct disasm_line *dl) > return false; > } > > +static bool is_stack_canary(struct arch *arch, struct annotated_op_loc *loc) > +{ > + /* On x86_64, %gs:40 is used for stack canary */ > + if (arch__is(arch, "x86")) { > + if (loc->segment == INSN_SEG_X86_GS && loc->offset == 40) > + return true; > + } > + > + return false; > +} > + > u64 annotate_calc_pcrel(struct map_symbol *ms, u64 ip, int offset, > struct disasm_line *dl) > { > @@ -3938,6 +3956,12 @@ struct annotated_data_type *hist_entry__get_data_type(struct hist_entry *he) > } > > mem_type = find_data_type(&dloc); > + > + if (mem_type == NULL && is_stack_canary(arch, op_loc)) { > + mem_type = &canary_type; > + dloc.type_offset = 0; > + } > + > if (mem_type) > istat->good++; > else > -- > 2.43.0.594.gd9cf4e227d-goog >
On Fri, Feb 2, 2024 at 7:21 PM Ian Rogers <irogers@google.com> wrote: > > On Fri, Feb 2, 2024 at 2:05 PM Namhyung Kim <namhyung@kernel.org> wrote: > > > > When the stack protector is enabled, compiler would generate code to > > check stack overflow with a special value called 'stack carary' at > > runtime. On x86_64, GCC hard-codes the stack canary as %gs:40. > > > > While there's a definition of fixed_percpu_data in asm/processor.h, > > it seems that the header is not included everywhere and many places > > it cannot find the type info. As it's in the well-known location (at > > %gs:40), let's add a pseudo stack canary type to handle it specially. > > I wonder if cases like this can be handled by debug info rather than > special cases in the tool. Special cases are fine too, but are > potentially less portable. Agreed, but I couldn't find anything special in DWARF. Thanks, Namhyung
On Tue, Feb 6, 2024 at 3:19 PM Namhyung Kim <namhyung@kernel.org> wrote: > > On Fri, Feb 2, 2024 at 7:21 PM Ian Rogers <irogers@google.com> wrote: > > > > On Fri, Feb 2, 2024 at 2:05 PM Namhyung Kim <namhyung@kernel.org> wrote: > > > > > > When the stack protector is enabled, compiler would generate code to > > > check stack overflow with a special value called 'stack carary' at > > > runtime. On x86_64, GCC hard-codes the stack canary as %gs:40. > > > > > > While there's a definition of fixed_percpu_data in asm/processor.h, > > > it seems that the header is not included everywhere and many places > > > it cannot find the type info. As it's in the well-known location (at > > > %gs:40), let's add a pseudo stack canary type to handle it specially. > > > > I wonder if cases like this can be handled by debug info rather than > > special cases in the tool. Special cases are fine too, but are > > potentially less portable. > > Agreed, but I couldn't find anything special in DWARF. The fs and gs selectors are commonly used for thread local storage, so could something like DW_OP_form_tls_address be used? https://dwarfstd.org/issues/110803.1.html Thanks, Ian > Thanks, > Namhyung
On Tue, Feb 6, 2024 at 3:40 PM Ian Rogers <irogers@google.com> wrote: > > On Tue, Feb 6, 2024 at 3:19 PM Namhyung Kim <namhyung@kernel.org> wrote: > > > > On Fri, Feb 2, 2024 at 7:21 PM Ian Rogers <irogers@google.com> wrote: > > > > > > On Fri, Feb 2, 2024 at 2:05 PM Namhyung Kim <namhyung@kernel.org> wrote: > > > > > > > > When the stack protector is enabled, compiler would generate code to > > > > check stack overflow with a special value called 'stack carary' at > > > > runtime. On x86_64, GCC hard-codes the stack canary as %gs:40. > > > > > > > > While there's a definition of fixed_percpu_data in asm/processor.h, > > > > it seems that the header is not included everywhere and many places > > > > it cannot find the type info. As it's in the well-known location (at > > > > %gs:40), let's add a pseudo stack canary type to handle it specially. > > > > > > I wonder if cases like this can be handled by debug info rather than > > > special cases in the tool. Special cases are fine too, but are > > > potentially less portable. > > > > Agreed, but I couldn't find anything special in DWARF. > > The fs and gs selectors are commonly used for thread local storage, so > could something like DW_OP_form_tls_address be used? > https://dwarfstd.org/issues/110803.1.html I'm not sure if it's the same thing. Maybe it's for variables with __thread annotation. I don't know if compilers generate a (pseudo) variable for stack canary. Thanks, Namhyung
diff --git a/tools/perf/util/annotate-data.h b/tools/perf/util/annotate-data.h index 0bfef29fa52c..e293980eb11b 100644 --- a/tools/perf/util/annotate-data.h +++ b/tools/perf/util/annotate-data.h @@ -77,6 +77,7 @@ struct annotated_data_type { extern struct annotated_data_type unknown_type; extern struct annotated_data_type stackop_type; +extern struct annotated_data_type canary_type; /** * struct data_loc_info - Data location information diff --git a/tools/perf/util/annotate.c b/tools/perf/util/annotate.c index 5f3136f57c62..f2683dadf3cf 100644 --- a/tools/perf/util/annotate.c +++ b/tools/perf/util/annotate.c @@ -116,6 +116,13 @@ struct annotated_data_type stackop_type = { }, }; +struct annotated_data_type canary_type = { + .self = { + .type_name = (char *)"(stack canary)", + .children = LIST_HEAD_INIT(canary_type.self.children), + }, +}; + static int arch__grow_instructions(struct arch *arch) { struct ins *new_instructions; @@ -3764,6 +3771,17 @@ static bool is_stack_operation(struct arch *arch, struct disasm_line *dl) return false; } +static bool is_stack_canary(struct arch *arch, struct annotated_op_loc *loc) +{ + /* On x86_64, %gs:40 is used for stack canary */ + if (arch__is(arch, "x86")) { + if (loc->segment == INSN_SEG_X86_GS && loc->offset == 40) + return true; + } + + return false; +} + u64 annotate_calc_pcrel(struct map_symbol *ms, u64 ip, int offset, struct disasm_line *dl) { @@ -3938,6 +3956,12 @@ struct annotated_data_type *hist_entry__get_data_type(struct hist_entry *he) } mem_type = find_data_type(&dloc); + + if (mem_type == NULL && is_stack_canary(arch, op_loc)) { + mem_type = &canary_type; + dloc.type_offset = 0; + } + if (mem_type) istat->good++; else