From patchwork Mon Jul 31 08:20:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Miquel Raynal X-Patchwork-Id: 1814811 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=PbunTeeE; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=bootlin.com header.i=@bootlin.com header.a=rsa-sha256 header.s=gm1 header.b=cJLlsIhY; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4RDrmQ0DxVz20G9 for ; Mon, 31 Jul 2023 18:21:10 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=9jag8jIfshOgYHUGISzVRH8ZH8QQlkyLvW+zHY9zjFU=; b=PbunTeeEgpF+Rh by9lJ20lAkLfR8eTqk8+gsc+PveBYjWaTtVJGKZdZo2Vq1CEa4bzsFfkkhvM4XdXVC27n/IJEZ3PL QYHN+1TNU4h8nyxd42/iFmD+l7jyE7LghhvKvPEhesnF2nSC50jwDdbPaunzRgdlq3T6YKUhZKBnF UVtgm+umcSHy7+nydvR+T50ewxdiPCbfTLZySAXGgjKL0+uIKVV3f1GgiILcchLvo60Bu6g6JWj/w rKR9v/mgzqHCi02Evr1riZXLhpOg1w4gYWSaSfsr3AS3HAMZDlInOGwBj2vbov/omh9/XLq6KXTk2 m/O+tMk83Eob6Lu//0Lg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qQO8t-00EVzK-2m; Mon, 31 Jul 2023 08:20:39 +0000 Received: from relay6-d.mail.gandi.net ([2001:4b98:dc4:8::226]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qQO8r-00EVxs-00 for linux-mtd@lists.infradead.org; Mon, 31 Jul 2023 08:20:38 +0000 Received: by mail.gandi.net (Postfix) with ESMTPSA id C1122C0005; Mon, 31 Jul 2023 08:20:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1690791632; 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; bh=Vh0LBuc712RYG6dXLPZbJhzecgDZyKrFvEGeieJODz0=; b=cJLlsIhY5UoVr3HfNGEn9ugyK2lbtgNprRKyO2tQ65z6IclwFQeEtlcoDC4wbnR6QTW04D msCMk48Vz0V+BCIop77ArW6lEYVWFAmyuRMmQj7vXliLABG5FWBVgMGDpWjJJCk/gxeAn9 hCNvkm4/M8IEMxjVc7QxeAlNgF5Qi3jCjyi1KEKfeEe9lrIfScLMdO0sf3zipHelcqH1LF nlQKnVkqwMv9ioKUR66EF+rBkQ94o2n9CwVr2ISw6yhjYvm01dXtO2Qt4TrVOF5BOa0LqB mRCmIAV24hM9mY5B6Zukxp6rB4kNFbBpqaX+GV5w2ta9gWoUc+E3LxHRyAeWSA== From: Miquel Raynal To: Richard Weinberger , Vignesh Raghavendra , Tudor Ambarus , Pratyush Yadav , Michael Walle , Cc: Miquel Raynal , Tomas Winkler , Alexander Usyskin , Zhang Xiaoxu Subject: [PATCH] mtd: Fix refcounting with MTD_PARTITIONED_MASTER Date: Mon, 31 Jul 2023 10:20:29 +0200 Message-Id: <20230731082029.762385-1-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-GND-Sasl: miquel.raynal@bootlin.com X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230731_012037_326440_0A2A37F2 X-CRM114-Status: GOOD ( 15.41 ) X-Spam-Score: -0.9 (/) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: The logic is way too convoluted, let's clean the kref_get/put section to clarify what this block does, hopefully solving the refcounting issue when using CONFIG_MTD_PARTITIONED_MASTER at the same time [...] Content analysis details: (-0.9 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at https://www.dnswl.org/, low trust [2001:4b98:dc4:8:0:0:0:226 listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record -0.0 SPF_HELO_PASS SPF: HELO matches SPF record -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Linux MTD discussion mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-mtd" Errors-To: linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org The logic is way too convoluted, let's clean the kref_get/put section to clarify what this block does, hopefully solving the refcounting issue when using CONFIG_MTD_PARTITIONED_MASTER at the same time: - Iterate through all the parent mtd devices - Grab a reference over them all but the master - Only grab the master whith CONFIG_MTD_PARTITIONED_MASTER Same logic must apply in the put path, otherwise it would be broken. Cc: Tomas Winkler Cc: Alexander Usyskin Cc: Zhang Xiaoxu Fixes: 19bfa9ebebb5 ("mtd: use refcount to prevent corruption") Signed-off-by: Miquel Raynal --- Hello, Alexander, Zhang, please test this version, it is close to what Zhang produced while not strictly identical. This compile-tested only, please check on you side whether it fixes the refcounting issue with and without PARTITIONED_MASTER, as well as with Kasan. Alexander, maybe there is something else to fix, in all cases the logic here was broken so let's start by this one and see if we need something else on top. Thanks, Miquèl --- drivers/mtd/mtdcore.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c index 2466ea466466..9b90e1be43e7 100644 --- a/drivers/mtd/mtdcore.c +++ b/drivers/mtd/mtdcore.c @@ -1241,14 +1241,15 @@ int __get_mtd_device(struct mtd_info *mtd) return -ENODEV; } - kref_get(&mtd->refcnt); - - while (mtd->parent) { - if (IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER) || mtd->parent != master) - kref_get(&mtd->parent->refcnt); + while (mtd) { + if (mtd != master) + kref_get(&mtd->refcnt); mtd = mtd->parent; } + if (IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER)) + kref_get(&master->refcnt); + return 0; } EXPORT_SYMBOL_GPL(__get_mtd_device); @@ -1332,11 +1333,10 @@ void __put_mtd_device(struct mtd_info *mtd) { struct mtd_info *master = mtd_get_master(mtd); - while (mtd != master) { - struct mtd_info *parent = mtd->parent; - - kref_put(&mtd->refcnt, mtd_device_release); - mtd = parent; + while (mtd) { + if (mtd != master) + kref_put(&mtd->refcnt, mtd_device_release); + mtd = mtd->parent; } if (IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER))