Received: by 2002:a89:48b:0:b0:1f5:f2ab:c469 with SMTP id a11csp1005373lqd; Thu, 25 Apr 2024 03:19:23 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCXhPChZbZ3+cEh5XAkewlSYkkaQB0IFrdH7S0PlO4LDwuRCFlX+YBiImZEzSDk7A5lYhNJsBpAXuysdvW3RwZvlMoamEYXIubEWtmBnFQ== X-Google-Smtp-Source: AGHT+IG11BZbhMfGMYBhRjMU+p0XcBUUs+H2HPW9a1nPbpZrC/MVAfwvcmRjUwf5L2OnxfsAe8cU X-Received: by 2002:a17:90a:be0f:b0:2ab:eff6:e2ce with SMTP id a15-20020a17090abe0f00b002abeff6e2cemr5417000pjs.0.1714040362952; Thu, 25 Apr 2024 03:19:22 -0700 (PDT) Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id e15-20020a63744f000000b00605451bffdasi3253181pgn.366.2024.04.25.03.19.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Apr 2024 03:19:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-158376-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20230601 header.b=gKZUmFgM; arc=fail (body hash mismatch); spf=pass (google.com: domain of linux-kernel+bounces-158376-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-158376-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com 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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 5FFB8287A44 for ; Thu, 25 Apr 2024 10:18:06 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9768B127B50; Thu, 25 Apr 2024 10:17:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="gKZUmFgM" Received: from mail-ed1-f53.google.com (mail-ed1-f53.google.com [209.85.208.53]) (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 11ED386AE2; Thu, 25 Apr 2024 10:17:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.53 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714040250; cv=none; b=e1hI3dK+KJmyxARZip1812oUG2/aLJ4YVBjT8Z3ct42+HjLIIDmBfGWUjEN3GZ9qx83gkOKuAKVuKrsoF+aKArO5OjylQraUBvyALpv3A8pn4KegCuzQIoCKP/gdkJFEyhe9eIYo79aM2vxapDgY/evONbsPVgwYo40XM+27OaQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714040250; c=relaxed/simple; bh=h6c/iz0/hGW+t/SoQR9KYo1rv6hXOZ73jgFdz1C30Eg=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=OJAtJfRDo/yT383R0QTvkecuANDMRtI0vvQ4rnBfLHFIc1WpTmcwZ8zu6mT9ky5hkgHyinOsl31OgALqq6oZ9l0jDLSkx0e53TRTuX3/YeVEN3EQzLVztyP9S+Tu4Rq0IC1UU++sp6HtH3w1I7PHLfb67Jsz+sCTuglZ9AC/NRE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=gKZUmFgM; arc=none smtp.client-ip=209.85.208.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-ed1-f53.google.com with SMTP id 4fb4d7f45d1cf-571c2055cb1so129411a12.1; Thu, 25 Apr 2024 03:17:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714040247; x=1714645047; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=DyJz1NSu4FQMeQ6GqAUKmKYgFhAoaWe3Xjy6oxrQH3g=; b=gKZUmFgMGrGL9wJM5bBEjPl2mIkywCPoNzMCyks9T9G19yeUK+7/iAKnkVN8BFRTXx N8/rW0B0TUlSDc14jiWM3uoBK3qCSXAu2DW9O64MHNHyuT5TIDOc0DmW0XQxkEyYuTkW kbXYgjUKnT2eShpe556tZocit0ki2NdoQooyy6t4aqmYEEgQjKUtrFyd767ZGICZQeVU B55tMqkivnJlG0HB+4IsyVAhbdpHa6ZIAMIInSQ1NQFhZXK9Rme+I47gCfWWJdC6dYLN xvSgcHgFqpHL6sAw7VhLmnI6LFVsh6WoX4QqlIz1NnBxudXc0yitLmf9CxoxAL5MUOIz BDJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714040247; x=1714645047; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=DyJz1NSu4FQMeQ6GqAUKmKYgFhAoaWe3Xjy6oxrQH3g=; b=qvQdltl/2rjI4uEhR9YYgsSRLmYgkIfOx0ejHlsG/x2Pb56eocKaJ6xFiZg0l5lF5E Fv1R4TzwtHrPMLQkOfN8ZT7FyhTxF1IdY6FylydLgEn47Rg5ZrjKLaq9unwrt+WBMfS0 ZyNQ7iMVuldX8zdiF276t57eUcFUle5dfs8J9yQjSOCD6lXdTc+zesUVsqNsF9P5wKeV flQJtO5R4lm9a4eysS6OGDUtkF97Q4TJl00n6KT6uabA/2aPy+2BPnvUJ52MS4kEeAP+ 6/IcC4TPBtnG0RT+ifqU33liwSFCyn2gaHSwWfejI4Tg0S0QQSCyWnFccGk0ELilxNmu XEjQ== X-Forwarded-Encrypted: i=1; AJvYcCWSsgCsHd1CkS3l8MBpQHw7kHFQ4FUTWMyiZQ1kuntp2G2xtKLq7rGaenKYU7+273ZN23UoLTV/cManiSmkzoiIqm5O3EgeQkvdxDyPaJXJwsgtZI3EnNiIJ8KFAf/SQSa0h6Jp8vzG6kjPsfNOkw== X-Gm-Message-State: AOJu0YzYFa7zkF0t9RHdCzVvnmKvTrE4vIhb7fHkzllxqZX6+tQ+1PZ5 BuyD4/u7U6aREEnBSwToAIJ5fK5kx8oIgl3e9XhXSxEz7SAblHeU X-Received: by 2002:a50:cd93:0:b0:56e:2e4d:884e with SMTP id p19-20020a50cd93000000b0056e2e4d884emr4478808edi.0.1714040247031; Thu, 25 Apr 2024 03:17:27 -0700 (PDT) Received: from fedora.iskraemeco.si ([193.77.86.250]) by smtp.gmail.com with ESMTPSA id o9-20020aa7c7c9000000b005721127eefbsm4004600eds.17.2024.04.25.03.17.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Apr 2024 03:17:26 -0700 (PDT) From: Uros Bizjak To: x86@kernel.org, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Uros Bizjak , Peter Zijlstra , Ingo Molnar , 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: [PATCH] perf/x86/amd: Use try_cmpxchg() in events/amd/{un,}core.c Date: Thu, 25 Apr 2024 12:16:14 +0200 Message-ID: <20240425101708.5025-1-ubizjak@gmail.com> X-Mailer: git-send-email 2.44.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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; } -- 2.44.0