public inbox for linux-mtd@lists.infradead.org
 help / color / mirror / Atom feed
From: "Winkler, Tomas" <tomas.winkler@intel.com>
To: Richard Weinberger <richard@nod.at>
Cc: linux-kernel <linux-kernel@vger.kernel.org>,
	linux-mtd <linux-mtd@lists.infradead.org>,
	Vignesh Raghavendra <vigneshr@ti.com>,
	Miquel Raynal <miquel.raynal@bootlin.com>
Subject: RE: [PATCH] mtd: use refcount to prevent corruption
Date: Fri, 29 Jan 2021 16:12:04 +0000	[thread overview]
Message-ID: <1a420da9edf449dcbc38dc17505b0ad5@intel.com> (raw)
In-Reply-To: <1363048722.339069.1611865409332.JavaMail.zimbra@nod.at>

> 
> Tomas,
> 
> ----- Ursprüngliche Mail -----
> >> As Richard was saying, we are really open to enhance MTD refcounting.
> >>
> >> However, the issue you are facing is, IMHO, not related to MTD but to MFD.
> >> There should be a way to avoid MFD to vanish by taking a reference of
> >> it through mtd->_get_device(). I don't think addressing the case
> >> where MFD vanishes while MTD (as a user) is still active is the right
> approach.
> >
> > I think it won't work because MFD sub-driver remove() is called   and it must
> > succeed because the main device  is not accessible unlike glueubi
> > which just returns -EBUSY.
> 
> Well, the trick in glubi (and other MTDs with "hotplug" support) is not to reject
> removal of the sub-device. ->_put_device() is of return type void.
> The key is grabbing a reference on the sub-device in ->_get_device() such that
> the layer below doesn't even try to remove while the MTD is in use.
> 
> > so we postpone the mtd unregister to  mtd_info->_put_device()  but it
> > that state we have nothing to hold on as the device is gone in
> > remove() User will fail anyway, as the underlying device is not
> > functional in that state.
> > Anyway I've tried your suggestion, the kernel is crashing, hope I
> > haven't done some silly bug.
> 
> Can you point us to the affected code?
> This would help a lot to understand the issue better.
Below is approximately the snippet 

> I'm sure we can find a solution.
Okay though  I've already found a working solution. 

struct my_spi {
        struct mtd_info mtd;
        struct kref refcnt;
        bool init;
};

static int my_spi_get_mtd(struct mtd_info *mtd)
{
        struct my_spi *spi = container_of(mtd, struct my_spi, mtd);

        if (!spi->init)
                return -ENODEV;

        kref_get(&spi->refcnt);

        spi->init = true;

        return 0;
}

static void my_spi_unregister_mtd(struct kref *kref)
{
        struct my_spi *spi = container_of(kref, struct my_spi, refcnt);

        mtd_device_unregister(&spi->mtd);
        kfree(spi);
}

static int my_spi_put_mtd(struct mtd_info *mtd)
{
        struct my_spi *spi = container_of(mtd, struct my_spi, mtd);

        kref_put(&spi->refcnt, my_spi_unregister_mtd);

        return 0;
}

static int my_spi_init_mtd(static my_spi *spi)
{
        ...
        spi->mtd._get_device = my_spi_get_mtd;
        spi->mtd._get_device = my_spi_put_mtd;
        kref_init(&spi->refcnt);
        ...
}

static int my_spi_probe(struct platform_device *platdev)
{
        ...
        spi = kzalloc(size, GFP_KERNEL);
        spi->init = true;
        my_spi_init_mtd(spi);
        platform_set_drvdata(platdev, spi);
        ...
        return 0;
}


static int my_spi_remove(struct platform_device *platdev)
{
        struct my_spi *spi = platform_get_drvdata(platdev);
        spi->init = false;
        my_spi_put_mtd(&spi->mtd);
        platform_set_drvdata(platdev, NULL);
}
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

  reply	other threads:[~2021-01-29 16:13 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-27 20:03 [PATCH] mtd: use refcount to prevent corruption Tomas Winkler
2021-01-27 20:46 ` Richard Weinberger
2021-01-27 20:55   ` Winkler, Tomas
2021-01-27 21:17     ` Richard Weinberger
2021-01-28  6:33       ` Winkler, Tomas
2021-01-28  7:47         ` Richard Weinberger
2021-01-28  8:53           ` Winkler, Tomas
2021-01-28  9:00             ` Miquel Raynal
2021-01-28 17:57               ` Winkler, Tomas
2021-01-28 20:23                 ` Richard Weinberger
2021-01-29 16:12                   ` Winkler, Tomas [this message]
2021-02-13 17:09                   ` Winkler, Tomas
2021-02-15 13:43                     ` Richard Weinberger

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=1a420da9edf449dcbc38dc17505b0ad5@intel.com \
    --to=tomas.winkler@intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mtd@lists.infradead.org \
    --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