From patchwork Tue May 2 16:48: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: 89420 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp759481vqo; Tue, 2 May 2023 09:54:25 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ46AvuLjwmRLf/AlzHh5wmX2shOGzopm5byRH5bzHKr+VSRNkJWmP/c+UjNA0VmgJkBqGKB X-Received: by 2002:a17:90a:2e82:b0:24e:18e3:37ec with SMTP id r2-20020a17090a2e8200b0024e18e337ecmr4651471pjd.32.1683046465278; Tue, 02 May 2023 09:54:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683046465; cv=none; d=google.com; s=arc-20160816; b=EHYj5KFaGlibKBBSQI01Cu52Pyi/KE24NWUePwEJ5pS2PQN5UqwX7RSILe4MPMGTq8 BPAo3mTvGmAUtkLxJCJlUQI9NKGXahXbpt2W/nA2oXDXX56v/Qk5q8pn71i8FCwbgOaN V15bGvRBbiaQn5AthFdCrL6dx54pbx/xTj+3C2yH/8beZzcRKjFX9zl8rLHPM8FcjFdy kMHwEWYjMpavSPQvZJRATpc/y7F7xvgkuo8rIT1x6vqI8Igaj2VWniR7zh6eUPfVX9Oq AfiWP/BO+Rol0iQSHBEHEmv9UlZjqhsHUiskZG9nK9CmKHlhQmorxbSvgeRe47LqQSyx GMdw== 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=K5rQZzkpLFh12/Tkfwp5eyA1dz0oMZkvcSzICdsncNo=; b=JIzRvnXBLt3ves54wNJjN/A2XlHIgfRdD3KUGYjN+u43ueMTY33WLsA7DHLwKnNbcE B9it1DGpLVZKT5zCMJ9NmelHkwbYSe2ljVyNcp3eCo/OgXVxnOLQobu6DFwauGNB/M7M LnqNd2ndxZ4kjPItyC0NXMNs5Ox/yvGec82xfNeIRNvLfZrLIgUZekfFitpBGnsiCXKi p82BEJZmf6nBV1S6m8+8cM7KEkAmQBK0QuKg5UbdtqwROi77mJHq6/V9K+svpcAO5YzJ X/776rfARimMxfoR16q5Mk2dKy/+ZHWHKzMs1B0cepp38d/qMAqyTanq7ZBMhHriQLDk hldQ== 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 bs188-20020a6328c5000000b00520b3928bebsi18027896pgb.7.2023.05.02.09.54.12; Tue, 02 May 2023 09:54:25 -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 S234154AbjEBQsS (ORCPT + 99 others); Tue, 2 May 2023 12:48:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36392 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233478AbjEBQsP (ORCPT ); Tue, 2 May 2023 12:48:15 -0400 Received: from fudo.makrotopia.org (fudo.makrotopia.org [IPv6:2a07:2ec0:3002::71]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 132442118 for ; Tue, 2 May 2023 09:48:14 -0700 (PDT) Received: from local by fudo.makrotopia.org with esmtpsa (TLS1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.96) (envelope-from ) id 1pttAi-0000UW-1L; Tue, 02 May 2023 18:48:12 +0200 Date: Tue, 2 May 2023 17:48:07 +0100 From: Daniel Golle To: linux-mtd@lists.infradead.org, linux-kernel@vger.kernel.org, Miquel Raynal , Richard Weinberger , Vignesh Raghavendra , Zhihao Cheng Subject: [PATCH 1/4] mtd: ubi: block: don't return on error when removing Message-ID: <4bca8ffa66fa094da37625e66f3a2681058531d6.1683043928.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,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE 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: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1764802130413005014?= X-GMAIL-MSGID: =?utf-8?q?1764802130413005014?= 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 a 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 3711d7f746003..6f5804f4b8f55 100644 --- a/drivers/mtd/ubi/block.c +++ b/drivers/mtd/ubi/block.c @@ -457,7 +457,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; @@ -471,7 +471,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; } @@ -546,7 +546,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 Tue May 2 16:48:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Golle X-Patchwork-Id: 89431 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp777950vqo; Tue, 2 May 2023 10:21:47 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ67JDQnI8aV5WUpEmSt9CBoVlYD+kwdBBZVOidnkrMmoZTsNeGq3Pobh2o/jRsERUyOQ3Yc X-Received: by 2002:a05:6a00:14d1:b0:63b:8df5:f8d with SMTP id w17-20020a056a0014d100b0063b8df50f8dmr29639624pfu.3.1683048106755; Tue, 02 May 2023 10:21:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683048106; cv=none; d=google.com; s=arc-20160816; b=znfIIMoXv9Ew55eFgs2CA51Z4On+nMewJhrq5n/yW4eteBM9LdGVBacSKUs/NYOVdl QRbUU1XF7DbX0votYfgDG+mlLuty9exQFJofiO17pc0+YFxnmjh4IQK2Rj/ssbUz2qMu ySVHPU/SPjE43aLnYa7TfPpX10cLrjRuwsOhYE2btENO8lVlfAQnxELaW6pOlqwCfk/8 TQiELgSugUpjrKM8Z2IbTSgR3C8LwcdxnySrvDmg64qfn1wv7AWY+anYUXwzWMqN1/2j kHCdbF7JIQgZMW/T+pl01A/eqNQnyRzq6JsAQ5S2U839szqRNZh179ujkvfBGd/NI9wF YrYw== 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=/i0rZ0ExOGMbyFWeG3s/rRav7Gohip265Q7lUb0gKck=; b=Mv8fqp869BoYpGbFYN4c6QS5pX9koV2k7Hs0eE9n0FFG3vpwjkXQ2q5VPU1EgViuSP Lgft2LxGBVEys63imdyj6q5TsAJFz8HyNn/jBu+IOmgFGiOkpNMKt/yLid1lsmwGPfUK hSU2899LFJt8dQIGcfCIasznN95fnGVJCOAPjiHoucOcrKu9UWRKcSWkm458j91EuD5a B6eE1calebnh9LoiJGskB5KUUpAK5OscdOqHVU3E5xqdVbEhKei/et3O4gJKHkcSu05V ON4OwxuQqDM7PNq9pc+2DGWZODwDQbi5jGd26+xscPOF2RM0V9wG92pfweNJ/1/3AiOT ajVw== 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 k4-20020aa79984000000b0063b8982c669si30534580pfh.317.2023.05.02.10.21.34; Tue, 02 May 2023 10:21:46 -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 S233730AbjEBQsf (ORCPT + 99 others); Tue, 2 May 2023 12:48:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36876 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233608AbjEBQse (ORCPT ); Tue, 2 May 2023 12:48:34 -0400 Received: from fudo.makrotopia.org (fudo.makrotopia.org [IPv6:2a07:2ec0:3002::71]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 76597212E for ; Tue, 2 May 2023 09:48:30 -0700 (PDT) Received: from local by fudo.makrotopia.org with esmtpsa (TLS1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.96) (envelope-from ) id 1pttAy-0000Us-1h; Tue, 02 May 2023 18:48:28 +0200 Date: Tue, 2 May 2023 17:48:23 +0100 From: Daniel Golle To: linux-mtd@lists.infradead.org, linux-kernel@vger.kernel.org, Miquel Raynal , Richard Weinberger , Vignesh Raghavendra , Zhihao Cheng Subject: [PATCH 2/4] mtd: ubi: block: use notifier to create ubiblock from parameter Message-ID: <726b7629d0819092edeb193f3bfc13c99bd13339.1683043928.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,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE 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: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1764803851411068034?= X-GMAIL-MSGID: =?utf-8?q?1764803851411068034?= Use UBI_VOLUME_ADDED notification to create ubiblock device specified on kernel cmdline or module parameter. This makes things 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 | 150 ++++++++++++++++++++++------------------ 1 file changed, 82 insertions(+), 68 deletions(-) diff --git a/drivers/mtd/ubi/block.c b/drivers/mtd/ubi/block.c index 6f5804f4b8f55..7f3024bddf306 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; @@ -533,6 +534,83 @@ 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; + } else 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; + } else { + 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) { @@ -540,10 +618,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); @@ -569,56 +644,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; @@ -644,18 +669,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 Tue May 2 16:48: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: 89424 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp765583vqo; Tue, 2 May 2023 10:03:30 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7rI+Aoq+yzF7TUadgjVxBGmrEFpWKsElMmMULi6IMEfTIzcKdtTIvhrc5RlNIJ1nfokaOw X-Received: by 2002:a9d:77c6:0:b0:697:bfe5:93a0 with SMTP id w6-20020a9d77c6000000b00697bfe593a0mr9229947otl.10.1683047009838; Tue, 02 May 2023 10:03:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683047009; cv=none; d=google.com; s=arc-20160816; b=vBezDdbLTtTJvhJ6uWj+9St8WBu6wTJ0r3y7Yok+8EiEIlgbUlIgJ7zMmVcvqNNu3c j+fOY7AJKQ0TsaMNU2FSvhtNRHTOpdPfy4Jc5c4FT4Ffz7TuXBiwnWPYFNoPasGIP7ow sBqIh9wjOg5DbL2i2i/XoE+vds2PQCl09kmtU/eKKOffYuj/ck2Xbgga1VyA0r2rKE4a 6XRcoxKvbb2RP336XXmqiYwVmIzB8N5Xb3F5m4oAyReOwZIGkfg8nOlz1UBQtYpGZrWd nScHavH9KXzW4fElXLE6muEHrodDDSWEkhF/Klm9m6GgqwwZlw+6uxy21zMR0cFDoACS jUxA== 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=S6QUnYJSAYr66Ud0rab5qBlK1fE/deuL2bPmBPZ2GT4=; b=V5XCpKCuJfTG13WudJT7YHFN3jrs+ZbnJkh5SiaNMWd8JWN3q8QuZCufwXFB/9vUIX eYsvjSx151kU7cdKHXx+RnRedQtPTB9PzYZ7B0zJQLz9evc9a8k8qI45l/v7HvNPdjjI KkOp+pnDeCbGt6Y6juqY3xcnyIjtDpWPNSx21iSVmt3AwPlXbMHRPII2nIaRLe0IZc9S n18C2RCzJ99slNsfDSYNp9dbop3LumoHsN+1Fs9m8FCjiFW6AmuNy8UKBeOEhCDEBaed qwrp6wdHilvD+rps14vvLIVexj5DT29XRZ4twChttcf0tO1y0rLkj1hpcRfsv7/aua0R 9sEQ== 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 x93-20020a9d37e6000000b006a60a317134si19910830otb.190.2023.05.02.10.02.53; Tue, 02 May 2023 10:03:29 -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 S233854AbjEBQtJ (ORCPT + 99 others); Tue, 2 May 2023 12:49:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37296 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234144AbjEBQtC (ORCPT ); Tue, 2 May 2023 12:49:02 -0400 Received: from fudo.makrotopia.org (fudo.makrotopia.org [IPv6:2a07:2ec0:3002::71]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B2D1E3C14; Tue, 2 May 2023 09:48:48 -0700 (PDT) Received: from local by fudo.makrotopia.org with esmtpsa (TLS1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.96) (envelope-from ) id 1pttBH-0000VJ-06; Tue, 02 May 2023 18:48:47 +0200 Date: Tue, 2 May 2023 17:48:39 +0100 From: Daniel Golle To: linux-mtd@lists.infradead.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Miquel Raynal , Richard Weinberger , Vignesh Raghavendra , Rob Herring , Krzysztof Kozlowski , Zhihao Cheng , =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= Subject: [PATCH 3/4] dt-bindings: mtd: partitions: add linux,ubi compatible 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,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE 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: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1764802701634145073?= X-GMAIL-MSGID: =?utf-8?q?1764802701634145073?= Add bindings for MTD partitions to be attached as UBI devices. Signed-off-by: Daniel Golle --- .../bindings/mtd/partitions/ubi.yaml | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 Documentation/devicetree/bindings/mtd/partitions/ubi.yaml diff --git a/Documentation/devicetree/bindings/mtd/partitions/ubi.yaml b/Documentation/devicetree/bindings/mtd/partitions/ubi.yaml new file mode 100644 index 0000000000000..aa02fbbd50716 --- /dev/null +++ b/Documentation/devicetree/bindings/mtd/partitions/ubi.yaml @@ -0,0 +1,49 @@ +# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/mtd/partitions/ubi.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Unsorted Block Images (UBI) + +description: | + Unsorted Block Images (UBI) is a volume management system typically + used on NAND flash providing bad block management as well as + wear-leveling. + Any partition containing the compatible "linux,ubi" will be attached + as UBI device. + +maintainers: + - Daniel Golle + +allOf: + - $ref: /schemas/mtd/partitions/partition.yaml# + +properties: + compatible: + const: linux,ubi + +required: + - compatible + +unevaluatedProperties: false + +examples: + - | + partitions { + compatible = "fixed-partitions"; + #address-cells = <1>; + #size-cells = <1>; + + partition@0 { + label = "bootloader"; + reg = <0x000000 0x100000>; + read-only; + }; + + ubi@100000 { + compatible = "linux,ubi"; + label = "ubi"; + reg = <0x100000 0x7f00000>; + }; + }; From patchwork Tue May 2 16:48:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Golle X-Patchwork-Id: 89428 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp776021vqo; Tue, 2 May 2023 10:18:30 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ51zgEIjJEoOz+EpumX0dTZVP0J2TwcnXpJvwIrU44dMSORy9DjTP+z5xnkm10EvoxeiALo X-Received: by 2002:a05:6a20:441b:b0:f2:4c39:8028 with SMTP id ce27-20020a056a20441b00b000f24c398028mr23306197pzb.21.1683047909722; Tue, 02 May 2023 10:18:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683047909; cv=none; d=google.com; s=arc-20160816; b=zOfg74nOnLkmSEzPyRi9YKxP325Ua+ULKaAK0YDRIJg+sMjgcTywTdTrToSBJvEgLe 8QE7KWqwQCo9ncnVMuNnBiPSx4bBLHUw5XyU58GiCB+d9mqcdAa2TsFWy6Vvk3uKHlqc q0CBpJw8xzIlR24Dgn9x7zaO+hKEvIEit9xk7NETZQ1yfBU0v55x9gCMYP1vEh0u4x5o 2IWe3AWaCQAppOV1E8vxo4vmGFi+4ZB86It9BnI1ZznJAAAD49PxHCUypaUd31HzT3pV pHEDNupzhqwAEnp9RgO35RjvAZXviaiXsxTWN2O4ecgOEnI7cAZozTmabcKWKzuUGCfD cQoQ== 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=eS/BGHfgOd0BTyEm5NgNkfPCvCLcq+sk29N/LHx7CgI=; b=I7hqsIMUHHV91Cacn1JdltV+auodTjHzDN4ZtFMWphzplnEZrgSw6VV8bC7aQ34G3w rPyBdI3DfXVsW6I/0oHz3s8CrLap40LTic5RDLeAKBS+76ARZ/OZeh0tFKJjrd8E+bKQ slo5CX8+jJo0KAiCf28gLOuJbsy8/wjs6a0wQ6QYwU9Tdo6m6a8iwOZGGZCrAOCu5w6E bJple+27EGh6j/bKNcOWDLXwWkx7wvLkMoJS2cbw4x3BYKZ3IVp3dPwNDIk/Bqukyzu+ bXT5OwqIxF/qbX7cmuFeFQa5ma9q0dLsQlORYmSg+Ri3h3ZTkfApJbBLNweI75NPJ1s9 1nMQ== 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 x187-20020a6363c4000000b004da377c33bbsi30718205pgb.85.2023.05.02.10.18.14; Tue, 02 May 2023 10:18:29 -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 S234092AbjEBQtM (ORCPT + 99 others); Tue, 2 May 2023 12:49:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37332 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234338AbjEBQtI (ORCPT ); Tue, 2 May 2023 12:49:08 -0400 Received: from fudo.makrotopia.org (fudo.makrotopia.org [IPv6:2a07:2ec0:3002::71]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9B13A1BF6 for ; Tue, 2 May 2023 09:49:04 -0700 (PDT) Received: from local by fudo.makrotopia.org with esmtpsa (TLS1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.96) (envelope-from ) id 1pttBW-0000Vf-34; Tue, 02 May 2023 18:49:03 +0200 Date: Tue, 2 May 2023 17:48:57 +0100 From: Daniel Golle To: linux-mtd@lists.infradead.org, linux-kernel@vger.kernel.org, Miquel Raynal , Richard Weinberger , Vignesh Raghavendra , Zhihao Cheng , =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= Subject: [PATCH 4/4] mtd: ubi: attach MTD partition from device-tree 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,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE 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: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1764803644849889872?= X-GMAIL-MSGID: =?utf-8?q?1764803644849889872?= 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 | 189 ++++++++++++++++++++++++++++------------ drivers/mtd/ubi/cdev.c | 2 +- drivers/mtd/ubi/ubi.h | 2 +- 4 files changed, 134 insertions(+), 61 deletions(-) diff --git a/drivers/mtd/ubi/block.c b/drivers/mtd/ubi/block.c index 7f3024bddf306..8754ed86eac3a 100644 --- a/drivers/mtd/ubi/block.c +++ b/drivers/mtd/ubi/block.c @@ -471,7 +471,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 bb1a90bbc8ffb..32422dcf1bf48 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" @@ -1066,6 +1067,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 @@ -1075,7 +1077,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; @@ -1131,7 +1136,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; } @@ -1208,43 +1217,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++) { @@ -1292,25 +1309,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: @@ -1320,18 +1391,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);