From patchwork Fri Jan 20 08:48:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "zhaoyang.huang" X-Patchwork-Id: 46211 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp88154wrn; Fri, 20 Jan 2023 00:56:25 -0800 (PST) X-Google-Smtp-Source: AMrXdXvdC+rqFhnMLSwrdpmndFd9DMtMVUV+N/qifnDgQu51bTpUtVMUvNKDg3VvPdyU82qsYiUi X-Received: by 2002:a17:90b:4a0c:b0:22a:3c8:9d1 with SMTP id kk12-20020a17090b4a0c00b0022a03c809d1mr2185799pjb.32.1674204984727; Fri, 20 Jan 2023 00:56:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674204984; cv=none; d=google.com; s=arc-20160816; b=d6mELY4j/iIOwEckFAYsiXXKhdPxJckiQMxNQv8OxPtvdn151B/Q/LeGf8RlJfppbK NsAlXjKuKyPC5mdJ4babBEuWA8s8vTN80LND6jMhGCkmN+j2eOksBleyk77p5iGGNG3T PHihocigfwYWWmLkZt9ihMVNu26aIUh+MRibUNlCuMYka3hI7uEP4xsqx0KYM+htS+o9 55dxNbzObOPrZ3550BS88jF7GUM0Kw9gkY3BXIwYXfjgrMgCP6jDUQo1FkhiN8T08lj1 v18favEeSyX2crowlEKfYgU7LqxnJnX395zs69mJqo8OtKRXZ0Jdi8hN/0cfF9xylYoz c7/Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:date:subject:to:from; bh=XAWL3NR/qtD4UodXPxTfVew78iFup8dANwaaAJKn0Ec=; b=BAiZ+sU25puoG6DClYYvG1WLj5fgoTGESqwwtDyoKbfNvHFEDz6obdZrTWxdfMaNZS WBBJl7vQI+6ZRKK4wJj5x35hB4QuoNWGn0eN5fdxOab1ajw1tUjCDk2835X1BiBudX32 cmqM0CImcUq2qSoOchxQomxoIttYQe0iCo43LiQDg7tXEg+jFWYe6ZFHc3smolbqf0mO bhk77OoevI5vUgKRNtrhgs2GMCeAS2jcWYbEYwdx6aK+Z4coDQrf9T7CUHfUDbdTvfVY ffjfArXB7G5MvMcE/lVgRSHnEbQtZc8G6wwViPpD6856SPI3LUIgO1/LKZj2A74/qZca xfwA== ARC-Authentication-Results: i=1; mx.google.com; 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i20-20020a63e914000000b00470422012f6si42520251pgh.687.2023.01.20.00.56.12; Fri, 20 Jan 2023 00:56:24 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229637AbjATIth (ORCPT + 99 others); Fri, 20 Jan 2023 03:49:37 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60476 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229448AbjATItf (ORCPT ); Fri, 20 Jan 2023 03:49:35 -0500 Received: from SHSQR01.spreadtrum.com (mx1.unisoc.com [222.66.158.135]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 906C979E81 for ; Fri, 20 Jan 2023 00:49:33 -0800 (PST) Received: from SHSend.spreadtrum.com (bjmbx01.spreadtrum.com [10.0.64.7]) by SHSQR01.spreadtrum.com with ESMTP id 30K8mb9x020394; Fri, 20 Jan 2023 16:48:37 +0800 (+08) (envelope-from zhaoyang.huang@unisoc.com) Received: from bj03382pcu.spreadtrum.com (10.0.74.65) by BJMBX01.spreadtrum.com (10.0.64.7) with Microsoft SMTP Server (TLS) id 15.0.1497.23; Fri, 20 Jan 2023 16:48:35 +0800 From: "zhaoyang.huang" To: Peter Zijlstra , Ingo Molnar , Will Deacon , Waiman Long , Boqun Feng , , Zhaoyang Huang , Subject: [RFC PATCH] kernel/locking: introduce stack_handle for tracing the callstack Date: Fri, 20 Jan 2023 16:48:22 +0800 Message-ID: <1674204502-32123-1-git-send-email-zhaoyang.huang@unisoc.com> X-Mailer: git-send-email 1.9.1 MIME-Version: 1.0 X-Originating-IP: [10.0.74.65] X-ClientProxiedBy: SHCAS03.spreadtrum.com (10.0.1.207) To BJMBX01.spreadtrum.com (10.0.64.7) X-MAIL: SHSQR01.spreadtrum.com 30K8mb9x020394 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS autolearn=ham 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?1755531165825988001?= X-GMAIL-MSGID: =?utf-8?q?1755531165825988001?= From: Zhaoyang Huang When deadlock happens, sometimes it is hard to know how the owner get the lock especially the owner is running when snapshot(ramdump). Introduce stack_handle to record the trace. Signed-off-by: Zhaoyang Huang --- include/linux/rwsem.h | 9 ++++++++- kernel/locking/rwsem.c | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/include/linux/rwsem.h b/include/linux/rwsem.h index efa5c32..ad4c591 100644 --- a/include/linux/rwsem.h +++ b/include/linux/rwsem.h @@ -16,6 +16,11 @@ #include #include +#define CONFIG_TRACE_RWSEMS +#ifdef CONFIG_TRACE_RWSEMS +typedef u32 depot_stack_handle_t; +#endif + #ifdef CONFIG_DEBUG_LOCK_ALLOC # define __RWSEM_DEP_MAP_INIT(lockname) \ .dep_map = { \ @@ -31,7 +36,6 @@ #ifdef CONFIG_RWSEM_SPIN_ON_OWNER #include #endif - /* * For an uncontended rwsem, count and owner are the only fields a task * needs to touch when acquiring the rwsem. So they are put next to each @@ -60,6 +64,9 @@ struct rw_semaphore { #ifdef CONFIG_DEBUG_RWSEMS void *magic; #endif +#ifdef CONFIG_TRACE_RWSEMS + depot_stack_handle_t trace_handle; +#endif #ifdef CONFIG_DEBUG_LOCK_ALLOC struct lockdep_map dep_map; #endif diff --git a/kernel/locking/rwsem.c b/kernel/locking/rwsem.c index 4487359..a12766e 100644 --- a/kernel/locking/rwsem.c +++ b/kernel/locking/rwsem.c @@ -28,6 +28,7 @@ #include #include #include +#include #ifndef CONFIG_PREEMPT_RT #include "lock_events.h" @@ -74,6 +75,7 @@ list_empty(&(sem)->wait_list) ? "" : "not ")) \ debug_locks_off(); \ } while (0) +#define MAX_TRACE 16 #else # define DEBUG_RWSEMS_WARN_ON(c, sem) #endif @@ -174,6 +176,9 @@ static inline void __rwsem_set_reader_owned(struct rw_semaphore *sem, (atomic_long_read(&sem->owner) & RWSEM_NONSPINNABLE); atomic_long_set(&sem->owner, val); +#ifdef CONFIG_TRACE_RWSEMS + sem->trace_handle = owner ? set_track_prepare() : NULL; +#endif } static inline void rwsem_set_reader_owned(struct rw_semaphore *sem) @@ -397,6 +402,19 @@ enum rwsem_wake_type { return false; } +#ifdef CONFIG_TRACE_RWSEMS +static inline depot_stack_handle_t set_track_prepare(void) +{ + depot_stack_handle_t trace_handle; + unsigned long entries[MAX_TRACE]; + unsigned int nr_entries; + + nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 3); + trace_handle = stack_depot_save(entries, nr_entries, GFP_NOWAIT); + + return trace_handle; +} +#endif /* * handle the lock release when processes blocked on it that can now run * - if we come here from up_xxxx(), then the RWSEM_FLAG_WAITERS bit must