Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp2468745pxj; Mon, 10 May 2021 03:51:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzk+vQoUzwMiCP77mC9zViUKbWjJ7MNfrhyvj1uphpVuA6T9/wTdJ+WeJTKsx06JkLxorKy X-Received: by 2002:a05:6e02:13e2:: with SMTP id w2mr21361015ilj.53.1620643877047; Mon, 10 May 2021 03:51:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620643877; cv=none; d=google.com; s=arc-20160816; b=rQCGh/sJM1QoV0MhVmDvWWohwSsNI3ayYgndHLVmiwOBGDbrI+9vYRV155S8okD4uB SksUGBy+B3ZItAnP2YoWfll7wlxu6gL3z+gQdQvMjMX/1WqdA5YjlGdvhWQOKrU0CWe8 hsGwGzADZdeCwKaLiQlmqOeQ0pw937wwUG1nLHMPbeXtu2jmq3RwHyYCL1NjdC6h7ctA +Dvf7VL1Gf+lplFYbddcGmvCfSp4lmOwXLuL0V33gvR3l8p3OHYQjxqBXopf3NH34SIq NKy4GPnLb0Yr/CYAdzFNUEAp1jvbFYu9qGr+0yXphnSYBZbkJEfVebzatoczX4fnVd4v t4FA== 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=RN1Kt6zjOuOOkS24Wk09f2xj65ntwbfIftnM0tRsZZo=; b=tu4HUDG7vtNZXpP3rnwgspVYMjYDCaPlyEnOqkRPyI0GWoFswtnobY98i+8ZxZo4m+ cD0upF95XY6kIrF3/mxQrSFPXaxSsII54RvB3iIYvRJFRLWrcebtXSY6hXQlximd3FIv 8uITo+jQxzDNRMvsgsHvd4K6y7RskTc0tYTQOt7flGjDJSAH+KySzKdK1+G5q15bMLc2 xw5eujgfVyiyil1GuuuubNGJXqKrHEeAfR5XZb9iyD95xSHPKCnAXEc5C29LcoIRsoFE vZp0DrJhG1U3OVV6XBPWuOvffAaAhcSrMFKsqHbgkRzLtNy/Z+GQPtciMvJgpJYGW+n4 uhFQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=LU3Lpw9H; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d21si6731229jak.119.2021.05.10.03.51.04; Mon, 10 May 2021 03:51:17 -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; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=LU3Lpw9H; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232867AbhEJKvP (ORCPT + 99 others); Mon, 10 May 2021 06:51:15 -0400 Received: from mail.kernel.org ([198.145.29.99]:49358 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231264AbhEJKlC (ORCPT ); Mon, 10 May 2021 06:41:02 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id E5C006191B; Mon, 10 May 2021 10:31:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1620642705; bh=Zc+odPENmvZ3jba+MZBcpCe6km+OfUduITkYNJZdoZQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LU3Lpw9HEVb37JZpmmfTyl5F4tMIBVRFmTlr5jVZUAi83HpkplsT9QJgt2voNRj2U f2C7QUK6l+pTFpncYCzhqrU8Q7yd7PKufkBJ5DflLMj/ID1Zwn+t2S6U/fd8Sp1Gaq jmitvz/blj28t9d8eOgzRb5utn/0JeiARgGhE224= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Xiang Chen , Yicong Yang , Tudor Ambarus , Michael Walle Subject: [PATCH 5.10 018/299] mtd: spi-nor: core: Fix an issue of releasing resources during read/write Date: Mon, 10 May 2021 12:16:55 +0200 Message-Id: <20210510102005.445088604@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210510102004.821838356@linuxfoundation.org> References: <20210510102004.821838356@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Xiang Chen commit be94215be1ab19e5d38f50962f611c88d4bfc83a upstream. If rmmod the driver during read or write, the driver will release the resources which are used during read or write, so it is possible to refer to NULL pointer. Use the testcase "mtd_debug read /dev/mtd0 0xc00000 0x400000 dest_file & sleep 0.5;rmmod spi_hisi_sfc_v3xx.ko", the issue can be reproduced in hisi_sfc_v3xx driver. To avoid the issue, fill the interface _get_device and _put_device of mtd_info to grab the reference to the spi controller driver module, so the request of rmmod the driver is rejected before read/write is finished. Fixes: b199489d37b2 ("mtd: spi-nor: add the framework for SPI NOR") Signed-off-by: Xiang Chen Signed-off-by: Yicong Yang Signed-off-by: Tudor Ambarus Tested-by: Michael Walle Tested-by: Tudor Ambarus Reviewed-by: Michael Walle Cc: stable@vger.kernel.org Link: https://lore.kernel.org/r/1617262486-4223-1-git-send-email-yangyicong@hisilicon.com Signed-off-by: Greg Kroah-Hartman --- drivers/mtd/spi-nor/core.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) --- a/drivers/mtd/spi-nor/core.c +++ b/drivers/mtd/spi-nor/core.c @@ -2981,6 +2981,37 @@ static void spi_nor_resume(struct mtd_in dev_err(dev, "resume() failed\n"); } +static int spi_nor_get_device(struct mtd_info *mtd) +{ + struct mtd_info *master = mtd_get_master(mtd); + struct spi_nor *nor = mtd_to_spi_nor(master); + struct device *dev; + + if (nor->spimem) + dev = nor->spimem->spi->controller->dev.parent; + else + dev = nor->dev; + + if (!try_module_get(dev->driver->owner)) + return -ENODEV; + + return 0; +} + +static void spi_nor_put_device(struct mtd_info *mtd) +{ + struct mtd_info *master = mtd_get_master(mtd); + struct spi_nor *nor = mtd_to_spi_nor(master); + struct device *dev; + + if (nor->spimem) + dev = nor->spimem->spi->controller->dev.parent; + else + dev = nor->dev; + + module_put(dev->driver->owner); +} + void spi_nor_restore(struct spi_nor *nor) { /* restore the addressing mode */ @@ -3157,6 +3188,8 @@ int spi_nor_scan(struct spi_nor *nor, co mtd->_erase = spi_nor_erase; mtd->_read = spi_nor_read; mtd->_resume = spi_nor_resume; + mtd->_get_device = spi_nor_get_device; + mtd->_put_device = spi_nor_put_device; if (nor->params->locking_ops) { mtd->_lock = spi_nor_lock;