All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] mtdcore: Infrastructure for the device release API
@ 2017-05-18  9:23 Martin Habets
  2017-05-27  0:13 ` Brian Norris
  0 siblings, 1 reply; 6+ messages in thread
From: Martin Habets @ 2017-05-18  9:23 UTC (permalink / raw)
  To: linux-mtd

This API adds a cleanup callback for the user of an MTD device.
Normally they will have allocated the mtd_info structure, and using
this callback they can free that memory when the last reference is
dropped.

CONFIG_DEBUG_KOBJECT_RELEASE can be used to detect users that free
the memory too early.

Signed-off-by: Martin Habets <mhabets@solarflare.com>
---
 drivers/mtd/mtdcore.c   |    7 ++++---
 include/linux/mtd/mtd.h |    1 +
 2 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c
index 1517da3ddd7d..c36752206edc 100644
--- a/drivers/mtd/mtdcore.c
+++ b/drivers/mtd/mtdcore.c
@@ -96,9 +96,7 @@ static LIST_HEAD(mtd_notifiers);

 #define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2)

-/* REVISIT once MTD uses the driver model better, whoever allocates
- * the mtd_info will probably want to use the release() hook...
- */
+/* Whoever allocates the mtd_info should populate the release() hook. */
 static void mtd_release(struct device *dev)
 {
        struct mtd_info *mtd = dev_get_drvdata(dev);
@@ -106,6 +104,9 @@ static void mtd_release(struct device *dev)

        /* remove /dev/mtdXro node */
        device_destroy(&mtd_class, index + 1);
+
+       if (mtd->_release)
+               (mtd->_release)(mtd);
 }

 static ssize_t mtd_type_show(struct device *dev,
diff --git a/include/linux/mtd/mtd.h b/include/linux/mtd/mtd.h
index 79b176eca04a..e61c30ac8fb6 100644
--- a/include/linux/mtd/mtd.h
+++ b/include/linux/mtd/mtd.h
@@ -327,6 +327,7 @@ struct mtd_info {
        int (*_suspend) (struct mtd_info *mtd);
        void (*_resume) (struct mtd_info *mtd);
        void (*_reboot) (struct mtd_info *mtd);
+       void (*_release) (struct mtd_info *mtd);
        /*
         * If the driver is something smart, like UBI, it may need to maintain
         * its own reference counting. The below functions are only for driver.

The information contained in this message is confidential and is intended for the addressee(s) only. If you have received this message in error, please notify the sender immediately and delete the message. Unless you are an addressee (or authorized to receive for an addressee), you may not use, copy or disclose to anyone this message or any information contained in this message. The unauthorized use, disclosure, copying or alteration of this message is strictly prohibited.

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] mtdcore: Infrastructure for the device release API
  2017-05-18  9:23 [PATCH] mtdcore: Infrastructure for the device release API Martin Habets
@ 2017-05-27  0:13 ` Brian Norris
  2017-05-31 16:15   ` Martin Habets
  0 siblings, 1 reply; 6+ messages in thread
From: Brian Norris @ 2017-05-27  0:13 UTC (permalink / raw)
  To: Martin Habets; +Cc: linux-mtd

On Thu, May 18, 2017 at 10:23:11AM +0100, Martin Habets wrote:
> This API adds a cleanup callback for the user of an MTD device.
> Normally they will have allocated the mtd_info structure, and using
> this callback they can free that memory when the last reference is
> dropped.

Can you provide an example user? This callback is meaningless (and won't
be merged) if it has no users.

> CONFIG_DEBUG_KOBJECT_RELEASE can be used to detect users that free
> the memory too early.

Are there any such users?

> The information contained in this message is confidential and is
> intended for the addressee(s) only. If you have received this message
> in error, please notify the sender immediately and delete the message.
> Unless you are an addressee (or authorized to receive for an
> addressee), you may not use, copy or disclose to anyone this message
> or any information contained in this message. The unauthorized use,
> disclosure, copying or alteration of this message is strictly
> prohibited.

Nice.

Brian

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] mtdcore: Infrastructure for the device release API
  2017-05-27  0:13 ` Brian Norris
@ 2017-05-31 16:15   ` Martin Habets
  2017-06-22 13:54     ` Martin Habets
  0 siblings, 1 reply; 6+ messages in thread
From: Martin Habets @ 2017-05-31 16:15 UTC (permalink / raw)
  To: Brian Norris; +Cc: linux-mtd

On 27/05/17 01:13, Brian Norris wrote:
> On Thu, May 18, 2017 at 10:23:11AM +0100, Martin Habets wrote:
>> This API adds a cleanup callback for the user of an MTD device.
>> Normally they will have allocated the mtd_info structure, and using
>> this callback they can free that memory when the last reference is
>> dropped.
> 
> Can you provide an example user? This callback is meaningless (and won't
> be merged) if it has no users.

I found this for the sfc driver (drivers/net/ethernet/sfc), but it also affects
all drivers that use mtd_partition (i.e. mtdpart.c).

>> CONFIG_DEBUG_KOBJECT_RELEASE can be used to detect users that free
>> the memory too early.
> 
> Are there any such users?

Apart from the sfc driver there are lots of drivers that use MTD partitions
it seems. There could be other drivers that I'm not aware of.

