Skip to content

drivers/mtd: Remove bad block management in FTL when it is not needed#19018

Merged
jerpelea merged 1 commit into
apache:masterfrom
tiiuae:remove_bad_block_management_when_not_using_nand
Jun 8, 2026
Merged

drivers/mtd: Remove bad block management in FTL when it is not needed#19018
jerpelea merged 1 commit into
apache:masterfrom
tiiuae:remove_bad_block_management_when_not_using_nand

Conversation

@jlaitine

@jlaitine jlaitine commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

Summary

Bad block management and the logical->physical mapping is only needed with NAND based memories, so we can compile that in conditionally, saving ~600 bytes of flash on a 32-bit arm when NAND is not used.

Impact

This only removes dead code on a system which doesn't have CONFIG_MTD_NAND defined

Testing

Building for stm32l476vg-disco:nsh:

The original size:

LD: nuttx                          
Memory region         Used Size  Region Size  %age Used
           flash:      117168 B       512 KB     22.35%
            sram:        9488 B        96 KB      9.65%
           sram2:          0 GB        32 KB      0.00%

The size after change:

LD: nuttx                                            
Memory region         Used Size  Region Size  %age Used
           flash:      116552 B       512 KB     22.23%
            sram:        9488 B        96 KB      9.65%
           sram2:          0 GB        32 KB      0.00%

Diff being 616 bytes smaller.

Functionality has been tested with PX4 on Pixhawk4 hardware (stm32f7), which has px4 parameters on an MTD partition and no NAND.

@github-actions github-actions Bot added Area: Drivers Drivers issues Size: S The size of the change in this PR is small labels Jun 2, 2026
jerpelea
jerpelea previously approved these changes Jun 2, 2026
@xiaoxiang781216

Copy link
Copy Markdown
Contributor

but NOR flash need skip the bad block too.

@jlaitine

jlaitine commented Jun 2, 2026

Copy link
Copy Markdown
Contributor Author

but NOR flash need skip the bad block too.

Typically there are no bad blocks (or error detection / ecc) on NOR flash. I'll come back to this tomorrow, perhaps I missed something nuttx specifc... Is there some lower level driver which implements such in sw?

Comment thread drivers/mtd/ftl.c Outdated
@jlaitine jlaitine force-pushed the remove_bad_block_management_when_not_using_nand branch from 5514b3f to 2e67f14 Compare June 3, 2026 06:13
@jlaitine jlaitine changed the title drivers/mtd: Remove bad block management in FTL when !CONFIG_MTD_NAND drivers/mtd: Remove bad block management in FTL when it is not needed Jun 3, 2026
Comment thread drivers/mtd/Kconfig

config FTL_BBM
bool "Enable bad block management in the FTL layer"
default y if MTD_NAND

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
default y if MTD_NAND
default !DEFAULT_SMALL

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xiaoxiang781216 I don’t agree with enabling this by default for non‑DEFAULT_SMALL configurations. I have several chips that use FTL without bad block management, so it’s unclear why this should be applied universally to those devices. Bad block management is typically only required for NAND-based technologies.

@jlaitine jlaitine Jun 3, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think DEFAULT_SMALL is the correct one here. All of the boards which I am working on are !DEFAULT_SMALL and bad block management is not needed. Only reasonable default that I can think of is really MTD_NAND, because BBM on the host controller side is commonly needed only for raw nand devices. If the motivation behind the suggestion is that there are other than MTD_NAND devices, where default y is wanted, please give an example of that... perhaps we can add some additiona "default y if ..."?

In addition, we could have a DEFAULT_SMALL board using raw nand, in which case the BBM would be needed.

So I'd rather find a reasonable default based on whether the functionality is required or not, and not couple it to DEFAULT_SMALL.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if so, please add assert if mtd support isbad or markbad, but FTL_BBM is false.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if so, please add assert if mtd support isbad or markbad, but FTL_BBM is false.

Sounds reasonable but where? In ftl_open perhaps? DEBUGASSERT?

What if user wants to intentionally disable the bad block management? Perhaps there is no reason to do so...

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I agree most of the drivers don't need it. But after this merge request you have to enable CONFIG_FTL_BBM if you use mtd_partition to create multiple partitions on your flash device. And that's not a theoretical bug, it's fairly common use case. If you don't enable CONFIG_FTL_BBM, you program runs into the debug assertion, even if your driver doesn't need bad block management. This should be solved by the diff above and we won't have to enable CONFIG_FTL_BBM for most of the NOR flash devices (perhaps for all of them...). I can create a merge request for the change if you want.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah sorry. Now I understand. The debugassert is definitely wrong! @xiaoxiang781216 I think we should remove that debugassert, and solve the potential misconfiguration you were worried about by other means.

Thanks @michallenc for you patience, sorry for me being so slow to understand what you were saying :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simplest fix would be to just remove the DEBUGASSERT. I can also check if something could be done to make proper compile time check for original purpose why it was suggested to be added.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps something in this direction: #19145 , do you think this would solve the issue @michallenc, @xiaoxiang781216 ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or then we can just pick the original patch that @michallenc provided and keep the debugassert?

@jlaitine jlaitine force-pushed the remove_bad_block_management_when_not_using_nand branch from 2e67f14 to 7c186e7 Compare June 3, 2026 09:54
@jlaitine

jlaitine commented Jun 3, 2026

Copy link
Copy Markdown
Contributor Author

re-based to master, hoping for the CI build errors to go away...

jerpelea
jerpelea previously approved these changes Jun 3, 2026
@lupyuen

lupyuen commented Jun 3, 2026

Copy link
Copy Markdown
Member

Hi: Could you Rebase your PR with the Master Branch? We fixed the Segger SystemView Error in the CI Build. Thanks :-)

@jlaitine jlaitine force-pushed the remove_bad_block_management_when_not_using_nand branch from 7c186e7 to 9d82ec2 Compare June 3, 2026 12:04
@lupyuen

lupyuen commented Jun 4, 2026

Copy link
Copy Markdown
Member

FYI: I restarted the CI Build to fix the Docker Image

…ng it

Bad block management and the logical->physical mapping is only needed
with raw NAND type memories, so we can compile that in conditionally,
saving ~600 bytes of flash on a 32-bit arm when NAND is not used.

Signed-off-by: Jukka Laitinen <jukka.laitinen@tii.ae>
@jlaitine jlaitine force-pushed the remove_bad_block_management_when_not_using_nand branch from 9d82ec2 to a190314 Compare June 4, 2026 07:14
@jerpelea jerpelea merged commit 9c10bbd into apache:master Jun 8, 2026
41 checks passed
@jlaitine jlaitine deleted the remove_bad_block_management_when_not_using_nand branch June 15, 2026 10:07
@jlaitine

jlaitine commented Jun 15, 2026 via email

Copy link
Copy Markdown
Contributor Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Drivers Drivers issues Size: S The size of the change in this PR is small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants