Received: by 2002:a25:d7c1:0:0:0:0:0 with SMTP id o184csp4057055ybg; Mon, 21 Oct 2019 03:07:04 -0700 (PDT) X-Google-Smtp-Source: APXvYqzFSFFFGn1t4sQ+wXJWg4RHcbfNOHE2nCT1pG/jOQln8aYjnzeeROffuU13vZofDcTumU9x X-Received: by 2002:aa7:d4d9:: with SMTP id t25mr24142298edr.76.1571652424365; Mon, 21 Oct 2019 03:07:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1571652424; cv=none; d=google.com; s=arc-20160816; b=QY1qn1ufL6jJlNzGbPcGz3ey2x2YSEjcqyja21DGBJDHFST7HjagR4jyLDqHjApGhW 4OVpPmGRg8M6QhUvURwpk1RZgP0rVrp74LDIp9k+9r0N5tlNvMPLqPOMpWzdtgE2tkE8 JM1I1sCcy6gvttTJZNW1FUy5rVHWKW+/IBi7xlq4/L8rFG7R+mRcpv+APLQmKKRKrZ6C OFuzZqOnQSMo4ymQ/8LdFvoxqG6HVHo2YMi3jlu2Pgt5ycLx4ibzIZkpwjqbnroLtmtB 94+/PEPzVf9p5ORHXwxWr1iu3JAHLLg9eA2omyMs3/YneYHdiVBBVV0UB/cTZnRJYuny U6MA== 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:dkim-signature; bh=2kwxvwEmTAe/D98HW8RU5ge31HdCDSxAfN1n0LHssq0=; b=QF/QwivipkK8PNEgA12az2jE1zZG3gIsloqs7WzltN18reFIB+HItEc1n1rYcbFx56 DXiHTANwTguW/7gQNTmoGOqdhk4oV4RJFkGlq0ePE76NzPtByctYiZoDfACBc0uwzsyr uvbvSRRFajVldZ0or2DZCPB+xvF8a9Dn7xMVEQ6gyXCF2HPOLqCCdXSrCaLWqe1FwsSl 7csh8RA9CYyW7v+UZapvMBDT9j+/HO7xk2RTHvbWYU7eYfZsbB+1cjSvc6vUHR6hZpK8 hs6Tb27apirsb9EWV9fc8L18PkQnmyF8MjV7PqPao1a/iaVryH4Cl/QIYJRBJYzhx6Ts 4+RA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b="opkIn8p/"; 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 q4si8238514ejb.136.2019.10.21.03.06.41; Mon, 21 Oct 2019 03:07:04 -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; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b="opkIn8p/"; 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 S1727942AbfJUKGJ (ORCPT + 99 others); Mon, 21 Oct 2019 06:06:09 -0400 Received: from bombadil.infradead.org ([198.137.202.133]:52406 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726767AbfJUKGI (ORCPT ); Mon, 21 Oct 2019 06:06:08 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=In-Reply-To:Content-Type:MIME-Version :References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=2kwxvwEmTAe/D98HW8RU5ge31HdCDSxAfN1n0LHssq0=; b=opkIn8p/+wSw7gu3RUkgH4JYi RLYYj/P9Ep3LxdeiOJS9bevy0wZrBSSbDPPJEOJ1ZSzQA7DMqdhb/E1Nzx8s1daJoJPQ0vAlbSKq6 Nq7jvI0OcW8ZfkN5FBPqE7yFqpKNwyE6jhbuyVrMJ9AJj+CEp0FsV+qHYN+rpNpf2QM+OozYTkCq8 vL/Kpax1Yy/ERAreX8OeadIKozCCkZaZoRnreH6WFpS8iERTlRfxM7u1iu2htwBUdUyALOrpXkQMw yna8LpYAXAhm51mOBtWfkw/Cs5U5yicu9saAR4SRz18adaZpW5xenMEU/R1XZsMFe/zqnOUYj/Y6H N0lU4AXTw==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=noisy.programming.kicks-ass.net) by bombadil.infradead.org with esmtpsa (Exim 4.92.3 #3 (Red Hat Linux)) id 1iMUZc-00030f-I5; Mon, 21 Oct 2019 10:06:00 +0000 Received: from hirez.programming.kicks-ass.net (hirez.programming.kicks-ass.net [192.168.1.225]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id 3BA9E303DA1; Mon, 21 Oct 2019 12:05:01 +0200 (CEST) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id 315CE202411A1; Mon, 21 Oct 2019 12:05:58 +0200 (CEST) Date: Mon, 21 Oct 2019 12:05:58 +0200 From: Peter Zijlstra To: Stephane Eranian Cc: linux-kernel@vger.kernel.org, mingo@elte.hu, acme@redhat.com, jolsa@redhat.com, kan.liang@intel.com, songliubraving@fb.com, irogers@google.com Subject: Re: [PATCH] perf/core: fix multiplexing event scheduling issue Message-ID: <20191021100558.GC1800@hirez.programming.kicks-ass.net> References: <20191018002746.149200-1-eranian@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20191018002746.149200-1-eranian@google.com> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Oct 17, 2019 at 05:27:46PM -0700, Stephane Eranian wrote: > @@ -2153,6 +2157,7 @@ __perf_remove_from_context(struct perf_event *event, > void *info) > { > unsigned long flags = (unsigned long)info; > + int was_necessary = ctx->rotate_necessary; > > if (ctx->is_active & EVENT_TIME) { > update_context_time(ctx); > @@ -2171,6 +2176,37 @@ __perf_remove_from_context(struct perf_event *event, > cpuctx->task_ctx = NULL; > } > } > + > + /* > + * sanity check that event_sched_out() does not and will not > + * change the state of ctx->rotate_necessary > + */ > + WARN_ON(was_necessary != event->ctx->rotate_necessary); It doesn't... why is this important to check? > + /* > + * if we remove an event AND we were multiplexing then, that means > + * we had more events than we have counters for, and thus, at least, > + * one event was in INACTIVE state. Now, that we removed an event, > + * we need to resched to give a chance to all events to get scheduled, > + * otherwise some may get stuck. > + * > + * By the time this function is called the event is usually in the OFF > + * state. > + * Note that this is not a duplicate of the same code in _perf_event_disable() > + * because the call path are different. Some events may be simply disabled It is the exact same code twice though; IIRC this C language has a feature to help with that. > + * others removed. There is a way to get removed and not be disabled first. > + */ > + if (ctx->rotate_necessary && ctx->nr_events) { > + int type = get_event_type(event); > + /* > + * In case we removed a pinned event, then we need to > + * resched for both pinned and flexible events. The > + * opposite is not true. A pinned event can never be > + * inactive due to multiplexing. > + */ > + if (type & EVENT_PINNED) > + type |= EVENT_FLEXIBLE; > + ctx_resched(cpuctx, cpuctx->task_ctx, type); > + } What you're relying on is that ->rotate_necessary implies ->is_active and there's pending events. And if we tighten ->rotate_necessary you can remove the && ->nr_events. > @@ -2232,6 +2270,35 @@ static void __perf_event_disable(struct perf_event *event, > event_sched_out(event, cpuctx, ctx); > > perf_event_set_state(event, PERF_EVENT_STATE_OFF); > + /* > + * sanity check that event_sched_out() does not and will not > + * change the state of ctx->rotate_necessary > + */ > + WARN_ON_ONCE(was_necessary != event->ctx->rotate_necessary); > + > + /* > + * if we disable an event AND we were multiplexing then, that means > + * we had more events than we have counters for, and thus, at least, > + * one event was in INACTIVE state. Now, that we disabled an event, > + * we need to resched to give a chance to all events to be scheduled, > + * otherwise some may get stuck. > + * > + * Note that this is not a duplicate of the same code in > + * __perf_remove_from_context() > + * because events can be disabled without being removed. It _IS_ a duplicate, it is the _exact_ same code twice. What you're trying to say is that we need it in both places, but that's something else entirely. > + */ > + if (ctx->rotate_necessary && ctx->nr_events) { > + int type = get_event_type(event); > + /* > + * In case we removed a pinned event, then we need to > + * resched for both pinned and flexible events. The > + * opposite is not true. A pinned event can never be > + * inactive due to multiplexing. > + */ > + if (type & EVENT_PINNED) > + type |= EVENT_FLEXIBLE; > + ctx_resched(cpuctx, cpuctx->task_ctx, type); > + } > }