Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp6197787rdb; Thu, 14 Dec 2023 10:49:17 -0800 (PST) X-Google-Smtp-Source: AGHT+IG7mTtuqwIFDsvN+0xIOiy9lzcnNtdbHAeCjnqDmb1IJEow+l3kIzNK8CkzwrjzQJwjsgGT X-Received: by 2002:a17:903:228e:b0:1d0:7761:31f6 with SMTP id b14-20020a170903228e00b001d0776131f6mr6914625plh.32.1702579757602; Thu, 14 Dec 2023 10:49:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702579757; cv=none; d=google.com; s=arc-20160816; b=ewE0mgzy+hPE9oRLd2gteb2y5iHMBR3Gi6zgbsmGSZsPnWwuzQoY5fUr1OOttsMNlR FhD7O6jWizcIE5ha9CcAQbkkzm6yGyjrhoOPAGlnUnT9RduRKvJdirBK2gmTrf4BbZr+ kFM4IfyTn/b/X1VrAmx+mUSeZEAmT1EvKegnfDCE5b2vrMtFtOrUVLiUBZ+LLOz6qHn/ tCqIoQmaYt/Cb3sOSu/E9a57NhKEpnMb0cFirYK3IVlLsgOdt7wVPGDDqXQ48tolHYTM U0pn5n0xdsNjCa72H1DqbKhKl5V1+vgKdvDLDhZ1spFEOuI6oFJOWjQnIBhplQ/Xask+ mcCQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=53kITy3tOA4usH8Xl0YPm2kFgqevQ7XX321m2q4Kelw=; fh=Li71AkBlg4Y+nf/Xb+pcfHXo3lN7BcKHHVrjtJVizRk=; b=JWZNFVCOTdUF6CDLmbvtZiiT9qCq9mTFCRPb0K8VuRR6yxdL4aLQxeOKDzj/mQNFhy JFhWywc9oUR4Alml/CaRNup2/4bllHYhi2XxDI/vuPFYoRe8vZ2lat4+KElfo22NAEDK Xe/LD6ShLWXfwq9FiTETyV6PES5CKrzu3/nl5gpRasnlUGMFDeYnwY+gJDJeocFEeAnQ GblutCjeGomkTqaGnG9ccxt/mEfudszPdTsicT0k+kQOjCXDKBOujrIKMxjVW7H/xUE6 2ORpqrTIg5VaN8ap4IMtIyieqLZSHpzX5loEJHNRKQA8Ly29y7iximFwLrBoApvScjwA vWWw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=SsHQm4Ep; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id g8-20020a17090a708800b002839be230b2si12949227pjk.44.2023.12.14.10.49.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 10:49:17 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=SsHQm4Ep; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id D14488229D18; Thu, 14 Dec 2023 10:49:13 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229753AbjLNSs7 (ORCPT + 99 others); Thu, 14 Dec 2023 13:48:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57348 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229519AbjLNSs6 (ORCPT ); Thu, 14 Dec 2023 13:48:58 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4733FFB for ; Thu, 14 Dec 2023 10:49:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1702579743; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=53kITy3tOA4usH8Xl0YPm2kFgqevQ7XX321m2q4Kelw=; b=SsHQm4Eptb4cN8krD2oFD3e1vPJ41rbZmthiiRYBaY/YbYhd5IVT80qX7rrbc9SAC91ueE O14cHZmL2O2QS3pIuKPQg5d8ZDp08QIBJ8nxcpv3pb4Ea9nyCevZpqaDCPCZ8O9v1RUeKm +moEUPQbqO//7I+D+aMyjdizxoPwzRM= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-655-Z86V0OOtPxaCfj8_NqiGgQ-1; Thu, 14 Dec 2023 13:48:58 -0500 X-MC-Unique: Z86V0OOtPxaCfj8_NqiGgQ-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (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) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 94F2A848944; Thu, 14 Dec 2023 18:48:55 +0000 (UTC) Received: from [10.22.17.13] (unknown [10.22.17.13]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0EFB2492BF0; Thu, 14 Dec 2023 18:48:52 +0000 (UTC) Message-ID: <5c10f66c-3fd8-4861-994b-13e71c24f10a@redhat.com> Date: Thu, 14 Dec 2023 13:48:52 -0500 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v4 02/10] locking: introduce devm_mutex_init Content-Language: en-US To: George Stark , 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, boqun.feng@gmail.com, nikitos.tr@gmail.com Cc: linux-leds@vger.kernel.org, linux-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kernel@salutedevices.com References: <20231214173614.2820929-1-gnstark@salutedevices.com> <20231214173614.2820929-3-gnstark@salutedevices.com> From: Waiman Long In-Reply-To: <20231214173614.2820929-3-gnstark@salutedevices.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.10 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Thu, 14 Dec 2023 10:49:14 -0800 (PST) On 12/14/23 12:36, 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 > --- > include/linux/mutex.h | 23 +++++++++++++++++++++++ > kernel/locking/mutex-debug.c | 22 ++++++++++++++++++++++ > 2 files changed, 45 insertions(+) > > diff --git a/include/linux/mutex.h b/include/linux/mutex.h > index a33aa9eb9fc3..ebd03ff1ef66 100644 > --- a/include/linux/mutex.h > +++ b/include/linux/mutex.h > @@ -21,6 +21,8 @@ > #include > #include > > +struct device; > + > #ifdef CONFIG_DEBUG_LOCK_ALLOC > # define __DEP_MAP_MUTEX_INITIALIZER(lockname) \ > , .dep_map = { \ > @@ -127,6 +129,20 @@ extern void __mutex_init(struct mutex *lock, const char *name, > */ > extern bool mutex_is_locked(struct mutex *lock); > > +#ifdef CONFIG_DEBUG_MUTEXES > + > +int devm_mutex_init(struct device *dev, struct mutex *lock); Please add "extern" to the function declaration to be consistent with other functional declarations in mutex.h. > + > +#else > + > +static inline int devm_mutex_init(struct device *dev, struct mutex *lock) > +{ > + mutex_init(lock); > + return 0; > +} I would prefer you to add a devm_mutex_init macro after the function declaration and put this inline function at the end of header if the devm_mutex_init macro isn't defined. In this way, you don't need to repeat this inline function twice as it has no dependency on PREEMPT_RT. By doing this, you can also move the function declaration right after mutex_destroy() without the need to add another #ifdef CONFIG_DEBUG_MUTEXES block. > + > +#endif > + > #else /* !CONFIG_PREEMPT_RT */ > /* > * Preempt-RT variant based on rtmutexes. > @@ -169,6 +185,13 @@ do { \ > \ > __mutex_init((mutex), #mutex, &__key); \ > } while (0) > + > +static inline int devm_mutex_init(struct device *dev, struct mutex *lock) > +{ > + mutex_init(lock); > + return 0; > +} > + > #endif /* CONFIG_PREEMPT_RT */ > > /* > 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); The mutex-debug.c change looks fine to me. Cheers, Longman