From patchwork Tue Nov 28 00:45:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Golle X-Patchwork-Id: 170515 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce62:0:b0:403:3b70:6f57 with SMTP id o2csp3585139vqx; Mon, 27 Nov 2023 16:47:25 -0800 (PST) X-Google-Smtp-Source: AGHT+IG4Fh9C4kju/IiZsryrUzzBNclK7jZNMn8EbbmTBbembKfQMetOWJ17HdJvsAEFPRhEdvzJ X-Received: by 2002:a17:90a:1910:b0:281:d55:6fe8 with SMTP id 16-20020a17090a191000b002810d556fe8mr10796703pjg.24.1701132445459; Mon, 27 Nov 2023 16:47:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701132445; cv=none; d=google.com; s=arc-20160816; b=FFnqVD/+eSuXne2IZ4H6fg3lWdgwJF5FmxEJU1Nfik+eCx5QslOkJiDL1usT9HytCl n/jDnpY9RP3f6T0NJjwI/vJQVy0z5fg5QVxKQ/YWhQtdcpOdfxaBProEshZTGHep69q1 R8MX0sSyN1yn0cg8UPcyufvxp11ourrZ9kWZqXsbTLNtMWM945eAEPtP6wGbQJ3Oi23G qTFA7nwGLrqACWRv5I91NA9fvCMl5VJsSI6oxfMpFLPPq8ovCaqVuwTf41l66S6NP1g/ nGdn8mdD+6WbJ0BP7QbS2vvIip0rdcWT0pbsyN0v5+NOrfku4hOKFY633MZWFN2aoxy8 KkEQ== 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 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id q3-20020a631f43000000b0057942bfab4dsi10559337pgm.395.2023.11.27.16.47.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Nov 2023 16:47:25 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 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 0A941809AFBB; Mon, 27 Nov 2023 16:46:16 -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-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:16 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1783737876802653873 X-GMAIL-MSGID: 1783766655325608477 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 Reviewed-by: Zhihao Cheng --- 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);