Received: by 2002:ab2:788f:0:b0:1ee:8f2e:70ae with SMTP id b15csp458484lqi; Thu, 7 Mar 2024 02:19:34 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUYPPtJxd9SBqS3wpSBZR/uzOuqnpP6XYj4A52JBfjdAxYhAPhDUpL0hRHFYUY/05iIVephVfVQEx8dTe6ToeZs693ElE1rS22fd3B+LQ== X-Google-Smtp-Source: AGHT+IGBwxdcdktWLG/B6J1CDWUej/b0URoqoT7eo+4njd2gshCh06W0Lm+SDajSgjgwgJ8VvlrO X-Received: by 2002:a05:6a20:d043:b0:1a1:431c:6696 with SMTP id hv3-20020a056a20d04300b001a1431c6696mr8390223pzb.49.1709806774180; Thu, 07 Mar 2024 02:19:34 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709806774; cv=pass; d=google.com; s=arc-20160816; b=LW9c606QqqHWfi/hx0uGdB3egR6SJ/3COmocHgc1ibukrx8IrlD+uVGyqNEeX/yXBg aQDN9U31fh/fgttDms9f4256nL9hLMevOReXP/4H3rgC7rE9iB+qv9iJeqafLjXh2VRz 1CDlUmJAGrcEC5rOAUIVsSMyDGjb90dpBNnsNjri/NhSzGFrGFgznDYqv7w8TTxEQTyU IXOZktQD+/Prp9etDf/lIT47Hjg0s0PST42/Ec0P0zn80XTBl3YLKASqW1tUMIjoDmru syQn+u1/zrynImD4mmSP4XljDWqWvSkcCghR6EgyAe5VuAZHlCmF1DMlCsJW6/NzdexN PCPw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=Wmf+aNe3F4YtdfwwCWe4mBnAac8n9Hpz9OCdwavlPsI=; fh=cV2PQxepJhpfLGBP9ZdEXg9u97KbJYKsEotJvugevxQ=; b=LXA7kPbHhB8WworUm2urrXmZizsIPAf2ltBO3JeO82DzOeFc1O+hi2rC4DnNpqswcs bqyyvodK+PJ797CTEhmyeUsOM0F92+ajjJHGEIWDz9ibvHQQHuj/4AVNWw/DXNIEIN9z QIHGW4y6GS+1se/wP8IBG9OYoAwGqxgdjSDrT2yBq8mCCD9q3owaUCeiOFkzD/h/U0yR ETRsRv4jJQi6WAjdgpQDAf1lO4jDSgMwOCMRhrCXXeh56DD3a3mgnQ0iHFc/vN6o3Crd 4F7xtf55UaHAdsIdCZjDVnq3ZFZg9YTW2QWywf3UTHNzgF42iyxbuAV0BOJHi0JeAafh quWQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@nic.cz header.s=default header.b="U0oRVyb/"; arc=pass (i=1 spf=pass spfdomain=nic.cz dkim=pass dkdomain=nic.cz dmarc=pass fromdomain=nic.cz); spf=pass (google.com: domain of linux-kernel+bounces-95301-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-95301-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=nic.cz Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id h24-20020a17090aa89800b0029b877e1b02si1354339pjq.140.2024.03.07.02.19.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Mar 2024 02:19:34 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-95301-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@nic.cz header.s=default header.b="U0oRVyb/"; arc=pass (i=1 spf=pass spfdomain=nic.cz dkim=pass dkdomain=nic.cz dmarc=pass fromdomain=nic.cz); spf=pass (google.com: domain of linux-kernel+bounces-95301-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-95301-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=nic.cz Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 2013CB24554 for ; Thu, 7 Mar 2024 10:09:16 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 357081CD29; Thu, 7 Mar 2024 10:06:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=nic.cz header.i=@nic.cz header.b="U0oRVyb/" Received: from mail.nic.cz (mail.nic.cz [217.31.204.67]) (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 004F184FCD; Thu, 7 Mar 2024 10:06:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.31.204.67 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709806012; cv=none; b=frdj3+6OP4Lrs4drnbU/enbnoOXvcqRnvMN2ZNwZpL/m61pqqMSCnW4iBwlPfm4i+C6BKZC+fEqX4SHckzBMfx8UbLyS5Z1hK5gdXC/x9LcwghWFlOFZ2rXdFc+5pztIA7Utvhn0eE0PbJ1BmHVXhNPrquk99uLfHTQy9LXvg3M= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709806012; c=relaxed/simple; bh=76Jo3UVwHUPVJ1Mxh3dPGM0nxOmpEeJydflFioZTAPQ=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=D6XtNifx1zOqSpGtzOpY2x7msCV/0e6XlXGxoi59zmDy6pgQ2Cs9ATXePtUUHn465yAIe+iTK4vnx3/NDdjcjPhPQugX/mjm4ZH2hjw1WMvVQc2CN6sM8phKdtaiu5nHmLHEQdSlvzME5kvuhl9xjQz8hYOm6AvMD+U4jgjI87I= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nic.cz; spf=pass smtp.mailfrom=nic.cz; dkim=pass (1024-bit key) header.d=nic.cz header.i=@nic.cz header.b=U0oRVyb/; arc=none smtp.client-ip=217.31.204.67 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nic.cz Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=nic.cz Received: from kandell (unknown [172.20.6.140]) by mail.nic.cz (Postfix) with ESMTPS id A3B251C035F; Thu, 7 Mar 2024 10:56:58 +0100 (CET) Authentication-Results: mail.nic.cz; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=nic.cz; s=default; t=1709805421; bh=76Jo3UVwHUPVJ1Mxh3dPGM0nxOmpEeJydflFioZTAPQ=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From:Reply-To: Subject:To:Cc; b=U0oRVyb/byvyZefng+g9v35ujBi7vq6aYkmzvkKCrDxKkrm4GdejErIA4KhO0xVnT WUqRgmcFvpZBesK2q11bJeZP+NENK/F4uNa1T0zLPva3uD4Ifw484KchmjTu7f5Ttu hIwK7zAsQ5g2CzFLPtjyTUvjxJV5Q6v7IBTxzfwg= Date: Thu, 7 Mar 2024 10:56:58 +0100 From: Marek =?utf-8?B?QmVow7pu?= To: George Stark Cc: andy.shevchenko@gmail.com, pavel@ucw.cz, lee@kernel.org, vadimp@nvidia.com, mpe@ellerman.id.au, npiggin@gmail.com, christophe.leroy@csgroup.eu, hdegoede@redhat.com, mazziesaccount@gmail.com, peterz@infradead.org, mingo@redhat.com, will@kernel.org, longman@redhat.com, boqun.feng@gmail.com, nikitos.tr@gmail.com, kabel@kernel.org, linux-leds@vger.kernel.org, linux-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kernel@salutedevices.com Subject: Re: [PATCH v5 02/10] locking/mutex: introduce devm_mutex_init Message-ID: <20240307095639.b6utkbzr36liuu3p@kandell> References: <20240307024034.1548605-1-gnstark@salutedevices.com> <20240307024034.1548605-3-gnstark@salutedevices.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240307024034.1548605-3-gnstark@salutedevices.com> X-Virus-Scanned: clamav-milter 0.103.10 at mail X-Virus-Status: Clean X-Rspamd-Queue-Id: A3B251C035F X-Rspamd-Pre-Result: action=no action; module=multimap; Matched map: WHITELISTED_IP X-Spamd-Result: default: False [-0.10 / 20.00]; MIME_GOOD(-0.10)[text/plain]; WHITELISTED_IP(0.00)[172.20.6.140]; MIME_TRACE(0.00)[0:+]; FREEMAIL_ENVRCPT(0.00)[gmail.com]; ARC_NA(0.00)[]; FROM_HAS_DN(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; TAGGED_RCPT(0.00)[]; FUZZY_BLOCKED(0.00)[rspamd.com] X-Rspamd-Action: no action X-Spamd-Bar: / X-Rspamd-Server: mail On Thu, Mar 07, 2024 at 05:40:26AM +0300, George Stark wrote: > Using of devm API leads to a certain order of releasing resources. > So all dependent resources which are not devm-wrapped should be deleted > with respect to devm-release order. Mutex is one of such objects that > often is bound to other resources and has no own devm wrapping. > Since mutex_destroy() actually does nothing in non-debug builds > frequently calling mutex_destroy() is just ignored which is safe for now > but wrong formally and can lead to a problem if mutex_destroy() will be > extended so introduce devm_mutex_init() > > Signed-off-by: George Stark > Signed-off-by: Christophe Leroy > --- > Hello Christophe. Hope you don't mind I put you SoB tag because you helped alot > to make this patch happen. > > include/linux/mutex.h | 13 +++++++++++++ > kernel/locking/mutex-debug.c | 22 ++++++++++++++++++++++ > 2 files changed, 35 insertions(+) > > diff --git a/include/linux/mutex.h b/include/linux/mutex.h > index f7611c092db7..9bcf72cb941a 100644 > --- a/include/linux/mutex.h > +++ b/include/linux/mutex.h > @@ -22,6 +22,8 @@ > #include > #include > > +struct device; > + > #ifdef CONFIG_DEBUG_LOCK_ALLOC > # define __DEP_MAP_MUTEX_INITIALIZER(lockname) \ > , .dep_map = { \ > @@ -115,10 +117,21 @@ do { \ > > #ifdef CONFIG_DEBUG_MUTEXES > > +int devm_mutex_init(struct device *dev, struct mutex *lock); > void mutex_destroy(struct mutex *lock); > > #else > > +static inline int devm_mutex_init(struct device *dev, struct mutex *lock) > +{ > + /* > + * since mutex_destroy is nop actually there's no need to register it > + * in devm subsystem. > + */ > + mutex_init(lock); > + return 0; > +} > + > static inline void mutex_destroy(struct mutex *lock) {} > > #endif > diff --git a/kernel/locking/mutex-debug.c b/kernel/locking/mutex-debug.c > index bc8abb8549d2..c9efab1a8026 100644 > --- a/kernel/locking/mutex-debug.c > +++ b/kernel/locking/mutex-debug.c > @@ -19,6 +19,7 @@ > #include > #include > #include > +#include > > #include "mutex.h" > > @@ -104,3 +105,24 @@ void mutex_destroy(struct mutex *lock) > } > > EXPORT_SYMBOL_GPL(mutex_destroy); > + > +static void devm_mutex_release(void *res) > +{ > + mutex_destroy(res); > +} > + > +/** > + * devm_mutex_init - Resource-managed mutex initialization > + * @dev: Device which lifetime mutex is bound to > + * @lock: Pointer to a mutex > + * > + * Initialize mutex which is automatically destroyed when the driver is detached. > + * > + * Returns: 0 on success or a negative error code on failure. > + */ > +int devm_mutex_init(struct device *dev, struct mutex *lock) > +{ > + mutex_init(lock); > + return devm_add_action_or_reset(dev, devm_mutex_release, lock); > +} > +EXPORT_SYMBOL_GPL(devm_mutex_init); Hi George, look at https://lore.kernel.org/lkml/7013bf9e-2663-4613-ae61-61872e81355b@redhat.com/ where Matthew and Hans explain that devm_mutex_init needs to be a macro because of the static lockdep key. so this should be something like: static inline int __devm_mutex_init(struct device *dev, struct mutex *mutex, const char *name, struct lock_class_key *key) { __mutex_init(mutex, name, key); return devm_add_action_or_reset(dev, devm_mutex_release, mutex); } #define devm_mutex_init(dev, mutex) \ do { \ static struct lock_class_key __key; \ \ __devm_mutex_init(dev, (mutex), #mutex, &__key); \ } while (0); Marek