From patchwork Sat Aug 5 16:23:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Golle X-Patchwork-Id: 131508 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp549751vqr; Sat, 5 Aug 2023 09:59:50 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGLeWzSm9h5+GW0zl4ebiLEzz2lPC9MeWJpotjO0MbTEtfl2GV3Dh9YM8DRI87o//rIGRmy X-Received: by 2002:a05:6a20:8f24:b0:13f:bc16:c34e with SMTP id b36-20020a056a208f2400b0013fbc16c34emr4419132pzk.41.1691254789812; Sat, 05 Aug 2023 09:59:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691254789; cv=none; d=google.com; s=arc-20160816; b=1Ldx5F4UkjSoMeWwLmfRwPffIxmb2/m5KA8x68XeinTv5EGD1j20XDqQjVWS338mht YGwPvzZZJv/rG/GzUWZmSJt3TZhHiIhqWKElNhABGAJe0XVKg/dZna5VOswC0ykAfjfG Jn5xdmCBUGp0kMW/mjnN8yW8AKt2ERuPX3PwdI3cJBjggODgxbuHtMEBm0yeIPotargH giOuPALgBCJLYk/YuRWw0gI1jRBz1lVIe435Y0OjCRDK86nWQaDgsM8gih5pVlEs7PTj YQ59fY2qP4VCpokPLG67mbsw+Amf5FeD1m3isvrhb+rSR1Cd5RRi+qIcjpqFBbskfdP/ XXzw== 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=X/V71hWhLQJvehOt1f8G0nQ4xIL80C1PJus8vSR+UuU=; fh=WrF5P4rt5GKLKZXFPrBfHwP5VFIZG8GPN/h4vsvn4l4=; b=O+CpmZI3qTQtf3IVUC7tqc4+FE4tOZR2dynwSVarfmjOGYuJozGxhQOQE20cNRawie OTwDAN4R9N4mDK33ukGJxCK/TBy+qNpAsWg12L6n8JhoytqxcVvMCYiWQqxFcX5YoGoJ ufV76hQJiHUOWzcTRMdfkaEkA82tSoiW5F9aOl50p+sSv7l6xC8vj78UoF4hZrnasTkI QdknsbvX4L0mMYGl36H/7u3VxQLGxw+jypm08FsuKP5QyBZPnYGvxpJfJoNWC7hMQQeV gfzsS8Vtvv/OJ809O3qHd1nNKrOEu8wYRbIBxA2L6PU/DO+EmBLNAXBFhb8wPrC8iK4j P9gA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z16-20020a056a001d9000b0067f69d10241si3306787pfw.176.2023.08.05.09.59.37; Sat, 05 Aug 2023 09:59:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229456AbjHEQX0 (ORCPT + 99 others); Sat, 5 Aug 2023 12:23:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37568 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229627AbjHEQXY (ORCPT ); Sat, 5 Aug 2023 12:23:24 -0400 Received: from pidgin.makrotopia.org (pidgin.makrotopia.org [185.142.180.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D9CC1103 for ; Sat, 5 Aug 2023 09:23:23 -0700 (PDT) Received: from local by pidgin.makrotopia.org with esmtpsa (TLS1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.96) (envelope-from ) id 1qSK3i-0000dH-1f; Sat, 05 Aug 2023 16:23:18 +0000 Date: Sat, 5 Aug 2023 17:23:11 +0100 From: Daniel Golle To: Randy Dunlap , Richard Weinberger , Miquel Raynal , Vignesh Raghavendra , linux-kernel@vger.kernel.org, linux-mtd@lists.infradead.org Subject: [PATCH v2 1/7] mtd: ubi: improve Kconfig formatting Message-ID: <62240f36e4838e925e32add167a2d7d201843b0d.1691252291.git.daniel@makrotopia.org> References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773409182464670102 X-GMAIL-MSGID: 1773409182464670102 Kconfig help text should be indented with one tab + 2 spaces according to coding-style.rst. Apply this rule in drivers/mtd/ubi/Kconfig Signed-off-by: Daniel Golle --- drivers/mtd/ubi/Kconfig | 60 ++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/drivers/mtd/ubi/Kconfig b/drivers/mtd/ubi/Kconfig index 2ed77b7b3fcb5..184118f9a2969 100644 --- a/drivers/mtd/ubi/Kconfig +++ b/drivers/mtd/ubi/Kconfig @@ -61,47 +61,47 @@ config MTD_UBI_FASTMAP bool "UBI Fastmap (Experimental feature)" default n help - Important: this feature is experimental so far and the on-flash - format for fastmap may change in the next kernel versions - - Fastmap is a mechanism which allows attaching an UBI device - in nearly constant time. Instead of scanning the whole MTD device it - only has to locate a checkpoint (called fastmap) on the device. - The on-flash fastmap contains all information needed to attach - the device. Using fastmap makes only sense on large devices where - attaching by scanning takes long. UBI will not automatically install - a fastmap on old images, but you can set the UBI module parameter - fm_autoconvert to 1 if you want so. Please note that fastmap-enabled - images are still usable with UBI implementations without - fastmap support. On typical flash devices the whole fastmap fits - into one PEB. UBI will reserve PEBs to hold two fastmaps. - - If in doubt, say "N". + Important: this feature is experimental so far and the on-flash + format for fastmap may change in the next kernel versions + + Fastmap is a mechanism which allows attaching an UBI device + in nearly constant time. Instead of scanning the whole MTD device it + only has to locate a checkpoint (called fastmap) on the device. + The on-flash fastmap contains all information needed to attach + the device. Using fastmap makes only sense on large devices where + attaching by scanning takes long. UBI will not automatically install + a fastmap on old images, but you can set the UBI module parameter + fm_autoconvert to 1 if you want so. Please note that fastmap-enabled + images are still usable with UBI implementations without + fastmap support. On typical flash devices the whole fastmap fits + into one PEB. UBI will reserve PEBs to hold two fastmaps. + + If in doubt, say "N". config MTD_UBI_GLUEBI tristate "MTD devices emulation driver (gluebi)" help - This option enables gluebi - an additional driver which emulates MTD - devices on top of UBI volumes: for each UBI volumes an MTD device is - created, and all I/O to this MTD device is redirected to the UBI - volume. This is handy to make MTD-oriented software (like JFFS2) - work on top of UBI. Do not enable this unless you use legacy - software. + This option enables gluebi - an additional driver which emulates MTD + devices on top of UBI volumes: for each UBI volumes an MTD device is + created, and all I/O to this MTD device is redirected to the UBI + volume. This is handy to make MTD-oriented software (like JFFS2) + work on top of UBI. Do not enable this unless you use legacy + software. config MTD_UBI_BLOCK bool "Read-only block devices on top of UBI volumes" default n depends on BLOCK help - This option enables read-only UBI block devices support. UBI block - devices will be layered on top of UBI volumes, which means that the - UBI driver will transparently handle things like bad eraseblocks and - bit-flips. You can put any block-oriented file system on top of UBI - volumes in read-only mode (e.g., ext4), but it is probably most - practical for read-only file systems, like squashfs. + This option enables read-only UBI block devices support. UBI block + devices will be layered on top of UBI volumes, which means that the + UBI driver will transparently handle things like bad eraseblocks and + bit-flips. You can put any block-oriented file system on top of UBI + volumes in read-only mode (e.g., ext4), but it is probably most + practical for read-only file systems, like squashfs. - When selected, this feature will be built in the UBI driver. + When selected, this feature will be built in the UBI driver. - If in doubt, say "N". + If in doubt, say "N". endif # MTD_UBI From patchwork Sat Aug 5 16:23:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Golle X-Patchwork-Id: 131511 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp554736vqr; Sat, 5 Aug 2023 10:08:48 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHnunZw1dbcwmhtE9I75at5tELVPXgN0ZqEbi32iPsPhS2umOLhfoW7g04HxH2yUGdSuEzH X-Received: by 2002:a17:907:2723:b0:99c:1ed1:65b2 with SMTP id d3-20020a170907272300b0099c1ed165b2mr3742715ejl.33.1691255327877; Sat, 05 Aug 2023 10:08:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691255327; cv=none; d=google.com; s=arc-20160816; b=C9NNcCRgTc5gtfZrcnsRRt1m/r7Ej2uPj7VosQJjT1cuxAHFT0azNtuusW0QbpdCGo BlcFlnrgy4Nk9MnaVWBojVO5n463TUr84eJSz99MXLc/2ub6TN6uuM9735zDgxaH5KEy aA/TU0C9QMwveGIH8t2G5A/lG+ulGBIGnc3I18kHsVGFD84EJfBYJPkcDq5k+ZEYD9fK KNUCxlZ+jiQJ3s599uz9soTPSht/u4KAFLiKpUE0dw9/hp4ZfbyEQedF7rAkUsiyE/ax KFP6dagZPayUVgNk/FdVFriit/AKm1bNcVSl0WlHYVnlAkCR0F6plsgUyMLGV1BxhA7S fVQw== 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=/17mzegf8JZHibMrzQvxR5LoTlQRR3yQ0c1DUaT3IG0=; fh=WrF5P4rt5GKLKZXFPrBfHwP5VFIZG8GPN/h4vsvn4l4=; b=OhoeIhpnTiCHMO5gC3Pw4Haoh1LYKZno9YeErgFAfGIzQ0GqLf1oYyMzSEiJKMLIDu 6Jru0KcTvSMFnDDLjmnmu8jIS2Ur8kOL0uVP77aaKDfiNWH6xav8lMIvoA3VlhDQfX+D 3jkbId1D5I/JOYcLfTR5NSwhvisj++1mHRkhkDMFEFHcg5rdBPlDUmRjOHpdGVIeaevI toEEdEwYHp4skYnOSBDHcz15IQJS0mWFuDMarRFcii5fAS0BzKsXAldVEJVwMsz6wAI2 s4WtRXP0F/t3UZJxReQI40JCO0kszuDHhs7Dfuof7Ah8ZvK+ofD8/++q84b6GH2NOC6B etBw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j10-20020a170906094a00b0099bcae257e3si3265911ejd.832.2023.08.05.10.08.23; Sat, 05 Aug 2023 10:08:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229817AbjHEQXp (ORCPT + 99 others); Sat, 5 Aug 2023 12:23:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37974 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229771AbjHEQXn (ORCPT ); Sat, 5 Aug 2023 12:23:43 -0400 Received: from pidgin.makrotopia.org (pidgin.makrotopia.org [185.142.180.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 31F304206 for ; Sat, 5 Aug 2023 09:23:37 -0700 (PDT) Received: from local by pidgin.makrotopia.org with esmtpsa (TLS1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.96) (envelope-from ) id 1qSK3x-0000dT-10; Sat, 05 Aug 2023 16:23:33 +0000 Date: Sat, 5 Aug 2023 17:23:25 +0100 From: Daniel Golle To: Randy Dunlap , Richard Weinberger , Miquel Raynal , Vignesh Raghavendra , linux-kernel@vger.kernel.org, linux-mtd@lists.infradead.org Subject: [PATCH v2 2/7] mtd: ubi: block: don't return on error when removing Message-ID: References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773409746909304469 X-GMAIL-MSGID: 1773409746909304469 There is no point on returning the error from ubiblock_remove in case it is being called due to a volume removal event -- the volume is gone, we should destroy and remove the ubiblock device no matter what. Introduce new boolean parameter 'force' to tell ubiblock_remove to go on even in case the ubiblock device is still busy. Use that new option when calling ubiblock_remove due to a UBI_VOLUME_REMOVED event. Signed-off-by: Daniel Golle --- drivers/mtd/ubi/block.c | 6 +++--- drivers/mtd/ubi/cdev.c | 2 +- drivers/mtd/ubi/ubi.h | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/drivers/mtd/ubi/block.c b/drivers/mtd/ubi/block.c index 437c5b83ffe51..69fa6fecb8494 100644 --- a/drivers/mtd/ubi/block.c +++ b/drivers/mtd/ubi/block.c @@ -456,7 +456,7 @@ static void ubiblock_cleanup(struct ubiblock *dev) idr_remove(&ubiblock_minor_idr, dev->gd->first_minor); } -int ubiblock_remove(struct ubi_volume_info *vi) +int ubiblock_remove(struct ubi_volume_info *vi, bool force) { struct ubiblock *dev; int ret; @@ -470,7 +470,7 @@ int ubiblock_remove(struct ubi_volume_info *vi) /* Found a device, let's lock it so we can check if it's busy */ mutex_lock(&dev->dev_mutex); - if (dev->refcnt > 0) { + if (dev->refcnt > 0 && !force) { ret = -EBUSY; goto out_unlock_dev; } @@ -545,7 +545,7 @@ static int ubiblock_notify(struct notifier_block *nb, */ break; case UBI_VOLUME_REMOVED: - ubiblock_remove(&nt->vi); + ubiblock_remove(&nt->vi, true); break; case UBI_VOLUME_RESIZED: ubiblock_resize(&nt->vi); diff --git a/drivers/mtd/ubi/cdev.c b/drivers/mtd/ubi/cdev.c index f43430b9c1e65..bb55e863dd296 100644 --- a/drivers/mtd/ubi/cdev.c +++ b/drivers/mtd/ubi/cdev.c @@ -572,7 +572,7 @@ static long vol_cdev_ioctl(struct file *file, unsigned int cmd, struct ubi_volume_info vi; ubi_get_volume_info(desc, &vi); - err = ubiblock_remove(&vi); + err = ubiblock_remove(&vi, false); break; } diff --git a/drivers/mtd/ubi/ubi.h b/drivers/mtd/ubi/ubi.h index c8f1bd4fa1008..44c0eeaf1e1b0 100644 --- a/drivers/mtd/ubi/ubi.h +++ b/drivers/mtd/ubi/ubi.h @@ -979,7 +979,7 @@ static inline void ubi_fastmap_destroy_checkmap(struct ubi_volume *vol) {} int ubiblock_init(void); void ubiblock_exit(void); int ubiblock_create(struct ubi_volume_info *vi); -int ubiblock_remove(struct ubi_volume_info *vi); +int ubiblock_remove(struct ubi_volume_info *vi, bool force); #else static inline int ubiblock_init(void) { return 0; } static inline void ubiblock_exit(void) {} @@ -987,7 +987,7 @@ static inline int ubiblock_create(struct ubi_volume_info *vi) { return -ENOSYS; } -static inline int ubiblock_remove(struct ubi_volume_info *vi) +static inline int ubiblock_remove(struct ubi_volume_info *vi, bool force) { return -ENOSYS; } From patchwork Sat Aug 5 16:25:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Golle X-Patchwork-Id: 131513 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp567973vqr; Sat, 5 Aug 2023 10:42:51 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGEi06SYaKdYVdZWzgn0avMenq4ntV4tmgZaON7Zk36U/AdimsIB8kH5tTFFWbAq4TXnhq/ X-Received: by 2002:a17:907:7708:b0:982:45ca:ac06 with SMTP id kw8-20020a170907770800b0098245caac06mr4205756ejc.60.1691257371211; Sat, 05 Aug 2023 10:42:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691257371; cv=none; d=google.com; s=arc-20160816; b=njtQpTSPN6hG+6l/S8dQBBPdtJNJUcV1ZE8CmZ7jsDMIfAIZvUI6CO5gdt+XoSb2oW soXEnBidRAPo3TE2ppdxGULJj4v0wRgEhoIB70uZ+h21olDoSEd7EmxHwKp4ZKH3eU7j zMFhpd1a6Xc2rq/bxnoYGY9VGqycRQHMfMGM8wq1cfxQCBtZiOxqEIXZRLpCOxQ0xRXt NkfnTMk2Dmnq5i2Wz251lWzNfLddSQA8cZGVexOesJ8Ii83AEfagGvv1Rc5ay3Axv3sA ZtV0GDJTfJM5n6Yx/0MrdMBK2/HXHs6RlyFHDjar7K1+GSGpk0T8NlOjbrcELnj+xjkG o/WQ== 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=gyt4xAsIrq5txTp5OjpxkNoVi3HwSGUGngOZAnCrLkE=; fh=WrF5P4rt5GKLKZXFPrBfHwP5VFIZG8GPN/h4vsvn4l4=; b=Lk/VbbA56BTS/Kv9LMw+9EHmcFmRtNHbOkTjVRCOP3L/X8pTCV0Y+j5wxjWZnfLooi feHS0hYri4IYstB1tpBBzwbWsMAoUdfm8/EqxbucCoUzBraN/oO3FGhVtkIzABQdjKLW liY5DCeq2ECLD0tB/mk7Go9RLkyJApFbqYhsWUYtBczM4sBSmS9r4Zi1whKLLzCvbdA0 xNSsx/N3FAAzkdoQlOLF2TRwDOUEiGbzcuDoAL/5z7tjevUIzQGTdX41uTUImGOJOCkB 20MaX5VUfTITh//65OxPFXszJPQnHsnhbVifk6AmgJ8V5QelGS7uJmGokXHv/TsFEhgh D7vw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k15-20020a1709065fcf00b0099cb60f1905si1419229ejv.263.2023.08.05.10.42.25; Sat, 05 Aug 2023 10:42:51 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229514AbjHEQZw (ORCPT + 99 others); Sat, 5 Aug 2023 12:25:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38932 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229436AbjHEQZv (ORCPT ); Sat, 5 Aug 2023 12:25:51 -0400 Received: from pidgin.makrotopia.org (pidgin.makrotopia.org [185.142.180.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 85FCECD for ; Sat, 5 Aug 2023 09:25:50 -0700 (PDT) Received: from local by pidgin.makrotopia.org with esmtpsa (TLS1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.96) (envelope-from ) id 1qSK66-0000eW-29; Sat, 05 Aug 2023 16:25:46 +0000 Date: Sat, 5 Aug 2023 17:25:39 +0100 From: Daniel Golle To: Randy Dunlap , Richard Weinberger , Miquel Raynal , Vignesh Raghavendra , linux-kernel@vger.kernel.org, linux-mtd@lists.infradead.org Subject: [PATCH v2 3/7] mtd: ubi: block: use notifier to create ubiblock from parameter Message-ID: References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773411889223622537 X-GMAIL-MSGID: 1773411889223622537 Use UBI_VOLUME_ADDED notification to create ubiblock device specified on kernel cmdline or module parameter. This makes thing more simple and has the advantage that ubiblock devices on volumes which are not present at the time the ubi module is probed will still be created. Suggested-by: Zhihao Cheng Signed-off-by: Daniel Golle --- drivers/mtd/ubi/block.c | 152 ++++++++++++++++++++++------------------ 1 file changed, 84 insertions(+), 68 deletions(-) diff --git a/drivers/mtd/ubi/block.c b/drivers/mtd/ubi/block.c index 69fa6fecb8494..e0618bbde3613 100644 --- a/drivers/mtd/ubi/block.c +++ b/drivers/mtd/ubi/block.c @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include @@ -65,10 +66,10 @@ struct ubiblock_pdu { }; /* Numbers of elements set in the @ubiblock_param array */ -static int ubiblock_devs __initdata; +static int ubiblock_devs; /* MTD devices specification parameters */ -static struct ubiblock_param ubiblock_param[UBIBLOCK_MAX_DEVICES] __initdata; +static struct ubiblock_param ubiblock_param[UBIBLOCK_MAX_DEVICES]; struct ubiblock { struct ubi_volume_desc *desc; @@ -532,6 +533,85 @@ static int ubiblock_resize(struct ubi_volume_info *vi) return 0; } +static bool +match_volume_desc(struct ubi_volume_info *vi, const char *name, int ubi_num, int vol_id) +{ + int err, len; + struct path path; + struct kstat stat; + + if (ubi_num == -1) { + /* No ubi num, name must be a vol device path */ + err = kern_path(name, LOOKUP_FOLLOW, &path); + if (err) + return false; + + err = vfs_getattr(&path, &stat, STATX_TYPE, AT_STATX_SYNC_AS_STAT); + path_put(&path); + if (err) + return false; + + if (!S_ISCHR(stat.mode)) + return false; + + if (vi->ubi_num != ubi_major2num(MAJOR(stat.rdev))) + return false; + + if (vi->vol_id != MINOR(stat.rdev) - 1) + return false; + + return true; + } + + if (vol_id == -1) { + if (vi->ubi_num != ubi_num) + return false; + + len = strnlen(name, UBI_VOL_NAME_MAX + 1); + if (len < 1 || vi->name_len != len) + return false; + + if (strcmp(name, vi->name)) + return false; + + return true; + } + + if (vi->ubi_num != ubi_num) + return false; + + if (vi->vol_id != vol_id) + return false; + + return true; +} + +static void +ubiblock_create_from_param(struct ubi_volume_info *vi) +{ + int i, ret = 0; + struct ubiblock_param *p; + + /* + * Iterate over ubiblock cmdline parameters. If a parameter matches the + * newly added volume create the ubiblock device for it. + */ + for (i = 0; i < ubiblock_devs; i++) { + p = &ubiblock_param[i]; + + if (!match_volume_desc(vi, p->name, p->ubi_num, p->vol_id)) + continue; + + ret = ubiblock_create(vi); + if (ret) { + pr_err( + "UBI: block: can't add '%s' volume on ubi%d_%d, err=%d\n", + vi->name, p->ubi_num, p->vol_id, ret); + } + break; + } +} + static int ubiblock_notify(struct notifier_block *nb, unsigned long notification_type, void *ns_ptr) { @@ -539,10 +619,7 @@ static int ubiblock_notify(struct notifier_block *nb, switch (notification_type) { case UBI_VOLUME_ADDED: - /* - * We want to enforce explicit block device creation for - * volumes, so when a volume is added we do nothing. - */ + ubiblock_create_from_param(&nt->vi); break; case UBI_VOLUME_REMOVED: ubiblock_remove(&nt->vi, true); @@ -568,56 +645,6 @@ static struct notifier_block ubiblock_notifier = { .notifier_call = ubiblock_notify, }; -static struct ubi_volume_desc * __init -open_volume_desc(const char *name, int ubi_num, int vol_id) -{ - if (ubi_num == -1) - /* No ubi num, name must be a vol device path */ - return ubi_open_volume_path(name, UBI_READONLY); - else if (vol_id == -1) - /* No vol_id, must be vol_name */ - return ubi_open_volume_nm(ubi_num, name, UBI_READONLY); - else - return ubi_open_volume(ubi_num, vol_id, UBI_READONLY); -} - -static void __init ubiblock_create_from_param(void) -{ - int i, ret = 0; - struct ubiblock_param *p; - struct ubi_volume_desc *desc; - struct ubi_volume_info vi; - - /* - * If there is an error creating one of the ubiblocks, continue on to - * create the following ubiblocks. This helps in a circumstance where - * the kernel command-line specifies multiple block devices and some - * may be broken, but we still want the working ones to come up. - */ - for (i = 0; i < ubiblock_devs; i++) { - p = &ubiblock_param[i]; - - desc = open_volume_desc(p->name, p->ubi_num, p->vol_id); - if (IS_ERR(desc)) { - pr_err( - "UBI: block: can't open volume on ubi%d_%d, err=%ld\n", - p->ubi_num, p->vol_id, PTR_ERR(desc)); - continue; - } - - ubi_get_volume_info(desc, &vi); - ubi_close_volume(desc); - - ret = ubiblock_create(&vi); - if (ret) { - pr_err( - "UBI: block: can't add '%s' volume on ubi%d_%d, err=%d\n", - vi.name, p->ubi_num, p->vol_id, ret); - continue; - } - } -} - static void ubiblock_remove_all(void) { struct ubiblock *next; @@ -643,18 +670,7 @@ int __init ubiblock_init(void) if (ubiblock_major < 0) return ubiblock_major; - /* - * Attach block devices from 'block=' module param. - * Even if one block device in the param list fails to come up, - * still allow the module to load and leave any others up. - */ - ubiblock_create_from_param(); - - /* - * Block devices are only created upon user requests, so we ignore - * existing volumes. - */ - ret = ubi_register_volume_notifier(&ubiblock_notifier, 1); + ret = ubi_register_volume_notifier(&ubiblock_notifier, 0); if (ret) goto err_unreg; return 0; From patchwork Sat Aug 5 16:25: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: 131505 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp548326vqr; Sat, 5 Aug 2023 09:55:49 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHDA6+yUSWsWVNemccmlQbjwJ2C5y0279n447T5FL6lnhTohjaxWBi3UH95bsP47Q5ctN5M X-Received: by 2002:a17:902:c44b:b0:1bc:2c83:f770 with SMTP id m11-20020a170902c44b00b001bc2c83f770mr4121828plm.45.1691254549144; Sat, 05 Aug 2023 09:55:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691254549; cv=none; d=google.com; s=arc-20160816; b=XrD1Wt+rENnVZuWojUpdRAxgOfjjPKQDmdGgoXCGgzCzQ/+BH7Q0yTkL6e6lbIq+bT vamlaAZnu4e8GJlVfL2D/DkHm6DpZeWdXQoq6DIS4zgubd3fgkC0G6AIp5y7NP5xFfq7 DIGElGRkIwUXIvf+3TwL6eXGRFpXIFcI38yFtBzK2tgkze0wE9DmmY+y+dbn/etNWSgy 0frf0r4j9m2E/Qmw1ZSTyaK1lVMKx7/jP6XQuW8LqtLwPhhgHWdGQRBsivozckHgrHw9 Mqw2coMck3izVXPg0oNbjl8BBm5gOgPQIHK/ir9Q6JEd4bCAS+LU0ujbOODDOTG0WLCA Ypyw== 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=p82vAH+RXovqt2uw8K4mlLa9xvc65TYjnJmLTkDdKsg=; fh=WrF5P4rt5GKLKZXFPrBfHwP5VFIZG8GPN/h4vsvn4l4=; b=xtCcdbRH42NNUgO4tvTmpA206DvsV80zpsGkgggVTvqQ8KbkoptdiVTogvoYT+33Nk VJOEfP7FFhlyz7K1f8ROYghl73raEGKg5M7vc7/jY9zof0QZbgJMXGwlGNoco3X21P2W 0lwb8sy9zcAJHnj71EUE1rT2gFGPxz3eQGSU11Bc5c8/WbBErmCivBOgiNcqlSyvdH+X 4EmGbk9ba3it5EP1Del02zrVgXVs7yI9LLV1GHdosL9p67bsk+/zXYdzKySn5CW5jP9r 7gr1u0kVhLRM2rqlsqgNe8zEUITUAZhbofoEheLCbeQyXemvU+S06Qzg1/QUbM3uJ3Sh HtHQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id kb4-20020a170903338400b001bb8df95094si3215271plb.509.2023.08.05.09.55.32; Sat, 05 Aug 2023 09:55:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229903AbjHEQ0M (ORCPT + 99 others); Sat, 5 Aug 2023 12:26:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39190 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229908AbjHEQ0J (ORCPT ); Sat, 5 Aug 2023 12:26:09 -0400 Received: from pidgin.makrotopia.org (pidgin.makrotopia.org [185.142.180.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 58759448B for ; Sat, 5 Aug 2023 09:26:05 -0700 (PDT) Received: from local by pidgin.makrotopia.org with esmtpsa (TLS1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.96) (envelope-from ) id 1qSK6L-0000eg-2L; Sat, 05 Aug 2023 16:26:01 +0000 Date: Sat, 5 Aug 2023 17:25:54 +0100 From: Daniel Golle To: Randy Dunlap , Richard Weinberger , Miquel Raynal , Vignesh Raghavendra , linux-kernel@vger.kernel.org, linux-mtd@lists.infradead.org Subject: [PATCH v2 4/7] mtd: ubi: attach MTD partition from device-tree Message-ID: <630223f0425a97f266892e1f123e298e143597bc.1691252291.git.daniel@makrotopia.org> References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773408930334329136 X-GMAIL-MSGID: 1773408930334329136 Split ubi_init() function into early function to be called by device_initcall() and keep cmdline attachment in late_initcall(). (when building ubi as module, both is still done in a single module_init() call) Register MTD notifier and attach MTD devices which are marked as compatible with 'linux,ubi' in OF device-tree when being added, detach UBI device from MTD device when it is being removed. For existing users this should not change anything besides automatic removal of (dead) UBI devices when their underlying MTD devices are already gone, e.g. in case of MTD driver module or (SPI) bus driver module being removed. For new users this opens up the option to attach UBI using device-tree which then happens early and in parallel with other drivers being probed which slightly reduces the total boot time. Attachment no longer happening late is also a requirement for other drivers to make use of UBI, e.g. drivers/nvmem/u-boot-env.c can now be extended to support U-Boot environment stored in UBI volumes. Signed-off-by: Daniel Golle --- drivers/mtd/ubi/block.c | 2 +- drivers/mtd/ubi/build.c | 153 +++++++++++++++++++++++++++++----------- drivers/mtd/ubi/cdev.c | 2 +- drivers/mtd/ubi/ubi.h | 2 +- 4 files changed, 115 insertions(+), 44 deletions(-) diff --git a/drivers/mtd/ubi/block.c b/drivers/mtd/ubi/block.c index e0618bbde3613..99b5f502c9dbc 100644 --- a/drivers/mtd/ubi/block.c +++ b/drivers/mtd/ubi/block.c @@ -470,7 +470,7 @@ int ubiblock_remove(struct ubi_volume_info *vi, bool force) } /* Found a device, let's lock it so we can check if it's busy */ - mutex_lock(&dev->dev_mutex); + mutex_lock_nested(&dev->dev_mutex, SINGLE_DEPTH_NESTING); if (dev->refcnt > 0 && !force) { ret = -EBUSY; goto out_unlock_dev; diff --git a/drivers/mtd/ubi/build.c b/drivers/mtd/ubi/build.c index 8b91a55ec0d28..c153373c13dab 100644 --- a/drivers/mtd/ubi/build.c +++ b/drivers/mtd/ubi/build.c @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include "ubi.h" @@ -1065,6 +1066,7 @@ int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num, * ubi_detach_mtd_dev - detach an MTD device. * @ubi_num: UBI device number to detach from * @anyway: detach MTD even if device reference count is not zero + * @have_lock: called by MTD notifier holding mtd_table_mutex * * This function destroys an UBI device number @ubi_num and detaches the * underlying MTD device. Returns zero in case of success and %-EBUSY if the @@ -1074,7 +1076,7 @@ int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num, * Note, the invocations of this function has to be serialized by the * @ubi_devices_mutex. */ -int ubi_detach_mtd_dev(int ubi_num, int anyway) +int ubi_detach_mtd_dev(int ubi_num, int anyway, bool have_lock) { struct ubi_device *ubi; @@ -1111,6 +1113,7 @@ int ubi_detach_mtd_dev(int ubi_num, int anyway) if (!ubi_dbg_chk_fastmap(ubi)) ubi_update_fastmap(ubi); #endif + /* * Before freeing anything, we have to stop the background thread to * prevent it from doing anything on this device while we are freeing. @@ -1130,7 +1133,11 @@ int ubi_detach_mtd_dev(int ubi_num, int anyway) vfree(ubi->peb_buf); vfree(ubi->fm_buf); ubi_msg(ubi, "mtd%d is detached", ubi->mtd->index); - put_mtd_device(ubi->mtd); + if (have_lock) + __put_mtd_device(ubi->mtd); + else + put_mtd_device(ubi->mtd); + put_device(&ubi->dev); return 0; } @@ -1207,43 +1214,51 @@ static struct mtd_info * __init open_mtd_device(const char *mtd_dev) return mtd; } -static int __init ubi_init(void) +static void ubi_notify_add(struct mtd_info *mtd) { - int err, i, k; + struct device_node *np = mtd_get_of_node(mtd); + int err; - /* Ensure that EC and VID headers have correct size */ - BUILD_BUG_ON(sizeof(struct ubi_ec_hdr) != 64); - BUILD_BUG_ON(sizeof(struct ubi_vid_hdr) != 64); + if (!of_device_is_compatible(np, "linux,ubi")) + return; - if (mtd_devs > UBI_MAX_DEVICES) { - pr_err("UBI error: too many MTD devices, maximum is %d\n", - UBI_MAX_DEVICES); - return -EINVAL; - } + /* + * we are already holding &mtd_table_mutex, but still need + * to bump refcount + */ + err = __get_mtd_device(mtd); + if (err) + return; - /* Create base sysfs directory and sysfs files */ - err = class_register(&ubi_class); + /* called while holding mtd_table_mutex */ + mutex_lock_nested(&ubi_devices_mutex, SINGLE_DEPTH_NESTING); + err = ubi_attach_mtd_dev(mtd, UBI_DEV_NUM_AUTO, 0, 0, false); + mutex_unlock(&ubi_devices_mutex); if (err < 0) - return err; + __put_mtd_device(mtd); +} - err = misc_register(&ubi_ctrl_cdev); - if (err) { - pr_err("UBI error: cannot register device\n"); - goto out; - } +static void ubi_notify_remove(struct mtd_info *mtd) +{ + int i; - ubi_wl_entry_slab = kmem_cache_create("ubi_wl_entry_slab", - sizeof(struct ubi_wl_entry), - 0, 0, NULL); - if (!ubi_wl_entry_slab) { - err = -ENOMEM; - goto out_dev_unreg; - } + /* called while holding mtd_table_mutex */ + mutex_lock_nested(&ubi_devices_mutex, SINGLE_DEPTH_NESTING); + for (i = 0; i < UBI_MAX_DEVICES; i++) + if (ubi_devices[i] && + ubi_devices[i]->mtd->index == mtd->index) + ubi_detach_mtd_dev(ubi_devices[i]->ubi_num, 1, true); + mutex_unlock(&ubi_devices_mutex); +} - err = ubi_debugfs_init(); - if (err) - goto out_slab; +static struct mtd_notifier ubi_mtd_notifier = { + .add = ubi_notify_add, + .remove = ubi_notify_remove, +}; +static int __init ubi_init_attach(void) +{ + int err, i, k; /* Attach MTD devices */ for (i = 0; i < mtd_devs; i++) { @@ -1291,25 +1306,79 @@ static int __init ubi_init(void) } } + return 0; + +out_detach: + for (k = 0; k < i; k++) + if (ubi_devices[k]) { + mutex_lock(&ubi_devices_mutex); + ubi_detach_mtd_dev(ubi_devices[k]->ubi_num, 1, false); + mutex_unlock(&ubi_devices_mutex); + } + return err; +} +#ifndef CONFIG_MTD_UBI_MODULE +late_initcall(ubi_init_attach); +#endif + +static int __init ubi_init(void) +{ + int err; + + /* Ensure that EC and VID headers have correct size */ + BUILD_BUG_ON(sizeof(struct ubi_ec_hdr) != 64); + BUILD_BUG_ON(sizeof(struct ubi_vid_hdr) != 64); + + if (mtd_devs > UBI_MAX_DEVICES) { + pr_err("UBI error: too many MTD devices, maximum is %d\n", + UBI_MAX_DEVICES); + return -EINVAL; + } + + /* Create base sysfs directory and sysfs files */ + err = class_register(&ubi_class); + if (err < 0) + return err; + + err = misc_register(&ubi_ctrl_cdev); + if (err) { + pr_err("UBI error: cannot register device\n"); + goto out; + } + + ubi_wl_entry_slab = kmem_cache_create("ubi_wl_entry_slab", + sizeof(struct ubi_wl_entry), + 0, 0, NULL); + if (!ubi_wl_entry_slab) { + err = -ENOMEM; + goto out_dev_unreg; + } + + err = ubi_debugfs_init(); + if (err) + goto out_slab; + err = ubiblock_init(); if (err) { pr_err("UBI error: block: cannot initialize, error %d\n", err); /* See comment above re-ubi_is_module(). */ if (ubi_is_module()) - goto out_detach; + goto out_slab; + } + + register_mtd_user(&ubi_mtd_notifier); + + if (ubi_is_module()) { + err = ubi_init_attach(); + if (err) + goto out_mtd_notifier; } return 0; -out_detach: - for (k = 0; k < i; k++) - if (ubi_devices[k]) { - mutex_lock(&ubi_devices_mutex); - ubi_detach_mtd_dev(ubi_devices[k]->ubi_num, 1); - mutex_unlock(&ubi_devices_mutex); - } - ubi_debugfs_exit(); +out_mtd_notifier: + unregister_mtd_user(&ubi_mtd_notifier); out_slab: kmem_cache_destroy(ubi_wl_entry_slab); out_dev_unreg: @@ -1319,18 +1388,20 @@ static int __init ubi_init(void) pr_err("UBI error: cannot initialize UBI, error %d\n", err); return err; } -late_initcall(ubi_init); +device_initcall(ubi_init); + static void __exit ubi_exit(void) { int i; ubiblock_exit(); + unregister_mtd_user(&ubi_mtd_notifier); for (i = 0; i < UBI_MAX_DEVICES; i++) if (ubi_devices[i]) { mutex_lock(&ubi_devices_mutex); - ubi_detach_mtd_dev(ubi_devices[i]->ubi_num, 1); + ubi_detach_mtd_dev(ubi_devices[i]->ubi_num, 1, false); mutex_unlock(&ubi_devices_mutex); } ubi_debugfs_exit(); diff --git a/drivers/mtd/ubi/cdev.c b/drivers/mtd/ubi/cdev.c index bb55e863dd296..0ba6aa6a2e11d 100644 --- a/drivers/mtd/ubi/cdev.c +++ b/drivers/mtd/ubi/cdev.c @@ -1065,7 +1065,7 @@ static long ctrl_cdev_ioctl(struct file *file, unsigned int cmd, } mutex_lock(&ubi_devices_mutex); - err = ubi_detach_mtd_dev(ubi_num, 0); + err = ubi_detach_mtd_dev(ubi_num, 0, false); mutex_unlock(&ubi_devices_mutex); break; } diff --git a/drivers/mtd/ubi/ubi.h b/drivers/mtd/ubi/ubi.h index 44c0eeaf1e1b0..54093858f3385 100644 --- a/drivers/mtd/ubi/ubi.h +++ b/drivers/mtd/ubi/ubi.h @@ -939,7 +939,7 @@ int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum, int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num, int vid_hdr_offset, int max_beb_per1024, bool disable_fm); -int ubi_detach_mtd_dev(int ubi_num, int anyway); +int ubi_detach_mtd_dev(int ubi_num, int anyway, bool have_lock); struct ubi_device *ubi_get_device(int ubi_num); void ubi_put_device(struct ubi_device *ubi); struct ubi_device *ubi_get_by_major(int major); From patchwork Sat Aug 5 16:26:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Golle X-Patchwork-Id: 131509 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp550218vqr; Sat, 5 Aug 2023 10:00:38 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEZhuh3tRLXdDpZMKz7RV7/O8BJoKCz/jy+OJEZQrLXttRczfQQfBYhdpJVjUeb0T9Ejy0r X-Received: by 2002:a17:90b:17cc:b0:25d:d224:9fb9 with SMTP id me12-20020a17090b17cc00b0025dd2249fb9mr3086572pjb.24.1691254838162; Sat, 05 Aug 2023 10:00:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691254838; cv=none; d=google.com; s=arc-20160816; b=Fs9Rt2m6ET95bbrzbG60fqy2yu0Go+Nq2BEATn+/Jczl9620/UX0Qyhvuz5ZnVlpMc kj+zM6947qamrDqVspX3+O6cylRpee5Pxdh9XHdq1mijyfuMjNEBlITB7cmW8cIBgWh8 WXyybqRz/dEmQst5D+7m/RjLXArUYt29VqVxpLcqLrytDaWLs+a+seAmnTmHq8OZk7uE My6C8cgiTRMJUr3eXgVXcz1+No2jXadLzDxnlfIL0bTmg+Ia74KfNhHqWCvZUvBouWIg hTuhfN907hj7xaHnDukQxT3WnKFwS9QSTtx3HHSag4Yoxr+hiGV9jFn24JLPAbyH3JIs oE/g== 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=2ZMFvnqg2YpdkSzv6vBNptDi787ZNGsdqaeBN9c5mX8=; fh=WrF5P4rt5GKLKZXFPrBfHwP5VFIZG8GPN/h4vsvn4l4=; b=fY5ALoldjxo5RC8OA6SNqQZuZGyjToDxOSpJYBzstI6a/yUXyNw+B70nk731IbTY2g m331kXJaHfasIN7Sat+ApmWKl8jDPVjBiV2kphUQ6xu1AK8LMJ7jy1ZlpqEYCwvkghfs ZAxHA02bzG5A39ae8gTJ2g9UZ0e6HbMp+nqTPVsgKB0TIgfkf7Y4u/kkszKBAMDgsJoK GSoXbjOURZ1Jg4r98AwG3H7UOljfySS+fqMggGx63xZ3WpdYe2mkEumt1GffojqYqShi cA4iLvHeu2etJi0/nQI2SVoT5xKWLuX+3AKsnvcps9Hr6AHGLBviO1zOT6DRXS4bgzKw l1Pw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id lw11-20020a17090b180b00b002685065230asi7351684pjb.37.2023.08.05.10.00.17; Sat, 05 Aug 2023 10:00:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229863AbjHEQ0b (ORCPT + 99 others); Sat, 5 Aug 2023 12:26:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39564 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229503AbjHEQ03 (ORCPT ); Sat, 5 Aug 2023 12:26:29 -0400 Received: from pidgin.makrotopia.org (pidgin.makrotopia.org [185.142.180.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 36D094499 for ; Sat, 5 Aug 2023 09:26:19 -0700 (PDT) Received: from local by pidgin.makrotopia.org with esmtpsa (TLS1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.96) (envelope-from ) id 1qSK6Z-0000f1-0O; Sat, 05 Aug 2023 16:26:15 +0000 Date: Sat, 5 Aug 2023 17:26:07 +0100 From: Daniel Golle To: Randy Dunlap , Richard Weinberger , Miquel Raynal , Vignesh Raghavendra , linux-kernel@vger.kernel.org, linux-mtd@lists.infradead.org Subject: [PATCH v2 5/7] mtd: ubi: introduce pre-removal notification for UBI volumes Message-ID: <02ba24fb52653080312cc59ae83ba23a745c0b21.1691252291.git.daniel@makrotopia.org> References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773409233224783786 X-GMAIL-MSGID: 1773409233224783786 Introduce a new notification type UBI_VOLUME_SHUTDOWN to inform users that a volume is just about to be removed. This is needed because users (such as the NVMEM subsystem) expect that at the time their removal function is called, the parenting device is still available (for removal of sysfs nodes, for example, in case of NVMEM which otherwise WARNs on volume removal). Signed-off-by: Daniel Golle --- drivers/mtd/ubi/block.c | 26 ++++++++++++++++++++++++++ drivers/mtd/ubi/build.c | 7 ++++++- drivers/mtd/ubi/vmt.c | 5 +++++ include/linux/mtd/ubi.h | 2 ++ 4 files changed, 39 insertions(+), 1 deletion(-) diff --git a/drivers/mtd/ubi/block.c b/drivers/mtd/ubi/block.c index 99b5f502c9dbc..1d5148371991b 100644 --- a/drivers/mtd/ubi/block.c +++ b/drivers/mtd/ubi/block.c @@ -533,6 +533,29 @@ static int ubiblock_resize(struct ubi_volume_info *vi) return 0; } +static int ubiblock_shutdown(struct ubi_volume_info *vi) +{ + struct ubiblock *dev; + struct gendisk *disk; + int ret = 0; + + mutex_lock(&devices_mutex); + dev = find_dev_nolock(vi->ubi_num, vi->vol_id); + if (!dev) { + ret = -ENODEV; + goto out_unlock; + } + disk = dev->gd; + +out_unlock: + mutex_unlock(&devices_mutex); + + if (!ret) + blk_mark_disk_dead(disk); + + return ret; +}; + static bool match_volume_desc(struct ubi_volume_info *vi, const char *name, int ubi_num, int vol_id) { @@ -624,6 +647,9 @@ static int ubiblock_notify(struct notifier_block *nb, case UBI_VOLUME_REMOVED: ubiblock_remove(&nt->vi, true); break; + case UBI_VOLUME_SHUTDOWN: + ubiblock_shutdown(&nt->vi); + break; case UBI_VOLUME_RESIZED: ubiblock_resize(&nt->vi); break; diff --git a/drivers/mtd/ubi/build.c b/drivers/mtd/ubi/build.c index c153373c13dab..ccee4a28ffe97 100644 --- a/drivers/mtd/ubi/build.c +++ b/drivers/mtd/ubi/build.c @@ -1088,7 +1088,6 @@ int ubi_detach_mtd_dev(int ubi_num, int anyway, bool have_lock) return -EINVAL; spin_lock(&ubi_devices_lock); - put_device(&ubi->dev); ubi->ref_count -= 1; if (ubi->ref_count) { if (!anyway) { @@ -1099,6 +1098,12 @@ int ubi_detach_mtd_dev(int ubi_num, int anyway, bool have_lock) ubi_err(ubi, "%s reference count %d, destroy anyway", ubi->ubi_name, ubi->ref_count); } + spin_unlock(&ubi_devices_lock); + + ubi_notify_all(ubi, UBI_VOLUME_SHUTDOWN, NULL); + + spin_lock(&ubi_devices_lock); + put_device(&ubi->dev); ubi_devices[ubi_num] = NULL; spin_unlock(&ubi_devices_lock); diff --git a/drivers/mtd/ubi/vmt.c b/drivers/mtd/ubi/vmt.c index 2c867d16f89f7..eed4b57c61bda 100644 --- a/drivers/mtd/ubi/vmt.c +++ b/drivers/mtd/ubi/vmt.c @@ -352,6 +352,11 @@ int ubi_remove_volume(struct ubi_volume_desc *desc, int no_vtbl) err = -EBUSY; goto out_unlock; } + spin_unlock(&ubi->volumes_lock); + + ubi_volume_notify(ubi, vol, UBI_VOLUME_SHUTDOWN); + + spin_lock(&ubi->volumes_lock); ubi->volumes[vol_id] = NULL; spin_unlock(&ubi->volumes_lock); diff --git a/include/linux/mtd/ubi.h b/include/linux/mtd/ubi.h index a529347fd75b2..562f92504f2b7 100644 --- a/include/linux/mtd/ubi.h +++ b/include/linux/mtd/ubi.h @@ -192,6 +192,7 @@ struct ubi_device_info { * or a volume was removed) * @UBI_VOLUME_RESIZED: a volume has been re-sized * @UBI_VOLUME_RENAMED: a volume has been re-named + * @UBI_VOLUME_SHUTDOWN: a volume is going to removed, shutdown users * @UBI_VOLUME_UPDATED: data has been written to a volume * * These constants define which type of event has happened when a volume @@ -202,6 +203,7 @@ enum { UBI_VOLUME_REMOVED, UBI_VOLUME_RESIZED, UBI_VOLUME_RENAMED, + UBI_VOLUME_SHUTDOWN, UBI_VOLUME_UPDATED, }; From patchwork Sat Aug 5 16:26:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Golle X-Patchwork-Id: 131506 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp548886vqr; Sat, 5 Aug 2023 09:57:19 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF1vfHYTmrKVtnuyyRT13j4bZTJUDKFriWVU0AIqvdWkFaSbdPvh1pwNR77mzSrwdbuTcsz X-Received: by 2002:a05:6a20:8409:b0:137:2f8c:fab0 with SMTP id c9-20020a056a20840900b001372f8cfab0mr5457588pzd.49.1691254639268; Sat, 05 Aug 2023 09:57:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691254639; cv=none; d=google.com; s=arc-20160816; b=BUBZI3sE5Dz8seSIdEGbDPWrRdlfBsFsQU7mysaBKsMyHxBUp2zoAM7wLpYIjoXr6I QMvvm6UvVvK7hRFMdICmhh11rnOJ2q/AxVCRfRT/hwcrrVoRPfpFJZaeyGqkmSgjpa1Y w0iKAU9h6S9liv7+RaqmSJjAyXRvvzpC9x7PLICs7AE/WMS1/6QVaZwRnwNPKv1Phzai JBppvrxUMnoZqP8x477wgRIIdR0ErR5pWLHZgsUHf2+knUS7rnDdN0DTOtnA+zsxCGeu 6Vu0ALf2nTdeofi31nTIRKhLseyG1Ez7Irr7peflgQqGqTtSi9zKiwmg+zR9PXjr2rzL y5Eg== 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=o9y8ga7ysIvZ1tA1LAIP7ohY/ZuMF48QFqDAHAq8I6A=; fh=WrF5P4rt5GKLKZXFPrBfHwP5VFIZG8GPN/h4vsvn4l4=; b=lZbdlOKOBgQyqEYmjOyczYkQif5UyagsDFbA8QGmwjSk/TmiiOOUOWl2KUanN/x1n7 ESpqyJuJ2w8MNnOLAxbxxdcPxoFhGmZh0SD9iRphde1x7ix+mj3WXk4gUyA6QQLdB3FW EjgpdC2Aa8kNU94h01m3JwX6F4nTQYXmOsi0ihAhpDUjlI2ciO7haly93uGBmFiHK64q AWaWedl9REBNHEuHj/OehlwdYMLEoPiY3khviOKTgFvY7FmnkaA2UdHCS/mXSElG8SLB daICmbdDJ4HuUKtBeLZnBsX0oF2rip8Ujx4TwamrI43hqGSE+xKwgwY09m8Az+IHCy15 OjDg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c66-20020a633545000000b00563fc0ccc20si3398604pga.818.2023.08.05.09.57.05; Sat, 05 Aug 2023 09:57:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229913AbjHEQ0n (ORCPT + 99 others); Sat, 5 Aug 2023 12:26:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39758 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229905AbjHEQ0l (ORCPT ); Sat, 5 Aug 2023 12:26:41 -0400 Received: from pidgin.makrotopia.org (pidgin.makrotopia.org [185.142.180.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 649364204 for ; Sat, 5 Aug 2023 09:26:31 -0700 (PDT) Received: from local by pidgin.makrotopia.org with esmtpsa (TLS1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.96) (envelope-from ) id 1qSK6l-0000fK-1j; Sat, 05 Aug 2023 16:26:27 +0000 Date: Sat, 5 Aug 2023 17:26:20 +0100 From: Daniel Golle To: Randy Dunlap , Richard Weinberger , Miquel Raynal , Vignesh Raghavendra , linux-kernel@vger.kernel.org, linux-mtd@lists.infradead.org Subject: [PATCH v2 6/7] mtd: ubi: populate ubi volume fwnode Message-ID: References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773409024561656157 X-GMAIL-MSGID: 1773409024561656157 Look for the 'volumes' subnode of an MTD partition attached to a UBI device and attach matching child nodes to UBI volumes. This allows UBI volumes to be referenced in device tree, e.g. for use as NVMEM providers. Signed-off-by: Daniel Golle --- drivers/mtd/ubi/vmt.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/drivers/mtd/ubi/vmt.c b/drivers/mtd/ubi/vmt.c index eed4b57c61bda..3b997256cfba7 100644 --- a/drivers/mtd/ubi/vmt.c +++ b/drivers/mtd/ubi/vmt.c @@ -124,6 +124,35 @@ static void vol_release(struct device *dev) kfree(vol); } +static struct fwnode_handle *find_volume_fwnode(struct ubi_volume *vol) +{ + struct fwnode_handle *fw_vols, *fw_vol; + const char *volname; + u32 reg; + + fw_vols = device_get_named_child_node(vol->dev.parent->parent, "volumes"); + if (!fw_vols) + return NULL; + + fwnode_for_each_child_node(fw_vols, fw_vol) { + if (!fwnode_property_read_string(fw_vol, "volname", &volname) && + strncmp(volname, vol->name, vol->name_len)) + continue; + + /* + * volume id (reg) in device tree greater than UBI_MAX_VOLUMES + * can be used to match by volname only + */ + if (!fwnode_property_read_u32(fw_vol, "reg", ®) && + reg < UBI_MAX_VOLUMES && vol->vol_id != reg) + continue; + + return fw_vol; + } + + return NULL; +} + /** * ubi_create_volume - create volume. * @ubi: UBI device description object @@ -223,6 +252,7 @@ int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req) vol->name_len = req->name_len; memcpy(vol->name, req->name, vol->name_len); vol->ubi = ubi; + device_set_node(&vol->dev, find_volume_fwnode(vol)); /* * Finish all pending erases because there may be some LEBs belonging @@ -597,6 +627,7 @@ int ubi_add_volume(struct ubi_device *ubi, struct ubi_volume *vol) vol->dev.class = &ubi_class; vol->dev.groups = volume_dev_groups; dev_set_name(&vol->dev, "%s_%d", ubi->ubi_name, vol->vol_id); + device_set_node(&vol->dev, find_volume_fwnode(vol)); err = device_register(&vol->dev); if (err) { cdev_del(&vol->cdev); From patchwork Sat Aug 5 16:26:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Golle X-Patchwork-Id: 131507 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp549639vqr; Sat, 5 Aug 2023 09:59:31 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEsY+kCxb7s8wX8DC0bh2odLwthYbACJGsdZpoaI7h+KaZnxA0WumqtDTVAYB0L93q0hbyv X-Received: by 2002:a05:6a00:1388:b0:687:1300:22ff with SMTP id t8-20020a056a00138800b00687130022ffmr3539202pfg.1.1691254770707; Sat, 05 Aug 2023 09:59:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691254770; cv=none; d=google.com; s=arc-20160816; b=JYNx7sorxDePxSgM2bZ5n5uBh72Tp2W4wLQ3KvDXw09NQwAADivcfbqH0Z/czk8oqJ 8+DXA25t2jZEV3tEeo1y3HJq39IvSi8/G4wN4rVQ+f/T+ciMS2p1rf+skq8Pn9cjDTk1 lu9W/+KKs/1aLMEmjznZBz5V854FabTFjGsY//qO5pH0Ho/7xuJjAO3gspo2ueJ0tx1J CfiltKpHujpFkF26VPRXNTEUkzlnVcURqbEQdAuiVo25Tu4qN4ObMOo0ZQHZd2n6UVvu 6FVaC7VvwQ9Nj1TlMiaPskb/cttYLAM39GGei+H/SLX35+8dXu7gKAa92Tt2w0e9U3TK us8g== 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=6tHDzll50T2gKNSMAIB0HHCIYU8y6KbrScP4vA7BGqY=; fh=WrF5P4rt5GKLKZXFPrBfHwP5VFIZG8GPN/h4vsvn4l4=; b=Yv5J6hXn/g6AGtIe9gCz3rwE1dRh30w37/A8yzT/pPsR/Te6+4a3PwZXbTPtmQ3C72 hsDgtO+lseYIkYH1erMYdspimCN0cpDsfv4qesTKgGhjil8Aszj+SfdKpXZFEEmkUfy1 aueAR0hKQc6EGEFJcMrG/mQOpJfT7Qp2YMHaqEvF6CreBn622/xDlMKYm6zj813QTfp6 hp+4rZmSr3YA4EYcasXgV8juqSA4UYDNJ9QStBODxQa5tcOs/9+kmL6SjP+xnfAQ8aSB /Zw4Dgnhc6AUyStAxXGrrNJETCU9CLVbXaflRYG3G5GOx0PwqDOlcDe7Rs8SJAg0Ep2S INrA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z16-20020a056a001d9000b0067f69d10241si3306787pfw.176.2023.08.05.09.59.17; Sat, 05 Aug 2023 09:59:30 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229940AbjHEQ1A (ORCPT + 99 others); Sat, 5 Aug 2023 12:27:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40058 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229917AbjHEQ05 (ORCPT ); Sat, 5 Aug 2023 12:26:57 -0400 Received: from pidgin.makrotopia.org (pidgin.makrotopia.org [185.142.180.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 740D94218 for ; Sat, 5 Aug 2023 09:26:44 -0700 (PDT) Received: from local by pidgin.makrotopia.org with esmtpsa (TLS1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.96) (envelope-from ) id 1qSK6y-0000fe-35; Sat, 05 Aug 2023 16:26:41 +0000 Date: Sat, 5 Aug 2023 17:26:33 +0100 From: Daniel Golle To: Randy Dunlap , Richard Weinberger , Miquel Raynal , Vignesh Raghavendra , linux-kernel@vger.kernel.org, linux-mtd@lists.infradead.org Subject: [PATCH v2 7/7] mtd: ubi: provide NVMEM layer over UBI volumes Message-ID: <3df11df592be6a8f681fc612217f75c2a04e8186.1691252291.git.daniel@makrotopia.org> References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773409162824683037 X-GMAIL-MSGID: 1773409162824683037 In an ideal world we would like UBI to be used where ever possible on a NAND chip. And with UBI support in ARM Trusted Firmware and U-Boot it is possible to achieve an (almost-)all-UBI flash layout. Hence the need for a way to also use UBI volumes to store board-level constants, such as MAC addresses and calibration data of wireless interfaces. Add UBI volume NVMEM driver module exposing UBI volumes as NVMEM providers. Allow UBI devices to have a "volumes" firmware subnode with volumes which may be compatible with "nvmem-cells". Access to UBI volumes via the NVMEM interface at this point is read-only, and it is slow, opening and closing the UBI volume for each access due to limitations of the NVMEM provider API. Signed-off-by: Daniel Golle --- drivers/mtd/ubi/Kconfig | 12 +++ drivers/mtd/ubi/Makefile | 1 + drivers/mtd/ubi/nvmem.c | 189 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 202 insertions(+) create mode 100644 drivers/mtd/ubi/nvmem.c diff --git a/drivers/mtd/ubi/Kconfig b/drivers/mtd/ubi/Kconfig index 184118f9a2969..fb15d4549d55f 100644 --- a/drivers/mtd/ubi/Kconfig +++ b/drivers/mtd/ubi/Kconfig @@ -104,4 +104,16 @@ config MTD_UBI_BLOCK If in doubt, say "N". +config MTD_UBI_NVMEM + tristate "UBI virtual NVMEM" + default n + depends on NVMEM + help + This option enables an additional driver exposing UBI volumes as NVMEM + providers, intended for platforms where UBI is part of the firmware + specification and used to store also e.g. MAC addresses or board- + specific Wi-Fi calibration data. + + If in doubt, say "N". + endif # MTD_UBI diff --git a/drivers/mtd/ubi/Makefile b/drivers/mtd/ubi/Makefile index 543673605ca72..4b51aaf00d1a2 100644 --- a/drivers/mtd/ubi/Makefile +++ b/drivers/mtd/ubi/Makefile @@ -7,3 +7,4 @@ ubi-$(CONFIG_MTD_UBI_FASTMAP) += fastmap.o ubi-$(CONFIG_MTD_UBI_BLOCK) += block.o obj-$(CONFIG_MTD_UBI_GLUEBI) += gluebi.o +obj-$(CONFIG_MTD_UBI_NVMEM) += nvmem.o diff --git a/drivers/mtd/ubi/nvmem.c b/drivers/mtd/ubi/nvmem.c new file mode 100644 index 0000000000000..dd7cc6afb8d00 --- /dev/null +++ b/drivers/mtd/ubi/nvmem.c @@ -0,0 +1,189 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (c) 2023 Daniel Golle + */ + +/* UBI NVMEM provider */ +#include "ubi.h" +#include + +/* List of all NVMEM devices */ +static LIST_HEAD(nvmem_devices); +static DEFINE_MUTEX(devices_mutex); + +struct ubi_nvmem { + struct nvmem_device *nvmem; + int ubi_num; + int vol_id; + int usable_leb_size; + struct list_head list; +}; + +static int ubi_nvmem_reg_read(void *priv, unsigned int from, + void *val, size_t bytes) +{ + struct ubi_nvmem *unv = priv; + struct ubi_volume_desc *desc; + int err = 0, lnum, offs, bytes_left; + size_t to_read; + + desc = ubi_open_volume(unv->ubi_num, unv->vol_id, UBI_READONLY); + if (IS_ERR(desc)) + return PTR_ERR(desc); + + lnum = div_u64_rem(from, unv->usable_leb_size, &offs); + bytes_left = bytes; + while (bytes_left) { + to_read = unv->usable_leb_size - offs; + + if (to_read > bytes_left) + to_read = bytes_left; + + err = ubi_read(desc, lnum, val, offs, to_read); + if (err) + break; + + lnum += 1; + offs = 0; + bytes_left -= to_read; + val += to_read; + } + ubi_close_volume(desc); + + if (err) + return err; + + return bytes_left == 0 ? 0 : -EIO; +} + +static int ubi_nvmem_add(struct ubi_volume_info *vi) +{ + struct nvmem_config config = {}; + struct ubi_nvmem *unv; + int ret; + + if (!device_is_compatible(vi->dev, "nvmem-cells")) + return 0; + + unv = kzalloc(sizeof(struct ubi_nvmem), GFP_KERNEL); + if (!unv) + return -ENOMEM; + + config.id = NVMEM_DEVID_NONE; + config.dev = vi->dev; + config.name = dev_name(vi->dev); + config.owner = THIS_MODULE; + config.priv = unv; + config.reg_read = ubi_nvmem_reg_read; + config.size = vi->usable_leb_size * vi->size; + config.word_size = 1; + config.stride = 1; + config.read_only = true; + config.root_only = true; + config.ignore_wp = true; + config.of_node = dev_of_node(vi->dev); + + if (!config.of_node) + config.no_of_node = true; + + unv->ubi_num = vi->ubi_num; + unv->vol_id = vi->vol_id; + unv->usable_leb_size = vi->usable_leb_size; + unv->nvmem = nvmem_register(&config); + if (IS_ERR(unv->nvmem)) { + /* Just ignore if there is no NVMEM support in the kernel */ + if (PTR_ERR(unv->nvmem) == -EOPNOTSUPP) + ret = 0; + else + ret = dev_err_probe(vi->dev, PTR_ERR(unv->nvmem), + "Failed to register NVMEM device\n"); + + kfree(unv); + return ret; + } + + mutex_lock(&devices_mutex); + list_add_tail(&unv->list, &nvmem_devices); + mutex_unlock(&devices_mutex); + + return 0; +} + +static void ubi_nvmem_remove(struct ubi_volume_info *vi) +{ + struct ubi_nvmem *unv_c, *unv = NULL; + + mutex_lock(&devices_mutex); + list_for_each_entry(unv_c, &nvmem_devices, list) + if (unv_c->ubi_num == vi->ubi_num && unv_c->vol_id == vi->vol_id) { + unv = unv_c; + break; + } + + if (!unv) { + mutex_unlock(&devices_mutex); + return; + } + + list_del(&unv->list); + mutex_unlock(&devices_mutex); + nvmem_unregister(unv->nvmem); + kfree(unv); +} + +/** + * nvmem_notify - UBI notification handler. + * @nb: registered notifier block + * @l: notification type + * @ns_ptr: pointer to the &struct ubi_notification object + */ +static int nvmem_notify(struct notifier_block *nb, unsigned long l, + void *ns_ptr) +{ + struct ubi_notification *nt = ns_ptr; + + switch (l) { + case UBI_VOLUME_RESIZED: + ubi_nvmem_remove(&nt->vi); + fallthrough; + case UBI_VOLUME_ADDED: + ubi_nvmem_add(&nt->vi); + break; + case UBI_VOLUME_SHUTDOWN: + ubi_nvmem_remove(&nt->vi); + break; + default: + break; + } + return NOTIFY_OK; +} + +static struct notifier_block nvmem_notifier = { + .notifier_call = nvmem_notify, +}; + +static int __init ubi_nvmem_init(void) +{ + return ubi_register_volume_notifier(&nvmem_notifier, 0); +} + +static void __exit ubi_nvmem_exit(void) +{ + struct ubi_nvmem *unv, *tmp; + + mutex_lock(&devices_mutex); + list_for_each_entry_safe(unv, tmp, &nvmem_devices, list) { + nvmem_unregister(unv->nvmem); + list_del(&unv->list); + kfree(unv); + } + mutex_unlock(&devices_mutex); + + ubi_unregister_volume_notifier(&nvmem_notifier); +} + +module_init(ubi_nvmem_init); +module_exit(ubi_nvmem_exit); +MODULE_DESCRIPTION("NVMEM layer over UBI volumes"); +MODULE_AUTHOR("Daniel Golle"); +MODULE_LICENSE("GPL");