From: Ping Li <leeonion.muyu@gmail.com>
To: miquel.raynal@bootlin.com, richard@nod.at, vigneshr@ti.com
Cc: linux-mtd@lists.infradead.org, linux-kernel@vger.kernel.org,
Ping Li <leeonion.muyu@gmail.com>
Subject: [RFC PATCH v2 2/2] mtd: mtdchar: use mtd_alloc_buf() for the read/write bounce buffers
Date: Tue, 24 Nov 2026 10:57:22 +0800 [thread overview]
Message-ID: <20261124025722.1476247-3-leeonion.muyu@gmail.com> (raw)
In-Reply-To: <20261124025722.1476247-1-leeonion.muyu@gmail.com>
mtdchar_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.
Signed-off-by: Ping Li <leeonion.muyu@gmail.com>
---
drivers/mtd/mtdchar.c | 24 +++++++++++++-----------
1 file changed, 13 insertions(+), 11 deletions(-)
diff --git a/drivers/mtd/mtdchar.c b/drivers/mtd/mtdchar.c
index bf01e6ac72931..0142cad65aee3 100644
--- a/drivers/mtd/mtdchar.c
+++ b/drivers/mtd/mtdchar.c
@@ -111,10 +111,12 @@ 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
+ * The implementation below, using mtd_alloc_buf(), 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.
+ * or if memory is highly fragmented. Devices whose driver did not
+ * opt into vmalloc'd buffers get a reduced buffer size, while
+ * vmalloc-safe drivers keep the requested size, so large transfers
+ * are not degraded into many small ones.
*
* A more complex but more memory-efficient implementation based on
* get_user_pages and iovecs to cover extents of those pages is a
@@ -148,7 +150,7 @@ static ssize_t mtdchar_read(struct file *file, char __user *buf, size_t count,
if (!count)
return 0;
- kbuf = mtd_kmalloc_up_to(mtd, &size);
+ kbuf = mtd_alloc_buf(mtd, &size);
if (!kbuf)
return -ENOMEM;
@@ -192,7 +194,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
@@ -204,13 +206,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 */
@@ -237,7 +239,7 @@ static ssize_t mtdchar_write(struct file *file, const char __user *buf, size_t c
if (!count)
return 0;
- kbuf = mtd_kmalloc_up_to(mtd, &size);
+ kbuf = mtd_alloc_buf(mtd, &size);
if (!kbuf)
return -ENOMEM;
@@ -245,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;
}
@@ -292,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 */
--
2.34.1
prev parent reply other threads:[~2026-11-24 2:57 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-19 9:00 [RFC PATCH 0/1] mtdchar: kvmalloc() for large read/write buffers? ping.li
2026-11-19 9:08 ` [RFC PATCH 1/1] mtd: mtdchar: use kvmalloc() for large read/write buffers Ping Li
2026-11-19 9:50 ` [RFC PATCH 0/1] mtdchar: kvmalloc() for large read/write buffers? Miquel Raynal
2026-11-23 3:32 ` Ping Li
2026-11-23 8:35 ` Miquel Raynal
2026-11-24 2:57 ` [RFC PATCH v2 0/2] mtdchar: per-device bounce buffer policy Ping Li
2026-11-24 2:57 ` [RFC PATCH v2 1/2] mtd: core: add per-device bounce buffer allocation helper Ping Li
2026-11-24 2:57 ` Ping Li [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261124025722.1476247-3-leeonion.muyu@gmail.com \
--to=leeonion.muyu@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mtd@lists.infradead.org \
--cc=miquel.raynal@bootlin.com \
--cc=richard@nod.at \
--cc=vigneshr@ti.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®