Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp3425786rdh; Mon, 27 Nov 2023 14:07:20 -0800 (PST) X-Google-Smtp-Source: AGHT+IGnge+nl2HYroJbiqY8O2mSBmWfyrdL4T5jO6LbD/0/9CCe00YzFJevLfh9egNMgJGtVXHx X-Received: by 2002:a17:90b:1b01:b0:285:9d5f:b7c9 with SMTP id nu1-20020a17090b1b0100b002859d5fb7c9mr9496842pjb.23.1701122839854; Mon, 27 Nov 2023 14:07:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701122839; cv=none; d=google.com; s=arc-20160816; b=Zczs7RfQ5wYgIm/gGbmjTj6B3/aYeiMYEySCT+EaVELVLRT24g6Qz95mQJmrXhAboB 5l5SwUNCYnIU9Up6zWec/NQDJqmZMBL2zALn2KR5HNsLilkxRBEW2x4To1U+haKSWQ0O 4wlvuiHPgMPm/0EqAUFjgS/IAvufXNGd207LAY5mtIiGiKF+7VRXYKStonHyfRd/4H00 5VBQYvPgsX2f4WvSXLWcQ7IAzAt7hWF7D7PfXL0RFWV1TKGejzlaAhOipm6mAZQIAFPl ZM20ka9kmJMn4kTwMwiNshrLNIzKI0PAjZ/p0OYGiIJgSfXyiajPfnr9MvPDNCXU7mwX A0nA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=u2+ysk/LYciDIkccpjaIWerpBDB/kN2mOkJR9Ltzxx0=; fh=Ulo6hc8buV1KvUHjE6U8VAJFLT2rcD/x8G5XrD8wYto=; b=xWQqZlzSNfguu5W2Zeo0xiCmYzHQm+67H8wX9Sd61eSiyYl9dBpm6y93/bg5yfZZij v5n+8ALF+8dwRp2MlStNKga5EqibXhIBD0k5iMg83nEVa/c/r24cnl8N+zZGdiCz6/HT hW5ITosg8BhcPAnTKsPLBMYbokr6vanluWVDesotds08BmL+HeKbv0j0tMEQ1nCV1XrF I95/FrL7GNdTD/3KGddt9zSVT2UlAbpej/dkyCgvgTkv8IY8XFCERb2q35oUuyMOcIHu /06d+IS+gNkIIH4r97ufVjmmLL+/4QVzLCiAok5pnhZzSxu5XA69oKNbKZBpgAxyHu5L lqMw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id x17-20020a17090a789100b002801dcaf6a9si7639972pjk.64.2023.11.27.14.07.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Nov 2023 14:07:19 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 8496180A6EC8; Mon, 27 Nov 2023 14:07:13 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233528AbjK0WG4 (ORCPT + 99 others); Mon, 27 Nov 2023 17:06:56 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55208 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233348AbjK0WGz (ORCPT ); Mon, 27 Nov 2023 17:06:55 -0500 Received: from pidgin.makrotopia.org (pidgin.makrotopia.org [185.142.180.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0FDD21AE for ; Mon, 27 Nov 2023 14:06:59 -0800 (PST) Received: from local by pidgin.makrotopia.org with esmtpsa (TLS1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.96.2) (envelope-from ) id 1r7jkh-0001Ad-1s; Mon, 27 Nov 2023 22:06:52 +0000 Date: Mon, 27 Nov 2023 22:06:50 +0000 From: Daniel Golle To: Richard Weinberger Cc: Miquel Raynal , Vignesh Raghavendra , Artem Bityutskiy , linux-mtd , linux-kernel , John Crispin Subject: Re: [PATCH] ubi: don't decrease ubi->ref_count on detach error Message-ID: References: <9857609999c5b7196417474938a7a09892cd1612.1701104870.git.daniel@makrotopia.org> <771902199.32600.1701116758852.JavaMail.zimbra@nod.at> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <771902199.32600.1701116758852.JavaMail.zimbra@nod.at> X-Spam-Status: No, score=-0.8 required=5.0 tests=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 howler.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 (howler.vger.email [0.0.0.0]); Mon, 27 Nov 2023 14:07:13 -0800 (PST) Hi Richard, On Mon, Nov 27, 2023 at 09:25:58PM +0100, Richard Weinberger wrote: > > If attempting to detach a UBI device while it is still busy, detaching > > is refused. However, the reference counter is still being decreased > > despite the error. Rework detach function to only decrease the refcnt > > once all conditions for detachment are met. > > > > Fixes: cdfa788acd13 ("UBI: prepare attach and detach functions") > > Signed-off-by: Daniel Golle > > Good catch! Did you find this by review or while testing? I was working on simplifying the NVMEM-on-UBI code which includes attaching UBI via MTD notifiers. You and others had rightously criticized the sketchy situation of the 'remove' handler which has now lead me to rework that part of my patches, which made me end up looking at the ref_count logic and error path at some point it popped into my eyes that this can't be right. > > > --- > > drivers/mtd/ubi/build.c | 6 +++--- > > 1 file changed, 3 insertions(+), 3 deletions(-) > > > > diff --git a/drivers/mtd/ubi/build.c b/drivers/mtd/ubi/build.c > > index 7d4ff1193db6f..f47987ee9a31b 100644 > > --- a/drivers/mtd/ubi/build.c > > +++ b/drivers/mtd/ubi/build.c > > @@ -1099,16 +1099,16 @@ int ubi_detach_mtd_dev(int ubi_num, int anyway) > > > > spin_lock(&ubi_devices_lock); > > put_device(&ubi->dev); > > - ubi->ref_count -= 1; > > - if (ubi->ref_count) { > > + if (ubi->ref_count > 1) { > > Is there a specific reason why you have modified the check to test only > for ref_count being positive? My idea was to really change only what I meant to change and make that change the least intrusive possible. > If rec_counts turns negative, due to a bug, we could still stop it here. ... here and in every other pleace where we touch it? Adding new sanity checks to the code probably doesn't hurt but goes beyond the scope of fixing this very bug, so I'll only do it there for now. > > > if (!anyway) { > > spin_unlock(&ubi_devices_lock); > > return -EBUSY; > > } > > /* This may only happen if there is a bug */ > > ubi_err(ubi, "%s reference count %d, destroy anyway", > > - ubi->ubi_name, ubi->ref_count); > > + ubi->ubi_name, ubi->ref_count - 1); > > } > > + ubi->ref_count -= 1; > > Please add there an ubi_asert() which tests whether ref_count is really zero. > ...just to be more bullet proof. That makes sense, now that it became clear that ref_count wasn't trustable for more than a decade, let's better make sure it is now.