Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp311433pxb; Fri, 15 Oct 2021 06:06:06 -0700 (PDT) X-Google-Smtp-Source: ABdhPJznmetN5u3625tOAax4ZH3pEzXruGeyDVw07FtutFUHQvFj+aIqzE/yZ15/3kVjuivdCM1c X-Received: by 2002:a17:903:32c7:b0:13e:ea76:f8cb with SMTP id i7-20020a17090332c700b0013eea76f8cbmr10989466plr.74.1634303165698; Fri, 15 Oct 2021 06:06:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634303165; cv=none; d=google.com; s=arc-20160816; b=hEP2vfwhmTvJPoW+3Xd/imfqeGS+yU+GgpDL/nxFkIT94BZ8MwSk72QAVM/xPqqdiT fdRQt3ym59yPYFWipjJRur2D75GSV1oEXFzMIgObNSN6kaybGZ6Ul0AM8BgLiK9YPV0D 5W3QDXhOVj8T1qgzn8Hz+qtDcVs4m+SErIlpm1H1dAPjJyWZlfKmetKDQkAdA1iBQD+4 RwkOJfU3lMpAuSBQgf5qHwoAJ/XknE3/DhJE+XgOMAHOwX4kN9QE/WYiueDa3dmYr8QL 4pPpv+HxXa2Wc+kpmCRswg7PoemN2NQpehVkalaloyMTAlLLItCgVgcbeuIdQYosAAWG 1YTg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :organization:references:in-reply-to:message-id:subject:cc:to:from :date; bh=GTZzGih8KYTld//nTko4A4JXQX+oUuJwPrcIL5LjWSw=; b=DAPdp07gPmzeXsrNNRBkKS77yWmbp66As4UOZr2D9gyp0zjhj03U+WGCyWEeTauZWn 1HtXxHI9FX1PlAqXeWBhRRr0P7y/tQV1h13pVkUKyVqzHmMLvX83qKdZQiaO+kLxgPWf G3iHDfYOpsq4t6BkI6NbK9DRCqGi+xc5JcplkLHIuUyHzlzGbUiqnVWpW4Ci7R22zidT OGO8MfqUyYufLHzXZo7/I3+Mc/vl4xS3PrYtTPIkZ4d/CijljDOlJ4+XRHJkmNxdhE03 E4uGg1Q9YtX8cv+eXvkqFAJrmDEzmpa4GIoVtGn5Wz8FrcJRMDrjj8PYU9kxJ1inP7Hx lkOA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=collabora.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id q4si6964645pln.264.2021.10.15.06.05.22; Fri, 15 Oct 2021 06:06:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=collabora.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234504AbhJOG5Z (ORCPT + 99 others); Fri, 15 Oct 2021 02:57:25 -0400 Received: from bhuna.collabora.co.uk ([46.235.227.227]:58324 "EHLO bhuna.collabora.co.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231387AbhJOG5Y (ORCPT ); Fri, 15 Oct 2021 02:57:24 -0400 Received: from localhost (unknown [IPv6:2a01:e0a:2c:6930:5cf4:84a1:2763:fe0d]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: bbrezillon) by bhuna.collabora.co.uk (Postfix) with ESMTPSA id 651671F4502B; Fri, 15 Oct 2021 07:55:17 +0100 (BST) Date: Fri, 15 Oct 2021 08:55:11 +0200 From: Boris Brezillon To: Sean Nyekjaer Cc: Miquel Raynal , Richard Weinberger , Vignesh Raghavendra , Boris Brezillon , linux-mtd@lists.infradead.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH 1/3] mtd: core: protect access to MTD devices while in suspend Message-ID: <20211015085511.0e2ac916@collabora.com> In-Reply-To: <20211011115253.38497-2-sean@geanix.com> References: <20211011115253.38497-1-sean@geanix.com> <20211011115253.38497-2-sean@geanix.com> Organization: Collabora X-Mailer: Claws Mail 3.18.0 (GTK+ 2.24.33; x86_64-redhat-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, 11 Oct 2021 13:52:51 +0200 Sean Nyekjaer wrote: > struct mtd_info { > @@ -476,10 +478,49 @@ static inline u32 mtd_oobavail(struct mtd_info *mtd, struct mtd_oob_ops *ops) > return ops->mode == MTD_OPS_AUTO_OOB ? mtd->oobavail : mtd->oobsize; > } > > +static inline void mtd_start_access(struct mtd_info *mtd) > +{ > + struct mtd_info *master = mtd_get_master(mtd); mtd_start_{access,end}() should only be called on master devices, so I guess you can drop the mtd_get_master() call and use mtd directly. Maybe add a WARN_ON_ONCE(mtd != mtd_get_master(mtd)) so we can easily catch silly mistakes. > + > + /* > + * Don't take the suspend_lock on devices that don't > + * implement the suspend hook. Otherwise, lockdep will > + * complain about nested locks when trying to suspend MTD > + * partitions or MTD devices created by gluebi which are > + * backed by real devices. > + */ > + if (!master->_suspend) > + return; > + > + /* > + * Wait until the device is resumed. Should we have a > + * non-blocking mode here? > + */ > + while (1) { > + down_read(&master->master.suspend_lock); > + if (!master->master.suspended) > + return; > + > + up_read(&master->master.suspend_lock); > + wait_event(master->master.resume_wq, master->master.suspended == 0); > + } > +} > + > +static inline void mtd_end_access(struct mtd_info *mtd) > +{ > + struct mtd_info *master = mtd_get_master(mtd); > + > + if (!master->_suspend) > + return; > + > + up_read(&master->master.suspend_lock); > +} > + > static inline int mtd_max_bad_blocks(struct mtd_info *mtd, > loff_t ofs, size_t len) > { > struct mtd_info *master = mtd_get_master(mtd); > + int ret; > > if (!master->_max_bad_blocks) > return -ENOTSUPP; > @@ -487,8 +528,12 @@ static inline int mtd_max_bad_blocks(struct mtd_info *mtd, > if (mtd->size < (len + ofs) || ofs < 0) > return -EINVAL; > > - return master->_max_bad_blocks(master, mtd_get_master_ofs(mtd, ofs), > - len); > + mtd_start_access(mtd); > + ret = master->_max_bad_blocks(master, mtd_get_master_ofs(mtd, ofs), > + len); > + mtd_end_access(mtd); Please pass the master to those functions, there's no point walking the parent chain again in the start/end_access() functions if you already have the master retrieved in the caller. Oh, and there seems to be a common pattern here, so maybe it's worth adding those macros: #define mtd_no_suspend_void_call(master, method, ...) \ mtd_start_access(master); \ master->method(master, __VA_ARGS__); \ mtd_end_access(master); #define mtd_no_suspend_ret_call(ret, master, method, ...) \ mtd_start_access(master); \ ret = master->method(master, __VA_ARGS__); \ mtd_end_access(master); I don't really like the helper names, so feel free to propose something else. > + > + return ret; > } >