Linux-mtd Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [RFC] mtd: virt-concat: define duplicate partition handling
@ 2026-07-22  4:19 Pengpeng Hou
  2026-07-24  9:39 ` Miquel Raynal
  0 siblings, 1 reply; 2+ messages in thread
From: Pengpeng Hou @ 2026-07-22  4:19 UTC (permalink / raw)
  To: Miquel Raynal
  Cc: Richard Weinberger, Vignesh Raghavendra, Amit Kumar Mahapatra,
	Luca Ceresoli, linux-mtd, linux-kernel, Pengpeng Hou

mtd_device_parse_register() contains compatibility handling for drivers that
call it more than once, while its comment notes that partition parsing can
register the same partitions again.

When CONFIG_MTD_PARTITIONED_MASTER is disabled and the first call finds
partitions, the master itself is not registered before parsing. A later call
can therefore reach partition parsing again.

With CONFIG_MTD_VIRT_CONCAT, each newly allocated partition whose OF node
belongs to a virtual concatenation reaches mtd_virt_concat_add(). That
function appends the partition based only on the OF node and increments
num_subdev without checking whether the node was already added or whether
the fixed subdev[] array is full.

A repeated parse can therefore append the same node again. If all configured
slots were already populated, the append is out of bounds. If other members
are still missing, the duplicate can make num_subdev reach the configured
count with the wrong membership.

A local bounds check does not seem sufficient for a correct patch:

  - concat_node_list and the concat population state are global, but node
    creation, addition, join creation and destruction have no common
    serialization.
  - mtd_virt_concat_add() returns bool. false means both "not a concat
    member" and "not added". Returning false for a duplicate makes
    add_mtd_partitions() register it as an ordinary partition, while returning
    true without storing it would leak the newly allocated partition.
  - join creation and destruction call back into MTD registration helpers, so
    a driver-global mutex must have a carefully defined scope to avoid
    recursive locking.

My current view is that the fix needs both a serialization contract for the
global virtual-concat state and a tri-state add result, so the caller can
distinguish "not a member" from a duplicate or capacity error and free the
new partition on error.

Would you prefer this serialization to live inside mtd_virt_concat, or should
the MTD registration layer serialize the node-create/partition-add/join
transaction? I have not attached a patch because choosing either scope
without agreement would encode a new lifetime and error-handling contract.


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

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

* Re: [RFC] mtd: virt-concat: define duplicate partition handling
  2026-07-22  4:19 [RFC] mtd: virt-concat: define duplicate partition handling Pengpeng Hou
@ 2026-07-24  9:39 ` Miquel Raynal
  0 siblings, 0 replies; 2+ messages in thread
From: Miquel Raynal @ 2026-07-24  9:39 UTC (permalink / raw)
  To: Pengpeng Hou
  Cc: Richard Weinberger, Vignesh Raghavendra, Amit Kumar Mahapatra,
	Luca Ceresoli, linux-mtd, linux-kernel

Hi Pengpeng,

On 22/07/2026 at 12:19:25 +08, Pengpeng Hou <pengpeng@iscas.ac.cn> wrote:

> mtd_device_parse_register() contains compatibility handling for drivers that
> call it more than once, while its comment notes that partition parsing can
> register the same partitions again.

Is this feature really relevant? Who uses that?

> When CONFIG_MTD_PARTITIONED_MASTER is disabled and the first call finds
> partitions, the master itself is not registered before parsing. A later call
> can therefore reach partition parsing again.
>
> With CONFIG_MTD_VIRT_CONCAT, each newly allocated partition whose OF node
> belongs to a virtual concatenation reaches mtd_virt_concat_add(). That
> function appends the partition based only on the OF node and increments
> num_subdev without checking whether the node was already added or whether
> the fixed subdev[] array is full.
>
> A repeated parse can therefore append the same node again. If all configured
> slots were already populated, the append is out of bounds. If other members
> are still missing, the duplicate can make num_subdev reach the configured
> count with the wrong membership.
>
> A local bounds check does not seem sufficient for a correct patch:
>
>   - concat_node_list and the concat population state are global, but node
>     creation, addition, join creation and destruction have no common
>     serialization.

But aren't these operations serialized just because of how the MTD core
is designed?

>   - mtd_virt_concat_add() returns bool. false means both "not a concat
>     member" and "not added". Returning false for a duplicate makes
>     add_mtd_partitions() register it as an ordinary partition, while returning
>     true without storing it would leak the newly allocated partition.
>   - join creation and destruction call back into MTD registration helpers, so
>     a driver-global mutex must have a carefully defined scope to avoid
>     recursive locking.
>
> My current view is that the fix needs both a serialization contract for the
> global virtual-concat state and a tri-state add result, so the caller can
> distinguish "not a member" from a duplicate or capacity error and free the
> new partition on error.
>
> Would you prefer this serialization to live inside mtd_virt_concat, or should
> the MTD registration layer serialize the node-create/partition-add/join
> transaction? I have not attached a patch because choosing either scope
> without agreement would encode a new lifetime and error-handling contract.

If it is specific to the virt concat layer, I would try to keep the
changes contained in the virt concat driver.

Thanks,
Miquèl

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

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

end of thread, other threads:[~2026-07-24  9:39 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-22  4:19 [RFC] mtd: virt-concat: define duplicate partition handling Pengpeng Hou
2026-07-24  9:39 ` Miquel Raynal

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox