Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.179 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796363199; b=YY0OzwCL3Eg0L66Cwucc4nC6TX52Iv5R9nLpggdukL+OPnu7YDfnQEBkoDdrgsMt0tSy v5LblAhHzuXqfiLqcAii26TAjMgBmziYqdL2HPsP76d9RD8n/YMMTFUGcALdj7sev9mLY tQlrD4G3x2NC+6NN0PIzPrF8//AJ0vlPcMiHz+xFsJZYCc/6I7AboEk1Cqpa02SytJJN1 MUxi9dEMD0azEYo9flw/ufuxJPovtmN5zS+OwqqGHljw/DJi2A0fE2nCs4U+3jna4h8uH XiSDUIuK1uvtPbmM7WYcqC903HWw0jYfo+WZwUDoyN5Pthzi33Jp1zwyETCRhlnbnGw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796363199; 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=H+uZoQjS1BIMRWiRpM7snGk69h3ThnDcheT6OPJs5xc=; b=exMix2ryZ3C+lHLpJj7W4zdO9Lb4ZvEqkbmyLarCnBtYQeEJdAXtiz70lA1VJeR0yr1u /fisOI8bhfw/0UHheSTiJT39VS61MAXEeD6lCm7rW72kCFn+L252VpgRpCyxxfECXB2lX vKrlyZNWstlbLD6rXmI0RK6WW2c9aPtI3ENljCMcRfUCgluhVhFDRz3hBjyTJiQzHbrfy oBq3117BCHgRAepJW1tCl2AaK3JLsFGcaSW2O7zk+CtrirjeVVcQj7m47RQ9AiSRcxcnb rOJpuQBcZfXnx+rcMzXRrKN93XTAfkJdLeJ0lQwvc+2v5hwqYYm+dP4/peKvs/YRp2Q== 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=uVpPDrE7; arc=none smtp.remote-ip=209.85.210.179 Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) (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 C9209606A33E for ; Fri, 4 Dec 2026 05:46:38 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org C9209606A33E 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=uVpPDrE7 Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-8c5ebbc5150so364511b3a.3 for ; Thu, 03 Dec 2026 21:46:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796363198; x=1796967998; 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=H+uZoQjS1BIMRWiRpM7snGk69h3ThnDcheT6OPJs5xc=; b=uVpPDrE7i/5s/2EyULVZV+jq0ODTjroZNZaWutgxO7k/R1316hMLONbnyrfabdx2M5 6Vb4LnN9w4avfMZDvbj7d3gWIfohWUl8wNrCeUEiW+pmFHPcbzjGwBiU8krDGdPF2V0v qiRGzbzFCjCuw6uDXnM+pHT/vW3LjbYnRrSJnzD9aIjjk+PeDU4Xe/iilFf8jjlCEly3 BXzGunaK4GVAisu6fsE74YM4t6kDzJ1sZUOwMh3hIEijY+q62VC/Er5YLTmJeBMwZ/s4 YJBd8YBUSJEU6pyuobF0jyRXnDaaWlWFLkiHD4zuFO5RELeXmPjdU5JOhhHAdP5mfsrD jFSw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796363198; x=1796967998; 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=H+uZoQjS1BIMRWiRpM7snGk69h3ThnDcheT6OPJs5xc=; b=S21gfrJgor4rd2CJI8v+jxug7ueniSJir73ognCg48wpXep2TSWv7gcElPwF4mTAuc m9Sx4cAq2nCSn2m4ioCUFUn6qRPjSFWijqKTvHv0UHFET+yAaM8SJhonQEYXOyxlLIMI OB+YCZvzcxUJT8Bz6tP86pjuTYPqDFJhFHGyOD6VsVMT0+eJ51ZeRxoIMG5o31CVAq9A dmkxtgFGSRgF70sTN8P7a3jCGM8PENZDbukfcGW7aYh2MNZtt05Xuz3c4pEIo4u+NQPV 3wuqeH+lN4396IaPKqOsWNUaXIUmU9kwOwMhQvzuy/m8PZj1lOyciL007H3xjGxMllpS o88A== X-Forwarded-Encrypted: i=1; AL+hErFuXiQdkjIoxM+WaEXNdjwIy3c0/1NWbnGytw90BSf4f9vcz6e+J+X6OxOW63fqfG5lYqwI7B85sw9nWpY=@vger.kernel.org X-Gm-Message-State: APuU4Z8fZH4+dVJXxSLrpbvvMOF7t5wZxAHWFsKK8zd+2RFGyUKcp1ss E2dEqF4rvS6mzXlEqHmMK++jtfZuQANt3NEUbxVKKi3eB3hyhhGtf4cC X-Gm-Gg: AZFdTZfvyW/vJGdQAQpOIS9/FYDjQgTDWHXD2wJNjUSimR14slbJlZ19HZt32StxwL6 2DfUkcrwFlVj6DijDBcGfBLBPgGl3uErVbY5H9F8zZD6GUXaW1YifK06DW0znqJ/Ba5qhSZq9AF x++EK9kkP9q4E/yf23WSRyFCV2c99DJIiapocTPDLDiRvQFJSvfzxQ3zoTfRL9v3SI9SY263uzP k1GNTFOAaHDe+Zm1O+4eyEaJCoH7LWjAdURe6seHk8mUzqlnOh1Pl7ewTOd2wmt+EPBdkddp0Za Uvxc0KAdE5eO0wPHuXPkLFZf65nGQBXxzRlv+FBRVehQkoYi1KCK8WchUKvtfVNL2zl/jOD6B+H yVvkiZDYSgFmIoySrHtQG7t/+VvG5a36dOxRaDC5wEUFJMMuIpo5/uykszvs1yH3vHG7x7tAgv0 vYjYT8dNleSMD4PCURwNTtfsULcpKC3rTIkUezw8+GO81GqXgJsXXXhFtA/jeGfF86buE+5UnyV zyZNbgCIJWnJhRVUE85IRYSpwHUBCZRA4zNfM7LDl1YEhVixZZj+7Wz3Y+fR7tt2E4F3m3kcCNU IlS/jTvkSvLbdAS09vMwaa6xK2D+Q9blPw== X-Received: by 2002:a05:6a00:e05:b0:899:632a:9613 with SMTP id d2e1a72fcca58-8de2deeffffmr1018632b3a.40.1796363197969; Thu, 03 Dec 2026 21:46:37 -0800 (PST) Received: from server225.. ([39.170.101.209]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-8de121f321asm745490b3a.15.2026.12.03.21.46.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Dec 2026 21:46:37 -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 1/4] mtd: core: add per-device bounce buffer allocation helper Date: Fri, 4 Dec 2026 13:46:27 +0800 Message-Id: <20261204054630.3443342-2-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 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. - 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 in the following patch. Signed-off-by: Ping Li --- drivers/mtd/mtdcore.c | 44 +++++++++++++++++++++++++++++++++++++++++ drivers/mtd/mtdpart.c | 1 + include/linux/mtd/mtd.h | 16 +++++++++++++++ 3 files changed, 61 insertions(+) diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c index b170c3144ca8f..83365c0fbf063 100644 --- a/drivers/mtd/mtdcore.c +++ b/drivers/mtd/mtdcore.c @@ -2566,6 +2566,50 @@ 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. + * + * 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); +} +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 /*====================================================================*/ diff --git a/drivers/mtd/mtdpart.c b/drivers/mtd/mtdpart.c index ddded0dbe77e0..9c245ad421a55 100644 --- a/drivers/mtd/mtdpart.c +++ b/drivers/mtd/mtdpart.c @@ -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; diff --git a/include/linux/mtd/mtd.h b/include/linux/mtd/mtd.h index 8d10d9d2e8300..4296d255c15d8 100644 --- a/include/linux/mtd/mtd.h +++ b/include/linux/mtd/mtd.h @@ -368,6 +368,20 @@ struct mtd_info { */ bool oops_panic_write; + /* + * Low-level drivers that only memcpy() the data buffers received + * from the MTD layer, or which handle non-linear-map buffers + * themselves (internal bounce), may set this to receive vmalloc'd + * buffers, keeping the requested size intact even under heavy + * memory fragmentation. + * + * Defaults to false: buffers always come from + * mtd_kmalloc_up_to(), which gracefully shrinks the size under + * fragmentation, so drivers never unexpectedly receive a buffer + * that is not DMA-able. + */ + bool allows_vmalloc_bufs; + struct notifier_block reboot_notifier; /* default mode before reboot */ /* ECC status information */ @@ -699,6 +713,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; -- 2.34.1