mtdchar: per-device bounce buffer policy
Subsystems (3)
v4 cover mtdchar: per-device bounce buffer policy
This series implements a per-device bounce buffer allocation policy:
low-level drivers decide whether they may receive non-linear-map buffers, and a helper applies the allocation policy accordingly. The design follows the discussion with Miquel [1][2], and the previous revision of this series is at [3].
Patch 1 introduces mtd->allows_vmalloc_bufs (defaults to false, partitions inherit it from their master) and mtd_alloc_buf()/ mtd_free_buf(). By default buffers come from mtd_kmalloc_up_to(), which shrinks the size under fragmentation. Drivers that can handle non-linear-map buffers may set the flag to get kvmalloc()'s vmalloc fallback instead, so a large read()/write() no longer degrades into many small mtd_read()/mtd_write() calls.
Patch 2 fixes a pre-existing bug found while re-reviewing v3:
the bounce buffer size is taken from the unclamped count, so a read() near the end of a device allocates far more than the actual transfer.
Patch 3 converts mtdchar_read()/mtdchar_write() to the helper.
Patch 4 is the first user: rpc-if copies data with the CPU (memcpy_fromio), so vmalloc'd buffers are safe to hand over; am654-hbmc, whose DMA fast path a vmalloc'd buffer would bypass, keeps the default behaviour. This conversion is compile-tested only.
Changes in v4:
- Add __GFP_NORETRY to the kvmalloc() call to avoid the OOM killer.
- Fix the unclamped bounce buffer size as a separate patch.
- Restrict the hyperbus opt-in to rpc-if only, instead of opting in both controllers from the core.
- Drop the onenand samsung opt-in (its DMA path only serves aligned, single-page accesses, so a vmalloc'd buffer would hurt performance for unaligned transfers).
- Re-run the platform tests on this revision: the opt-in still delivers the full-size transfer in one piece under heavy fragmentation, matching the v3 numbers.
[1] https://lore.kernel.org/linux-mtd/[email protected]/
[2] https://lore.kernel.org/linux-mtd/[email protected]/
[3] https://lore.kernel.org/linux-mtd/[email protected]/
Ping Li (4):
mtd: core: add per-device bounce buffer allocation helper
mtd: mtdchar: clamp bounce buffer size to the remaining transfer
mtd: mtdchar: use mtd_alloc_buf() for the read/write bounce buffers
mtd: hyperbus: rpc-if: accept vmalloc'd bounce buffers
drivers/mtd/hyperbus/hyperbus-core.c | 1 + drivers/mtd/hyperbus/rpc-if.c | 1 + drivers/mtd/mtdchar.c | 30 ++++++++++--------- drivers/mtd/mtdcore.c | 45 ++++++++++++++++++++++++++++ drivers/mtd/mtdpart.c | 1 + include/linux/mtd/hyperbus.h | 2 ++ include/linux/mtd/mtd.h | 9 ++++++ 7 files changed, 75 insertions(+), 14 deletions(-)
base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3
Signature · 1 line
2.34.1
PATCH v4 1/4 mtd: core: add per-device bounce buffer allocation helper
Introduce mtd->allows_vmalloc_bufs and two helpers for the callers:
- mtd_alloc_buf(): by default buffers come from mtd_kmalloc_up_to(), which gracefully reduces the size under fragmentation so that drivers never receive a buffer which is not DMA-able. Drivers that only memcpy() the buffer, or which bounce it internally, may set the flag in their probe to get kvmalloc()'s vmalloc fallback instead, so the requested size is kept even when the buddy allocator is too fragmented to serve a contiguous block; this avoids degrading large transfers into many small ones. The size reduction is intentionally not kept as a last resort for those devices: an allocation failure is reported to userspace rather than silently shrinking the buffer, so that the declared contract cannot be quietly broken. __GFP_NORETRY is passed so the vmalloc fallback does not invoke the OOM killer either.
- mtd_free_buf(): the matching release, kvfree() based.
The flag defaults to false (zero-initialized) and partitions inherit it from their master, so a driver which does not opt in -- on the master or any partition -- keeps today's behaviour and can never unexpectedly receive a non-DMA-able buffer.
No functional change yet; mtdchar is converted later in this series.
--- drivers/mtd/mtdcore.c | 45 +++++++++++++++++++++++++++++++++++++++++ drivers/mtd/mtdpart.c | 1 + include/linux/mtd/mtd.h | 9 +++++++++ 3 files changed, 55 insertions(+)
drivers/mtd/mtdcore.c +45 -0
@@ -2566,6 +2566,51 @@ void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size) }EXPORT_SYMBOL_GPL (mtd_kmalloc_up_to); +/** +* mtd_alloc_buf - allocate a bounce buffer for an MTD device +* @mtd: the MTD device the buffer will be used for +* @size: requested size, updated to the size actually allocated +* +* By default (see mtd->allows_vmalloc_bufs) buffers come from +* mtd_kmalloc_up_to(), which gracefully reduces the size under +* fragmentation so that drivers never receive a buffer which is not +* DMA-able. +* +* Drivers that declared themselves vmalloc-safe (they only memcpy() +* the buffer or bounce it internally) get kvmalloc()'s fallback +* instead, so the requested size is kept even when the buddy +* allocator is too fragmented to serve a contiguous block; this +* avoids degrading large transfers into many small ones. The size +* reduction is intentionally NOT kept as a last resort for those +* devices: an allocation failure is reported to userspace rather +* than silently shrinking the buffer, so that the declared contract +* cannot be quietly broken. __GFP_NORETRY is passed so the vmalloc +* fallback does not invoke the OOM killer either. +* +* Buffers must be released with mtd_free_buf(). +* +* Returns a pointer to the allocated buffer on success; otherwise, NULL. +*/ +void *mtd_alloc_buf (const struct mtd_info *mtd,size_t *size) +{ +if (!mtd->allows_vmalloc_bufs) +return mtd_kmalloc_up_to (mtd, size); + + *size =min_t (size_t, *size,KMALLOC_MAX_SIZE ); +return kvmalloc (*size,GFP_KERNEL | __GFP_NORETRY); +} +EXPORT_SYMBOL_GPL (mtd_alloc_buf); + +/** +* mtd_free_buf - release a buffer allocated with mtd_alloc_buf() +* @buf: buffer returned by mtd_alloc_buf() +*/ +void mtd_free_buf (void *buf) +{ +kvfree (buf); +} +EXPORT_SYMBOL_GPL (mtd_free_buf); +#ifdef CONFIG_PROC_FS /*====================================================================*/
drivers/mtd/mtdpart.c +1 -0
@@ -75,6 +75,7 @@ static struct mtd_info *allocate_partition(struct mtd_info *parent, child->oobsize = parent->oobsize; child->oobavail = parent->oobavail; child->subpage_sft = parent->subpage_sft; + child->allows_vmalloc_bufs = parent->allows_vmalloc_bufs; child->name = name; child->owner = parent->owner;
include/linux/mtd/mtd.h +9 -0
@@ -368,6 +368,13 @@ struct mtd_info {*/ bool oops_panic_write; +/* +* Set if the driver can handle vmalloc'd buffers (memcpy-only +* or internal bounce). Defaults to false: buffers always come +* from mtd_kmalloc_up_to(). +*/ +bool allows_vmalloc_bufs; +struct notifier_block reboot_notifier;/* default mode before reboot */ /* ECC status information */
@@ -699,6 +706,8 @@ struct mtd_notifier {extern void register_mtd_user (struct mtd_notifier *new);extern int unregister_mtd_user (struct mtd_notifier *old);void *mtd_kmalloc_up_to (const struct mtd_info *mtd,size_t *size); +void *mtd_alloc_buf (const struct mtd_info *mtd,size_t *size); +void mtd_free_buf (void *buf);static inline int mtd_is_bitflip (int err) {return err == -EUCLEAN ;
base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v4 1/4] mtd: core: add per-device bounce buffer allocation helper
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [Medium] mtd: mtd_alloc_buf() uses arch-dependent KMALLOC_MAX_SIZE for vmalloc cap - [Low] mtd: missing…
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [Medium] mtd: mtd_alloc_buf() uses arch-dependent KMALLOC_MAX_SIZE for vmalloc cap
- [Low] mtd: missing Return: section colon in mtd_alloc_buf() kernel-doc
- [Low] mtd: mtd_alloc_buf() skips mtd_kmalloc_up_to() on !CONFIG_MMU kernels
--
commit efcdd59002b1c9f468f5f039f8a5c7ecd370e313
Author: Ping Li <[email protected]>
mtd: core: add per-device bounce buffer allocation helper
This commit adds mtd->allows_vmalloc_bufs along with mtd_alloc_buf() and mtd_free_buf() so drivers that can handle vmalloc'd buffers can opt into kvmalloc() instead of mtd_kmalloc_up_to(). Partitions inherit the flag from their parent.
Quoted 10 lines
drivers/mtd/mtdcore.c +2
-0@@ -2566,6 +2566,51 @@ void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size) }EXPORT_SYMBOL_GPL (mtd_kmalloc_up_to); +/** +* mtd_alloc_buf - allocate a bounce buffer for an MTD device
[ ... ]
Quoted 2 lines
+ * Returns a pointer to the allocated buffer on success; otherwise, NULL.
+ */
[Severity: Low]
Should "Returns" be "Return:" with a colon? Without the colon, scripts/kernel-doc -Wall (W=2 builds) does not recognize the return section and warns that mtd_alloc_buf() has no return value description.
drivers/mtd/mtdcore.c void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size) · line 2601
Quoted 8 lines
drivers/mtd/mtdcore.c +8
-0+void *mtd_alloc_buf (const struct mtd_info *mtd,size_t *size) +{ +if (!mtd->allows_vmalloc_bufs) +return mtd_kmalloc_up_to (mtd, size); + + *size =min_t (size_t, *size,KMALLOC_MAX_SIZE ); +return kvmalloc (*size,GFP_KERNEL | __GFP_NORETRY); +}
[Severity: Low]
Should mtd_alloc_buf() also fall back to mtd_kmalloc_up_to() when !IS_ENABLED(CONFIG_MMU)?
On !CONFIG_MMU kernels, __vmalloc_node_range_noprof() delegates to kmalloc_noprof() with no non-contiguous fallback. For transfers larger than PAGE_ALLOC_COSTLY_ORDER, __alloc_pages_slowpath() strips __GFP_DIRECT_RECLAIM when __GFP_NORETRY is set.
As a result, kvmalloc() fails immediately under physical fragmentation without reclaim, compaction, or shrinking, regressing transfers that mtd_kmalloc_up_to() would have served by halving the buffer size.
[Severity: Medium]
Is KMALLOC_MAX_SIZE a safe upper bound for non-shrinking vmalloc bounce buffers?
KMALLOC_MAX_SIZE scales with page size and ARCH_FORCE_MAX_ORDER: it is 4 MiB on 4K arm64, 32 MiB on ARM64_16K_PAGES, and 512 MiB on ARM64_64K_PAGES. Unlike mtd_kmalloc_up_to(), mtd_alloc_buf() does not halve the size on failure.
On an ARM64_64K_PAGES board with modest RAM (such as a 384 MiB R-Car D3 Draak with a 64 MiB RPC-IF HyperFlash), a large read or write can request tens of MiB in a single unswappable vmalloc buffer, driving heavy reclaim or failing with -ENOMEM where a 4K kernel succeeds.
Clamping *size to a fixed ceiling (or mtd->erasesize, as mtdchar_write_ioctl() does) would keep bounce buffer sizes predictable across page sizes.
Signature · 5 lines
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1 ______________________________________________________ Linux MTD discussion mailing list http://lists.infradead.org/mailman/listinfo/linux-mtd/
PATCH v4 2/4 mtd: mtdchar: clamp bounce buffer size to the remaining transfer
In reply to Ping Limtdchar_read() and mtdchar_write() initialize the bounce buffer size with the raw count from userspace, then clamp count to what actually remains in the device. The bounce buffer therefore gets allocated with the unclamped size: a read() of a few MiB near the end of a small device allocates a few MiB of bounce buffer to carry only a few KiB, which wastes memory and vmalloc space and may even fail with -ENOMEM for a trivially small transfer.
Set the bounce buffer size after count has been clamped.
--- drivers/mtd/mtdchar.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/mtd/mtdchar.c +4 -2
@@ -133,7 +133,7 @@ static ssize_t mtdchar_read(struct file *file, char __user *buf, size_t count,size_t total_retlen=0 ;int ret=0 ;int len; -size_t size = count; +size_t size;char *kbuf;pr_debug ("MTD_read\n" );
@@ -148,6 +148,7 @@ static ssize_t mtdchar_read(struct file *file, char __user *buf, size_t count,if (!count)return 0 ; + size = count; kbuf =mtd_kmalloc_up_to (mtd, &size);if (!kbuf)return -ENOMEM ;
@@ -219,7 +220,7 @@ static ssize_t mtdchar_write(struct file *file, const char __user *buf, size_t c {struct mtd_file_info *mfi = file->private_data;struct mtd_info *mtd = mfi->mtd; -size_t size = count; +size_t size;char *kbuf;size_t retlen;size_t total_retlen=0 ;
@@ -237,6 +238,7 @@ static ssize_t mtdchar_write(struct file *file, const char __user *buf, size_t cif (!count)return 0 ; + size = count; kbuf =mtd_kmalloc_up_to (mtd, &size);if (!kbuf)return -ENOMEM ;
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v4 2/4] mtd: mtdchar: clamp bounce buffer size to the remaining transfer
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Low] mtd: mtdchar: Fixes tag cites 1da177e4c3f4 instead of 3e45cf5e85a4 - [Low] mtd: mtdchar: commit…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Low] mtd: mtdchar: Fixes tag cites 1da177e4c3f4 instead of 3e45cf5e85a4
- [Low] mtd: mtdchar: commit message inaccurately claims vmalloc space waste
--
commit 2cebff5f1ea985ddd7180eb8271bf4aa1d5d75b0
Author: Ping Li <[email protected]>
Quoted 9 lines
mtd: mtdchar: clamp bounce buffer size to the remaining transfer
mtdchar_read()andmtdchar_write()initialize the bounce buffer size with the raw count from userspace, then clamp count to what actually remains in the device. The bounce buffer therefore gets allocated with the unclamped size: aread()of a few MiB near the end of a small device allocates a few MiB of bounce buffer to carry only a few KiB, which wastes memory and vmalloc space and may even fail with-ENOMEMfor a trivially small transfer.
[Severity: Low]
Is the mention of vmalloc space accurate here? Both mtdchar_read() and mtdchar_write() still allocate via mtd_kmalloc_up_to(), which only calls kmalloc() and never uses vmalloc space.
Quoted 5 lines
Set the bounce buffer size after count has been clamped.
Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")Signed-off-by Ping Li <[email protected]>
[Severity: Low]
Should this Fixes tag point to 3e45cf5e85a4 ("mtd: mtdchar: retry large buffer allocations") instead? Prior to that commit, mtd_read() and mtd_write() clamped count before allocating kbuf; initializing size from the unclamped count was introduced when switching to mtd_kmalloc_up_to().
Signature · 5 lines
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2 ______________________________________________________ Linux MTD discussion mailing list http://lists.infradead.org/mailman/listinfo/linux-mtd/
PATCH v4 3/4 mtd: mtdchar: use mtd_alloc_buf() for the read/write bounce buffers
In reply to Ping Limtdchar_read() and mtdchar_write() so far rely on mtd_kmalloc_up_to(), which halves the requested size until the allocation succeeds. Under heavy memory fragmentation a multi-megabyte read()/write() thus silently degrades into many small mtd_read()/mtd_write() calls, which is visible when the low-level driver cost scales with the number of transactions (e.g. drivers forwarding each call to a remote controller).
Use mtd_alloc_buf() instead. Devices whose driver did not opt into vmalloc'd buffers keep the current behaviour; vmalloc-safe drivers keep the requested size. Also update the comment above mtdchar_read(), which still described the old mitigation.
--- drivers/mtd/mtdchar.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-)
drivers/mtd/mtdchar.c +12 -12
@@ -111,10 +111,10 @@ static int mtdchar_close(struct inode *inode, struct file *file)* FIXME: This _really_ needs to die. In 2.5, we should lock the * userspace buffer down and use it directly with readv/writev. * -* The implementation below, using mtd_kmalloc_up_to, mitigates -* allocation failures when the system is under low-memory situations -* or if memory is highly fragmented at the cost of reducing the -* performance of the requested transfer due to a smaller buffer size. +* The implementation below, using mtd_alloc_buf(), mitigates +* allocation failures for devices that did not opt into +* vmalloc'd buffers by reducing the buffer size, while +* vmalloc-safe drivers keep the requested size. * * A more complex but more memory-efficient implementation based on * get_user_pages and iovecs to cover extents of those pages is a
@@ -149,7 +149,7 @@ static ssize_t mtdchar_read(struct file *file, char __user *buf, size_t count,return 0 ; size = count; - kbuf =mtd_kmalloc_up_to (mtd, &size); + kbuf =mtd_alloc_buf (mtd, &size);if (!kbuf)return -ENOMEM ;
@@ -193,7 +193,7 @@ static ssize_t mtdchar_read(struct file *file, char __user *buf, size_t count,if (!ret ||mtd_is_bitflip_or_eccerr (ret)) { *ppos += retlen;if (copy_to_user (buf, kbuf, retlen)) { -kfree (kbuf); +mtd_free_buf (kbuf);return -EFAULT ; }else
@@ -205,13 +205,13 @@ static ssize_t mtdchar_read(struct file *file, char __user *buf, size_t count, count =0 ; }else { -kfree (kbuf); +mtd_free_buf (kbuf);return ret; } } -kfree (kbuf); +mtd_free_buf (kbuf);return total_retlen; }/* mtdchar_read */
@@ -239,7 +239,7 @@ static ssize_t mtdchar_write(struct file *file, const char __user *buf, size_t creturn 0 ; size = count; - kbuf =mtd_kmalloc_up_to (mtd, &size); + kbuf =mtd_alloc_buf (mtd, &size);if (!kbuf)return -ENOMEM ;
@@ -247,7 +247,7 @@ static ssize_t mtdchar_write(struct file *file, const char __user *buf, size_t c len =min_t (size_t, count, size);if (copy_from_user (kbuf, buf, len)) { -kfree (kbuf); +mtd_free_buf (kbuf);return -EFAULT ; }
@@ -294,12 +294,12 @@ static ssize_t mtdchar_write(struct file *file, const char __user *buf, size_t c buf += retlen; }else { -kfree (kbuf); +mtd_free_buf (kbuf);return ret; } } -kfree (kbuf); +mtd_free_buf (kbuf);return total_retlen; }/* mtdchar_write */
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v4 3/4] mtd: mtdchar: use mtd_alloc_buf() for the read/write bounce buffers
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] mtd: mtdchar: mtd_alloc_buf() drops low-memory backoff and warns on failure - [Medium] mtd…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] mtd: mtdchar: mtd_alloc_buf() drops low-memory backoff and warns on failure
- [Medium] mtd: mtdchar: mtd_alloc_buf() caps bounce buffer at KMALLOC_MAX_SIZE
--
commit 9a4e811d7951c8dffd66c630d36a3b204c649eaf
Author: Ping Li <[email protected]>
mtd: mtdchar: use mtd_alloc_buf() for the read/write bounce buffers
This commit switches mtdchar_read() and mtdchar_write() from mtd_kmalloc_up_to()/kfree() to mtd_alloc_buf()/mtd_free_buf() and updates the comment above mtdchar_read().
Quoted 4 lines
drivers/mtd/mtdchar.c +0
-0
[ ... ]
drivers/mtd/mtdchar.c static ssize_t mtdchar_read(struct file *file, char __user *buf, size_t count, · line 154
Quoted 8 lines
drivers/mtd/mtdchar.c +1
-1@@ -149,7 +149,7 @@ static ssize_t mtdchar_read(struct file *file, char __user *buf, size_t count,return 0 ; size = count; - kbuf =mtd_kmalloc_up_to (mtd, &size); + kbuf =mtd_alloc_buf (mtd, &size);if (!kbuf)return -ENOMEM ;
[Severity: Medium]
Can switching mtdchar_read() and mtdchar_write() to mtd_alloc_buf() regress read() and write() under memory pressure for drivers with allows_vmalloc_bufs set?
Unlike mtd_kmalloc_up_to(), mtd_alloc_buf() makes a single kvmalloc(*size, GFP_KERNEL | __GFP_NORETRY) call without size backoff. In memalloc_apply_gfp_scope(), __GFP_NORETRY disables reclaim for vmalloc page tables, causing -ENOMEM even under moderate memory pressure.
Because mtd_alloc_buf() omits __GFP_NOWARN, those failures also emit warn_alloc() stack traces to dmesg from ordinary userspace I/O. Falling back to smaller chunks with __GFP_NOWARN on large attempts would keep low-memory transfers working cleanly.
[Severity: Medium]
Is KMALLOC_MAX_SIZE an appropriate upper bound for the bounce buffer allocated here and in mtdchar_write()?
mtd_alloc_buf() clamps *size to KMALLOC_MAX_SIZE, which scales with page size and ARCH_FORCE_MAX_ORDER: on arm64 with 16 KiB or 64 KiB pages, it reaches 32 MiB or 512 MiB.
A full-device or full-partition read()/write() on a 64 MiB flash then requests 32 MiB to 64 MiB of vmalloc memory in one shot, increasing vmap overhead and __GFP_NORETRY allocation failures. A fixed ceiling (such as mtd->erasesize, as in mtdchar_read_ioctl()) would avoid this scaling.
Signature · 5 lines
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3 ______________________________________________________ Linux MTD discussion mailing list http://lists.infradead.org/mailman/listinfo/linux-mtd/
PATCH v4 4/4 mtd: hyperbus: rpc-if: accept vmalloc'd bounce buffers
In reply to Ping Lirpc-if copies data with the CPU only: bulk reads end up in memcpy_fromio() (rpcif_dirmap_read()), and writes go through write16()/manual transfers, so vmalloc'd bounce buffers are safe to hand over. Opt in to keep large read()/write() transfers in one piece under fragmentation.
Only rpc-if opts in; am654-hbmc has a DMA fast path which a vmalloc'd buffer would bypass, so it keeps the default behaviour.
The flag is set on the hyperbus device before registration and copied to the mtd_info before mtd_device_register(): partitions are parsed inside that call and inherit the flag from their master at allocation time, so setting it on the registered master would come too late for them.
--- drivers/mtd/hyperbus/hyperbus-core.c | 1 + drivers/mtd/hyperbus/rpc-if.c | 1 + include/linux/mtd/hyperbus.h | 2 ++ 3 files changed, 4 insertions(+)
drivers/mtd/hyperbus/hyperbus-core.c +1 -0
@@ -114,6 +114,7 @@ int hyperbus_register_device(struct hyperbus_device *hbdev) hbdev->mtd->dev.parent = dev;mtd_set_of_node (hbdev->mtd, np); + hbdev->mtd->allows_vmalloc_bufs = hbdev->allows_vmalloc_bufs; ret =mtd_device_register (hbdev->mtd,NULL ,0 );if (ret) {
drivers/mtd/hyperbus/rpc-if.c +1 -0
@@ -142,6 +142,7 @@ static int rpcif_hb_probe(struct platform_device *pdev) hyperbus->ctlr.dev = dev; hyperbus->ctlr.ops = &rpcif_hb_ops; hyperbus->hbdev.ctlr = &hyperbus->ctlr; + hyperbus->hbdev.allows_vmalloc_bufs = true; hyperbus->hbdev.np =of_get_next_child (pdev->dev.parent->of_node,NULL ); error =hyperbus_register_device (&hyperbus->hbdev);if (error)
include/linux/mtd/hyperbus.h +2 -0
@@ -32,6 +32,7 @@ enum hyperbus_memtype {* @ctlr: pointer to HyperBus controller struct * @memtype: type of memory device: HyperFlash or HyperRAM * @priv: pointer to controller specific per device private data +* @allows_vmalloc_bufs: whether the device may receive vmalloc'd buffers */ struct hyperbus_device {
@@ -41,6 +42,7 @@ struct hyperbus_device {struct hyperbus_ctlr *ctlr;enum hyperbus_memtype memtype;void *priv; +bool allows_vmalloc_bufs; };/**
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v4 4/4] mtd: hyperbus: rpc-if: accept vmalloc'd bounce buffers
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] mtd: rpc-if: allows_vmalloc_bufs drops size fallback and adds __GFP_NORETRY --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] mtd: rpc-if: allows_vmalloc_bufs drops size fallback and adds __GFP_NORETRY
--
commit 3478fa469db25de80edd29b0bbcd17ad07a31745
Author: Ping Li <[email protected]>
mtd: hyperbus: rpc-if: accept vmalloc'd bounce buffers
This commit adds an allows_vmalloc_bufs flag to struct hyperbus_device, copies it to the MTD device before registration, and enables it for the Renesas RPC-IF HyperBus driver.
drivers/mtd/hyperbus/rpc-if.c static int rpcif_hb_probe(struct platform_device *pdev) · line 148
Quoted 12 lines
drivers/mtd/hyperbus/rpc-if.c +1
-0@@ -142,6 +142,7 @@ static int rpcif_hb_probe(struct platform_device *pdev) hyperbus->ctlr.dev = dev; hyperbus->ctlr.ops = &rpcif_hb_ops; hyperbus->hbdev.ctlr = &hyperbus->ctlr; + hyperbus->hbdev.allows_vmalloc_bufs = true; hyperbus->hbdev.np =of_get_next_child (pdev->dev.parent->of_node,NULL ); error =hyperbus_register_device (&hyperbus->hbdev);if (error)
[Severity: Medium]
Can enabling allows_vmalloc_bufs cause rpc-if read() and write() calls on /dev/mtdX to regress to -ENOMEM under memory pressure?
Previously, mtdchar_read() and mtdchar_write() allocated bounce buffers via mtd_kmalloc_up_to(), which halved requests under fragmentation and used plain GFP_KERNEL for the final allocation and all requests of PAGE_SIZE or less. With allows_vmalloc_bufs set, mtd_alloc_buf() skips that fallback:
drivers/mtd/mtdcore.c:mtd_alloc_buf() {
...
*size = min_t(size_t, *size, KMALLOC_MAX_SIZE);
return kvmalloc(*size, GFP_KERNEL | __GFP_NORETRY);
}For transfers of PAGE_SIZE or less, __kvmalloc_node_noprof() keeps __GFP_NORETRY intact and returns NULL without attempting vmalloc. __alloc_pages_slowpath() then gives up after a single direct reclaim pass, so small transfers get no vmalloc benefit but can fail where GFP_KERNEL retried.
For larger transfers, __GFP_NORETRY makes vmalloc's memalloc_apply_gfp_scope() apply memalloc_noreclaim_save() to page-table allocations, so they can fail under moderate memory pressure. Without chunked fallback, large rpc-if transfers can now return -ENOMEM.
Should mtd_alloc_buf() drop __GFP_NORETRY for PAGE_SIZE or smaller requests, or fall back to mtd_kmalloc_up_to() when vmalloc fails?
Signature · 5 lines
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4 ______________________________________________________ Linux MTD discussion mailing list http://lists.infradead.org/mailman/listinfo/linux-mtd/