From nobody Thu Dec 18 19:04:24 2025 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (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 5E96FC2C9 for ; Thu, 27 Feb 2025 00:35:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740616549; cv=none; b=KuXSe64QTrz32zAr3wmXI4NjkJ0caLeKmXfj6Nxi6AAgtUD2E/RtIQFEk6JhE01ewLgMz9TIwwOX68p/9d73+orfBDonK1Qnf8RaNp7Y6AxfkvIV1QCdr4JEnBGgGhq1aPzGWAJQtGVBz7T0x7rYEp5q4NKB/LizDqm5goRVsy8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740616549; c=relaxed/simple; bh=sX6k+ExVgGNyDnumdmJIdayUEA+By8ga08Ti0Wfuoc8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=YOeAbt6WBirTGOx1CQOuvX5bHKStMpmcLPiyDVlid5L1uT/DvH5eeia8Fw8OVpW1X9IQaBvAJLqVCJgnP/RfEdJt5tsk1PF857xvr0Y4Vauuz7ZvmW+8UYnRucpTVd9Tjj3nZRqxzVDGPWz0Cfl6FJ2B/AiNH0cALch+YmnfWqU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ctshao.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=CV93pHNl; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ctshao.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="CV93pHNl" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-22331df540aso9876685ad.1 for ; Wed, 26 Feb 2025 16:35:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1740616547; x=1741221347; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=uT5qN4teqIhN8OgGcN83osIEOThqIeAxronJTF18w94=; b=CV93pHNlEJa0nnH2FFCd9LoeYfdJBYrBlB3/XXlf3ud1rtRaS1obrYT60g9HIZxA/p j2cghqq8xyGh8p6dt+b7fHZe/l57Ilga7psO1P+uHr4o0Fmaxv8mueppNUERFaVdMO/T DTCzNmYyQHxwyxQgrmW1CbgMCcuM+hQ/PtevE2f3sVlBxdhMjWPLgCbziapv0HI9cm1M 2vOA3zGsxCB3CTAryo7DEJ8alLbogjXmrOwHP/nQldk1gAZsZ4ALmLmUmmyXDTTAf5x6 5+vmydmrWyXzLTCpVe8la6b4TVzV8PWjeZW+pOyGGt607ys3xdjahxHLXsi9ZPaPEDUw 8O2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740616547; x=1741221347; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=uT5qN4teqIhN8OgGcN83osIEOThqIeAxronJTF18w94=; b=McHrXO9sTNCSKiPFqqFsdm7Z77Xu9ZBFul4DvMrDZho71Qd0Il/A6ABdl7prWPDvKI Lg7SB2ErChizp4OAiPCKcbhbU0CEsJI3PcDL8hqRYJOJJTzjeafGRTTCcYjCci2jWvVp 3JgX1xaHDvDCja5v8kI/H9s81cIHI+Gf/mwMCcb2gjyoShK009EOkV2WHS9M3dIs0amv G2zFbRVCpc1bDKmjmVFPse4BlLJ4RdTK/gy8y/D8O1M3+2DcI4GnG3GUO7GC34MDy5bA eJvbpPe0L11urAAaZ7+dpAZdV0oZmH57qCASnDXn1iBREFlVdGSu+OCQOFZNA5ZK9MQl mjbQ== X-Gm-Message-State: AOJu0Yxwy4mrN+wejk+nCOXI4uy9fNXL7mi8LCy9a3vaQ1rf3yPSRl0F GbWgEAUPDFWBpa4qrcDLrtEwTWNE6gBQU0EDExBqce8/w1hdkDUrSrJxw2WUInfLa00Whp5hgaO KjPJrMbhgc3SdzFRom7i5lF86q3n7qKIqU49AilPGZVuffQLP4hFOliMT8wTqyaW/K7u0iz0F/D Vxn3XtbdON84eQRJ+McApU07uDrWpkjZed2v60H4DY X-Google-Smtp-Source: AGHT+IEhVou1kAichbSUs8mzNp9wPwBY0b2D1W3ERA7S8+Dol9j5TmEUBzuy/uk5ihEM3zbiUALvmCyQzkQ= X-Received: from pfuw10.prod.google.com ([2002:a05:6a00:14ca:b0:732:9235:5f2]) (user=ctshao job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:1709:b0:732:2967:400 with SMTP id d2e1a72fcca58-7347910977emr14896254b3a.12.1740616546640; Wed, 26 Feb 2025 16:35:46 -0800 (PST) Date: Wed, 26 Feb 2025 16:28:54 -0800 In-Reply-To: <20250227003359.732948-1-ctshao@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250227003359.732948-1-ctshao@google.com> X-Mailer: git-send-email 2.48.1.658.g4767266eb4-goog Message-ID: <20250227003359.732948-3-ctshao@google.com> Subject: [PATCH v8 2/4] perf lock: Retrieve owner callstack in bpf program From: Chun-Tse Shao To: linux-kernel@vger.kernel.org Cc: Chun-Tse Shao , peterz@infradead.org, mingo@redhat.com, acme@kernel.org, namhyung@kernel.org, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, irogers@google.com, adrian.hunter@intel.com, kan.liang@linux.intel.com, nick.forrington@arm.com, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This implements per-callstack aggregation of lock owners in addition to per-thread. The owner callstack is captured using `bpf_get_task_stack()` at `contention_begin()` and it also adds a custom stackid function for the owner stacks to be compared easily. The owner info is kept in a hash map using lock addr as a key to handle multiple waiters for the same lock. At `contention_end()`, it updates the owner lock stat based on the info that was saved at `contention_begin()`. If there are more waiters, it'd update the owner pid to itself as `contention_end()` means it gets the lock now. But it also needs to check the return value of the lock function in case task was killed by a signal or something. Signed-off-by: Chun-Tse Shao --- .../perf/util/bpf_skel/lock_contention.bpf.c | 212 +++++++++++++++++- 1 file changed, 203 insertions(+), 9 deletions(-) diff --git a/tools/perf/util/bpf_skel/lock_contention.bpf.c b/tools/perf/ut= il/bpf_skel/lock_contention.bpf.c index 23fe9cc980ae..69be7a4234e0 100644 --- a/tools/perf/util/bpf_skel/lock_contention.bpf.c +++ b/tools/perf/util/bpf_skel/lock_contention.bpf.c @@ -197,6 +197,9 @@ int data_fail; int task_map_full; int data_map_full; =20 +struct task_struct *bpf_task_from_pid(s32 pid) __ksym __weak; +void bpf_task_release(struct task_struct *p) __ksym __weak; + static inline __u64 get_current_cgroup_id(void) { struct task_struct *task; @@ -420,6 +423,61 @@ static inline struct tstamp_data *get_tstamp_elem(__u3= 2 flags) return pelem; } =20 +static inline s32 get_owner_stack_id(u64 *stacktrace) +{ + s32 *id, new_id; + static s64 id_gen =3D 1; + + id =3D bpf_map_lookup_elem(&owner_stacks, stacktrace); + if (id) + return *id; + + new_id =3D (s32)__sync_fetch_and_add(&id_gen, 1); + + bpf_map_update_elem(&owner_stacks, stacktrace, &new_id, BPF_NOEXIST); + + id =3D bpf_map_lookup_elem(&owner_stacks, stacktrace); + if (id) + return *id; + + return -1; +} + +static inline void update_contention_data(struct contention_data *data, u6= 4 duration, u32 count) +{ + __sync_fetch_and_add(&data->total_time, duration); + __sync_fetch_and_add(&data->count, count); + + /* FIXME: need atomic operations */ + if (data->max_time < duration) + data->max_time =3D duration; + if (data->min_time > duration) + data->min_time =3D duration; +} + +static inline void update_owner_stat(u32 id, u64 duration, u32 flags) +{ + struct contention_key key =3D { + .stack_id =3D id, + .pid =3D 0, + .lock_addr_or_cgroup =3D 0, + }; + struct contention_data *data =3D bpf_map_lookup_elem(&owner_stat, &key); + + if (!data) { + struct contention_data first =3D { + .total_time =3D duration, + .max_time =3D duration, + .min_time =3D duration, + .count =3D 1, + .flags =3D flags, + }; + bpf_map_update_elem(&owner_stat, &key, &first, BPF_NOEXIST); + } else { + update_contention_data(data, duration, 1); + } +} + SEC("tp_btf/contention_begin") int contention_begin(u64 *ctx) { @@ -437,6 +495,72 @@ int contention_begin(u64 *ctx) pelem->flags =3D (__u32)ctx[1]; =20 if (needs_callstack) { + u32 i =3D 0; + u32 id =3D 0; + int owner_pid; + u64 *buf; + struct task_struct *task; + struct owner_tracing_data *otdata; + + if (!lock_owner) + goto skip_owner; + + task =3D get_lock_owner(pelem->lock, pelem->flags); + if (!task) + goto skip_owner; + + owner_pid =3D BPF_CORE_READ(task, pid); + + buf =3D bpf_map_lookup_elem(&stack_buf, &i); + if (!buf) + goto skip_owner; + for (i =3D 0; i < max_stack; i++) + buf[i] =3D 0x0; + + if (!bpf_task_from_pid) + goto skip_owner; + + task =3D bpf_task_from_pid(owner_pid); + if (!task) + goto skip_owner; + + bpf_get_task_stack(task, buf, max_stack * sizeof(unsigned long), 0); + bpf_task_release(task); + + otdata =3D bpf_map_lookup_elem(&owner_data, &pelem->lock); + id =3D get_owner_stack_id(buf); + + /* + * Contention just happens, or corner case `lock` is owned by process not + * `owner_pid`. For the corner case we treat it as unexpected internal e= rror and + * just ignore the precvious tracing record. + */ + if (!otdata || otdata->pid !=3D owner_pid) { + struct owner_tracing_data first =3D { + .pid =3D owner_pid, + .timestamp =3D pelem->timestamp, + .count =3D 1, + .stack_id =3D id, + }; + bpf_map_update_elem(&owner_data, &pelem->lock, &first, BPF_ANY); + } + /* Contention is ongoing and new waiter joins */ + else { + __sync_fetch_and_add(&otdata->count, 1); + + /* + * The owner is the same, but stacktrace might be changed. In this case= we + * store/update `owner_stat` based on current owner stack id. + */ + if (id !=3D otdata->stack_id) { + update_owner_stat(id, pelem->timestamp - otdata->timestamp, + pelem->flags); + + otdata->timestamp =3D pelem->timestamp; + otdata->stack_id =3D id; + } + } +skip_owner: pelem->stack_id =3D bpf_get_stackid(ctx, &stacks, BPF_F_FAST_STACK_CMP | stack_skip); if (pelem->stack_id < 0) @@ -473,6 +597,7 @@ int contention_end(u64 *ctx) struct tstamp_data *pelem; struct contention_key key =3D {}; struct contention_data *data; + __u64 timestamp; __u64 duration; bool need_delete =3D false; =20 @@ -500,12 +625,88 @@ int contention_end(u64 *ctx) need_delete =3D true; } =20 - duration =3D bpf_ktime_get_ns() - pelem->timestamp; + timestamp =3D bpf_ktime_get_ns(); + duration =3D timestamp - pelem->timestamp; if ((__s64)duration < 0) { __sync_fetch_and_add(&time_fail, 1); goto out; } =20 + if (needs_callstack && lock_owner) { + struct owner_tracing_data *otdata =3D bpf_map_lookup_elem(&owner_data, &= pelem->lock); + + if (!otdata) + goto skip_owner; + + /* Update `owner_stat` */ + update_owner_stat(otdata->stack_id, timestamp - otdata->timestamp, pelem= ->flags); + + /* No contention is occurring, delete `lock` entry in `owner_data` */ + if (otdata->count <=3D 1) + bpf_map_delete_elem(&owner_data, &pelem->lock); + /* + * Contention is still ongoing, with a new owner (current task). `owner_= data` + * should be updated accordingly. + */ + else { + u32 i =3D 0; + s32 ret =3D (s32)ctx[1]; + u64 *buf; + + otdata->timestamp =3D timestamp; + __sync_fetch_and_add(&otdata->count, -1); + + buf =3D bpf_map_lookup_elem(&stack_buf, &i); + if (!buf) + goto skip_owner; + for (i =3D 0; i < (u32)max_stack; i++) + buf[i] =3D 0x0; + + /* + * `ret` has the return code of the lock function. + * If `ret` is negative, the current task terminates lock waiting witho= ut + * acquiring it. Owner is not changed, but we still need to update the = owner + * stack. + */ + if (ret < 0) { + s32 id =3D 0; + struct task_struct *task; + + if (!bpf_task_from_pid) + goto skip_owner; + + task =3D bpf_task_from_pid(otdata->pid); + if (!task) + goto skip_owner; + + bpf_get_task_stack(task, buf, + max_stack * sizeof(unsigned long), 0); + bpf_task_release(task); + + id =3D get_owner_stack_id(buf); + + /* + * If owner stack is changed, update owner stack id for this lock. + */ + if (id !=3D otdata->stack_id) + otdata->stack_id =3D id; + } + /* + * Otherwise, update tracing data with the current task, which is the n= ew + * owner. + */ + else { + otdata->pid =3D pid; + /* + * We don't want to retrieve callstack here, since it is where the + * current task acquires the lock and provides no additional + * information. We simply assign -1 to invalidate it. + */ + otdata->stack_id =3D -1; + } + } + } +skip_owner: switch (aggr_mode) { case LOCK_AGGR_CALLER: key.stack_id =3D pelem->stack_id; @@ -589,14 +790,7 @@ int contention_end(u64 *ctx) } =20 found: - __sync_fetch_and_add(&data->total_time, duration); - __sync_fetch_and_add(&data->count, 1); - - /* FIXME: need atomic operations */ - if (data->max_time < duration) - data->max_time =3D duration; - if (data->min_time > duration) - data->min_time =3D duration; + update_contention_data(data, duration, 1); =20 out: pelem->lock =3D 0; --=20 2.48.1.658.g4767266eb4-goog