Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp3512698rdh; Mon, 27 Nov 2023 16:47:26 -0800 (PST) X-Google-Smtp-Source: AGHT+IHHB3B6chIRz8YnuaGs7UsJfE6yQMFKscdKt6qVn86pgLTsmuz0OyN2WEWwchmrucy1S/tC X-Received: by 2002:a17:903:246:b0:1c5:d063:b70e with SMTP id j6-20020a170903024600b001c5d063b70emr12580400plh.53.1701132446258; Mon, 27 Nov 2023 16:47:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701132446; cv=none; d=google.com; s=arc-20160816; b=pCgDUcVi1LQVMfyk8Fgq60jbs+zG3voI0SfbRi3tAN7SeG/b7K8oxUgy1pWYaytQhQ gT+TE30/rQS4v8X13PPUB6opfu3i0b6XANsC+ZNec4W4QcJpUcBgHsf+YbnmFSPjRERe TgN0/WY3437hdTZNCRk/uF4yK4FiK0u51Ohir9VcF89retcm4/WuxTuKg1cfUzgqnwPV qZedthqjx+ztigKFTQ9MAp9eIkkUkFs3sU4S4ixK5uL6D4NzKhnWGfYqGHBnLUqoIlax ajxYa0jhu+W5oUmP0DRc5mes3z9VJVH7vj1OMBOpv94BwAc5L30aQoVYF2voKX8WDUAw 5KuA== 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:to:from:date; bh=p7X6c+hcqP8eLCcJypd0BOadb9DKCBzxgIKRzHgTu78=; fh=8gdEQ0XRL0jyzAV1xDlobvbTetvdnn25GkzTV0+HoHU=; b=RoWPCpsiJZXuDOFJ63iq2dHDRcxsl4CbRz+HmJYyTqsL+pzyNVL4Vu9t4cQjmON5hz x7IKCpKeYxKJ/1Ts9clndtKFyV0OArtoByoakAFjLfgjs9Z2z7mnWCjuag6/13MjuOrm MOoZWnaYB9gxCeQ67eAx+KmnxAR2COjFswyMRv8prKXqSjT+6lUoXAYgyi3jl6TTIhjl ZoazSNoGY7BMFxIp1v8HJZX3lL7u+zbdZH69P38zj6IHZrEK8Hg+Nc4okUjhwecGW4jQ RHraeVtcSVgjetrh8hgFGVIJQTCs1BsLKXHuf8MhMi9212T6EvrFQtkbw/eNw/hQAfNA QEjA== 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 d7-20020a170902cec700b001b222cd9826si11620910plg.349.2023.11.27.16.47.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Nov 2023 16:47:26 -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 8715B8247765; Mon, 27 Nov 2023 16:46:30 -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 S231391AbjK1AqF (ORCPT + 99 others); Mon, 27 Nov 2023 19:46:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51384 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229637AbjK1AqE (ORCPT ); Mon, 27 Nov 2023 19:46:04 -0500 Received: from pidgin.makrotopia.org (pidgin.makrotopia.org [185.142.180.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2577E1B1 for ; Mon, 27 Nov 2023 16:46:06 -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 1r7mEg-0001sM-2y; Tue, 28 Nov 2023 00:46:00 +0000 Date: Tue, 28 Nov 2023 00:45:54 +0000 From: Daniel Golle To: Richard Weinberger , Miquel Raynal , Vignesh Raghavendra , Artem Bityutskiy , linux-mtd@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH v2] ubi: don't decrease ubi->ref_count on detach error Message-ID: <8d48839a1bf928886e86de8fdc09dc0676fc187f.1701132215.git.daniel@makrotopia.org> References: <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 16:46:31 -0800 (PST) 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 reference counter once all conditions for detachment are met. Fixes: cdfa788acd13 ("UBI: prepare attach and detach functions") Signed-off-by: Daniel Golle --- v2: also catch negative ref_count and add ubi_assert as suggested drivers/mtd/ubi/build.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/drivers/mtd/ubi/build.c b/drivers/mtd/ubi/build.c index 7d4ff1193db6f..2316f6014c7f5 100644 --- a/drivers/mtd/ubi/build.c +++ b/drivers/mtd/ubi/build.c @@ -1099,16 +1099,17 @@ 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) { 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; + ubi_assert(ubi->ref_count == 0); ubi_devices[ubi_num] = NULL; spin_unlock(&ubi_devices_lock); -- 2.43.0