Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp3577050imm; Sun, 17 Jun 2018 23:56:32 -0700 (PDT) X-Google-Smtp-Source: ADUXVKK77FQ+TmhBLZ9ybWF9csDSJEEU+3hcNGatHLKOjGvAO5ZCo7keusCK6MQWdtMsY0oTQH6j X-Received: by 2002:a63:3348:: with SMTP id z69-v6mr9720917pgz.171.1529304992151; Sun, 17 Jun 2018 23:56:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1529304992; cv=none; d=google.com; s=arc-20160816; b=CFk7us85qIqacHVejunDglQYiV4MaRWik1Vpp4VfaPpCg6JZ9gbrmHU6n7aFaDiter nPW+XQlOo6zAF6qALkgoQ+sPMF9dh1kg6xuUguqdgaD5r6CvveEHPLYWV2VCA/HtjuP3 o29EUmnIgy8c9MxkcjL1N/pruCnhkQaBSWvoGCi4DXGh5hioNzObnNB4o8j2eE2iFUJy ObvUcESYWTJXUz8BMOVAdjzTb/JAodi+wWHgx51i7hwvlZpoAVMgVAxXMpLWDfYtUyug nTJyyTrlCCBawHjC2X5pTUZs8McP5TQ6FSbaIFajeb9kP+cTN9Jm0fF74NdkfnJ1jnLH WNBw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:arc-authentication-results; bh=kUZnq3+EsOrngFEs+ULFbWhJnqbXlZrfYWyBmwI/Nxc=; b=Mw3rkpp9Nh4Jqelo2d9xz41R4kyiZHJe0ySa6BDUzu3rSL1dt0pOmyfczcL2yaTG5a ilfJvWO8UNloHsnXwqpLSASpNJ4k+i/Gc3PBYFnoJIaXPuZ+avFd4qBYSKMO3YwMnlzL h/NfIf9cm4F5FwdNvSU6v9JHpdtXpBQO8aLP80nez8a/5U9jT/60FEX2IbvBSQbS4e+Y tbiJKcTePTLjUqdQzNq6MHaJ/3YBLznyhgD6PFvKzsR+s9/jlHVKGWGSnOTw3VUiZWcK ess9ZidEPy/KY2sTq4M7h4ssuFpRWzyqMJIMFDJFZjacJ6rDkVXpe0A2R7g4/RpzymX0 g/Vg== 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j12-v6si11308577pgf.359.2018.06.17.23.56.16; Sun, 17 Jun 2018 23:56:32 -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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755003AbeFRGzi (ORCPT + 99 others); Mon, 18 Jun 2018 02:55:38 -0400 Received: from mga03.intel.com ([134.134.136.65]:9437 "EHLO mga03.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754722AbeFRGzh (ORCPT ); Mon, 18 Jun 2018 02:55:37 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by orsmga103.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 17 Jun 2018 23:55:36 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.51,238,1526367600"; d="scan'208";a="58448407" Received: from yjin15-mobl.ccr.corp.intel.com (HELO [10.254.208.136]) ([10.254.208.136]) by fmsmga002.fm.intel.com with ESMTP; 17 Jun 2018 23:55:33 -0700 Subject: Re: [PATCH v1 1/2] perf/core: Use sysctl to turn on/off dropping leaked kernel samples To: Mark Rutland 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 References: <1529057003-2212-1-git-send-email-yao.jin@linux.intel.com> <1529057003-2212-2-git-send-email-yao.jin@linux.intel.com> <20180615113608.6m74sm7gpl5p6oqe@lakrids.cambridge.arm.com> From: "Jin, Yao" Message-ID: <52c75f12-1f91-405d-0b05-0aa6a9c09306@linux.intel.com> Date: Mon, 18 Jun 2018 14:55:32 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.8.0 MIME-Version: 1.0 In-Reply-To: <20180615113608.6m74sm7gpl5p6oqe@lakrids.cambridge.arm.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 6/15/2018 7:36 PM, Mark Rutland wrote: > 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; > > Hi Mark, Thanks for providing the patch. I understand this approach. In my opinion, the skid window is from counter overflow to interrupt delivered. While if the skid window is too *big* (e.g. user -> kernel), it should be not very useful. So personally, I'd prefer to drop the samples. Thanks Jin Yao