Received: by 2002:ab2:6816:0:b0:1f9:5764:f03e with SMTP id t22csp1417483lqo; Sat, 18 May 2024 02:16:00 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUJVaPWprdvgRVZG0fzQL6XZGjMfiKOfCJ9Jc3el/EoUA7gdCZNGeDuT4PUP6m5MkBoU1TGP/5z1NMKJ2vXLW1uNxpSuigmixAIGmvI+w== X-Google-Smtp-Source: AGHT+IE58P5INYFWrx/slkDMu2e8OqIN3QuGkNjO2CA6pTNUuyxSqpB0YONDUcGRvtJB74f5dY+t X-Received: by 2002:a05:6a00:1415:b0:6f4:b6fc:dfc7 with SMTP id d2e1a72fcca58-6f4e02ab7d0mr24342727b3a.9.1716023760025; Sat, 18 May 2024 02:16:00 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716023760; cv=pass; d=google.com; s=arc-20160816; b=J3w4fl2CMUlrQtZca9zGpNEcNx/HuM15087yJqA8dflTTDmFnvTOkGvBT2fAyaBMRc GyHLkqKUoTYkhMN0BcrU80CJpzZlSTe4Cnx7xGwxwjYHjm2EjpDlUTC13jqe6pT8dg0g vE416VPUHYWyPG3LoOmOLRoauylhCU50ZfaI/rwQSQex09Fes77PG8HmS7OqrlfObr8x C5uwjMlYOG0BV+ZxVXTeVQKQ2uBa77n7jug8fdT4GVa0oBDFJRKUVjh+/LG8xa0UPJlg r0MMW8yqTyQlu5n3CNQnWnqHJQO3Iv9vURlwIRdS50xoQGmKPB+9TA6pyvj85OmMmqQg iVgw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:sender:dkim-signature; bh=wIcX/mNWbGbXqi1DrcVgyMGxih1ay8vwVcGDjl7azI8=; fh=iA6bpm01QrRsiNHw6jdFUR1Fv4Go2feXsxBzanAm7VY=; b=gEaHiB6M74HPTfJxGqNTf8W+CKDlKYmdmqwbL3gu8EIcEagBASuMqjuKi1ewtcFqYG L5sCDZScSOyGb+l66q9GrbPlrbg+yFHTt4G4osAZvC03IFjpkfjOT2JMAgeSAMarmuC4 DKTPq7w/Qu7nCEykryu18w3h4tQcNdXfghU+b61/yVBmeJQMTrrS0/vKQPNOzCWQyU1D rQ6RZYrS3t1/4BbSQD4Jd4io6DXCBIq0JZzHVowJillVs2NPuH5923nLL9Ex8cn7JkV2 xflvSBUUuZJXx6LwgAWycLg6jIMJxFMm1K3bFtx5CNoUMgV8YN3lXwjtYnUMNLUqbmfb Zjmw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=D97c34ws; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-182807-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-182807-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id 41be03b00d2f7-6341180e35bsi19663224a12.549.2024.05.18.02.15.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 18 May 2024 02:16:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-182807-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=D97c34ws; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-182807-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-182807-linux.lists.archive=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 sy.mirrors.kernel.org (Postfix) with ESMTPS id A6EDFB212D1 for ; Sat, 18 May 2024 09:15:54 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 67C86168C4; Sat, 18 May 2024 09:15:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="D97c34ws" Received: from mail-ej1-f46.google.com (mail-ej1-f46.google.com [209.85.218.46]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9AB144A2D; Sat, 18 May 2024 09:15:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.46 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716023745; cv=none; b=PlmvWJ7UHkjjbkm1WePRPEmDOUHb1owRybF4l2A2JLgFV4PFsFrYDs4fKNAbOZRmNm1RLIl2yHbO9alVejhKNC9y0zfaBnCO0fz9DAYa46ESuOxyche2H+Kv8Arz9Zr9D5GzCi4TTnpkKfhy8YFycTX0FblcPG9DA5D/8E1skhE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716023745; c=relaxed/simple; bh=qmyHIIKcPVB794MM9BsicZz6ygBU5n10mGxUG2Nh+sM=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=MR5qHLiGY9etdTB7iEwZi0r6LYYN7xToN+S4Wbu3H+je56uEmObOBk8z6QyUxVclwONqHij8HccZzwC6pN2Zl62x6MpkRA8Einj6dVbiOydhvDZcRcmDXZkydF6xl8hQhAHVrwQXoJJa4aLnIPuqvMkSw1ouJR4ajjskDOT7/zE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=D97c34ws; arc=none smtp.client-ip=209.85.218.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-ej1-f46.google.com with SMTP id a640c23a62f3a-a59e4136010so588699366b.3; Sat, 18 May 2024 02:15:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716023742; x=1716628542; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:sender:from:to:cc:subject:date:message-id :reply-to; bh=wIcX/mNWbGbXqi1DrcVgyMGxih1ay8vwVcGDjl7azI8=; b=D97c34wsffoVBzmRtjDV2RqxJ/AflERloo2hQPGncW2zycsCOvAwigJPl0CV6/qAwH leqNS/IEML3ljip9CaGYfnorTRzNPcl9xwWRaSQ7XlzAUdv0b4E3o75y1dlphCWaTqdd 7tanAD73pWFM6Wqd4fjc9X5hs0qCoN+K1lWof+FdVxfKprDYS2dSI4fcP+7gDQ0J5vzH lN4Do264Qp7+THgUWN5i/LAM0TQbS5ELFU+o95QSSlrNhoWZUoRJfpQO8F7Lxc/ErrIA CQ6RaVh3wHHt0dVrJIEhoP9Nnmtm3n4ISZl6lVmkHP0IiAuFohR4+q5h0A1ekwuM6uZ7 pQNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716023742; x=1716628542; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:sender:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=wIcX/mNWbGbXqi1DrcVgyMGxih1ay8vwVcGDjl7azI8=; b=AVk8sXCoVR2ivF16mvJgGAT1ECcJAePdcs5yhhMkLcXUHrXt4lsptlh0bmbU/GQhM1 lFK8KDfm4PRmIq4E7cnWoilk6gxQw08kpG81XDNbOy+Xj3L91EgsymsmQfSyFofIMxzC VI8PoOgg15zIC8sDCB0b4FCivrB1kvDNVxuv/2SW4gvRSyY6VifF37EIWxi8z3+N91nz pGVWZ/cYa1/rAnKj3LCkgxOgcvwMDeJbZhbYODUrw0jxHvdzJvB4S6m9tq6lPgTJJYuo oLclBDOzwVF9DDqMzw0kzXOv1bsVlEtt0AgjWnVYzjz4Rc0s8rDwvPVaLDC6Mv+68WWx 49Vw== X-Forwarded-Encrypted: i=1; AJvYcCXUY8RYuCWq/S5lXNjkOIvJJy1r5TMk8nEu/sl9XE4tl/qEGnN4J/ZLt9C3oIZxyb7KWS67xyEJBj4wD4MkrK9UfE0rJnqEl043tIKS/zZv60SCHsBzTIV1wxog75K8GVN8Qj2fLd4z3kc41Amgvw== X-Gm-Message-State: AOJu0Yy+vfxm84Ls05OzR2x49qhLTPCYKC52RlP6ZhFyCG0LMDS5YFWz ejxV7sN257z3h3yg+K9ps0GeBt3VZ5cwefv/oZeVQBINTLH4GrTp X-Received: by 2002:a17:907:25c5:b0:a5a:6367:717e with SMTP id a640c23a62f3a-a5a6367bc70mr1470315266b.17.1716023741430; Sat, 18 May 2024 02:15:41 -0700 (PDT) Received: from gmail.com (1F2EF1AE.unconfigured.pool.telekom.hu. [31.46.241.174]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a5a1789247csm1199183466b.82.2024.05.18.02.15.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 18 May 2024 02:15:40 -0700 (PDT) Sender: Ingo Molnar Date: Sat, 18 May 2024 11:15:38 +0200 From: Ingo Molnar To: Uros Bizjak Cc: x86@kernel.org, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Peter Zijlstra , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Thomas Gleixner , Borislav Petkov , Dave Hansen , "H. Peter Anvin" Subject: Re: [PATCH] perf/x86/amd: Use try_cmpxchg() in events/amd/{un,}core.c Message-ID: References: <20240425101708.5025-1-ubizjak@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240425101708.5025-1-ubizjak@gmail.com> * Uros Bizjak wrote: > Replace this pattern in events/amd/{un,}core.c: > > cmpxchg(*ptr, old, new) == old > > ... with the simpler and faster: > > try_cmpxchg(*ptr, &old, new) > > The x86 CMPXCHG instruction returns success in the ZF flag, so this change > saves a compare after the CMPXCHG. > > No functional change intended. > > Signed-off-by: Uros Bizjak > Cc: Peter Zijlstra > Cc: Ingo Molnar > Cc: Arnaldo Carvalho de Melo > Cc: Namhyung Kim > Cc: Mark Rutland > Cc: Alexander Shishkin > Cc: Jiri Olsa > Cc: Ian Rogers > Cc: Adrian Hunter > Cc: Thomas Gleixner > Cc: Borislav Petkov > Cc: Dave Hansen > Cc: "H. Peter Anvin" > --- > arch/x86/events/amd/core.c | 3 ++- > arch/x86/events/amd/uncore.c | 6 ++++-- > 2 files changed, 6 insertions(+), 3 deletions(-) > > diff --git a/arch/x86/events/amd/core.c b/arch/x86/events/amd/core.c > index 1fc4ce44e743..c1b8aeff7bc0 100644 > --- a/arch/x86/events/amd/core.c > +++ b/arch/x86/events/amd/core.c > @@ -433,7 +433,8 @@ static void __amd_put_nb_event_constraints(struct cpu_hw_events *cpuc, > * when we come here > */ > for (i = 0; i < x86_pmu.num_counters; i++) { > - if (cmpxchg(nb->owners + i, event, NULL) == event) > + struct perf_event *tmp = event; > + if (try_cmpxchg(nb->owners + i, &tmp, NULL)) > break; > } > } > diff --git a/arch/x86/events/amd/uncore.c b/arch/x86/events/amd/uncore.c > index 4ccb8fa483e6..cac7c20d1aee 100644 > --- a/arch/x86/events/amd/uncore.c > +++ b/arch/x86/events/amd/uncore.c > @@ -162,7 +162,8 @@ static int amd_uncore_add(struct perf_event *event, int flags) > /* if not, take the first available counter */ > hwc->idx = -1; > for (i = 0; i < pmu->num_counters; i++) { > - if (cmpxchg(&ctx->events[i], NULL, event) == NULL) { > + struct perf_event *tmp = NULL; > + if (try_cmpxchg(&ctx->events[i], &tmp, event)) { > hwc->idx = i; > break; > } > @@ -196,7 +197,8 @@ static void amd_uncore_del(struct perf_event *event, int flags) > event->pmu->stop(event, PERF_EF_UPDATE); > > for (i = 0; i < pmu->num_counters; i++) { > - if (cmpxchg(&ctx->events[i], event, NULL) == event) > + struct perf_event *tmp = event; > + if (try_cmpxchg(&ctx->events[i], &tmp, NULL)) > break; > } Note that's a newline missing between local variable declaration block and the first statement after that. I've added all 3 to this patch (see below), but please keep an eye on this in future patches. Thanks, Ingo ================> arch/x86/events/amd/core.c | 1 + arch/x86/events/amd/uncore.c | 2 ++ 2 files changed, 3 insertions(+) diff --git a/arch/x86/events/amd/core.c b/arch/x86/events/amd/core.c index c1b8aeff7bc0..18bfe3451f3a 100644 --- a/arch/x86/events/amd/core.c +++ b/arch/x86/events/amd/core.c @@ -434,6 +434,7 @@ static void __amd_put_nb_event_constraints(struct cpu_hw_events *cpuc, */ for (i = 0; i < x86_pmu.num_counters; i++) { struct perf_event *tmp = event; + if (try_cmpxchg(nb->owners + i, &tmp, NULL)) break; } diff --git a/arch/x86/events/amd/uncore.c b/arch/x86/events/amd/uncore.c index cac7c20d1aee..0fafe233bba4 100644 --- a/arch/x86/events/amd/uncore.c +++ b/arch/x86/events/amd/uncore.c @@ -163,6 +163,7 @@ static int amd_uncore_add(struct perf_event *event, int flags) hwc->idx = -1; for (i = 0; i < pmu->num_counters; i++) { struct perf_event *tmp = NULL; + if (try_cmpxchg(&ctx->events[i], &tmp, event)) { hwc->idx = i; break; @@ -198,6 +199,7 @@ static void amd_uncore_del(struct perf_event *event, int flags) for (i = 0; i < pmu->num_counters; i++) { struct perf_event *tmp = event; + if (try_cmpxchg(&ctx->events[i], &tmp, NULL)) break; }