From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.172 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796363202; b=k2RskRgEfBaH3UUPNwntjr3VYKUXEuxCl12g1IeNdQKWa4urZYFmimFE4+E6sMZwz1k4 NvR/KvQN9CvjseK2wLAxaLfvoT9UJXSAzViz4tDhxp2qODn+K0U3CpCtZY2j1CNlezaqR /5/iDenFRFDGvqc+xvOz0zWQA6oIlQOj91/kOM+++i9uJ1G1deVP7zFFXY3ZuwwcfKuhS PhdMlEfSZk2k2FMW72DTJZbjAyaYh7Iq6YX0jCJSGFVC096bsS12jmZ++Mr4kdewhSfW1 9BiScHarZvu8K10+WVlhdh2jLcSlB4TLpt7imlqBGj2yrKIl9Ri2NL8Git8e8TIsglA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796363202; 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=iGzjYYK2RrRv1SuL2YUffxkbeCFA16ZG+Je/ZdpQngg=; b=LnA4y7bfkVGqXS87ACOT+GZS4vHrxNBCVpHznVlbdzIYOcUY+8jCv7sIVOQaVWOrrRI3 CbkjfCkm4pyIf5q+1QNNFZ+9UxTWYRa8Yx0QN2YtPCAflEjVAhP8/vrx2pKSN3trZT5/e oH++W7ZpJHBWWSPmqOp78M5u8MEHBse5s02IzyNwupRljOauwh66jU1u/PY6ihBH99lxc O2V6G4kMBXRZUMMpRyJ1ft5VxQh1dDr9N7QrtGYWNEvqAa2JUgxiom57pVJZDBNRn00bX +Dd29djYEbkMneeFoeknN02dZ8jrW5ClU4aDlmIyOiDL67AZYpU8Hd3dUJrR+YcFDmQ== 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=gqwiq188; arc=none smtp.remote-ip=209.85.210.172 Received: from mail-pf1-f172.google.com (mail-pf1-f172.google.com [209.85.210.172]) (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 9F4A1606A33F for ; Fri, 4 Dec 2026 05:46:41 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 9F4A1606A33F 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=gqwiq188 Received: by mail-pf1-f172.google.com with SMTP id d2e1a72fcca58-8a06cc097c7so409871b3a.0 for ; Thu, 03 Dec 2026 21:46:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796363201; x=1796968001; 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=iGzjYYK2RrRv1SuL2YUffxkbeCFA16ZG+Je/ZdpQngg=; b=gqwiq188BIPNHeupm9/fTt4AfSw+5F8+xRbJeqRw8XqfonfT/+ExkXUlK6vzqZLsMj MMAFbMwPLUwrIMfe/noqCrGT6RN+3spFgrs9Xs1ZChgsDbBNQxpqVGH22SCv4n0SnDXM zh6JdFuWaUiIToZPXOomjrAtQDpGuctmfMa+qIgyDmiziNiWDBkdbE4nr52Lc7Bap9FR Ara1togOwzaOO8mad0um62M7qTizVrb/ElvwrCMBF7cuJmNf+P+B1LkSLMCg8MDFqIeK 37LSEaPe7dp4kWFzv7hhpfLYwDHjP3A3qV4U6f0uhuAPERhtUmJA/tticOgNCyffkvIH GWzQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796363201; x=1796968001; 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=iGzjYYK2RrRv1SuL2YUffxkbeCFA16ZG+Je/ZdpQngg=; b=WcR8FOn/8lw9BfefXv8ka4qU8XoaIGKnBsU86b0B3MxMZNKianxaTodVTosG2N7zNo i9qwXaXSXh9Yrvse8ju2Fb6FhuchYayBsKIJlJZgTSn5KFXT7Ezzi7y3xkc4fhzxl/e0 SCHYR7cdBDE3XLdUiniu5qGLgBo5vADhEfcwVweTKbWk4qzHDPYYfJ/2I8JjzxNPYtUB xAhtlqLybTAn+P5ZcuTJIbHF+LdxtBbmdEcClxUjzzAZh9GKDsrPNN8pHciNrtaTlO+U 62qtICvtVkfkrMS2h3fmx+UMGMYUcjEUhU43G4kMO23ihQhvpWefTKqJZsLznRsByuot jiRA== X-Forwarded-Encrypted: i=1; AL+hErEkIFqjmoA0IH4Ve3B5unRwpFtH3WzkKgY6Fcd0P5WyIt61hq2JlWRlaKi6x9qDjCXpcubzQLQFJbkq1to=@vger.kernel.org X-Gm-Message-State: APuU4Z86U9Vk1+rVVtoysgETy1ZZtOtFi7IkVeUGOT6EZKUyYu0XvDLb GzS+lpAlz7jfR2EVI4vKvsxway95YkIjh5VAtGzNqRi5f7b+qxPjTocN X-Gm-Gg: AZFdTZeEGosxxHi4GGnSOkNnnndWNefewH4a+mooKaoOrHvVhh4RRQeyZtcGoPM+nj/ 5LIYtXiad0MGRreJV6eBOnsiO8MgPAjoiwEDkWL2WPz45n+0HJi6nxAIWB+hjXugaNfx0TtPB3R n/fU1KQxUsEfqNjN6qZTaAuwbd+XraeJl5UQFipDCmecA57+PPWoh3RjMDakt4CeqRuBQhnRnuw AHM0uk7sYl7JbOQ26sJ/W5oPLMkB9LR/IE1Q1djQb1xlIlnBAoCJ/869vm7DoWJRtyUNvoan3R7 5t6R5g9mXqQ0w9/5zvLknqdqbgOR124GuSlVipUqn1B9qYhGBUUwLgQrugb8Z/LmO/3ob0DXhf+ teAkjSRAeeM2UQgAsd6OFBlGxD43lzlSNGdXmoeoGC7vQGBZ/84uPNgZeOJO9ifUxzAra61OtX8 QeclqEj0zWOvpfesn0RNMVALxsiZZ6WUbHLZdz1Nubx0iyRvDaeCObpmDTXO1ArFwu6SWn6uyK4 RYTAYffKTOpI1Pb0fuCBFSjtXT5Jm0xmAzwB3Q8GbU+AjKUHKq1IoWhwrUyV+gXGo5euHko0+ke gQ3+sRTcJ/8HkFiOZ0HAcY+M9BBDClNW5Q== X-Received: by 2002:aa7:8891:0:b0:8a5:a8a2:95a0 with SMTP id d2e1a72fcca58-8de2e0e3863mr1152322b3a.52.1796363200773; Thu, 03 Dec 2026 21:46:40 -0800 (PST) Received: from server225.. ([39.170.101.209]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-8de121f321asm745490b3a.15.2026.12.03.21.46.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Dec 2026 21:46:40 -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 v3 2/4] mtd: mtdchar: use mtd_alloc_buf() for the read/write bounce buffers Date: Fri, 4 Dec 2026 13:46:28 +0800 Message-Id: <20261204054630.3443342-3-leeonion.muyu@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261204054630.3443342-1-leeonion.muyu@gmail.com> References: <20261204054630.3443342-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, 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