From patchwork Fri Jul 28 16:54:23 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Will Deacon X-Patchwork-Id: 108894 Delivered-To: patch@linaro.org Received: by 10.182.45.195 with SMTP id p3csp676904obm; Fri, 28 Jul 2017 09:54:21 -0700 (PDT) X-Received: by 10.84.236.70 with SMTP id h6mr8442183pln.348.1501260861075; Fri, 28 Jul 2017 09:54:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1501260861; cv=none; d=google.com; s=arc-20160816; b=eaC527TwyLZQyV6xgW/84ocyYkHUi9RRlH7fYsjlv32p2A+Toz/gROysgZyPlFYhtt NYEC66fjl0q+LZIJxDC5Uq7nXlEBk4472xF1ZbqbI27IXtlbBKyYm+/2oeVPFPhm7ymv bH/od9VuVhd2/E9sVzzLobo3LiRbUtk+7G0zdmbOp/Z3jksKDR0W/gFyMh1Sfc3WOv22 L2TeMr/SSKtG3kYEbmY+5W5mWCtiFTInHwn3gVXRqXKf/Zs+mAXABOlzNjjc1+AdkW81 FKFF39XnrdxMwcbqYG3rzzRnJ0M/F7OoT3+KRqTxdmw5RJWa61NfcAdZNRaLU9gaOjSN B24g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:arc-authentication-results; bh=Gu2QZU/9mF5Ze/utuIHVdsDhQOhkPmDfK98jk3XJpuo=; b=tOHHOhPVix6fIdNs4csShyCwwR9x4QtjA58UatOaDV2gOD7qetrjpLjUGYzVaymreB HDqON4SH6ER9RGaidkTWexzxrWqwvP8R4nxnuXWJqxMJt24kx1wqaTcsQVwNTXr0qUFv grJbtreeV7y5D3IJfgK7SaUTmojVEPspm9YlIJpDyTiFFklPnKb9ff4pBOkgUIkW9JHY GuqttZZmx07bxNgF0SaxbJmQTICRNqfWt6ic5NfXuvo/ouejYC9gTq5jhQWBMmhkzDqH rArEcXiSbNnnDE9yNshq5+NLHn68v1JZ4PE5NHug/aqZb8dRCm8BOKv2yv92q/vzm4Ke UWkA== 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 39si4061342plc.386.2017.07.28.09.54.20; Fri, 28 Jul 2017 09:54: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 S1752544AbdG1QyT (ORCPT + 26 others); Fri, 28 Jul 2017 12:54:19 -0400 Received: from usa-sjc-mx-foss1.foss.arm.com ([217.140.101.70]:33318 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751882AbdG1QyR (ORCPT ); Fri, 28 Jul 2017 12:54:17 -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 E446D15BE; Fri, 28 Jul 2017 09:54:16 -0700 (PDT) Received: from edgewater-inn.cambridge.arm.com (usa-sjc-imap-foss1.foss.arm.com [10.72.51.249]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id B206A3F59D; Fri, 28 Jul 2017 09:54:16 -0700 (PDT) Received: by edgewater-inn.cambridge.arm.com (Postfix, from userid 1000) id D8A501AE06E0; Fri, 28 Jul 2017 17:54:24 +0100 (BST) From: Will Deacon To: linux-kernel@vger.kernel.org Cc: mark.rutland@arm.com, linux-arm-kernel@lists.infradead.org, Will Deacon , Alexander Shishkin , Peter Zijlstra Subject: [PATCH 2/2] perf/aux: Ensure aux_wakeup represents most recent wakeup index Date: Fri, 28 Jul 2017 17:54:23 +0100 Message-Id: <1501260863-14687-2-git-send-email-will.deacon@arm.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1501260863-14687-1-git-send-email-will.deacon@arm.com> References: <1501260863-14687-1-git-send-email-will.deacon@arm.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The aux_watermark member of struct ring_buffer represents the period (in terms of bytes) at which wakeup events should be generated when data is written to the aux buffer in non-snapshot mode. On hardware that cannot generate an interrupt when the aux_head reaches an arbitrary wakeup index (such as ARM SPE), the aux_head sampled from handle->head in perf_aux_output_{skip,end} may in fact be past the wakeup index. This can lead to wakeup slowly falling behind the head. For example, consider the case where hardware can only generate an interrupt on a page-boundary and the aux buffer is initialised as follows: // Buffer size is 2 * PAGE_SIZE rb->aux_head = rb->aux_wakeup = 0 rb->aux_watermark = PAGE_SIZE / 2 following the first perf_aux_output_begin call, the handle is initialised with: handle->head = 0 handle->size = 2 * PAGE_SIZE handle->wakeup = PAGE_SIZE / 2 and the hardware will be programmed to generate an interrupt at PAGE_SIZE. When the interrupt is raised, the hardware head will be at PAGE_SIZE, so calling perf_aux_output_end(handle, PAGE_SIZE) puts the ring buffer into the following state: rb->aux_head = PAGE_SIZE rb->aux_wakeup = PAGE_SIZE / 2 rb->aux_watermark = PAGE_SIZE / 2 and then the next call to perf_aux_output_begin will result in: handle->head = handle->wakeup = PAGE_SIZE for which the semantics are unclear and, for a smaller aux_watermark (e.g. PAGE_SIZE / 4), then the wakeup would in fact be behind head at this point. This patch fixes the problem by rounding down the aux_head (as sampled from the handle) to the nearest aux_watermark boundary when updating rb->aux_wakeup, therefore taking into account any overruns by the hardware. Cc: Alexander Shishkin Cc: Peter Zijlstra Reported-by: Mark Rutland Signed-off-by: Will Deacon --- kernel/events/ring_buffer.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) -- 2.1.4 Acked-by: Alexander Shishkin diff --git a/kernel/events/ring_buffer.c b/kernel/events/ring_buffer.c index 330df5a7f762..8e511e52fc1b 100644 --- a/kernel/events/ring_buffer.c +++ b/kernel/events/ring_buffer.c @@ -455,7 +455,7 @@ void perf_aux_output_end(struct perf_output_handle *handle, unsigned long size) if (aux_head - rb->aux_wakeup >= rb->aux_watermark) { wakeup = true; - rb->aux_wakeup += rb->aux_watermark; + rb->aux_wakeup = rounddown(aux_head, rb->aux_watermark); } if (wakeup) { @@ -490,7 +490,7 @@ int perf_aux_output_skip(struct perf_output_handle *handle, unsigned long size) aux_head = rb->user_page->aux_head = rb->aux_head; if (aux_head - rb->aux_wakeup >= rb->aux_watermark) { perf_output_wakeup(handle); - rb->aux_wakeup += rb->aux_watermark; + rb->aux_wakeup = rounddown(aux_head, rb->aux_watermark); handle->wakeup = rb->aux_wakeup + rb->aux_watermark; }