Re: [PATCH v3 1/2] mtd: core: drop the module reference after ->_put_device()

From: Adrian Ambrozewicz

Date: Sat Nov 28 2026 - 02:05:30 EST


On Thu, Oct 22, 2026 at 11:53:51AM +0800, Tze Yee Ng wrote:
> __put_mtd_device() calls module_put() on the MTD owner and then calls
> ->_put_device(). If ->_put_device() frees the object that embeds the
> mtd_info, it is still running code from the owner module after the
> module reference is gone, so the module can be unloaded underneath it.

I hit the same window independently, with an out-of-tree MTD driver
whose ->_put_device() frees its state at the last put: a process holds
/dev/mtdN, the parent device is removed, rmmod is retried, the file is
closed. With msleep(200) after module_put() the close oopsed in the
freed module text in 6 of 6 tries, in 0 of 200 without the delay. With
this patch and the same delay after ->_put_device(), 25 of 25 delayed
and 200 of 200 plain tries ran clean (x86_64 QEMU, v7.3-rc6 plus
unrelated patches, KASAN, lockdep). In tree, mtd_intel_dg has the same
shape.

The order comes from 79c4a5625021, which stable took as a dependency:

Fixes: 79c4a5625021 ("mtd: call external _get and _put in right order")
Cc: stable@xxxxxxxxxxxxxxx

Tested-by: Adrian Ambrozewicz <aambrozewicz@xxxxxxxxxx>

Adding the MTD maintainers and the author of 79c4a5625021.