From patchwork Thu Sep 12 22:39:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bart Van Assche X-Patchwork-Id: 828127 Received: from 009.lax.mailroute.net (009.lax.mailroute.net [199.89.1.12]) (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 0ABD119F13D for ; Thu, 12 Sep 2024 22:40:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=199.89.1.12 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726180823; cv=none; b=KMvYS9JLVwT9S6DlHUonfptQjrmydnWoczKVSStUdb7FBGC5IsCOiIGdaBItR/inUiCjq1tY8mn0H5Ev05z+iBPbc4cprUrobzz+LQYRb2lCthg8OnsshwgIf+bMw4HA4JOkEqHSx0G0lM1+NDh9+9swFtJEpqlbkXWgGJCXeB8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726180823; c=relaxed/simple; bh=MzTIZgO8Pr01Ze9i2JkO5CNFqOMRNj4unFZQJXRzUDE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=cKMcvkW4x/CpDid0iyimptaHc9Bmzstv15ZXKPqrbRE4IEXZUI5368S3HPpO9/id+OLBVYDw1cYDEVZFz8CfIVwdrfUZt21lcV2rPOmC2xjjB2oxwb1zVHQi5de+8vl45br99w5kcuBsq10HLHik98gpiNEI+scdeL5O4QG+u9Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=acm.org; spf=pass smtp.mailfrom=acm.org; dkim=pass (2048-bit key) header.d=acm.org header.i=@acm.org header.b=oblpu6NR; arc=none smtp.client-ip=199.89.1.12 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=acm.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=acm.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=acm.org header.i=@acm.org header.b="oblpu6NR" Received: from localhost (localhost [127.0.0.1]) by 009.lax.mailroute.net (Postfix) with ESMTP id 4X4XT13XGzzlgMVW; Thu, 12 Sep 2024 22:40:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=acm.org; h= content-transfer-encoding:mime-version:references:in-reply-to :x-mailer:message-id:date:date:subject:subject:from:from :received:received; s=mr01; t=1726180818; x=1728772819; bh=PVwFA aSgprFDzebneQxEdDzHnbI9aOLw0GmSwc9jzo0=; b=oblpu6NRB1LEy7e7l69LA YOGeIk0JjEBmvtDgfAVeVnPoBT8lm059vuevulDE6SL8jMgAdQjeHnxK+DmPB/aO sUH/2HVK/auePC36vu5zh89T1Fx26orb2qR5JUzkDQJwGFFADJeBJH8n9s5GF//6 PPBPQn2V6JGqSSfZKppkQJ+hbyjm/VmmIC4Tqn0bAMkDOhsaKDgB+Imxl4NMBD8K c7x4UyPq7O+tKLECkC5sdun/uKchA67sdnqkop1+0r5HjHr8yokDMsYYs+12u4FM mm85FwiPBzPr3jwDCkr9vNhlw09jQhX4ZEtbkKXqmPlsgdJbQiEyZJYyEFFZFBNv g== X-Virus-Scanned: by MailRoute Received: from 009.lax.mailroute.net ([127.0.0.1]) by localhost (009.lax [127.0.0.1]) (mroute_mailscanner, port 10029) with LMTP id jf7NGG2hHlQY; Thu, 12 Sep 2024 22:40:18 +0000 (UTC) Received: from bvanassche.mtv.corp.google.com (unknown [104.135.204.82]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: bvanassche@acm.org) by 009.lax.mailroute.net (Postfix) with ESMTPSA id 4X4XSx4V2GzlgMVh; Thu, 12 Sep 2024 22:40:17 +0000 (UTC) From: Bart Van Assche To: Greg Kroah-Hartman Cc: linux-usb@vger.kernel.org, Thomas Gleixner , Peter Zijlstra , Ingo Molnar , Andy Shevchenko , Amit Sunil Dhamne , Bart Van Assche Subject: [PATCH v3 1/3] locking/mutex: Define mutex_init() once Date: Thu, 12 Sep 2024 15:39:54 -0700 Message-ID: <20240912223956.3554086-2-bvanassche@acm.org> X-Mailer: git-send-email 2.46.0.662.g92d0881bb0-goog In-Reply-To: <20240912223956.3554086-1-bvanassche@acm.org> References: <20240912223956.3554086-1-bvanassche@acm.org> Precedence: bulk X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 With CONFIG_PREEMPT_RT disabled __mutex_init() is a function. With CONFIG_PREEMPT_RT enabled, __mutex_init() is a macro. I assume this is why mutex_init() is defined twice as exactly the same macro. Prepare for introducing a new macro for mutex initialization by combining the two identical mutex_init() definitions into a single definition. This patch does not change any functionality because the C preprocessor expands macros when it encounters the macro name and not when a macro definition is encountered. See also commit bb630f9f7a7d ("locking/rtmutex: Add mutex variant for RT"). Acked-by: Peter Zijlstra (Intel) Cc: Thomas Gleixner Cc: Ingo Molnar Signed-off-by: Bart Van Assche --- include/linux/mutex.h | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/include/linux/mutex.h b/include/linux/mutex.h index a561c629d89f..ef617089db19 100644 --- a/include/linux/mutex.h +++ b/include/linux/mutex.h @@ -49,7 +49,6 @@ static inline void mutex_destroy(struct mutex *lock) {} #endif -#ifndef CONFIG_PREEMPT_RT /** * mutex_init - initialize the mutex * @mutex: the mutex to be initialized @@ -65,6 +64,7 @@ do { \ __mutex_init((mutex), #mutex, &__key); \ } while (0) +#ifndef CONFIG_PREEMPT_RT #define __MUTEX_INITIALIZER(lockname) \ { .owner = ATOMIC_LONG_INIT(0) \ , .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(lockname.wait_lock) \ @@ -111,12 +111,6 @@ do { \ __mutex_rt_init((mutex), name, key); \ } while (0) -#define mutex_init(mutex) \ -do { \ - static struct lock_class_key __key; \ - \ - __mutex_init((mutex), #mutex, &__key); \ -} while (0) #endif /* CONFIG_PREEMPT_RT */ #ifdef CONFIG_DEBUG_MUTEXES From patchwork Thu Sep 12 22:39:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bart Van Assche X-Patchwork-Id: 828613 Received: from 009.lax.mailroute.net (009.lax.mailroute.net [199.89.1.12]) (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 27DDB1C244F for ; Thu, 12 Sep 2024 22:40:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=199.89.1.12 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726180824; cv=none; b=Bc3WLRI+P+fyqBXM55yldKFWaG+pMR9T/Ii+BCzR4mwCHB3fE7N1vzgbsXmOopM1xQLe7KDTgOSsBTav/PEyXQbosI0AE3u7u/ttnJW207XWITuvEZM9xlVu6eGBpNg9cXC3Kr3uH68pllYmHpTTYrK0Wub/bFGpYj4F7I3w0g8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726180824; c=relaxed/simple; bh=ONsHOgeMoqgcozIioe8vsB4I9pIWlWgLwpnnbd+YRXs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=A5+RQd73gW3NUN9yBq5RBSDdiHjInw6R5w+6IdVqoi3lvb1v2l2UX1X9v1BUwqwyShtgOUGCobabgmRuvlm+kOdAs4fEmE1djCnF9AZ2iUA/lDoBrhwbmi40xuC7fHJK//fBX9QNNjuZzbRBSYdvF/hV72B1Ol3oJvtyergVrHM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=acm.org; spf=pass smtp.mailfrom=acm.org; dkim=pass (2048-bit key) header.d=acm.org header.i=@acm.org header.b=XhoYbKbd; arc=none smtp.client-ip=199.89.1.12 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=acm.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=acm.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=acm.org header.i=@acm.org header.b="XhoYbKbd" Received: from localhost (localhost [127.0.0.1]) by 009.lax.mailroute.net (Postfix) with ESMTP id 4X4XT23mfrzlgMVh; Thu, 12 Sep 2024 22:40:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=acm.org; h= content-transfer-encoding:mime-version:references:in-reply-to :x-mailer:message-id:date:date:subject:subject:from:from :received:received; s=mr01; t=1726180819; x=1728772820; bh=EjgFf 8+wlvPOTQeTYDkAOEa7NsvwgC+QrqwbcVIqNnc=; b=XhoYbKbdVMirqmD4i9bO3 hniPw+ipTsTOVqnLDby5sMFHMQUaeHzN4sVIjPw+PyuhGAK5zntWtmrd3d5Pum37 j1JfFMz9EOQ3ywLRgG4WWcOIyUemjiq7qY45y4dNYpgDVrHFwZ/EACMn0x9485OO ++sOGhdZxe9np81NoJC9comlK7MI+L4zcRkBJ9MQWcWOZRbxNJ9BMEP9c3E7ek8l rH56VpnZVDEeWxDGhGrPaPzrOJcztIO2Gzk/A1Wm93F5Jw/KOUd7GPzJtFKkzjuE HB9TEycSNL2bdnqPmc+rExjdoGcr5ZUD3oDy3UFCMU0EyQVPq4NmPkAN1R6Zx6di g== X-Virus-Scanned: by MailRoute Received: from 009.lax.mailroute.net ([127.0.0.1]) by localhost (009.lax [127.0.0.1]) (mroute_mailscanner, port 10029) with LMTP id uj-b4hwG_5q3; Thu, 12 Sep 2024 22:40:19 +0000 (UTC) Received: from bvanassche.mtv.corp.google.com (unknown [104.135.204.82]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: bvanassche@acm.org) by 009.lax.mailroute.net (Postfix) with ESMTPSA id 4X4XSy3l6rzlgMVj; Thu, 12 Sep 2024 22:40:18 +0000 (UTC) From: Bart Van Assche To: Greg Kroah-Hartman Cc: linux-usb@vger.kernel.org, Thomas Gleixner , Peter Zijlstra , Ingo Molnar , Andy Shevchenko , Amit Sunil Dhamne , Bart Van Assche Subject: [PATCH v3 2/3] locking/mutex: Introduce mutex_init_with_key() Date: Thu, 12 Sep 2024 15:39:55 -0700 Message-ID: <20240912223956.3554086-3-bvanassche@acm.org> X-Mailer: git-send-email 2.46.0.662.g92d0881bb0-goog In-Reply-To: <20240912223956.3554086-1-bvanassche@acm.org> References: <20240912223956.3554086-1-bvanassche@acm.org> Precedence: bulk X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The following pattern occurs 5 times in kernel drivers: lockdep_register_key(key); __mutex_init(mutex, name, key); In several cases the 'name' argument matches #mutex. Hence, introduce the mutex_init_with_key() macro. This macro derives the 'name' argument from the 'mutex' argument. Suggested-by: Andy Shevchenko Acked-by: Peter Zijlstra (Intel) Reviewed-by: Andy Shevchenko Cc: Thomas Gleixner Cc: Ingo Molnar Signed-off-by: Bart Van Assche --- include/linux/mutex.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/include/linux/mutex.h b/include/linux/mutex.h index ef617089db19..2bf91b57591b 100644 --- a/include/linux/mutex.h +++ b/include/linux/mutex.h @@ -64,6 +64,17 @@ do { \ __mutex_init((mutex), #mutex, &__key); \ } while (0) +/** + * mutex_init_with_key - initialize a mutex with a given lockdep key + * @mutex: the mutex to be initialized + * @key: the lockdep key to be associated with the mutex + * + * Initialize the mutex to the unlocked state. + * + * It is not allowed to initialize an already locked mutex. + */ +#define mutex_init_with_key(mutex, key) __mutex_init((mutex), #mutex, (key)) + #ifndef CONFIG_PREEMPT_RT #define __MUTEX_INITIALIZER(lockname) \ { .owner = ATOMIC_LONG_INIT(0) \ From patchwork Thu Sep 12 22:39:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bart Van Assche X-Patchwork-Id: 828126 Received: from 009.lax.mailroute.net (009.lax.mailroute.net [199.89.1.12]) (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 2EAF81C244F for ; Thu, 12 Sep 2024 22:40:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=199.89.1.12 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726180827; cv=none; b=mtn1kn1AmCib4cJi5z5Oq9VttnrCG5Gtw3vZJRWcsjhOl6uWw42dx6aW4jiuLjw5GAyTN9jpBdVQeBB0RbFrO9i7NNT/LkoRk5yrgX65wyoYshqejXdJF7kZyFd2r/s20p9/N47ecRUk17Qd3EpIGDkJq8WzuabRNdixEkd8G98= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726180827; c=relaxed/simple; bh=sxAKHP3cQV0x7xhU/eX/1732Tjzdx2vIP6oJohlXeQ0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=pupgHiuCkkCkDBxIBWpsHivfOi6DGw36UaDXDJ1I2UU976XfXcl+vzBOFH97Xr55a2RxABWA5URTo7t4wKykbLWAWsc4cZ47j0/9e6vW59RymhmVcrkHdft7NthaHsD/mawrBeJP3hRt5b81Jtu6uaZoJgyooyr6Ah+03JYQRPo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=acm.org; spf=pass smtp.mailfrom=acm.org; dkim=pass (2048-bit key) header.d=acm.org header.i=@acm.org header.b=kaAQ40wD; arc=none smtp.client-ip=199.89.1.12 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=acm.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=acm.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=acm.org header.i=@acm.org header.b="kaAQ40wD" Received: from localhost (localhost [127.0.0.1]) by 009.lax.mailroute.net (Postfix) with ESMTP id 4X4XT558QTzlgMVj; Thu, 12 Sep 2024 22:40:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=acm.org; h= content-transfer-encoding:mime-version:references:in-reply-to :x-mailer:message-id:date:date:subject:subject:from:from :received:received; s=mr01; t=1726180820; x=1728772821; bh=guJG9 KumtE8xMLtZPQDShZaNC8ri4kiOKkGzlIM4Teg=; b=kaAQ40wDaRMxJ4+ZmnIAx 8tqeFL+1gpjrgv7VoH/2UFv+/T4ARgrtg7KaIi/zctyEY2iS75r/zmIwXQfRcowt bHufIZBDFQ8A6MzIfeASK6bfjlRMlim+9d8CH6OQwP21JOri0cgX9MkLRzu3R217 sGlduz2ii34zXk2K8FhYqchUYTdVpmaAiJ0qf1JGQI1UAjL3TEQAumb1qt8+sbon hnZQ6OgIWUp/dQuu94acsx7/JJOIAxp1MBVQVN+hwAkd+jd7yRmulJRO/8r7Lur6 12alSgD6/CABEnjE6lmZvQzQr93arwxl0d+QYDbkA1U948B4y7FKhcPyKU07bWyx Q== X-Virus-Scanned: by MailRoute Received: from 009.lax.mailroute.net ([127.0.0.1]) by localhost (009.lax [127.0.0.1]) (mroute_mailscanner, port 10029) with LMTP id zeVFqAQgqILO; Thu, 12 Sep 2024 22:40:20 +0000 (UTC) Received: from bvanassche.mtv.corp.google.com (unknown [104.135.204.82]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: bvanassche@acm.org) by 009.lax.mailroute.net (Postfix) with ESMTPSA id 4X4XSz2x6XzlgMVl; Thu, 12 Sep 2024 22:40:19 +0000 (UTC) From: Bart Van Assche To: Greg Kroah-Hartman Cc: linux-usb@vger.kernel.org, Thomas Gleixner , Peter Zijlstra , Ingo Molnar , Andy Shevchenko , Amit Sunil Dhamne , Bart Van Assche , Badhri Jagan Sridharan , Hans de Goede , Heikki Krogerus Subject: [PATCH v3 3/3] usb: roles: Improve the fix for a false positive recursive locking complaint Date: Thu, 12 Sep 2024 15:39:56 -0700 Message-ID: <20240912223956.3554086-4-bvanassche@acm.org> X-Mailer: git-send-email 2.46.0.662.g92d0881bb0-goog In-Reply-To: <20240912223956.3554086-1-bvanassche@acm.org> References: <20240912223956.3554086-1-bvanassche@acm.org> Precedence: bulk X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Improve commit fc88bb116179 ("usb: roles: add lockdep class key to struct usb_role_switch") as follows: * Move the lock class key declaration just above the mutex declaration such that the declaration order of these objects matches their initialization order. * Destroy the mutex and lock class key just before these objects are freed. This makes it easier to verify that the destruction calls happen after the last use of these objects. * Instead of switching the mutex key to the dynamic lock class key after initialization of the mutex has completed, initialize the mutex with the dynamic lock class key. Cc: Amit Sunil Dhamne Cc: Badhri Jagan Sridharan Cc: Hans de Goede Cc: Andy Shevchenko Cc: Heikki Krogerus Cc: Greg Kroah-Hartman Signed-off-by: Bart Van Assche --- drivers/usb/roles/class.c | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/drivers/usb/roles/class.c b/drivers/usb/roles/class.c index 7aca1ef7f44c..c58a12c147f4 100644 --- a/drivers/usb/roles/class.c +++ b/drivers/usb/roles/class.c @@ -22,6 +22,7 @@ static const struct class role_class = { struct usb_role_switch { struct device dev; + struct lock_class_key key; struct mutex lock; /* device lock*/ struct module *module; /* the module this device depends on */ enum usb_role role; @@ -34,8 +35,6 @@ struct usb_role_switch { usb_role_switch_set_t set; usb_role_switch_get_t get; bool allow_userspace_control; - - struct lock_class_key key; }; #define to_role_switch(d) container_of(d, struct usb_role_switch, dev) @@ -329,6 +328,8 @@ static void usb_role_switch_release(struct device *dev) { struct usb_role_switch *sw = to_role_switch(dev); + mutex_destroy(&sw->lock); + lockdep_unregister_key(&sw->key); kfree(sw); } @@ -367,7 +368,8 @@ usb_role_switch_register(struct device *parent, if (!sw) return ERR_PTR(-ENOMEM); - mutex_init(&sw->lock); + lockdep_register_key(&sw->key); + mutex_init_with_key(&sw->lock, &sw->key); sw->allow_userspace_control = desc->allow_userspace_control; sw->usb2_port = desc->usb2_port; @@ -399,9 +401,6 @@ usb_role_switch_register(struct device *parent, sw->registered = true; - lockdep_register_key(&sw->key); - lockdep_set_class(&sw->lock, &sw->key); - /* TODO: Symlinks for the host port and the device controller. */ return sw; @@ -418,9 +417,6 @@ void usb_role_switch_unregister(struct usb_role_switch *sw) { if (IS_ERR_OR_NULL(sw)) return; - - lockdep_unregister_key(&sw->key); - sw->registered = false; if (dev_fwnode(&sw->dev)) component_del(&sw->dev, &connector_ops);