From nobody Wed Dec 17 08:54:38 2025 Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) (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 B93FC1EF0BB for ; Thu, 20 Mar 2025 06:49:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742453391; cv=none; b=nEHl+NlQVh/qNx9WQm40kX9EQWku1jiQvyWikpOqxHM0SpWqJs21g0BHvxvgGCcSjczXobZnWEi+YWZYN51yEg1veHX39WpqbzCJ0ovDwqE7pvbxcOExAOY9xIqwvHDEBpWj1gVFgBh8mDMkv7Pp6dq1aSFkH2+J9GcDsbQtM0U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742453391; c=relaxed/simple; bh=Q3B4ztC5UfKKbD8pZjDo1mFpMDP6UCdo0zeWuFAosUg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=VbVKYb74w8zQAnt/8NnDQNqRBzjFgAOXfQ1RTBnAuTSUdvwWlsl7v8s4Sn2HGcw8g8Wu0Em/oXhY644/w4oV2Sv8cyfcdh8fuXft9mQTn0NdUlThMglTMuCXJJrh26BzGxQwQ7TKja+AFx49eitsIs7rpLOheBQCLZqgoUkXPK4= 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=co3y5Ypd; arc=none smtp.client-ip=209.85.214.170 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="co3y5Ypd" Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-223fd89d036so5705625ad.1 for ; Wed, 19 Mar 2025 23:49:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1742453389; x=1743058189; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=03Xv1keiWzxrHieU5ZbpkW3iG/vVYUxPpzBjO5i3q8A=; b=co3y5YpdRcBOoa5VOHGLovtKSRfjsCmI+MhCD2JtCfnuG8PPvYOydagCTcUd64rCUy cGTuoS98GOyYgD4p3pv5NWQFfkNyykzl2gVosb8OdwPrEs6KwgvXVRIsKoxd7LO6GMoP YWXJXNFm7RkpKcw/ZSBsphY0WQoaCARSBDermcPym2Nne4bN764f4TcN1Og4Y9SG+Edz kuboL5aFbsypB1tzQCN/HUTs3+WHTLPcHfE7bmu5uc9onaYgwnKARIz22s5ZSzS55SjC 6xtEZkeYS1vH49Y21aRFs7tBFbVfuXN2ebPazMQ6RVjYrFRtuW/OY676o1BxGMS8DD9g yDTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742453389; x=1743058189; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=03Xv1keiWzxrHieU5ZbpkW3iG/vVYUxPpzBjO5i3q8A=; b=Z5zRHRd6+X/mwYO0vVIoSB/UMavJRLAhdX1gPkKBQOE17A2uS7UstGxGB49NuD5xHH zRhL1wqnC3IKsGrEZn2S+3x+4FI7rLk0qk9x94U6X7H89D9PgmBMgmDB8KxoW9YR+2D3 nZsIOLwhMHaUZu3grHNj8KS/GoDo1gFKBr3EhXD06pBVujsa03oggSNlqNZXYtTUHtpL xkPYjibE28yLbS1nEG/kfsrQcCMO69ANKSgzDNPr/MZzoL5GDHi8cW6dpfv1HW8CwYkw vtoDpmlhylRszbOIzxXqeKBqE74lmXXW30ThGAHNwrge+7H4hfpwj976IPAhFk9DmJiv bCHQ== X-Forwarded-Encrypted: i=1; AJvYcCWxXwzQZZsb6KYDKRAW7VfBVw/bS4/inTriB8kVNqdIy3z2yLBOXV+b0+wMDZl3A+IdW4MdhLrVvk2rzpE=@vger.kernel.org X-Gm-Message-State: AOJu0YyQzXcBv0baucDEoVlmz0FjEhZCMX4RbSptR72wrpjVmnklvzZ7 LFK+9ywxwWrnkKDZu6kUhjBU9UDRj3tsuJgPBR9HHsyNBhOPEztJ X-Gm-Gg: ASbGncvu4d24TvM67zF5bIdRKFe9EZ7g4X0S9cfh6qYBSqpGDg/fns1lzLbZectgRVD 2o29kpK+mXbwzRBZAXzQWbWbAgce68MO5inG8AF2ksdIpt1R72RIvtFROHKpdN4eA9MCZrxCEDm kjjEOyX5j49q1te9qvjJ+Cb3kQFkNPbwU+qTSldj2Ttepz4lCJb8RWgF/z2wlX1x+zzwt2RApqv XxMbzYH4jZ7rp2wGAODeyHbis7pya/LLZDNHitTdQF44+Y8U+dntNb6VwQDPDfp2Xqd9u5iq68K b4meL8eds2ls7rx72Tct89Zm02FezlM5YHXIjLgPV/AqQruTnNARLFEcSUWDzFM= X-Google-Smtp-Source: AGHT+IF3RJQvrZXSax/z/cCKngBADtM/wtxhJQDhvbqiQpvsNxBAPAKbhaEyCy5y29hu3OBaFfxmKw== X-Received: by 2002:a17:902:db01:b0:224:1c95:451e with SMTP id d9443c01a7336-22649c8e7c3mr80690285ad.33.1742453388916; Wed, 19 Mar 2025 23:49:48 -0700 (PDT) Received: from EBJ9932692.tcent.cn ([124.156.216.125]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-225c68885a1sm127260155ad.13.2025.03.19.23.49.42 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 19 Mar 2025 23:49:48 -0700 (PDT) From: Lance Yang To: akpm@linux-foundation.org Cc: will@kernel.org, peterz@infradead.org, mingo@redhat.com, longman@redhat.com, mhiramat@kernel.org, anna.schumaker@oracle.com, boqun.feng@gmail.com, joel.granados@kernel.org, kent.overstreet@linux.dev, leonylgao@tencent.com, linux-kernel@vger.kernel.org, rostedt@goodmis.org, senozhatsky@chromium.org, tfiga@chromium.org, amaindex@outlook.com, jstultz@google.com, Lance Yang , Mingzhe Yang Subject: [PATCH v4 1/3] hung_task: replace blocker_mutex with encoded blocker Date: Thu, 20 Mar 2025 14:49:21 +0800 Message-ID: <20250320064923.24000-2-ioworker0@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20250320064923.24000-1-ioworker0@gmail.com> References: <20250320064923.24000-1-ioworker0@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This patch replaces 'struct mutex *blocker_mutex' with 'unsigned long blocker', as only one blocker is active at a time. The blocker filed can store both the lock addrees and the lock type, with LSB used to encode the type as Masami suggested, making it easier to extend the feature to cover other types of locks. Also, once the lock type is determined, we can directly extract the address and cast it to a lock pointer ;) Suggested-by: Masami Hiramatsu (Google) Signed-off-by: Mingzhe Yang Signed-off-by: Lance Yang Reviewed-by: Masami Hiramatsu (Google) --- include/linux/hung_task.h | 99 +++++++++++++++++++++++++++++++++++++++ include/linux/sched.h | 2 +- kernel/hung_task.c | 13 +++-- kernel/locking/mutex.c | 5 +- 4 files changed, 111 insertions(+), 8 deletions(-) create mode 100644 include/linux/hung_task.h diff --git a/include/linux/hung_task.h b/include/linux/hung_task.h new file mode 100644 index 000000000000..a5414d7b402d --- /dev/null +++ b/include/linux/hung_task.h @@ -0,0 +1,99 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Detect Hung Task: detecting tasks stuck in D state + * + * Copyright (C) 2025 Tongcheng Travel (www.ly.com) + * Author: Lance Yang + */ +#ifndef __LINUX_HUNG_TASK_H +#define __LINUX_HUNG_TASK_H + +#include +#include +#include + +/* + * @blocker: Combines lock address and blocking type. + * + * Since lock pointers are at least 4-byte aligned(32-bit) or 8-byte + * aligned(64-bit). This leaves the 2 least bits (LSBs) of the pointer + * always zero. So we can use these bits to encode the specific blocking + * type. + * + * Type encoding: + * 00 - Blocked on mutex (BLOCKER_TYPE_MUTEX) + * 01 - Blocked on semaphore (BLOCKER_TYPE_SEM) + * 10 - Blocked on rt-mutex (BLOCKER_TYPE_RTMUTEX) + * 11 - Blocked on rw-semaphore (BLOCKER_TYPE_RWSEM) + */ +#define BLOCKER_TYPE_MUTEX 0x00UL +#define BLOCKER_TYPE_SEM 0x01UL +#define BLOCKER_TYPE_RTMUTEX 0x02UL +#define BLOCKER_TYPE_RWSEM 0x03UL + +#define BLOCKER_TYPE_MASK 0x03UL + +#ifdef CONFIG_DETECT_HUNG_TASK_BLOCKER +static inline void hung_task_set_blocker(void *lock, unsigned long type) +{ + unsigned long lock_ptr =3D (unsigned long)lock; + + WARN_ON_ONCE(!lock_ptr); + WARN_ON_ONCE(READ_ONCE(current->blocker)); + + /* + * If the lock pointer matches the BLOCKER_TYPE_MASK, return + * without writing anything. + */ + if (WARN_ON_ONCE(lock_ptr & BLOCKER_TYPE_MASK)) + return; + + WRITE_ONCE(current->blocker, lock_ptr | type); +} + +static inline void hung_task_clear_blocker(void) +{ + WARN_ON_ONCE(!READ_ONCE(current->blocker)); + + WRITE_ONCE(current->blocker, 0UL); +} + +/* + * hung_task_get_blocker_type - Extracts blocker type from encoded blocker + * address. + * + * @blocker: Blocker pointer with encoded type (via LSB bits) + * + * Returns: BLOCKER_TYPE_MUTEX, BLOCKER_TYPE_SEM, etc. + */ +static inline unsigned long hung_task_get_blocker_type(unsigned long block= er) +{ + WARN_ON_ONCE(!blocker); + + return blocker & BLOCKER_TYPE_MASK; +} + +static inline void *hung_task_blocker_to_lock(unsigned long blocker) +{ + WARN_ON_ONCE(!blocker); + + return (void *)(blocker & ~BLOCKER_TYPE_MASK); +} +#else +static inline void hung_task_set_blocker(void *lock, unsigned long type) +{ +} +static inline void hung_task_clear_blocker(void) +{ +} +static inline unsigned long hung_task_get_blocker_type(unsigned long block= er) +{ + return 0UL; +} +static inline void *hung_task_blocker_to_lock(unsigned long blocker) +{ + return NULL; +} +#endif + +#endif /* __LINUX_HUNG_TASK_H */ diff --git a/include/linux/sched.h b/include/linux/sched.h index 1419d94c8e87..f27060dac499 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1218,7 +1218,7 @@ struct task_struct { #endif =20 #ifdef CONFIG_DETECT_HUNG_TASK_BLOCKER - struct mutex *blocker_mutex; + unsigned long blocker; #endif =20 #ifdef CONFIG_DEBUG_ATOMIC_SLEEP diff --git a/kernel/hung_task.c b/kernel/hung_task.c index dc898ec93463..79558d76ef06 100644 --- a/kernel/hung_task.c +++ b/kernel/hung_task.c @@ -22,6 +22,7 @@ #include #include #include +#include =20 #include =20 @@ -98,16 +99,18 @@ static struct notifier_block panic_block =3D { static void debug_show_blocker(struct task_struct *task) { struct task_struct *g, *t; - unsigned long owner; - struct mutex *lock; + unsigned long owner, blocker; =20 RCU_LOCKDEP_WARN(!rcu_read_lock_held(), "No rcu lock held"); =20 - lock =3D READ_ONCE(task->blocker_mutex); - if (!lock) + blocker =3D READ_ONCE(task->blocker); + if (!blocker || + hung_task_get_blocker_type(blocker) !=3D BLOCKER_TYPE_MUTEX) return; =20 - owner =3D mutex_get_owner(lock); + owner =3D mutex_get_owner( + (struct mutex *)hung_task_blocker_to_lock(blocker)); + if (unlikely(!owner)) { pr_err("INFO: task %s:%d is blocked on a mutex, but the owner is not fou= nd.\n", task->comm, task->pid); diff --git a/kernel/locking/mutex.c b/kernel/locking/mutex.c index 6a543c204a14..e9ef70a6cb5f 100644 --- a/kernel/locking/mutex.c +++ b/kernel/locking/mutex.c @@ -29,6 +29,7 @@ #include #include #include +#include =20 #define CREATE_TRACE_POINTS #include @@ -189,7 +190,7 @@ __mutex_add_waiter(struct mutex *lock, struct mutex_wai= ter *waiter, struct list_head *list) { #ifdef CONFIG_DETECT_HUNG_TASK_BLOCKER - WRITE_ONCE(current->blocker_mutex, lock); + hung_task_set_blocker(lock, BLOCKER_TYPE_MUTEX); #endif debug_mutex_add_waiter(lock, waiter, current); =20 @@ -207,7 +208,7 @@ __mutex_remove_waiter(struct mutex *lock, struct mutex_= waiter *waiter) =20 debug_mutex_remove_waiter(lock, waiter, current); #ifdef CONFIG_DETECT_HUNG_TASK_BLOCKER - WRITE_ONCE(current->blocker_mutex, NULL); + hung_task_clear_blocker(); #endif } =20 --=20 2.45.2 From nobody Wed Dec 17 08:54:38 2025 Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) (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 15DC81EBFFF for ; Thu, 20 Mar 2025 06:49:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742453397; cv=none; b=EcPUn15/beG6DFL1qen4XaRylwLcs5sL1EbqItM3ZsUTraAwBYZhIbuK0mVIQ9yWgWAvzQJImiKXl1bCHusydI0+eMC/QuXsavmhVjNRByYkAw6UmIzlOOxMtTL5q5nPCuNpauP7nO7/Wi/Dk1haJiIlQhIIkiB4RjQ9vdvZMcY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742453397; c=relaxed/simple; bh=a2mHZ2iJT2uh1sea/6KzfQvVbMAxtxd7XIdL94kpylM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=LxAcQnVOIBMoX1A0pAMqhgKJGkjCRHUvixixiUmo+YpD0jVKYwO+DGOwr2BpepqMXL39whB5G7A5oZ2vNfE+DUrItpw9aOM35gd08/vyI7uVV5jDO22ztuG1sk7+P4HLz4y9Sbrwy/iOu1YPP07457nCQkw4kZoyCPU5kjooL/c= 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=RagyR6xx; arc=none smtp.client-ip=209.85.214.174 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="RagyR6xx" Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-223f4c06e9fso6437445ad.1 for ; Wed, 19 Mar 2025 23:49:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1742453395; x=1743058195; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=zgTfmmcbrECq+SMoxCLy2EmU0/2NLJ8Y+ljJ0B1foO8=; b=RagyR6xxOrEs/WALACB5GHFpdTKPz+onQwhcy9G+mlvRiuvDcCbugfyAzgYZMuWWkx qBnbXrRvnV4OvmVaU5vpI3hRV3/XQIB/+55fVl+CJpsFvCLeQZ6jwsyW1W87NiWr3D2j xF32RLc4oQMn+b/45ByCRcbkN30JTSe/ksqV0JYfSohKldovB7p224HReMT//aiqJpI9 nIDVdzje80SwMOgZzBrOxDaUBCSsUaeU8ZFORlEQ1msy+tDrghHan4DuIvseZP0/BHsg NTiuqHECWGPQgnnTL2k74JkkNHaHFZDpOMQRkQ94L3v8Q/9XIxIM30XV9v8Su2NkdkP4 YlZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742453395; x=1743058195; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=zgTfmmcbrECq+SMoxCLy2EmU0/2NLJ8Y+ljJ0B1foO8=; b=QDFJWn3DB9nwmYc4kO2wVDighsCHO+FNS+jMv1D7O8dxywDvndVAK4xl/XhGxqwWfk dmSwVBGmU7v9TGADlgs7m5H9qHFKzsgWUmb3j/1SLPEOE9t1QSdgDip2R1l5BwUFvg7F aqMgdMlD5S4TOP62/mAracTMqQYCSVnjdKT2+sOu8x4mLbYtrdVdRZ7WEq+XFO9fiPha WpNAxI9DGkWKDFpxZ48wfLACQ+j/T0jHPlftmed5/PwCVFa7q0pUOvvLIU6rm9TQakzS yk9NUnsfAsNI9kskZ7YEWkmcPNR9+JvSBpkLNFE+swGwMYUgrDdBFi849qPbUeaj3qO5 3oaQ== X-Forwarded-Encrypted: i=1; AJvYcCXMcFL+URvWTBXlqP6PnO2aIXcatv5Q+WLcTo7LiWKi47NTrQL6vW8dN0KukgOTrRObwwGEUvGEPKHWcHs=@vger.kernel.org X-Gm-Message-State: AOJu0YzxsDOGSnCHN/1glrmzFR2KudwXw4hryMsL8XsRuwGvvpprSvOb pxQSYWzj6OXgM8QA9MEmIKxTT682uMkTwZcFOQ1FhqBUJh3EecxI X-Gm-Gg: ASbGncu13LH0D2t7OxTFkXEzHlyU/iFHqUf5zeZZMs+5rcoSOimPir4V35qr6i5p134 dD9RuA3YLVSbOBfZ33fTwV1wwnLMwRwmOPYErPXPsNBSiPoQ8ewQz250TqzhFf28VFl3sV/Mt3q 1pycb0ibQPLnJv8e92o4aY5TBuxMoCwBq7YwvxoAy9YDc70sibdpCHCCVXJo1BG2oglQDZnat0G v/cFLivKQyMbG566UBn/jIPSQ4qgdwX6890P+rZvSJcaKoENbuKGzG4UXHl7G+cDOzejad2sf5b +QnscRyw+m6aMcFoNJWmrudTCGNnbkAG/9P4glLpDLXk8ouppa7i X-Google-Smtp-Source: AGHT+IHYFxD7x3BTvt6GRVnlniV7Ypq6Pf2Sbop3/K613KEv9gv26enJb9RRS/7d6c2WXKfBLgRVfQ== X-Received: by 2002:a17:902:f603:b0:215:ba2b:cd55 with SMTP id d9443c01a7336-2265e6916dbmr41034695ad.2.1742453395214; Wed, 19 Mar 2025 23:49:55 -0700 (PDT) Received: from EBJ9932692.tcent.cn ([124.156.216.125]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-225c68885a1sm127260155ad.13.2025.03.19.23.49.49 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 19 Mar 2025 23:49:54 -0700 (PDT) From: Lance Yang To: akpm@linux-foundation.org Cc: will@kernel.org, peterz@infradead.org, mingo@redhat.com, longman@redhat.com, mhiramat@kernel.org, anna.schumaker@oracle.com, boqun.feng@gmail.com, joel.granados@kernel.org, kent.overstreet@linux.dev, leonylgao@tencent.com, linux-kernel@vger.kernel.org, rostedt@goodmis.org, senozhatsky@chromium.org, tfiga@chromium.org, amaindex@outlook.com, jstultz@google.com, Lance Yang , Mingzhe Yang Subject: [PATCH v4 2/3] hung_task: show the blocker task if the task is hung on semaphore Date: Thu, 20 Mar 2025 14:49:22 +0800 Message-ID: <20250320064923.24000-3-ioworker0@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20250320064923.24000-1-ioworker0@gmail.com> References: <20250320064923.24000-1-ioworker0@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Inspired by mutex blocker tracking[1], this patch makes a trade-off to balance the overhead and utility of the hung task detector. Unlike mutexes, semaphores lack explicit ownership tracking, making it challenging to identify the root cause of hangs. To address this, we introduce a last_holder field to the semaphore structure, which is updated when a task successfully calls down() and cleared during up(). The assumption is that if a task is blocked on a semaphore, the holders must not have released it. While this does not guarantee that the last holder is one of the current blockers, it likely provides a practical hint for diagnosing semaphore-related stalls. With this change, the hung task detector can now show blocker task's info like below: [Thu Mar 20 04:52:21 2025] INFO: task cat:955 blocked for more than 120 sec= onds. [Thu Mar 20 04:52:21 2025] Tainted: G E 6.14.0-rc6+ #1 [Thu Mar 20 04:52:21 2025] "echo 0 > /proc/sys/kernel/hung_task_timeout_sec= s" disables this message. [Thu Mar 20 04:52:21 2025] task:cat state:D stack:0 pid:955= tgid:955 ppid:917 task_flags:0x400000 flags:0x00000000 [Thu Mar 20 04:52:21 2025] Call Trace: [Thu Mar 20 04:52:21 2025] [Thu Mar 20 04:52:21 2025] __schedule+0x491/0xbd0 [Thu Mar 20 04:52:21 2025] schedule+0x27/0xf0 [Thu Mar 20 04:52:21 2025] schedule_timeout+0xe3/0xf0 [Thu Mar 20 04:52:21 2025] ? __folio_mod_stat+0x2a/0x80 [Thu Mar 20 04:52:21 2025] ? set_ptes.constprop.0+0x27/0x90 [Thu Mar 20 04:52:21 2025] __down_common+0x155/0x280 [Thu Mar 20 04:52:21 2025] down+0x53/0x70 [Thu Mar 20 04:52:21 2025] read_dummy_semaphore+0x23/0x60 [Thu Mar 20 04:52:21 2025] full_proxy_read+0x5f/0xa0 [Thu Mar 20 04:52:21 2025] vfs_read+0xbc/0x350 [Thu Mar 20 04:52:21 2025] ? __count_memcg_events+0xa5/0x140 [Thu Mar 20 04:52:21 2025] ? count_memcg_events.constprop.0+0x1a/0x30 [Thu Mar 20 04:52:21 2025] ? handle_mm_fault+0x180/0x260 [Thu Mar 20 04:52:21 2025] ksys_read+0x66/0xe0 [Thu Mar 20 04:52:21 2025] do_syscall_64+0x51/0x120 [Thu Mar 20 04:52:21 2025] entry_SYSCALL_64_after_hwframe+0x76/0x7e [Thu Mar 20 04:52:21 2025] RIP: 0033:0x7ff96d4ab46e [Thu Mar 20 04:52:21 2025] RSP: 002b:00007ffe2f47f3a8 EFLAGS: 00000246 ORIG= _RAX: 0000000000000000 [Thu Mar 20 04:52:21 2025] RAX: ffffffffffffffda RBX: 0000000000020000 RCX:= 00007ff96d4ab46e [Thu Mar 20 04:52:21 2025] RDX: 0000000000020000 RSI: 00007ff96d39f000 RDI:= 0000000000000003 [Thu Mar 20 04:52:21 2025] RBP: 00007ff96d39f000 R08: 00007ff96d39e010 R09:= 0000000000000000 [Thu Mar 20 04:52:21 2025] R10: fffffffffffffbc5 R11: 0000000000000246 R12:= 0000000000000000 [Thu Mar 20 04:52:21 2025] R13: 0000000000000003 R14: 0000000000020000 R15:= 0000000000020000 [Thu Mar 20 04:52:21 2025] [Thu Mar 20 04:52:21 2025] INFO: task cat:955 blocked on a semaphore likely= last held by task cat:909 [Thu Mar 20 04:52:21 2025] task:cat state:S stack:0 pid:909= tgid:909 ppid:771 task_flags:0x400000 flags:0x00000000 [Thu Mar 20 04:52:21 2025] Call Trace: [Thu Mar 20 04:52:21 2025] [Thu Mar 20 04:52:21 2025] __schedule+0x491/0xbd0 [Thu Mar 20 04:52:21 2025] ? _raw_spin_unlock_irqrestore+0xe/0x40 [Thu Mar 20 04:52:21 2025] schedule+0x27/0xf0 [Thu Mar 20 04:52:21 2025] schedule_timeout+0x77/0xf0 [Thu Mar 20 04:52:21 2025] ? __pfx_process_timeout+0x10/0x10 [Thu Mar 20 04:52:21 2025] msleep_interruptible+0x49/0x60 [Thu Mar 20 04:52:21 2025] read_dummy_semaphore+0x2d/0x60 [Thu Mar 20 04:52:21 2025] full_proxy_read+0x5f/0xa0 [Thu Mar 20 04:52:21 2025] vfs_read+0xbc/0x350 [Thu Mar 20 04:52:21 2025] ? __count_memcg_events+0xa5/0x140 [Thu Mar 20 04:52:21 2025] ? count_memcg_events.constprop.0+0x1a/0x30 [Thu Mar 20 04:52:21 2025] ? handle_mm_fault+0x180/0x260 [Thu Mar 20 04:52:21 2025] ksys_read+0x66/0xe0 [Thu Mar 20 04:52:21 2025] do_syscall_64+0x51/0x120 [Thu Mar 20 04:52:21 2025] entry_SYSCALL_64_after_hwframe+0x76/0x7e [Thu Mar 20 04:52:21 2025] RIP: 0033:0x7fe6bf7a046e [Thu Mar 20 04:52:21 2025] RSP: 002b:00007ffd6e1a4028 EFLAGS: 00000246 ORIG= _RAX: 0000000000000000 [Thu Mar 20 04:52:21 2025] RAX: ffffffffffffffda RBX: 0000000000020000 RCX:= 00007fe6bf7a046e [Thu Mar 20 04:52:21 2025] RDX: 0000000000020000 RSI: 00007fe6bf694000 RDI:= 0000000000000003 [Thu Mar 20 04:52:21 2025] RBP: 00007fe6bf694000 R08: 00007fe6bf693010 R09:= 0000000000000000 [Thu Mar 20 04:52:21 2025] R10: fffffffffffffbc5 R11: 0000000000000246 R12:= 0000000000000000 [Thu Mar 20 04:52:21 2025] R13: 0000000000000003 R14: 0000000000020000 R15:= 0000000000020000 [1] https://lore.kernel.org/all/174046694331.2194069.15472952050240807469.s= tgit@mhiramat.tok.corp.google.com Suggested-by: Masami Hiramatsu (Google) Signed-off-by: Mingzhe Yang Signed-off-by: Lance Yang Reviewed-by: Masami Hiramatsu (Google) --- include/linux/semaphore.h | 15 ++++++++++- kernel/hung_task.c | 52 ++++++++++++++++++++++++++++++-------- kernel/locking/semaphore.c | 52 +++++++++++++++++++++++++++++++++----- 3 files changed, 101 insertions(+), 18 deletions(-) diff --git a/include/linux/semaphore.h b/include/linux/semaphore.h index 04655faadc2d..89706157e622 100644 --- a/include/linux/semaphore.h +++ b/include/linux/semaphore.h @@ -16,13 +16,25 @@ struct semaphore { raw_spinlock_t lock; unsigned int count; struct list_head wait_list; + +#ifdef CONFIG_DETECT_HUNG_TASK_BLOCKER + unsigned long last_holder; +#endif }; =20 +#ifdef CONFIG_DETECT_HUNG_TASK_BLOCKER +#define __LAST_HOLDER_SEMAPHORE_INITIALIZER \ + , .last_holder =3D 0UL +#else +#define __LAST_HOLDER_SEMAPHORE_INITIALIZER +#endif + #define __SEMAPHORE_INITIALIZER(name, n) \ { \ .lock =3D __RAW_SPIN_LOCK_UNLOCKED((name).lock), \ .count =3D n, \ - .wait_list =3D LIST_HEAD_INIT((name).wait_list), \ + .wait_list =3D LIST_HEAD_INIT((name).wait_list) \ + __LAST_HOLDER_SEMAPHORE_INITIALIZER \ } =20 /* @@ -47,5 +59,6 @@ extern int __must_check down_killable(struct semaphore *s= em); extern int __must_check down_trylock(struct semaphore *sem); extern int __must_check down_timeout(struct semaphore *sem, long jiffies); extern void up(struct semaphore *sem); +extern unsigned long sem_last_holder(struct semaphore *sem); =20 #endif /* __LINUX_SEMAPHORE_H */ diff --git a/kernel/hung_task.c b/kernel/hung_task.c index 79558d76ef06..d2432df2b905 100644 --- a/kernel/hung_task.c +++ b/kernel/hung_task.c @@ -99,32 +99,62 @@ static struct notifier_block panic_block =3D { static void debug_show_blocker(struct task_struct *task) { struct task_struct *g, *t; - unsigned long owner, blocker; + unsigned long owner, blocker, blocker_type; =20 RCU_LOCKDEP_WARN(!rcu_read_lock_held(), "No rcu lock held"); =20 blocker =3D READ_ONCE(task->blocker); - if (!blocker || - hung_task_get_blocker_type(blocker) !=3D BLOCKER_TYPE_MUTEX) + if (!blocker) return; =20 - owner =3D mutex_get_owner( - (struct mutex *)hung_task_blocker_to_lock(blocker)); + blocker_type =3D hung_task_get_blocker_type(blocker); + + switch (blocker_type) { + case BLOCKER_TYPE_MUTEX: + owner =3D mutex_get_owner( + (struct mutex *)hung_task_blocker_to_lock(blocker)); + break; + case BLOCKER_TYPE_SEM: + owner =3D sem_last_holder( + (struct semaphore *)hung_task_blocker_to_lock(blocker)); + break; + default: + WARN_ON_ONCE(1); + return; + } + =20 if (unlikely(!owner)) { - pr_err("INFO: task %s:%d is blocked on a mutex, but the owner is not fou= nd.\n", - task->comm, task->pid); + switch (blocker_type) { + case BLOCKER_TYPE_MUTEX: + pr_err("INFO: task %s:%d is blocked on a mutex, but the owner is not fo= und.\n", + task->comm, task->pid); + break; + case BLOCKER_TYPE_SEM: + pr_err("INFO: task %s:%d is blocked on a semaphore, but the last holder= is not found.\n", + task->comm, task->pid); + break; + } return; } =20 /* Ensure the owner information is correct. */ for_each_process_thread(g, t) { - if ((unsigned long)t =3D=3D owner) { + if ((unsigned long)t !=3D owner) + continue; + + switch (blocker_type) { + case BLOCKER_TYPE_MUTEX: pr_err("INFO: task %s:%d is blocked on a mutex likely owned by task %s:= %d.\n", - task->comm, task->pid, t->comm, t->pid); - sched_show_task(t); - return; + task->comm, task->pid, t->comm, t->pid); + break; + case BLOCKER_TYPE_SEM: + pr_err("INFO: task %s:%d blocked on a semaphore likely last held by tas= k %s:%d\n", + task->comm, task->pid, t->comm, t->pid); + break; } + sched_show_task(t); + return; } } #else diff --git a/kernel/locking/semaphore.c b/kernel/locking/semaphore.c index 34bfae72f295..3d06d4adc05b 100644 --- a/kernel/locking/semaphore.c +++ b/kernel/locking/semaphore.c @@ -33,12 +33,14 @@ #include #include #include +#include =20 static noinline void __down(struct semaphore *sem); static noinline int __down_interruptible(struct semaphore *sem); static noinline int __down_killable(struct semaphore *sem); static noinline int __down_timeout(struct semaphore *sem, long timeout); static noinline void __up(struct semaphore *sem); +static inline void __sem_acquire(struct semaphore *sem); =20 /** * down - acquire the semaphore @@ -58,7 +60,7 @@ void __sched down(struct semaphore *sem) might_sleep(); raw_spin_lock_irqsave(&sem->lock, flags); if (likely(sem->count > 0)) - sem->count--; + __sem_acquire(sem); else __down(sem); raw_spin_unlock_irqrestore(&sem->lock, flags); @@ -82,7 +84,7 @@ int __sched down_interruptible(struct semaphore *sem) might_sleep(); raw_spin_lock_irqsave(&sem->lock, flags); if (likely(sem->count > 0)) - sem->count--; + __sem_acquire(sem); else result =3D __down_interruptible(sem); raw_spin_unlock_irqrestore(&sem->lock, flags); @@ -109,7 +111,7 @@ int __sched down_killable(struct semaphore *sem) might_sleep(); raw_spin_lock_irqsave(&sem->lock, flags); if (likely(sem->count > 0)) - sem->count--; + __sem_acquire(sem); else result =3D __down_killable(sem); raw_spin_unlock_irqrestore(&sem->lock, flags); @@ -139,7 +141,7 @@ int __sched down_trylock(struct semaphore *sem) raw_spin_lock_irqsave(&sem->lock, flags); count =3D sem->count - 1; if (likely(count >=3D 0)) - sem->count =3D count; + __sem_acquire(sem); raw_spin_unlock_irqrestore(&sem->lock, flags); =20 return (count < 0); @@ -164,7 +166,7 @@ int __sched down_timeout(struct semaphore *sem, long ti= meout) might_sleep(); raw_spin_lock_irqsave(&sem->lock, flags); if (likely(sem->count > 0)) - sem->count--; + __sem_acquire(sem); else result =3D __down_timeout(sem, timeout); raw_spin_unlock_irqrestore(&sem->lock, flags); @@ -185,6 +187,12 @@ void __sched up(struct semaphore *sem) unsigned long flags; =20 raw_spin_lock_irqsave(&sem->lock, flags); + +#ifdef CONFIG_DETECT_HUNG_TASK_BLOCKER + if (READ_ONCE(sem->last_holder) =3D=3D (unsigned long)current) + WRITE_ONCE(sem->last_holder, 0UL); +#endif + if (likely(list_empty(&sem->wait_list))) sem->count++; else @@ -224,8 +232,12 @@ static inline int __sched ___down_common(struct semaph= ore *sem, long state, raw_spin_unlock_irq(&sem->lock); timeout =3D schedule_timeout(timeout); raw_spin_lock_irq(&sem->lock); - if (waiter.up) + if (waiter.up) { +#ifdef CONFIG_DETECT_HUNG_TASK_BLOCKER + WRITE_ONCE(sem->last_holder, (unsigned long)current); +#endif return 0; + } } =20 timed_out: @@ -242,10 +254,18 @@ static inline int __sched __down_common(struct semaph= ore *sem, long state, { int ret; =20 +#ifdef CONFIG_DETECT_HUNG_TASK_BLOCKER + hung_task_set_blocker(sem, BLOCKER_TYPE_SEM); +#endif + trace_contention_begin(sem, 0); ret =3D ___down_common(sem, state, timeout); trace_contention_end(sem, ret); =20 +#ifdef CONFIG_DETECT_HUNG_TASK_BLOCKER + hung_task_clear_blocker(); +#endif + return ret; } =20 @@ -277,3 +297,23 @@ static noinline void __sched __up(struct semaphore *se= m) waiter->up =3D true; wake_up_process(waiter->task); } + +#ifdef CONFIG_DETECT_HUNG_TASK_BLOCKER +unsigned long sem_last_holder(struct semaphore *sem) +{ + return READ_ONCE(sem->last_holder); +} +#else +unsigned long sem_last_holder(struct semaphore *sem) +{ + return 0UL; +} +#endif + +static inline void __sem_acquire(struct semaphore *sem) +{ + sem->count--; +#ifdef CONFIG_DETECT_HUNG_TASK_BLOCKER + WRITE_ONCE(sem->last_holder, (unsigned long)current); +#endif +} --=20 2.45.2 From nobody Wed Dec 17 08:54:38 2025 Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) (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 BACA91F4C81 for ; Thu, 20 Mar 2025 06:50:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742453403; cv=none; b=AixcWjUOU6ENBrJT1Qjy3SO7WVYvufx2oqFSRClUcpLNnwz8sX5iz47GBa+BaDTSDH0YVbHELl7u9HnE39Hk05V+oNkZmrAVW4ufha8avutYnzXgVjTFhnawo9SH0ACVcjDMIWue2QOz/zv8YlGbeCKFSECCiTnQGXNxYZcHEbk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742453403; c=relaxed/simple; bh=ODHGBUPKrKAqP4VfE6D6tTebFzVEioPEUE2yIRPh+XM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fXD5xDaRt9biLb5qh9DfUTlqE38rINVUgTfKiARu2WyI4iplob3L0spAN6jbPneT/flsxOHJlYuJgjx2kK3+t5NkfXMsyFAj306su2rqxP2g3tlzE4BqVkrtjy7mzRjl0hT8mOGD0EY434f2UuSCM2j2HuoQyQLnryZIAloX0hE= 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=itej+m5C; arc=none smtp.client-ip=209.85.214.171 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="itej+m5C" Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-223fd89d036so5707525ad.1 for ; Wed, 19 Mar 2025 23:50:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1742453401; x=1743058201; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=qhCbqNpnCfaE8zH3DllA9v9+aa48ezRrrdydUEDq1QA=; b=itej+m5CpJwkgcliMZ9+u5cq9gqmGKU9a5CMJEbjDTIsmcsAaFTicNOJKYhHRHTypm g8qH83/40r8AkzwdznPPhWc/f1On/ATLyqypt5J1jFU/DIJ3EuYTTie0IzCxLYAlCFUz 5JsqlDre6bZaVm7SxKnAUeAeD5sDX7C5E7TYS6R1OnOHvMcJFAc/a8k1a8raocVVe1Er v/77aObUyTjySFAff+S+gpuC9CVqKHClQnBIVt5x/neGFIGOVNO5MHSddC3xQ0E1Smci JJ9oNfeOMEy4QkwgaeVPePEG4/u2pqdYAis4bKQeBtUjgLdVmbEtluClovU/OApKggMZ QlCw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742453401; x=1743058201; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=qhCbqNpnCfaE8zH3DllA9v9+aa48ezRrrdydUEDq1QA=; b=BCKtakj1XQ2sgsZgjYxt/vwOU+0zsxzjF2Cm4A5cMmdx22JsE2mHHbGTU5pImX9e73 10vQ9CE9ccowPaU3dTsTEfT+Pf32U2Zuwwzva61hK8sEPi4QyH8/bK6wHjRqB1c2//9o XfT87/etNhH0xWXUVSRDuheFApBvB149/32YvW3nUzTPEslK0rxB3wehHxm867paOTF+ Rim4qZ2RAPnCdlpFh329H8ZAs/pxYxMbqtvmfc0KEB4C/+96kFvziXl0bgiVPQSFrDcq 79GXnbkrtotUDyZRjO3K1LVqT7HpGLFFt9aCrT6IbB6W8ASdMQ/F/IO0hbpwdHu09cN/ r4MQ== X-Forwarded-Encrypted: i=1; AJvYcCVhbTcGTJkha1gffiDVZhopyHDCkk8UHPNi8StcPGlKsT0Q9TGOVcd4CRPPJbJzzifqWgPiak7f45jG85E=@vger.kernel.org X-Gm-Message-State: AOJu0YxzZ5sj2PHmOALTBk332V0lk91jTvdGv5cAv9f59c162KL/v1mp N0CSdi3hidtE5vutESC5vt1Z54kMy4P/6/7Qo6jSaFmusTWIM2yqeR7rFOhC X-Gm-Gg: ASbGncvZmiS6ArseT1eK0sZvtYF+iXrMiWawpKzhyNp+b1bbOmHnC5Rp0thPRjQe0g0 pFlqJihFSEQBZwOaZhHx+dbx4jdbv3FrYFqCR/rDb7L1LZXzrZYHStYlTNtns3NdC2cdwXbkyI0 mmU6My8RJNBYQuy8ZCTRs/ICK2iJ+453eTZE64zxZVtle6nc61S2ys6keFYqlzTd8gQQeW6kwFr uGi6g8fcWuoQHD+vs3+X6zx5N+PCqMgFtlKNW9VhT88rcHSX8+KSocRhiPNvJDzCTgdrbr2ry0W vuM2iry099dVM2NHoLk0XvDZrfKBo/DLej650oOg6SnEj/oe1vxc7LXX1ncxuuI= X-Google-Smtp-Source: AGHT+IHZD1t7I1qHKGxY1sjtj9w2oXmXmmEJvpBfcSZbblUhlcPEBJAsnZ5oZCS0S2hbGvML46vvtA== X-Received: by 2002:a17:902:f70c:b0:215:9bc2:42ec with SMTP id d9443c01a7336-22649cb4d2emr82996815ad.47.1742453400990; Wed, 19 Mar 2025 23:50:00 -0700 (PDT) Received: from EBJ9932692.tcent.cn ([124.156.216.125]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-225c68885a1sm127260155ad.13.2025.03.19.23.49.55 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 19 Mar 2025 23:50:00 -0700 (PDT) From: Lance Yang To: akpm@linux-foundation.org Cc: will@kernel.org, peterz@infradead.org, mingo@redhat.com, longman@redhat.com, mhiramat@kernel.org, anna.schumaker@oracle.com, boqun.feng@gmail.com, joel.granados@kernel.org, kent.overstreet@linux.dev, leonylgao@tencent.com, linux-kernel@vger.kernel.org, rostedt@goodmis.org, senozhatsky@chromium.org, tfiga@chromium.org, amaindex@outlook.com, jstultz@google.com, Lance Yang Subject: [PATCH v4 3/3] samples: extend hung_task detector test with semaphore support Date: Thu, 20 Mar 2025 14:49:23 +0800 Message-ID: <20250320064923.24000-4-ioworker0@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20250320064923.24000-1-ioworker0@gmail.com> References: <20250320064923.24000-1-ioworker0@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Zi Li Extend the existing hung_task detector test module to support multiple lock types, including mutex and semaphore, with room for future additions (e.g., spinlock, etc.). This module creates dummy files under /hung_task, such as 'mutex' and 'semaphore'. The read process on any of these files will sleep for enough long time (256 seconds) while holding the respective lock. As a result, the second process will wait on the lock for a prolonged duration and be detected by the hung_task detector. This change unifies the previous mutex-only sample into a single, extensible hung_task_tests module, reducing code duplication and improving maintainability. Usage is: > cd /sys/kernel/debug/hung_task > cat mutex & cat mutex # Test mutex blocking > cat semaphore & cat semaphore # Test semaphore blocking Update the Kconfig description to reflect multiple debugfs files support. Suggested-by: Masami Hiramatsu (Google) Signed-off-by: Lance Yang Signed-off-by: Zi Li Acked-by: Masami Hiramatsu (Google) --- samples/Kconfig | 9 +-- samples/hung_task/Makefile | 2 +- samples/hung_task/hung_task_mutex.c | 66 -------------------- samples/hung_task/hung_task_tests.c | 97 +++++++++++++++++++++++++++++ 4 files changed, 103 insertions(+), 71 deletions(-) delete mode 100644 samples/hung_task/hung_task_mutex.c create mode 100644 samples/hung_task/hung_task_tests.c diff --git a/samples/Kconfig b/samples/Kconfig index 09011be2391a..753ed1f170b5 100644 --- a/samples/Kconfig +++ b/samples/Kconfig @@ -304,10 +304,11 @@ config SAMPLE_HUNG_TASK tristate "Hung task detector test code" depends on DETECT_HUNG_TASK && DEBUG_FS help - Build a module which provide a simple debugfs file. If user reads - the file, it will sleep long time (256 seconds) with holding a - mutex. Thus if there are 2 or more processes read this file, it - will be detected by the hung_task watchdog. + Build a module that provides debugfs files (e.g., mutex, semaphore, + etc.) under /hung_task. If user reads one of these files, + it will sleep long time (256 seconds) with holding a lock. Thus, + if 2 or more processes read the same file concurrently, it will + be detected by the hung_task watchdog. =20 source "samples/rust/Kconfig" =20 diff --git a/samples/hung_task/Makefile b/samples/hung_task/Makefile index f4d6ab563488..86036f1a204d 100644 --- a/samples/hung_task/Makefile +++ b/samples/hung_task/Makefile @@ -1,2 +1,2 @@ # SPDX-License-Identifier: GPL-2.0-only -obj-$(CONFIG_SAMPLE_HUNG_TASK) +=3D hung_task_mutex.o +obj-$(CONFIG_SAMPLE_HUNG_TASK) +=3D hung_task_tests.o diff --git a/samples/hung_task/hung_task_mutex.c b/samples/hung_task/hung_t= ask_mutex.c deleted file mode 100644 index 47ed38239ea3..000000000000 --- a/samples/hung_task/hung_task_mutex.c +++ /dev/null @@ -1,66 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0-or-later -/* - * hung_task_mutex.c - Sample code which causes hung task by mutex - * - * Usage: load this module and read `/hung_task/mutex` - * by 2 or more processes. - * - * This is for testing kernel hung_task error message. - * Note that this will make your system freeze and maybe - * cause panic. So do not use this except for the test. - */ - -#include -#include -#include -#include -#include - -#define HUNG_TASK_DIR "hung_task" -#define HUNG_TASK_FILE "mutex" -#define SLEEP_SECOND 256 - -static const char dummy_string[] =3D "This is a dummy string."; -static DEFINE_MUTEX(dummy_mutex); -static struct dentry *hung_task_dir; - -static ssize_t read_dummy(struct file *file, char __user *user_buf, - size_t count, loff_t *ppos) -{ - /* If the second task waits on the lock, it is uninterruptible sleep. */ - guard(mutex)(&dummy_mutex); - - /* When the first task sleep here, it is interruptible. */ - msleep_interruptible(SLEEP_SECOND * 1000); - - return simple_read_from_buffer(user_buf, count, ppos, - dummy_string, sizeof(dummy_string)); -} - -static const struct file_operations hung_task_fops =3D { - .read =3D read_dummy, -}; - -static int __init hung_task_sample_init(void) -{ - hung_task_dir =3D debugfs_create_dir(HUNG_TASK_DIR, NULL); - if (IS_ERR(hung_task_dir)) - return PTR_ERR(hung_task_dir); - - debugfs_create_file(HUNG_TASK_FILE, 0400, hung_task_dir, - NULL, &hung_task_fops); - - return 0; -} - -static void __exit hung_task_sample_exit(void) -{ - debugfs_remove_recursive(hung_task_dir); -} - -module_init(hung_task_sample_init); -module_exit(hung_task_sample_exit); - -MODULE_LICENSE("GPL"); -MODULE_AUTHOR("Masami Hiramatsu"); -MODULE_DESCRIPTION("Simple sleep under mutex file for testing hung task"); diff --git a/samples/hung_task/hung_task_tests.c b/samples/hung_task/hung_t= ask_tests.c new file mode 100644 index 000000000000..a5c09bd3a47d --- /dev/null +++ b/samples/hung_task/hung_task_tests.c @@ -0,0 +1,97 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * hung_task_tests.c - Sample code for testing hung tasks with mutex, + * semaphore, etc. + * + * Usage: Load this module and read `/hung_task/mutex`, + * `/hung_task/semaphore`, etc., with 2 or more processes. + * + * This is for testing kernel hung_task error messages with various locking + * mechanisms (e.g., mutex, semaphore, etc.). Note that this may freeze + * your system or cause a panic. Use only for testing purposes. + */ + +#include +#include +#include +#include +#include +#include + +#define HUNG_TASK_DIR "hung_task" +#define HUNG_TASK_MUTEX_FILE "mutex" +#define HUNG_TASK_SEM_FILE "semaphore" +#define SLEEP_SECOND 256 + +static const char dummy_string[] =3D "This is a dummy string."; +static DEFINE_MUTEX(dummy_mutex); +static DEFINE_SEMAPHORE(dummy_sem, 1); +static struct dentry *hung_task_dir; + +/* Mutex-based read function */ +static ssize_t read_dummy_mutex(struct file *file, char __user *user_buf, + size_t count, loff_t *ppos) +{ + /* Second task waits on mutex, entering uninterruptible sleep */ + guard(mutex)(&dummy_mutex); + + /* First task sleeps here, interruptible */ + msleep_interruptible(SLEEP_SECOND * 1000); + + return simple_read_from_buffer(user_buf, count, ppos, dummy_string, + sizeof(dummy_string)); +} + +/* Semaphore-based read function */ +static ssize_t read_dummy_semaphore(struct file *file, char __user *user_b= uf, + size_t count, loff_t *ppos) +{ + /* Second task waits on semaphore, entering uninterruptible sleep */ + down(&dummy_sem); + + /* First task sleeps here, interruptible */ + msleep_interruptible(SLEEP_SECOND * 1000); + + up(&dummy_sem); + + return simple_read_from_buffer(user_buf, count, ppos, dummy_string, + sizeof(dummy_string)); +} + +/* File operations for mutex */ +static const struct file_operations hung_task_mutex_fops =3D { + .read =3D read_dummy_mutex, +}; + +/* File operations for semaphore */ +static const struct file_operations hung_task_sem_fops =3D { + .read =3D read_dummy_semaphore, +}; + +static int __init hung_task_tests_init(void) +{ + hung_task_dir =3D debugfs_create_dir(HUNG_TASK_DIR, NULL); + if (IS_ERR(hung_task_dir)) + return PTR_ERR(hung_task_dir); + + /* Create debugfs files for mutex and semaphore tests */ + debugfs_create_file(HUNG_TASK_MUTEX_FILE, 0400, hung_task_dir, NULL, + &hung_task_mutex_fops); + debugfs_create_file(HUNG_TASK_SEM_FILE, 0400, hung_task_dir, NULL, + &hung_task_sem_fops); + + return 0; +} + +static void __exit hung_task_tests_exit(void) +{ + debugfs_remove_recursive(hung_task_dir); +} + +module_init(hung_task_tests_init); +module_exit(hung_task_tests_exit); + +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("Masami Hiramatsu "); +MODULE_AUTHOR("Zi Li "); +MODULE_DESCRIPTION("Simple sleep under lock files for testing hung task"); --=20 2.45.2