From: James Hilliard <james.hilliard1@gmail.com>
To: Miquel Raynal <miquel.raynal@bootlin.com>,
Richard Weinberger <richard@nod.at>,
Vignesh Raghavendra <vigneshr@ti.com>
Cc: linux-mtd@lists.infradead.org, linux-kernel@vger.kernel.org,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
James Hilliard <james.hilliard1@gmail.com>,
Maxim Levitsky <maximlevitsky@gmail.com>
Subject: [PATCH v3 55/58] mtd: rawnand: r852: retain chip state across removal
Date: Wed, 11 Nov 2026 18:28:34 -0700 [thread overview]
Message-ID: <20261111-submit-rawnand-retained-handle-lifetime-v1-v3-55-ce537999741d@gmail.com> (raw)
In-Reply-To: <20261111-submit-rawnand-retained-handle-lifetime-v1-v3-0-ce537999741d@gmail.com>
A removed card can still have open MTD handles. Reinitializing the same
nand_chip for a newly inserted card lets those handles access
replacement media, while freeing it during PCI removal leaves dangling
references.
Allocate a fresh chip for every insertion and separate SmartMedia
scanning from publication so lifetime ownership is installed first.
Disconnect the old chip before disabling its hardware, and retain it and
the controller until their final MTD reference is released. Stop
IRQ-driven card-work scheduling before destroying the workqueue during
PCI removal.
Run NAND cleanup during disconnection; the final core-owned put only
releases retained allocation and layout data.
Cc: Maxim Levitsky <maximlevitsky@gmail.com>
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
drivers/mtd/nand/raw/r852.c | 103 +++++++++++++++++++++++----------------
drivers/mtd/nand/raw/r852.h | 1 +
drivers/mtd/nand/raw/sm_common.c | 15 ++----
drivers/mtd/nand/raw/sm_common.h | 2 +-
4 files changed, 65 insertions(+), 56 deletions(-)
diff --git a/drivers/mtd/nand/raw/r852.c b/drivers/mtd/nand/raw/r852.c
index 566662d45c2c..3c3161bc4b30 100644
--- a/drivers/mtd/nand/raw/r852.c
+++ b/drivers/mtd/nand/raw/r852.c
@@ -625,16 +625,29 @@ static void r852_update_media_status(struct r852_device *dev)
spin_unlock_irqrestore(&dev->irqlock, flags);
}
-/*
- * Register the nand device
- * Called when the card is detected
- */
+/* Register a new chip instance for each inserted card. */
static int r852_register_nand_device(struct r852_device *dev)
{
- struct mtd_info *mtd = nand_to_mtd(dev->chip);
+ struct nand_chip *chip;
+ struct mtd_info *mtd;
+ int ret;
WARN_ON(dev->card_registered);
+ chip = kzalloc_obj(*chip);
+ if (!chip)
+ goto error1;
+
+ chip->legacy.cmd_ctrl = r852_cmdctl;
+ chip->legacy.waitfunc = r852_wait;
+ chip->legacy.dev_ready = r852_ready;
+ chip->legacy.read_byte = r852_read_byte;
+ chip->legacy.read_buf = r852_read_buf;
+ chip->legacy.write_buf = r852_write_buf;
+ chip->controller = &dev->controller;
+ nand_set_controller_data(chip, dev);
+ dev->chip = chip;
+ mtd = nand_to_mtd(chip);
mtd->parent_dev = &dev->pci_dev->dev;
if (dev->readonly)
@@ -642,8 +655,17 @@ static int r852_register_nand_device(struct r852_device *dev)
r852_engine_enable(dev);
- if (sm_register_device(mtd, dev->sm))
- goto error1;
+ ret = sm_scan_device(mtd, dev->sm);
+ if (ret)
+ goto free_chip;
+ ret = nand_set_lifetime(chip, chip);
+ if (ret) {
+ nand_cleanup(chip);
+ goto free_chip;
+ }
+ ret = mtd_device_register(mtd, NULL, 0);
+ if (ret)
+ goto put_chip;
if (device_create_file(mtd->dev, &dev_attr_media_type)) {
message("can't create media type sysfs attribute");
@@ -653,8 +675,15 @@ static int r852_register_nand_device(struct r852_device *dev)
dev->card_registered = 1;
return 0;
error3:
- WARN_ON(mtd_device_unregister(nand_to_mtd(dev->chip)));
- nand_cleanup(dev->chip);
+ WARN_ON(mtd_device_unregister(mtd));
+put_chip:
+ nand_disconnect(chip);
+ dev->chip = NULL;
+ mtd_put_master(mtd);
+ goto error1;
+free_chip:
+ dev->chip = NULL;
+ kfree(chip);
error1:
/* Force card redetect */
dev->card_detected = 0;
@@ -667,16 +696,19 @@ static int r852_register_nand_device(struct r852_device *dev)
static void r852_unregister_nand_device(struct r852_device *dev)
{
- struct mtd_info *mtd = nand_to_mtd(dev->chip);
+ struct mtd_info *mtd;
if (!dev->card_registered)
return;
+ mtd = nand_to_mtd(dev->chip);
device_remove_file(mtd->dev, &dev_attr_media_type);
WARN_ON(mtd_device_unregister(mtd));
- nand_cleanup(dev->chip);
+ nand_disconnect(dev->chip);
r852_engine_disable(dev);
+ dev->chip = NULL;
dev->card_registered = 0;
+ mtd_put_master(mtd);
}
/* Card state updater */
@@ -734,7 +766,7 @@ static irqreturn_t r852_irq(int irq, void *data)
card_status = r852_read_reg(dev, R852_CARD_IRQ_STA);
r852_write_reg(dev, R852_CARD_IRQ_STA, card_status);
- if (card_status & (R852_CARD_IRQ_INSERT|R852_CARD_IRQ_REMOVE)) {
+ if (card_status & (R852_CARD_IRQ_INSERT | R852_CARD_IRQ_REMOVE)) {
ret = IRQ_HANDLED;
dev->card_detected = !!(card_status & R852_CARD_IRQ_INSERT);
@@ -752,8 +784,10 @@ static irqreturn_t r852_irq(int irq, void *data)
/* let, card state to settle a bit, and then do the work */
dev->card_unstable = 1;
- queue_delayed_work(dev->card_workqueue,
- &dev->card_detect_work, msecs_to_jiffies(100));
+ if (!dev->removing)
+ queue_delayed_work(dev->card_workqueue,
+ &dev->card_detect_work,
+ msecs_to_jiffies(100));
goto out;
}
@@ -844,7 +878,6 @@ static const struct nand_controller_ops r852_ops = {
static int r852_probe(struct pci_dev *pci_dev, const struct pci_device_id *id)
{
int error;
- struct nand_chip *chip;
struct r852_device *dev;
/* pci initialization */
@@ -866,42 +899,27 @@ static int r852_probe(struct pci_dev *pci_dev, const struct pci_device_id *id)
error = -ENOMEM;
- /* init nand chip, but register it only on card insert */
- chip = kzalloc_obj(struct nand_chip);
-
- if (!chip)
- goto error4;
-
- /* commands */
- chip->legacy.cmd_ctrl = r852_cmdctl;
- chip->legacy.waitfunc = r852_wait;
- chip->legacy.dev_ready = r852_ready;
-
- /* I/O */
- chip->legacy.read_byte = r852_read_byte;
- chip->legacy.read_buf = r852_read_buf;
- chip->legacy.write_buf = r852_write_buf;
-
/* init our device structure */
dev = kzalloc_obj(struct r852_device);
if (!dev)
- goto error5;
+ goto error4;
+ error = devm_nand_controller_init(&pci_dev->dev, &dev->controller, dev);
+ if (error)
+ goto error4;
- nand_set_controller_data(chip, dev);
- dev->chip = chip;
dev->pci_dev = pci_dev;
pci_set_drvdata(pci_dev, dev);
- nand_controller_init(&dev->controller);
dev->controller.ops = &r852_ops;
- chip->controller = &dev->controller;
dev->bounce_buffer = dma_alloc_coherent(&pci_dev->dev, R852_DMA_LEN,
&dev->phys_bounce_buffer, GFP_KERNEL);
- if (!dev->bounce_buffer)
+ if (!dev->bounce_buffer) {
+ error = -ENOMEM;
goto error6;
+ }
error = -ENODEV;
@@ -961,9 +979,6 @@ static int r852_probe(struct pci_dev *pci_dev, const struct pci_device_id *id)
dma_free_coherent(&pci_dev->dev, R852_DMA_LEN, dev->bounce_buffer,
dev->phys_bounce_buffer);
error6:
- kfree(dev);
-error5:
- kfree(chip);
error4:
pci_release_regions(pci_dev);
error3:
@@ -976,6 +991,11 @@ static int r852_probe(struct pci_dev *pci_dev, const struct pci_device_id *id)
static void r852_remove(struct pci_dev *pci_dev)
{
struct r852_device *dev = pci_get_drvdata(pci_dev);
+ unsigned long flags;
+
+ spin_lock_irqsave(&dev->irqlock, flags);
+ dev->removing = true;
+ spin_unlock_irqrestore(&dev->irqlock, flags);
/* Stop detect workqueue -
we are going to unregister the device anyway*/
@@ -995,9 +1015,6 @@ static void r852_remove(struct pci_dev *pci_dev)
dma_free_coherent(&pci_dev->dev, R852_DMA_LEN, dev->bounce_buffer,
dev->phys_bounce_buffer);
- kfree(dev->chip);
- kfree(dev);
-
/* Shutdown the PCI device */
pci_release_regions(pci_dev);
pci_disable_device(pci_dev);
diff --git a/drivers/mtd/nand/raw/r852.h b/drivers/mtd/nand/raw/r852.h
index 96fe301d15da..a89ab61ea51e 100644
--- a/drivers/mtd/nand/raw/r852.h
+++ b/drivers/mtd/nand/raw/r852.h
@@ -128,6 +128,7 @@ struct r852_device {
struct delayed_work card_detect_work;
struct workqueue_struct *card_workqueue;
int card_registered; /* card registered with mtd */
+ bool removing; /* stop card work during PCI removal */
int card_detected; /* card detected in slot */
int card_unstable; /* whenever the card is inserted,
is not known yet */
diff --git a/drivers/mtd/nand/raw/sm_common.c b/drivers/mtd/nand/raw/sm_common.c
index e238784c8c3e..a79e3c3c77d1 100644
--- a/drivers/mtd/nand/raw/sm_common.c
+++ b/drivers/mtd/nand/raw/sm_common.c
@@ -182,28 +182,19 @@ static const struct nand_controller_ops sm_controller_ops = {
.attach_chip = sm_attach_chip,
};
-int sm_register_device(struct mtd_info *mtd, int smartmedia)
+int sm_scan_device(struct mtd_info *mtd, int smartmedia)
{
struct nand_chip *chip = mtd_to_nand(mtd);
struct nand_flash_dev *flash_ids;
- int ret;
chip->options |= NAND_SKIP_BBTSCAN;
/* Scan for card properties */
chip->legacy.dummy_controller.ops = &sm_controller_ops;
flash_ids = smartmedia ? nand_smartmedia_flash_ids : nand_xd_flash_ids;
- ret = nand_scan_with_ids(chip, 1, flash_ids);
- if (ret)
- return ret;
-
- ret = mtd_device_register(mtd, NULL, 0);
- if (ret)
- nand_cleanup(chip);
-
- return ret;
+ return nand_scan_with_ids(chip, 1, flash_ids);
}
-EXPORT_SYMBOL_GPL(sm_register_device);
+EXPORT_SYMBOL_GPL(sm_scan_device);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Maxim Levitsky <maximlevitsky@gmail.com>");
diff --git a/drivers/mtd/nand/raw/sm_common.h b/drivers/mtd/nand/raw/sm_common.h
index 57fc9f86f9ee..d8a330d6b730 100644
--- a/drivers/mtd/nand/raw/sm_common.h
+++ b/drivers/mtd/nand/raw/sm_common.h
@@ -33,7 +33,7 @@ struct sm_oob {
#define SM_SMALL_OOB_SIZE 8
-int sm_register_device(struct mtd_info *mtd, int smartmedia);
+int sm_scan_device(struct mtd_info *mtd, int smartmedia);
static inline int sm_sector_valid(struct sm_oob *oob)
--
2.53.0
next prev parent reply other threads:[~2026-11-12 1:29 UTC|newest]
Thread overview: 60+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-12 1:27 [PATCH v3 00/58] mtd: rawnand: retain chip and controller " James Hilliard
2026-11-12 1:27 ` [PATCH v3 01/58] nvmem: disconnect provider callbacks before unregister returns James Hilliard
2026-11-12 1:27 ` [PATCH v3 02/58] mtd: acquire an MTD handle from a referenced class device James Hilliard
2026-11-19 15:28 ` Vadim Fedorenko
2026-11-12 1:27 ` [PATCH v3 03/58] mtd: separate class devices from retained backing objects James Hilliard
2026-11-12 1:27 ` [PATCH v3 04/58] mtd: retain virtual concatenations across component removal James Hilliard
2026-11-12 1:27 ` [PATCH v3 05/58] mtd: rawnand: separate hardware disconnection from cleanup James Hilliard
2026-11-12 1:27 ` [PATCH v3 06/58] mtd: rawnand: share retained chip and controller ownership James Hilliard
2026-11-12 1:27 ` [PATCH v3 07/58] mtd: rawnand: sunxi: retain chip state until the final MTD put James Hilliard
2026-11-12 1:27 ` [PATCH v3 08/58] mtd: rawnand: plat_nand: retain open-handle state on removal James Hilliard
2026-11-12 1:27 ` [PATCH v3 09/58] mtd: rawnand: gpio: " James Hilliard
2026-11-12 1:27 ` [PATCH v3 10/58] mtd: rawnand: stm32_fmc2: " James Hilliard
2026-11-12 1:27 ` [PATCH v3 11/58] mtd: rawnand: denali: retain chips across controller removal James Hilliard
2026-11-12 1:27 ` [PATCH v3 12/58] mtd: rawnand: arasan: " James Hilliard
2026-11-12 1:27 ` [PATCH v3 13/58] mtd: rawnand: mxic: retain open-handle state on removal James Hilliard
2026-11-12 1:27 ` [PATCH v3 14/58] mtd: rawnand: intel: " James Hilliard
2026-11-12 1:27 ` [PATCH v3 15/58] mtd: rawnand: tegra: " James Hilliard
2026-11-12 1:27 ` [PATCH v3 16/58] mtd: rawnand: mtk: retain chips across controller removal James Hilliard
2026-11-12 1:27 ` [PATCH v3 17/58] mtd: rawnand: rockchip: " James Hilliard
2026-11-12 1:27 ` [PATCH v3 18/58] mtd: rawnand: atmel: " James Hilliard
2026-11-12 1:27 ` [PATCH v3 19/58] mtd: rawnand: orion: retain open-handle state on removal James Hilliard
2026-11-12 1:27 ` [PATCH v3 20/58] mtd: rawnand: socrates: " James Hilliard
2026-11-12 1:28 ` [PATCH v3 21/58] mtd: rawnand: fsl_upm: " James Hilliard
2026-11-12 1:28 ` [PATCH v3 22/58] mtd: rawnand: pasemi: " James Hilliard
2026-11-12 1:28 ` [PATCH v3 23/58] mtd: rawnand: ams-delta: " James Hilliard
2026-11-12 1:28 ` [PATCH v3 24/58] mtd: rawnand: xway: " James Hilliard
2026-11-12 1:28 ` [PATCH v3 25/58] mtd: rawnand: hisi504: " James Hilliard
2026-11-12 1:28 ` [PATCH v3 26/58] mtd: rawnand: vf610: " James Hilliard
2026-11-12 1:28 ` [PATCH v3 27/58] mtd: rawnand: technologic: " James Hilliard
2026-11-12 1:28 ` [PATCH v3 28/58] mtd: rawnand: loongson: " James Hilliard
2026-11-12 1:28 ` [PATCH v3 29/58] mtd: rawnand: ma35d1: " James Hilliard
2026-11-12 1:28 ` [PATCH v3 30/58] mtd: rawnand: pl35x: " James Hilliard
2026-11-12 1:28 ` [PATCH v3 31/58] mtd: rawnand: mxc: " James Hilliard
2026-11-12 1:28 ` [PATCH v3 32/58] mtd: rawnand: fsmc: " James Hilliard
2026-11-12 1:28 ` [PATCH v3 33/58] mtd: rawnand: gpmi: " James Hilliard
2026-11-12 1:28 ` [PATCH v3 34/58] mtd: rawnand: omap2: " James Hilliard
2026-11-12 1:28 ` [PATCH v3 35/58] mtd: rawnand: marvell: " James Hilliard
2026-11-12 1:28 ` [PATCH v3 36/58] mtd: rawnand: renesas: " James Hilliard
2026-11-12 1:28 ` [PATCH v3 37/58] mtd: rawnand: meson: " James Hilliard
2026-11-12 1:28 ` [PATCH v3 38/58] mtd: rawnand: cadence: " James Hilliard
2026-11-12 1:28 ` [PATCH v3 39/58] mtd: rawnand: qcom: " James Hilliard
2026-11-12 1:28 ` [PATCH v3 40/58] mtd: rawnand: cafe: " James Hilliard
2026-11-12 1:28 ` [PATCH v3 41/58] mtd: rawnand: brcmnand: " James Hilliard
2026-11-12 1:28 ` [PATCH v3 42/58] mtd: rawnand: au1550nd: retain chip state across removal James Hilliard
2026-11-12 1:28 ` [PATCH v3 43/58] mtd: rawnand: sharpsl: " James Hilliard
2026-11-12 1:28 ` [PATCH v3 44/58] mtd: rawnand: davinci: " James Hilliard
2026-11-12 1:28 ` [PATCH v3 45/58] mtd: rawnand: sh_flctl: " James Hilliard
2026-11-12 1:28 ` [PATCH v3 46/58] mtd: rawnand: lpc32xx_slc: " James Hilliard
2026-11-12 1:28 ` [PATCH v3 47/58] mtd: rawnand: lpc32xx_mlc: " James Hilliard
2026-11-12 1:28 ` [PATCH v3 48/58] mtd: rawnand: ndfc: " James Hilliard
2026-11-12 1:28 ` [PATCH v3 49/58] mtd: rawnand: txx9ndfmc: " James Hilliard
2026-11-12 1:28 ` [PATCH v3 50/58] mtd: rawnand: ingenic: " James Hilliard
2026-11-12 1:28 ` [PATCH v3 51/58] mtd: rawnand: mpc5121: " James Hilliard
2026-11-12 1:28 ` [PATCH v3 52/58] mtd: rawnand: bcm47xxnflash: " James Hilliard
2026-11-12 1:28 ` [PATCH v3 53/58] mtd: rawnand: fsl_elbc: " James Hilliard
2026-11-12 1:28 ` [PATCH v3 54/58] mtd: rawnand: fsl_ifc: " James Hilliard
2026-11-12 1:28 ` James Hilliard [this message]
2026-11-12 1:28 ` [PATCH v3 56/58] mtd: rawnand: nandsim: " James Hilliard
2026-11-12 1:28 ` [PATCH v3 57/58] mtd: rawnand: cs553x: " James Hilliard
2026-11-12 1:28 ` [PATCH v3 58/58] mtd: rawnand: diskonchip: " James Hilliard
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261111-submit-rawnand-retained-handle-lifetime-v1-v3-55-ce537999741d@gmail.com \
--to=james.hilliard1@gmail.com \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mtd@lists.infradead.org \
--cc=maximlevitsky@gmail.com \
--cc=miquel.raynal@bootlin.com \
--cc=richard@nod.at \
--cc=vigneshr@ti.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®