Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp1014134pxb; Wed, 6 Apr 2022 06:44:57 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxR9zvCZvwVS5W05MXUoDWgK7YL+7Rww89JrRxtb49R016sw7N8KVTaQXjozh9GjZ4rHfXA X-Received: by 2002:a05:6a00:138a:b0:4fd:a7ec:6131 with SMTP id t10-20020a056a00138a00b004fda7ec6131mr8940690pfg.10.1649252697675; Wed, 06 Apr 2022 06:44:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649252697; cv=none; d=google.com; s=arc-20160816; b=hK84B+jO5Ha0Jetis3LLF6c1ntg7SKhaGep/GIIhq58LP7G+tClpfD4h2cDGBaZB1Y mAie+XkReGhRvVKtTTNFT7Y7s21tpEu8SAfxn9n1zIiRmjDacnsRGt0fUnBlzJrIArUK GRJZsdCG+nqXbehStDVYRlkLkwWI5Oo1SvorZXMgTR+/C8CDLmcseSCPeXejZ/6Hugvf Y7+22ygFxQ9gt0Bh8zPOilpykn/HcPDwkp/g/I45ceiyAQRB3UTOlHOQeo3GkU3UN2L8 VXwIl8nDvQhBwolnN8Xgb+MaI6Pl6hxG8EoVEsNJOdIqFU+ddxNhfBjYA0xDV1FL//eH o+xg== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=O+9oek69zUfoY21zLTWhDqRvOzCIMLyxO2T9XBdUECA=; b=jFp9yJdVloPecrWdwVeVraT61jKqrfHx5x86GzF+Xk9UyAnNkOWTlitt9EU5CY0wIC xhhbdp+0c+187fdOIHRNSyRUDt6Bqv2W7Wb1Ce1GdMU+h+GllGLiKuSNvbl4FOFIyHAV 0oqWP/5PyfsQqPJJpuyJr+e9ByJxr9bpN+MxNJvorXn6ZBk5ebZ2m8diF+/cx+iRdumQ UKb4xuXDPryvVLc3VdM9qFaxWrSJ6V+s3au88S7EQYr9am5xAFPhnU3nXS8RsjMpvt4G xHcWKwf2UJfAVruNW5S4yzKU+LPx4tOhxH0lxysiUoRnYB6MisDhMsbWciZXGMRMp/zt 1lig== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=PtG+X1yt; 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=linuxfoundation.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id x2-20020a170902ec8200b00153e9a38290si15496904plg.264.2022.04.06.06.44.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Apr 2022 06:44:57 -0700 (PDT) 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=@linuxfoundation.org header.s=korg header.b=PtG+X1yt; 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=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 1B618605B04; Wed, 6 Apr 2022 04:32:21 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1450821AbiDEW3I (ORCPT + 99 others); Tue, 5 Apr 2022 18:29:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45470 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1354785AbiDEKPt (ORCPT ); Tue, 5 Apr 2022 06:15:49 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 01F744A3F6; Tue, 5 Apr 2022 03:02:49 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 910F0616E7; Tue, 5 Apr 2022 10:02:49 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A1C15C385A1; Tue, 5 Apr 2022 10:02:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1649152969; bh=j4snWJe9nUqm/qqeWyVMDCxVVL4IKFAL6keBPT7uBzQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PtG+X1ytDp3Hq8oY3J+YWCjtr1RpIq3IngfKKLdt6v6sY7U17SKu4CtrnmTv9LuzF 4hr9aMe9SEnoCh0BtNPFt0S2o6ly8b3jgxdDKxemI2CLzIeogfrRJQlBBqVEVSFaxu Vq/cuOqJiQlsK9bVM8Zru9SQr/vUNXa2SlQhNag8= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Sean Nyekjaer , Boris Brezillon , Miquel Raynal Subject: [PATCH 5.10 054/599] mtd: rawnand: protect access to rawnand devices while in suspend Date: Tue, 5 Apr 2022 09:25:48 +0200 Message-Id: <20220405070300.435966919@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220405070258.802373272@linuxfoundation.org> References: <20220405070258.802373272@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, 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 From: Sean Nyekjaer commit 8cba323437a49a45756d661f500b324fc2d486fe upstream. 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 Signed-off-by: Miquel Raynal Link: https://lore.kernel.org/linux-mtd/20220208085213.1838273-1-sean@geanix.com Signed-off-by: Greg Kroah-Hartman --- drivers/mtd/nand/raw/nand_base.c | 44 +++++++++++++++++---------------------- include/linux/mtd/rawnand.h | 2 + 2 files changed, 22 insertions(+), 24 deletions(-) --- a/drivers/mtd/nand/raw/nand_base.c +++ b/drivers/mtd/nand/raw/nand_base.c @@ -297,16 +297,19 @@ static int nand_isbad_bbm(struct nand_ch * * 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); + } } /** @@ -531,9 +534,7 @@ static int nand_block_markbad_lowlevel(s 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); @@ -3534,9 +3535,7 @@ static int nand_read_oob(struct mtd_info 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); @@ -4119,13 +4118,11 @@ static int nand_write_oob(struct mtd_inf 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: @@ -4181,9 +4178,7 @@ int nand_erase_nand(struct nand_chip *ch return -EINVAL; /* 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); @@ -4270,7 +4265,7 @@ static void nand_sync(struct mtd_info *m 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); } @@ -4287,9 +4282,7 @@ static int nand_block_isbad(struct mtd_i int ret; /* Select the NAND device */ - ret = nand_get_device(chip); - if (ret) - return ret; + nand_get_device(chip); nand_select_target(chip, chipnr); @@ -4360,6 +4353,8 @@ static void nand_resume(struct mtd_info __func__); } mutex_unlock(&chip->lock); + + wake_up_all(&chip->resume_wq); } /** @@ -5068,6 +5063,7 @@ static int nand_scan_ident(struct nand_c 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(); --- a/include/linux/mtd/rawnand.h +++ b/include/linux/mtd/rawnand.h @@ -1083,6 +1083,7 @@ struct nand_manufacturer { * @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 @@ -1135,6 +1136,7 @@ struct nand_chip { /* Internals */ struct mutex lock; unsigned int suspended : 1; + wait_queue_head_t resume_wq; int cur_cs; int read_retries;