Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.216.42 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796375816; b=CXa1xdA4qhvPQ9cOX7O9wB684jpHdKdj6uGAY3sri9wFuOo6qg4HLhc5a379gubXQGCL XRLsLXozdmqd6SxIRy96p7O+EXssURfXs6stcoZxg2g5povRTkfuTScQyYca4ov6ND+q9 FBNcmCxaxWeT43nOBd6pxExVh7X/kUdrNgAom3Ezk7eD1egaxBLqQ2TSRWNWfF7hCwBsR dOuRNGRByd2ab4EuGIoME119VUB/ZmP4R0/JcIJM0BrRq3awLVzit2b/YjYkUOYB8GvHl ZrjFjdry0SK8St8QqB3QD8bsRxKZKQqUNSC6ohDTtTiN1vePxvP4LToLyMVA13R8rnQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796375816; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-Id:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=wzX3GqdV/kAdUiAXtqS5GKzq+Rx0oLA401Gsd7YChGc=; b=xTfEkpjtWorwljtwv/DZN6nWtKySkgf9UEVPqKuOBTZ6fQQoLCe8g6bAXF7YomX9FZFb mmGVOCwceYDrbk24/HcP7XUL5KPxuo/yA4XRIVHD02YZkEbBN4S8pYjUxZSLgLRUyoysQ ZXAsgN7kJH/cscAJZwxwyqyxWf9oaSC3BCjFNccK/qBemltWhD+FTfXbwVhsY1dbKO9b+ SAHWLHwLxYXYVti80miardNVMLzlBlIMtRwcDZPe7s/BtVF+KcNJYJ67YZBYauar2Rut+ usbV+vflh9mn/oNha2DUSnzhVf33U0ioWCiViZ1fFgYMjtZml7MDpcuJEIG9jFsFp/A== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=FDDGVS1R; arc=none smtp.remote-ip=209.85.216.42 Received: from mail-pj1-f42.google.com (mail-pj1-f42.google.com [209.85.216.42]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9D406606A332 for ; Fri, 4 Dec 2026 09:16:55 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 9D406606A332 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=FDDGVS1R Received: by mail-pj1-f42.google.com with SMTP id 98e67ed59e1d1-3b59bcb3301so1069811a91.0 for ; Fri, 04 Dec 2026 01:16:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796375815; x=1796980615; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=wzX3GqdV/kAdUiAXtqS5GKzq+Rx0oLA401Gsd7YChGc=; b=FDDGVS1R0Yp6Q5E/X98WCUQ0iJ5d6t5bcwN1jS+/SjvXCvc+Om1+bsLlc3NcxhF4C+ kGl28jsT9BW7tlOqhIrTcCOTyVW3sLOlHy5Bu5gOOlJE2pXMHIPbcdU6aJbf3EaLUwYi HxjQCq6xfk5sLbFoKlq51ron+YIH+roiS02pllwkHUVL/xdEZB2g4HmP3hl87bbTJ1OL ixfmXz0YYG8PsfE4g/rZ4UnFvHGxTBi4KiYIN4BqLNFHWmpO5FSnN4H/wnZ0TIH0MsZw 1IBKbcCNI/RXVxc/hx7ENXl9xaFYJmH0pcOq4zzAIjInSLGd3j1FqJiM6ojcOCQXamx1 b9Tw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796375815; x=1796980615; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=wzX3GqdV/kAdUiAXtqS5GKzq+Rx0oLA401Gsd7YChGc=; b=1PdU7gc2xstfHrJbF/D1ovptOvTsO61CcuPeetphD62vl4SUW90fDZL60yA5uuUXWx QyaPD5Ei25CSn1Rh8DrSeouVs8+K0wDZsJAVAyZDWAgwSE8NK88uT1CiyQogLsf00UWu gcQ4DeAqOtx4h8TD4OZiR1eT1Zm+KWpsIsOPk0qUC0Sw2uX9l+DKLo3VJsZSR5LFbe64 An+iSfbR1DruYzKhk6kPY41Q+OwWxvt3J1JChwBtj/l05Ho1zisDN8vT8W0CGtnMa5av tlJZaw5b+Y8CcDfhvkWSktmAX2dCMNk0vVLEn/4snX/NxYqMcRaxFaR81+Q54ulFlofR L5Ag== X-Forwarded-Encrypted: i=1; AL+hErFlQ7mtEWbmjRk0D9Fwo6NrLren1HRhkGvNFLWi58RF9uovxOidAQqaLNbUGfYtRPH59IR3MenvSTyJkhs=@vger.kernel.org X-Gm-Message-State: APuU4Z8MTQsdNJhbmsYfNXJYXAgYHq+NtoITlHaHdo4hp1WL6AfmtlDR fKG3aBjNlgikdUMRpRRIOZweEFHiKmWXdy+XOUy8/r5KynGE3vDzyh1+ X-Gm-Gg: AZFdTZfJbbVTpzSUmInV9l/rFb7p6xOIxayyoaS4Hwgy/Bq4ROcubbVxLnoL/B2QCMb v90Ma2vPpebMc4tCtWG07n5aLU1vJ0cnDfTdXURCYly5f7Kfc65QBZHIAjlvBXOMYFXSsJsPzCu yB5ZKxA+ItgGV4awNjAUhxRcmxblfn+qaqP+aABURCuJY+AKxtcwL5z83I0Lt7X3C+ft9osUjfg vZemUUac2kT4dDvPqIiTeyEWQQqbQEAKxqh2RtEIlvi67MnywryfqTOeC/DqVbsOU+0BEXStFq2 q7A2V5nmlufVoHTPhacozE/pgVm7PIcVt3UjLAKSVlaUjKxz2GLpJQIumdgNqX8D4aB4fTXuxsV OtcZG41e2f/lH7ouHuz8U6OWmdA1ox0CV8YhqNj0Pw0NZGiN73LYNh/fDLk8AlidLfpS2Y0MelQ MbW4jFeaw0qI9VOU8zoFlle2otwJroDPViZQMVNTzbIBc2re53DWL9JFMn5mpcVcCVIyq2vAv3l NhxVIGY3QoZ8EBgio/Y2WGTrInD6NA2/87CzMgEEw6D8kilZcqWlUtP73TkRnsHzvjZv7ZpcExw B+L6IQILhdeyZufzEjQ1Lk+paEpcUE6DUg== X-Received: by 2002:a17:90b:4b87:b0:3a0:d82b:237b with SMTP id 98e67ed59e1d1-3b5e0648cd3mr813179a91.25.1796375814946; Fri, 04 Dec 2026 01:16:54 -0800 (PST) Received: from server225.. ([39.170.101.209]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3b5ddc54888sm2885970a91.11.2026.12.04.01.16.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Dec 2026 01:16:54 -0800 (PST) From: Ping Li To: miquel.raynal@bootlin.com, richard@nod.at, vigneshr@ti.com Cc: tudor.ambarus@linaro.org, kyungmin.park@samsung.com, linux-mtd@lists.infradead.org, linux-kernel@vger.kernel.org, Ping Li Subject: [PATCH v4 3/4] mtd: mtdchar: use mtd_alloc_buf() for the read/write bounce buffers Date: Fri, 4 Dec 2026 17:16:39 +0800 Message-Id: <20261204091640.444442-4-leeonion.muyu@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261204091640.444442-1-leeonion.muyu@gmail.com> References: <20261204091640.444442-1-leeonion.muyu@gmail.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- drivers/mtd/mtdchar.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/drivers/mtd/mtdchar.c b/drivers/mtd/mtdchar.c index 2b6e70864bfbb..047540e24b01c 100644 --- a/drivers/mtd/mtdchar.c +++ b/drivers/mtd/mtdchar.c @@ -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 c return 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 */ -- 2.34.1