From patchwork Fri Aug 30 06:51:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 13784363 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CF6026CDBA; Fri, 30 Aug 2024 06:51:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725000711; cv=none; b=XcmiCg1r1da270kyXqJgJ4CLnKxo77xnrl/lRl7Zd/HIXy76B4GGkr359itQz9fcBciC/jrvniwsFlOClXmvosSMUiia7qhAL2ci97H/2UNTtt+ExIwkf3Q4E7ya4fwuGRjXKc8vQzG8T+9ilSwk6m5S4FtL/tEqerhthUvLmNQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725000711; c=relaxed/simple; bh=l7/nDEE1MX86mKrPXhx4ECRvV7lM/5hZGbDxDxymaoA=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=dMpEcPyCf8u412mJQX4Vwt21jAUzXIBZahM+ofv3OuubPgGk8Z8V1TYJShidJRkE/FLIEkl/p5zrrNjvWYyD4BGZuG5X9Yx4zyA0UKSqyc6/gbuxtSh2RNoG5rFdwz40+60dQMo7IG7YMlv530T9vQk+BksBEugifBxg8tEobOU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=gSZu/w8a; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="gSZu/w8a" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E8520C4CEC2; Fri, 30 Aug 2024 06:51:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1725000711; bh=l7/nDEE1MX86mKrPXhx4ECRvV7lM/5hZGbDxDxymaoA=; h=From:To:Cc:Subject:Date:From; b=gSZu/w8aYVrAzMOhJlI2Z61ZkTR2iWmFdvKF0bEIG82077RcpbHu6f616YrbCm/3p s3TNV49rc18keRoKlKFhwsNuBy1jJfe4f/dlHgRvPCNPg6RHVOjn8wcSji4kFVfa/P Stk0mcldAmMZW7tJsvFEubrjqAkR2V6WBy/GZiA2BGWtmhahMm9QMo7nB4Qe/0Xn2v Ebz2BHE+qGyoWm9BPKCq6/7q7skiKlPUuxDqo1o0W+Z7iLSxy17iZbxHfUToDvRfOq mj11hwALPpDfMkeI0XnbEA1IuW2OoQBJVNFG59RlBW3SdyX5b0q93PmL5X2uxpIUWx f7vx82vem0+jw== From: Namhyung Kim To: Arnaldo Carvalho de Melo , Ian Rogers , Kan Liang Cc: Jiri Olsa , Adrian Hunter , Peter Zijlstra , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org, Song Liu , bpf@vger.kernel.org Subject: [PATCH 1/3] perf lock contention: Handle error in a single place Date: Thu, 29 Aug 2024 23:51:48 -0700 Message-ID: <20240830065150.1758962-1-namhyung@kernel.org> X-Mailer: git-send-email 2.46.0.469.g59c65b2a67-goog Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 It has some duplicate codes to do the same job. Let's add a label and goto there to handle errors in a single place. Signed-off-by: Namhyung Kim --- tools/perf/util/bpf_skel/lock_contention.bpf.c | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/tools/perf/util/bpf_skel/lock_contention.bpf.c b/tools/perf/util/bpf_skel/lock_contention.bpf.c index d931a898c434..e8a6f6463019 100644 --- a/tools/perf/util/bpf_skel/lock_contention.bpf.c +++ b/tools/perf/util/bpf_skel/lock_contention.bpf.c @@ -439,11 +439,8 @@ int contention_end(u64 *ctx) duration = bpf_ktime_get_ns() - pelem->timestamp; if ((__s64)duration < 0) { - pelem->lock = 0; - if (need_delete) - bpf_map_delete_elem(&tstamp, &pid); __sync_fetch_and_add(&time_fail, 1); - return 0; + goto out; } switch (aggr_mode) { @@ -477,11 +474,8 @@ int contention_end(u64 *ctx) data = bpf_map_lookup_elem(&lock_stat, &key); if (!data) { if (data_map_full) { - pelem->lock = 0; - if (need_delete) - bpf_map_delete_elem(&tstamp, &pid); __sync_fetch_and_add(&data_fail, 1); - return 0; + goto out; } struct contention_data first = { @@ -502,10 +496,7 @@ int contention_end(u64 *ctx) data_map_full = 1; __sync_fetch_and_add(&data_fail, 1); } - pelem->lock = 0; - if (need_delete) - bpf_map_delete_elem(&tstamp, &pid); - return 0; + goto out; } __sync_fetch_and_add(&data->total_time, duration); @@ -517,6 +508,7 @@ int contention_end(u64 *ctx) if (data->min_time > duration) data->min_time = duration; +out: pelem->lock = 0; if (need_delete) bpf_map_delete_elem(&tstamp, &pid); From patchwork Fri Aug 30 06:51:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 13784364 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5F4231531DC; Fri, 30 Aug 2024 06:51:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725000712; cv=none; b=TWZoJMBlFEPfSKskbdgjkpCMrG5MF4IfcFZAqQXws2deV4mqz/31cIwdY/wMsWTdWYvn034da0SXbnokUyK2u5N9+eQ/s0yEiAAlnEDAUneJ3GJ33VbuJsWU5EvorGEX/QAFdh8hHuBVY6ZMZNjKiNtJ2rJHNqRlXxkFIVnf+cs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725000712; c=relaxed/simple; bh=q4/3jfLknydUp8HCEo8sK+b+5kXMu3szQ6Mw0K+Tp88=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=E3fmpL/le4ShE/GoPakAd3VgMF+KWrD7jfb89TV1vP2/nLFumc14bbeAhLrmeQJ2tlSYfoeimOEuRv4lJbGWBlFwLgxgpfcKfstPsMyn2c6s6AteC6F9VMIp4hIdKI5rLt4Y6enWfsiDvIzupEmZGByJd8/y6a7Zzhp9WIZh8OU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=CG2SaVt+; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="CG2SaVt+" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7B1ACC4CECA; Fri, 30 Aug 2024 06:51:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1725000711; bh=q4/3jfLknydUp8HCEo8sK+b+5kXMu3szQ6Mw0K+Tp88=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=CG2SaVt+pytwgzo19CuFRkT6ZlwILs1M5j1P5aWhvNnN26M7+EO6cn3XeR1+sB6WR C8ZwgYvI0NlWqw/oY/ThuAeElfdqGpf1gffkvdnVle3niOvxUxw2e8l+kS1kzk2dth tIFdaF8VfzAmndElUz+M7H6ewNcfFelUkCZe6EwJlyI9lrjk+XBYe64/CySmVkujkk e9UlPKnXQHueVOOKG5FhPI2j/SoxYyuECHWb8RGxnRyuqU/JSp/mVUqB2BAnM8hIl8 SHZVw2RZ63Q0NZR1X6B7yAgScWIQzRR4QJoZY8DaBS/hGy66nSnfE67St0gpqaFfiV eCpzXKspWPrrw== From: Namhyung Kim To: Arnaldo Carvalho de Melo , Ian Rogers , Kan Liang Cc: Jiri Olsa , Adrian Hunter , Peter Zijlstra , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org, Song Liu , bpf@vger.kernel.org Subject: [PATCH 2/3] perf lock contention: Simplify spinlock check Date: Thu, 29 Aug 2024 23:51:49 -0700 Message-ID: <20240830065150.1758962-2-namhyung@kernel.org> X-Mailer: git-send-email 2.46.0.469.g59c65b2a67-goog In-Reply-To: <20240830065150.1758962-1-namhyung@kernel.org> References: <20240830065150.1758962-1-namhyung@kernel.org> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The LCB_F_SPIN bit is used for spinlock, rwlock and optimistic spinning in mutex. In get_tstamp_elem() it needs to check spinlock and rwlock only. As mutex sets the LCB_F_MUTEX, it can check those two bits and reduce the number of operations. Signed-off-by: Namhyung Kim --- tools/perf/util/bpf_skel/lock_contention.bpf.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tools/perf/util/bpf_skel/lock_contention.bpf.c b/tools/perf/util/bpf_skel/lock_contention.bpf.c index e8a6f6463019..4b7237e178bd 100644 --- a/tools/perf/util/bpf_skel/lock_contention.bpf.c +++ b/tools/perf/util/bpf_skel/lock_contention.bpf.c @@ -323,8 +323,7 @@ static inline struct tstamp_data *get_tstamp_elem(__u32 flags) struct tstamp_data *pelem; /* Use per-cpu array map for spinlock and rwlock */ - if (flags == (LCB_F_SPIN | LCB_F_READ) || flags == LCB_F_SPIN || - flags == (LCB_F_SPIN | LCB_F_WRITE)) { + if ((flags & (LCB_F_SPIN | LCB_F_MUTEX)) == LCB_F_SPIN) { __u32 idx = 0; pelem = bpf_map_lookup_elem(&tstamp_cpu, &idx); From patchwork Fri Aug 30 06:51:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 13784365 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DC8521531FE; Fri, 30 Aug 2024 06:51:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725000713; cv=none; b=cD4iXub3LDYQaOKeudvf08TJrZp91ZQItss0QWnVxLJsA86mP8bzzcNV9AT618QApDkrJv4+4thEcs37fkwBQaRdRDNYLWhRAOpvmIn0a93i/hD4lDy7Q1qtLUc/IE136/Nixr1BIujiXKzEWckAhU8kZUkbkVZ5itm46g+C2Dg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725000713; c=relaxed/simple; bh=wCR9miOWHuvC38OVs2dILCHpxCTCD26WoFfBRNFtywY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=BSAfcVYptVp/s5YDNRPFBwrZbvO7e1MwBZuQRCX8h1UfMfe+dtiDcgHY2+aZHbHWuuGmbbhzJqyrl7UmSXnsFgEcNPH7xmxa3cinsxHzo8b8u16qEnT3SEFu8gsRwqgmI+/Shr4I7NGzFhp1Q6hZABHKzOegbKKicZP57G3PNdQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=nEISNJ5h; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="nEISNJ5h" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0C1DAC4CEC2; Fri, 30 Aug 2024 06:51:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1725000712; bh=wCR9miOWHuvC38OVs2dILCHpxCTCD26WoFfBRNFtywY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nEISNJ5hNoowtpXMTYmu6CxZzOrlXDCrbXZfokfHyCZ/hgLiFChgj8N1jGvcrewIQ xyd70x7tpZxElAKM7Nm7Sd2Y5T1T8YNGz0pPUsJ7Hk3RqzcZuOsgFCoRZqCB8ytoBu KjDQIa8aJ+WUik9qqRaRAT9K/b2GV3psU+nNqU1bi4/pW0oOIwb0d/ptATMbXT2mMI y1jefhatg5Ah1ePHS9XVz+GQw6mQ688SV+887Wh5LV6uoSRiMIoxWBjcnLvgRatY+3 L0inblkGn6jJZGRav1lgI1fzt8Y3rCF+iv3h1ckgC5JgbXkG/l8+J/r7lGgnbgV2z6 YSVJBu2mzQHRQ== From: Namhyung Kim To: Arnaldo Carvalho de Melo , Ian Rogers , Kan Liang Cc: Jiri Olsa , Adrian Hunter , Peter Zijlstra , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org, Song Liu , bpf@vger.kernel.org Subject: [PATCH 3/3] perf lock contention: Do not fail EEXIST for update Date: Thu, 29 Aug 2024 23:51:50 -0700 Message-ID: <20240830065150.1758962-3-namhyung@kernel.org> X-Mailer: git-send-email 2.46.0.469.g59c65b2a67-goog In-Reply-To: <20240830065150.1758962-1-namhyung@kernel.org> References: <20240830065150.1758962-1-namhyung@kernel.org> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 When it updates the lock stat for the first time, it needs to create an element in the BPF hash map. But if there's a concurrent thread waiting for the same lock (like for rwsem or rwlock), it might race with the thread and possibly failed to update with -EEXIST. In that case, it can lookup the map again and put the data there instead of failing. Signed-off-by: Namhyung Kim --- tools/perf/util/bpf_skel/lock_contention.bpf.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tools/perf/util/bpf_skel/lock_contention.bpf.c b/tools/perf/util/bpf_skel/lock_contention.bpf.c index 4b7237e178bd..52a876b42699 100644 --- a/tools/perf/util/bpf_skel/lock_contention.bpf.c +++ b/tools/perf/util/bpf_skel/lock_contention.bpf.c @@ -491,6 +491,12 @@ int contention_end(u64 *ctx) err = bpf_map_update_elem(&lock_stat, &key, &first, BPF_NOEXIST); if (err < 0) { + if (err == -EEXIST) { + /* it lost the race, try to get it again */ + data = bpf_map_lookup_elem(&lock_stat, &key); + if (data != NULL) + goto found; + } if (err == -E2BIG) data_map_full = 1; __sync_fetch_and_add(&data_fail, 1); @@ -498,6 +504,7 @@ int contention_end(u64 *ctx) goto out; } +found: __sync_fetch_and_add(&data->total_time, duration); __sync_fetch_and_add(&data->count, 1);