To highlight the issue in mtdpart.c: in del_mtd_partitions() it calls
del_mtd_device() to unregister the device and then frees the memory in
free_partition().
The freeing of memory could happen before the last reference to the device
has been dropped (and off course the memory could be handed out again to
something else).

If this callback is acceptable we can fix mtdpart.c all other drivers over
time. Fixes for the sfc driver go through netdev, and I would not want to
burden this mailing lists with the patch for that driver.

>> The information contained in this message is confidential and is
>> intended for the addressee(s) only. If you have received this message
>> in error, please notify the sender immediately and delete the message.
>> Unless you are an addressee (or authorized to receive for an
>> addressee), you may not use, copy or disclose to anyone this message
>> or any information contained in this message. The unauthorized use,
>> disclosure, copying or alteration of this message is strictly
>> prohibited.
> 
> Nice.

Sorry, I hope this has been fixed now.

Martin

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] mtdcore: Infrastructure for the device release API
  2017-05-31 16:15   ` Martin Habets
@ 2017-06-22 13:54     ` Martin Habets
  2017-07-08  1:47       ` Brian Norris
  0 siblings, 1 reply; 6+ messages in thread
From: Martin Habets @ 2017-06-22 13:54 UTC (permalink / raw)
  Cc: linux-mtd, Brian Norris

Brian, any opinion about this?

Thanks,
Martin

On 31/05/17 17:15, Martin Habets wrote:
> On 27/05/17 01:13, Brian Norris wrote:
>> On Thu, May 18, 2017 at 10:23:11AM +0100, Martin Habets wrote:
>>> This API adds a cleanup callback for the user of an MTD device.
>>> Normally they will have allocated the mtd_info structure, and using
>>> this callback they can free that memory when the last reference is
>>> dropped.
>>
>> Can you provide an example user? This callback is meaningless (and won't
>> be merged) if it has no users.
> 
> I found this for the sfc driver (drivers/net/ethernet/sfc), but it also affects
> all drivers that use mtd_partition (i.e. mtdpart.c).
> 
>>> CONFIG_DEBUG_KOBJECT_RELEASE can be used to detect users that free
>>> the memory too early.
>>
>> Are there any such users?
> 
> Apart from the sfc driver there are lots of drivers that use MTD partitions
> it seems. There could be other drivers that I'm not aware of.
> 
> To highlight the issue in mtdpart.c: in del_mtd_partitions() it calls
> del_mtd_device() to unregister the device and then frees the memory in
> free_partition().
> The freeing of memory could happen before the last reference to the device
> has been dropped (and off course the memory could be handed out again to
> something else).
> 
> If this callback is acceptable we can fix mtdpart.c all other drivers over
> time. Fixes for the sfc driver go through netdev, and I would not want to
> burden this mailing lists with the patch for that driver.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] mtdcore: Infrastructure for the device release API
  2017-06-22 13:54     ` Martin Habets
@ 2017-07-08  1:47       ` Brian Norris
  2017-07-18 15:51         ` Martin Habets
  0 siblings, 1 reply; 6+ messages in thread
From: Brian Norris @ 2017-07-08  1:47 UTC (permalink / raw)
  To: Martin Habets; +Cc: linux-mtd

On Thu, Jun 22, 2017 at 02:54:07PM +0100, Martin Habets wrote:
> Brian, any opinion about this?

I'm sure this is all awesome, but I haven't seen a patch that utilizes
this. It's save me some mental energy of trying to figure out what all
is supposed to be happening here if I had one.

Or this can wait until I've spent more time trying to figure out what
you're trying to do with this. And I haven't proven myself to spend a
lot of time looking at your patches so far ;)

Brian

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] mtdcore: Infrastructure for the device release API
  2017-07-08  1:47       ` Brian Norris
@ 2017-07-18 15:51         ` Martin Habets
  0 siblings, 0 replies; 6+ messages in thread
From: Martin Habets @ 2017-07-18 15:51 UTC (permalink / raw)
  To: Brian Norris; +Cc: linux-mtd

On 08/07/17 02:47, Brian Norris wrote:
> On Thu, Jun 22, 2017 at 02:54:07PM +0100, Martin Habets wrote:
>> Brian, any opinion about this?
> 
> I'm sure this is all awesome, but I haven't seen a patch that utilizes
> this. It's save me some mental energy of trying to figure out what all
> is supposed to be happening here if I had one.

I can cook up a patch for mtdpart.c, but I cannot test it since I do not have any
hardware that uses this. Or maybe this is enough reason that I should post my
patch for the sfc driver here.
In fact I'll probably post both those patches here.

> Or this can wait until I've spent more time trying to figure out what
> you're trying to do with this. And I haven't proven myself to spend a
> lot of time looking at your patches so far ;)

The easy step is to build a kernel with CONFIG_DEBUG_KOBJECT_RELEASE enabled,
the tricky bit is to analyse the output it spews into dmesg when a module that
uses mtd is unloaded.

Martin

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2017-07-18 15:52 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-05-18  9:23 [PATCH] mtdcore: Infrastructure for the device release API Martin Habets
2017-05-27  0:13 ` Brian Norris
2017-05-31 16:15   ` Martin Habets
2017-06-22 13:54     ` Martin Habets
2017-07-08  1:47       ` Brian Norris
2017-07-18 15:51         ` Martin Habets

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.