Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp1748077pxb; Wed, 9 Feb 2022 03:46:05 -0800 (PST) X-Google-Smtp-Source: ABdhPJzaNIHzLR/zhlauNpi1YIthBcz0X2qAu5OPW4v0XgO2wBKOroin2n0B4/w1q01AWLV54pBt X-Received: by 2002:a05:6a00:b96:: with SMTP id g22mr1848866pfj.31.1644407165314; Wed, 09 Feb 2022 03:46:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644407165; cv=none; d=google.com; s=arc-20160816; b=Yl8VuBoZ+fSzqWPydNGdFFBX3/zNK/jq/5lI9SzedUoIy9bP+Idfjlk2S36KmveNYs 21z97kfYIDTjKP9AWMMMsbOQQ1qm5qN/kgTD4kSFIxtPjMd54IWBNK+jnYhxsCOY979n SbbbG20rK+JwNO3QV5Wo1U1Co3hN6vdGtAwcwdOZ1YL/JSoflA4yF0E0aCVnQgsVDDmr AdWKja7c2lkxdy2MRc82e8vflt0QrMYYjhnA3bNai204Z61DdqkHPC8i+De71hOLq1Ht Bt95fW3YXNkJMFmEoL86ZbDzRcs5VLfu8PWXXlBbbtZgND2lDXTL6xpHDrlOZmISH1SU zH4Q== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=6NqDj6g6jSQ6Hnek3lmlxM5trKboIZujCI+XtFlCA9w=; b=0LTOYCmki+eIN65VkTVqyBCe3PM0L1nBUYx6bstVBQSHeccGdmjzqvOJW4vf7+b5Eu C4fI0h2aIvd0Db5YRbALIiOgiHt1oIsZhtZ9+oxsejtrI0Yzls15ZIxsDNTkJIRszUWD DRTGkfonmMOVOgIbe8mpogXwXo7Cs7H9Eq0Xs4Qv/UTnU/kLJinWm5KLWJ7t0Er7e61l opcuEiWWNQZBt1jgVfeSakPByXi173SJBzJpDzdKxe/iDxm3GpTlzr5Cd3/FS/NXKRDz olr8oIpEA0gZyhFDvC0HoJ2xprsHeOShfN1MInTVxXlcH0bbSFeRKvqZ7Yo3Em8F5B6M KXTw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@geanix.com header.s=first header.b="js/n16a0"; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=geanix.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id b4si16627640pfl.173.2022.02.09.03.45.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Feb 2022 03:46:05 -0800 (PST) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@geanix.com header.s=first header.b="js/n16a0"; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=geanix.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id AD150E0DE2FC; Wed, 9 Feb 2022 01:58:09 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1352037AbiBHIwm (ORCPT + 99 others); Tue, 8 Feb 2022 03:52:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46086 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232577AbiBHIwl (ORCPT ); Tue, 8 Feb 2022 03:52:41 -0500 Received: from first.geanix.com (first.geanix.com [116.203.34.67]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C1B32C03FEC0; Tue, 8 Feb 2022 00:52:40 -0800 (PST) Received: from zen.. (unknown [185.17.218.86]) by first.geanix.com (Postfix) with ESMTPSA id A64FDE28FB; Tue, 8 Feb 2022 08:52:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=geanix.com; s=first; t=1644310357; bh=IfhFtV9h7TZeyZofkNglQMWg6AkUIHKmkWYK/JylW0g=; h=From:To:Cc:Subject:Date; b=js/n16a0zimkjRoS2heJRpgLxJGxRQ3w5s1bEzO1ieNAumVGSI7w5G1GH1QKZmRzw +wXhb4jTDUu+AqSO6al4a2SXOVm1Sy8xyOBERiMG9w5WZjCLMBtPqxPRU2ebUKkIdG wRJ35PC28Vyq+EcrPH3T7Fsp9Tx1LZMuktC0w1Wa47zbo4XuHqi0cOtkz1gxeP24vT SK4Wm18IMHuqeyeuQs7CKIcz58NJu2UMGYugELMimgyqzbazH3cSawSHqZSq/FK0ID PuYv3quhdyGmzRxlbcQe90tZdtbv0bIAXe+5cJkwyzPI04AnAlXVFtEc9YYnLd3/Jy G7XFHn7oxmv6g== From: Sean Nyekjaer To: Miquel Raynal , Richard Weinberger , Vignesh Raghavendra , Boris Brezillon Cc: Sean Nyekjaer , Boris Brezillon , stable@vger.kernel.org, linux-mtd@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH v4] mtd: rawnand: protect access to rawnand devices while in suspend Date: Tue, 8 Feb 2022 09:52:13 +0100 Message-Id: <20220208085213.1838273-1-sean@geanix.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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 Prevent rawnand access while in a suspended state. Commit 013e6292aaf5 ("mtd: rawnand: Simplify the locking") allows the rawnand layer to return errors rather than waiting in a blocking wait. Tested on a iMX6ULL. Fixes: 013e6292aaf5 ("mtd: rawnand: Simplify the locking") Signed-off-by: Sean Nyekjaer Reviewed-by: Boris Brezillon Cc: stable@vger.kernel.org --- Changes since v1: - fixed uninitialized return Changes since v2: - fixed wait queue description Changes since v3: - fixed typo in commit msg - added stable tag in signoff area drivers/mtd/nand/raw/nand_base.c | 44 +++++++++++++++----------------- include/linux/mtd/rawnand.h | 2 ++ 2 files changed, 22 insertions(+), 24 deletions(-) diff --git a/drivers/mtd/nand/raw/nand_base.c b/drivers/mtd/nand/raw/nand_base.c index e7b2ba016d8c..8daaba96edb2 100644 --- a/drivers/mtd/nand/raw/nand_base.c +++ b/drivers/mtd/nand/raw/nand_base.c @@ -338,16 +338,19 @@ static int nand_isbad_bbm(struct nand_chip *chip, loff_t ofs) * * Return: -EBUSY if the chip has been suspended, 0 otherwise */ -static int nand_get_device(struct nand_chip *chip) +static void nand_get_device(struct nand_chip *chip) { - mutex_lock(&chip->lock); - if (chip->suspended) { + /* Wait until the device is resumed. */ + while (1) { + mutex_lock(&chip->lock); + if (!chip->suspended) { + mutex_lock(&chip->controller->lock); + return; + } mutex_unlock(&chip->lock); - return -EBUSY; - } - mutex_lock(&chip->controller->lock); - return 0; + wait_event(chip->resume_wq, !chip->suspended); + } } /** @@ -576,9 +579,7 @@ static int nand_block_markbad_lowlevel(struct nand_chip *chip, loff_t ofs) nand_erase_nand(chip, &einfo, 0); /* Write bad block marker to OOB */ - ret = nand_get_device(chip); - if (ret) - return ret; + nand_get_device(chip); ret = nand_markbad_bbm(chip, ofs); nand_release_device(chip); @@ -3826,9 +3827,7 @@ static int nand_read_oob(struct mtd_info *mtd, loff_t from, ops->mode != MTD_OPS_RAW) return -ENOTSUPP; - ret = nand_get_device(chip); - if (ret) - return ret; + nand_get_device(chip); if (!ops->datbuf) ret = nand_do_read_oob(chip, from, ops); @@ -4415,13 +4414,11 @@ static int nand_write_oob(struct mtd_info *mtd, loff_t to, struct mtd_oob_ops *ops) { struct nand_chip *chip = mtd_to_nand(mtd); - int ret; + int ret = 0; ops->retlen = 0; - ret = nand_get_device(chip); - if (ret) - return ret; + nand_get_device(chip); switch (ops->mode) { case MTD_OPS_PLACE_OOB: @@ -4481,9 +4478,7 @@ int nand_erase_nand(struct nand_chip *chip, struct erase_info *instr, return -EIO; /* Grab the lock and see if the device is available */ - ret = nand_get_device(chip); - if (ret) - return ret; + nand_get_device(chip); /* Shift to get first page */ page = (int)(instr->addr >> chip->page_shift); @@ -4570,7 +4565,7 @@ static void nand_sync(struct mtd_info *mtd) pr_debug("%s: called\n", __func__); /* Grab the lock and see if the device is available */ - WARN_ON(nand_get_device(chip)); + nand_get_device(chip); /* Release it and go back */ nand_release_device(chip); } @@ -4587,9 +4582,7 @@ static int nand_block_isbad(struct mtd_info *mtd, loff_t offs) int ret; /* Select the NAND device */ - ret = nand_get_device(chip); - if (ret) - return ret; + nand_get_device(chip); nand_select_target(chip, chipnr); @@ -4660,6 +4653,8 @@ static void nand_resume(struct mtd_info *mtd) __func__); } mutex_unlock(&chip->lock); + + wake_up_all(&chip->resume_wq); } /** @@ -5437,6 +5432,7 @@ static int nand_scan_ident(struct nand_chip *chip, unsigned int maxchips, chip->cur_cs = -1; mutex_init(&chip->lock); + init_waitqueue_head(&chip->resume_wq); /* Enforce the right timings for reset/detection */ chip->current_interface_config = nand_get_reset_interface_config(); diff --git a/include/linux/mtd/rawnand.h b/include/linux/mtd/rawnand.h index 5b88cd51fadb..dcf90144d70b 100644 --- a/include/linux/mtd/rawnand.h +++ b/include/linux/mtd/rawnand.h @@ -1240,6 +1240,7 @@ struct nand_secure_region { * @lock: Lock protecting the suspended field. Also used to serialize accesses * to the NAND device * @suspended: Set to 1 when the device is suspended, 0 when it's not + * @resume_wq: wait queue to sleep if rawnand is in suspended state. * @cur_cs: Currently selected target. -1 means no target selected, otherwise we * should always have cur_cs >= 0 && cur_cs < nanddev_ntargets(). * NAND Controller drivers should not modify this value, but they're @@ -1294,6 +1295,7 @@ struct nand_chip { /* Internals */ struct mutex lock; unsigned int suspended : 1; + wait_queue_head_t resume_wq; int cur_cs; int read_retries; struct nand_secure_region *secure_regions; -- 2.34.1