Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp678354imm; Fri, 15 Jun 2018 04:38:21 -0700 (PDT) X-Google-Smtp-Source: ADUXVKLe8Ff1kfuR7NPwbFRrNuwUoxWBLzlFHBslyQTejnoDSa9Whecao3CZQNxdemP7VvbfuVib X-Received: by 2002:a65:4a10:: with SMTP id s16-v6mr1267880pgq.57.1529062701267; Fri, 15 Jun 2018 04:38:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1529062701; cv=none; d=google.com; s=arc-20160816; b=n/3JKiBStCFyvV36EZGxwRMh3jNFWTAKFIO79XTBY7ZSF2kV3NpntV2s9Z6ycJ9ZAi EYRY5igjUkZ5BGb/7cwRr6sY/Sj/YbgrJrxfcK0YZf1pSlcOt4mu8yaLkNAIap0MZZWM Q0T5uSLn2qus88TF+/f7nEuF1hC1aLiCjqtdQtGja24Z8JCH9vRfdczqKPOA5g5xkzor /u1rNds/QA0hBC61Z+CvXLH7IYc5MWKfcFA5y3guyEPDbNXuYa6l+1FH5vsqgjuzKgqt zObdnPYUgucush6O1WhWUmyhHg9NM5oHxcdZgx5KlEWx+6ras8sKmPx4Q1Ftb+K9wyL7 N+DA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:arc-authentication-results; bh=FUn1JBYl0sgQo1dRnvXOiuYTfb6/xVLDXvIfsQvhM3U=; b=iXwhK14JPkP+cLhueKWILyvJRqoNACeMDNW6ELeCvqhzX8QrqLMCzbzPJ0kiCNfr4M Z/mBY078zLn5yYEP1c68v3+09oBME+BbQFWiAWrANBOFmhz6GXssl2KnngZ8R2hmMGdk HXnrLn19NSkxEs8xTXRzJE0M45SBdk9ZBO8J0cOlNjnn9prlhbfqT6bOUBD7kEPoVIAF PwM5SiDWH0C4Q+2RVqN5Vw7hgMcos0LrbR7tkrea6Jjxqxo7+vA/pLtvdVogNp86c/sr Z0cGwwG7jwd6X5Qdnn4SP1USJhV5O2p0ru4e/c3y1gveBM9HVyuH9pL7gVCwFV+tVJh8 I84w== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b39-v6si8177357plb.249.2018.06.15.04.38.06; Fri, 15 Jun 2018 04:38:21 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S965853AbeFOLgO (ORCPT + 99 others); Fri, 15 Jun 2018 07:36:14 -0400 Received: from usa-sjc-mx-foss1.foss.arm.com ([217.140.101.70]:41534 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S965701AbeFOLgO (ORCPT ); Fri, 15 Jun 2018 07:36:14 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id A7CEA1529; Fri, 15 Jun 2018 04:36:13 -0700 (PDT) Received: from lakrids.cambridge.arm.com (usa-sjc-imap-foss1.foss.arm.com [10.72.51.249]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 2D1B33F557; Fri, 15 Jun 2018 04:36:11 -0700 (PDT) Date: Fri, 15 Jun 2018 12:36:08 +0100 From: Mark Rutland To: Jin Yao Cc: acme@kernel.org, jolsa@kernel.org, peterz@infradead.org, mingo@redhat.com, alexander.shishkin@linux.intel.com, me@kylehuey.com, Linux-kernel@vger.kernel.org, vincent.weaver@maine.edu, will.deacon@arm.com, eranian@google.com, namhyung@kernel.org, ak@linux.intel.com, kan.liang@intel.com, yao.jin@intel.com Subject: Re: [PATCH v1 1/2] perf/core: Use sysctl to turn on/off dropping leaked kernel samples Message-ID: <20180615113608.6m74sm7gpl5p6oqe@lakrids.cambridge.arm.com> References: <1529057003-2212-1-git-send-email-yao.jin@linux.intel.com> <1529057003-2212-2-git-send-email-yao.jin@linux.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1529057003-2212-2-git-send-email-yao.jin@linux.intel.com> User-Agent: NeoMutt/20170113 (1.7.2) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jun 15, 2018 at 06:03:22PM +0800, Jin Yao wrote: > When doing sampling, for example: > > perf record -e cycles:u ... > > On workloads that do a lot of kernel entry/exits we see kernel > samples, even though :u is specified. This is due to skid existing. > > This might be a security issue because it can leak kernel addresses even > though kernel sampling support is disabled. > > One patch "perf/core: Drop kernel samples even though :u is specified" > was posted in last year but it was reverted because it introduced a > regression issue that broke the rr-project, which used sampling > events to receive a signal on overflow. These signals were critical > to the correct operation of rr. > > See '6a8a75f32357 ("Revert "perf/core: Drop kernel samples even > though :u is specified"")' for detail. > > Now the idea is to use sysctl to control the dropping of leaked > kernel samples. > > /sys/devices/cpu/perf_allow_sample_leakage: > > 0 - default, drop the leaked kernel samples. > 1 - don't drop the leaked kernel samples. Does this need to be conditional at all? At least for sampling the GPRs, we could do something like below unconditionally, which seems sufficient for my test cases. Mark. ---->8---- diff --git a/kernel/events/core.c b/kernel/events/core.c index 67612ce359ad..79a21531d57c 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -6359,6 +6359,24 @@ perf_callchain(struct perf_event *event, struct pt_regs *regs) return callchain ?: &__empty_callchain; } +static struct pt_regs *perf_get_sample_regs(struct perf_event *event, struct pt_regs *regs) +{ + /* + * Due to interrupt latency (AKA "skid"), we may enter the kernel + * before taking an overflow, even if the PMU is only counting user + * events. + * + * If we're not counting kernel events, always use the user regs when + * sampling. + * + * TODO: how does this interact with guest sampling? + */ + if (event->attr.exclude_kernel && !user_mode(regs)) + return task_pt_regs(current); + + return regs; +} + void perf_prepare_sample(struct perf_event_header *header, struct perf_sample_data *data, struct perf_event *event, @@ -6366,6 +6384,8 @@ void perf_prepare_sample(struct perf_event_header *header, { u64 sample_type = event->attr.sample_type; + regs = perf_get_sample_regs(event, regs); + header->type = PERF_RECORD_SAMPLE; header->size = sizeof(*header) + event->header_size;