From patchwork Tue Feb 7 00:24:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 53570 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2548306wrn; Mon, 6 Feb 2023 16:25:35 -0800 (PST) X-Google-Smtp-Source: AK7set9GIk6JacACKppXPfrxdJ+QG0kiAp6f+4RxP6wiv3RjBQ+/Jbiy7zFG6hk0xLfg65WdSp2d X-Received: by 2002:a62:1a4f:0:b0:593:b491:40b8 with SMTP id a76-20020a621a4f000000b00593b49140b8mr1268620pfa.4.1675729535461; Mon, 06 Feb 2023 16:25:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675729535; cv=none; d=google.com; s=arc-20160816; b=RRLX8wNyftaXAQn1Vl8MYNnjHVpdWvsC/pqvfX3zwK4Kxnj9TZBSS4NhOyQVNcSDQt zhXe3gMTYP6chho/r01bQ/Xw8uDSb0fhhiu67wUvMA2KVSytmT0orc27GuSLbV8fMnGt I9fxTd0BuqIACG2mK2HmqJJSN12n/LI5PX92QXRRxFuqoH7CGK9BLUy2JjcSk7/PF4Pq vvwQA80G/hGbKdubwIKylJeeEfpOJc5E6N3EKRZAP9uZrsDz1zsno7LyBf1V8comzpsn 7Z3Jfx9LafHAY57Po8+sIaXkXCIUWnQnRQfg1JjVtr4oau03/ZWLjfF/m6TKkN4xCHB6 45AQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from:sender :dkim-signature; bh=T0Snqb6CMiuetiO6S5s5rorG7sEC2nCZI7mBQw8ulK8=; b=SWLGU+ZEgq4VE+YUHi8ZCi3rA+MSaM0yanU4RsFDLCTmJVqmyaqqXclcm4D3aHMCGj Ro/k6qYomxrkHidrsGH5ENxoV1wwYnhbRtgPGkk72kboewMVZMmOrxSX1FWzfDVH43XE 77wm1VblXegYrXbRx6/EF4wicZ5DtxI7mOzKa8KG+tabJyKTIxlR2IQHWkfDSQ5xHZr9 QXypaDTTMM/gGUpoXrTLQMx8CSIyQZ/amWXiDJqWeMpVBtpot+j7WXcAq4KgEq45pllK 2yM37Tz6UeCEwyEN9C6xyCEcFm7x7U1kaSicgZ5tVs1AiPr8nneVgXmAlGzbGiRNC29H U4rg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=jSEVYwkQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y2-20020aa79e02000000b00593115af5a6si13404580pfq.260.2023.02.06.16.25.22; Mon, 06 Feb 2023 16:25:35 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=jSEVYwkQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230224AbjBGAYa (ORCPT + 99 others); Mon, 6 Feb 2023 19:24:30 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56488 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229803AbjBGAYV (ORCPT ); Mon, 6 Feb 2023 19:24:21 -0500 Received: from mail-pj1-x102c.google.com (mail-pj1-x102c.google.com [IPv6:2607:f8b0:4864:20::102c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 327C330E95; Mon, 6 Feb 2023 16:24:12 -0800 (PST) Received: by mail-pj1-x102c.google.com with SMTP id d6-20020a17090ae28600b00230aa72904fso4962692pjz.5; Mon, 06 Feb 2023 16:24:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=T0Snqb6CMiuetiO6S5s5rorG7sEC2nCZI7mBQw8ulK8=; b=jSEVYwkQBzQaYKCLjFi2jxJc8xvzQiOP2buZt/7haPKVMnJsT4GKq0rvX+np26uIp9 VRXjrnHuDVX/JFgreGZ8mPIeGahkLYpJ94A4vm2zBPvxVCeO5LUEtKq87gWschHbVMBs 1vmLQjDWKT5rTNV2IxZHe35GTPXuQjNDb75R+g7TbkIgtsFemSnW5a1oCLM4+t3so/R3 PppYSnVR7/TQIqYn25nEIgNtULtK/f0dYKNlYEyWvfsk0L5+TaKMRXcUGn7WWJPlJjoj JTgODwguyhloF0FJuFNuAgzkeVC/cTz3lgun7E1lenxfc9vHg/fKhUnYb5qNKN7Dls5s /jjg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=T0Snqb6CMiuetiO6S5s5rorG7sEC2nCZI7mBQw8ulK8=; b=XL8Spp4NqaCeKIyQa7LhwQxeelfH3uQU3PNRfoKfIFKmW138qLICX1H/HToLPl1fEn mokGQcWw/xclklYXlAcgUjWbmTGH+vWN6vs0G3gNux0ionjK80zuN/ls00oICDADTow+ PTMAXo1bjoqOAJVOcvc/hsZdUqcPeC1czKm21i+TqPRjzqZ2LhRqkT9vSZyU21FMTP9P iCb9M7unQPfYMGXkSyc6hj9FLkQJm6WFBd5eHQGqNDgiKa/6oZiy/8BnDLy3Ikk50fzw /1rkqBBGIrQsZj2wkTx49uftZCiQfwqfSzSFsU6n0KgxMxoWznSUGPQaasK3Y05Vu+zI FsPw== X-Gm-Message-State: AO0yUKVZiUMCQr4XlB6r2ET7a/F7JLlnapBzPV/30kcpYnoi2bwtI18C FjZEALpivAsxZ5pjWQzYZ4o= X-Received: by 2002:a17:903:182:b0:199:14a5:405b with SMTP id z2-20020a170903018200b0019914a5405bmr1070603plg.44.1675729451569; Mon, 06 Feb 2023 16:24:11 -0800 (PST) Received: from balhae.hsd1.ca.comcast.net ([2601:647:6780:ff0:47cc:64dd:1572:cf18]) by smtp.gmail.com with ESMTPSA id y1-20020a1709029b8100b00189b2b8dbedsm3544080plp.228.2023.02.06.16.24.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Feb 2023 16:24:11 -0800 (PST) Sender: Namhyung Kim From: Namhyung Kim To: Arnaldo Carvalho de Melo , Jiri Olsa Cc: Ingo Molnar , Peter Zijlstra , LKML , Ian Rogers , Adrian Hunter , linux-perf-users@vger.kernel.org, Will Deacon , Waiman Long , Boqun Feng , Davidlohr Bueso , Song Liu , Hao Luo , bpf@vger.kernel.org Subject: [PATCH 3/3] perf lock contention: Support old rw_semaphore type Date: Mon, 6 Feb 2023 16:24:03 -0800 Message-Id: <20230207002403.63590-4-namhyung@kernel.org> X-Mailer: git-send-email 2.39.1.519.gcb327c4b5f-goog In-Reply-To: <20230207002403.63590-1-namhyung@kernel.org> References: <20230207002403.63590-1-namhyung@kernel.org> MIME-Version: 1.0 X-Spam-Status: No, score=-1.5 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1757129772922700111?= X-GMAIL-MSGID: =?utf-8?q?1757129772922700111?= The old kernel has a different type of the owner field in rwsem. We can check it using bpf_core_type_matches() builtin in clang but it also needs its own version check since it's available on recent versions. Cc: Hao Luo Signed-off-by: Namhyung Kim --- .../perf/util/bpf_skel/lock_contention.bpf.c | 57 ++++++++++++++----- 1 file changed, 44 insertions(+), 13 deletions(-) diff --git a/tools/perf/util/bpf_skel/lock_contention.bpf.c b/tools/perf/util/bpf_skel/lock_contention.bpf.c index c5556606134e..e6007eaeda1a 100644 --- a/tools/perf/util/bpf_skel/lock_contention.bpf.c +++ b/tools/perf/util/bpf_skel/lock_contention.bpf.c @@ -84,6 +84,14 @@ struct { __uint(max_entries, 1); } addr_filter SEC(".maps"); +struct rw_semaphore___old { + struct task_struct *owner; +} __attribute__((preserve_access_index)); + +struct rw_semaphore___new { + atomic_long_t owner; +} __attribute__((preserve_access_index)); + /* control flags */ int enabled; int has_cpu; @@ -161,6 +169,41 @@ static inline int update_task_data(struct task_struct *task) return 0; } +#ifndef __has_builtin +# define __has_builtin(x) 0 +#endif + +static inline struct task_struct *get_lock_owner(__u64 lock, __u32 flags) +{ + struct task_struct *task; + __u64 owner = 0; + + if (flags & LCB_F_MUTEX) { + struct mutex *mutex = (void *)lock; + owner = BPF_CORE_READ(mutex, owner.counter); + } else if (flags == LCB_F_READ || flags == LCB_F_WRITE) { +#if __has_builtin(bpf_core_type_matches) + if (bpf_core_type_matches(struct rw_semaphore___old)) { + struct rw_semaphore___old *rwsem = (void *)lock; + owner = (unsigned long)BPF_CORE_READ(rwsem, owner); + } else if (bpf_core_type_matches(struct rw_semaphore___new)) { + struct rw_semaphore___new *rwsem = (void *)lock; + owner = BPF_CORE_READ(rwsem, owner.counter); + } +#else + /* assume new struct */ + struct rw_semaphore *rwsem = (void *)lock; + owner = BPF_CORE_READ(rwsem, owner.counter); +#endif + } + + if (!owner) + return NULL; + + task = (void *)(owner & ~7UL); + return task; +} + SEC("tp_btf/contention_begin") int contention_begin(u64 *ctx) { @@ -199,19 +242,7 @@ int contention_begin(u64 *ctx) struct task_struct *task; if (lock_owner) { - if (pelem->flags & LCB_F_MUTEX) { - struct mutex *lock = (void *)pelem->lock; - unsigned long owner = BPF_CORE_READ(lock, owner.counter); - - task = (void *)(owner & ~7UL); - } else if (pelem->flags == LCB_F_READ || pelem->flags == LCB_F_WRITE) { - struct rw_semaphore *lock = (void *)pelem->lock; - unsigned long owner = BPF_CORE_READ(lock, owner.counter); - - task = (void *)(owner & ~7UL); - } else { - task = NULL; - } + task = get_lock_owner(pelem->lock, pelem->flags); /* The flags is not used anymore. Pass the owner pid. */ if (task)