From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.174 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796375810; b=aT1fr0TlUjy/1ub/p+bDGALmxltf7zrW8TVjdw75B39Os1cyg6YoA62CtfJMACM010Ek SHDd5lIST/VwN5ZyztpbZ8EzcGHbdPdaSkg8JkeBxm1PQSCKSQVuB3yaDpm+HQkomz/mK SvI8VPLkxKoi6vUOA3jjpQ3vfIXPaxrpKN2pp9Yvn3dysORcFDbshatuJJuyd7xgzsN4D waXaOQIU0N+26kDN/JEHJY1N1fk+CDaXFAyNJBjF5t0En/gMdBwKNkFmRw0v6aZcvA4AD icZSmXYIbVvr1qyP5DQ8Cd8OWgs3yVF5ZjYhciSIrkmJWkDGtPmDX4RSF7h9N9n64og== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796375810; 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=1RR8Vimy36qW6LJy5Qh4FyHVSshx341j1F+xDGqVPbU=; b=UhAcLEbtuA0wcPtqrWGlmPsWZAYLx8efA3kSE40nNG+cxnrp6gt5Pzk8cyucetCO1fsL Ao1WF3mW+sx5Lx1xYwl942fDq5IWM0u9UtmcmjyE1tVOVn/ZjqglKJpqvXtzmIjJJPyuX oOhCN3FqrzYzPNqoKPG/4UatR29EtZ/4azhFViMP/jYDgJJ0YEsXdEMWkGF1T7NhugTUu cZE0yLTYzOZmcCFonxyGFHzd8ca1vqMRIlrUyWrFl7ss0r/92PEwmgvzKoDeHme03hTrL U7n6I9kMAHM76SjReDg3NaKBdqpOSYE10QvQfLbR1cLKjrZ9zCr5rdhfoSyDwLLon0A== 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=vbJnbp7O; arc=none smtp.remote-ip=209.85.214.174 Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) (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 0CE62606A330 for ; Fri, 4 Dec 2026 09:16:49 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 0CE62606A330 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=vbJnbp7O Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-2f020d0169eso4184905ad.0 for ; Fri, 04 Dec 2026 01:16:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796375809; x=1796980609; 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=1RR8Vimy36qW6LJy5Qh4FyHVSshx341j1F+xDGqVPbU=; b=vbJnbp7OzbD2yPrNjWjJx/iHpKogUCiCHp564Dev7ZtEn86xWDPM1Ne9wNwXcWMQAd 52j3PhG8YBWsVq8mqjOS25hYWEYCeOk5tcZ/rJacGSQxYyXqUWrMbeYFwl24eBynlOgq e//w6g7NEW4yxD7FZ+P3JKDmRAKTdJKdeJbCQRMcamstTf32UuDPpQzD+E7YMTubopOG 44fzkprwLv08ceXKTEty5O3h8rDGmSTS6Xyxt3dctlM2k9uTewgSd0i708CfDFGx3oDR EIrrPZyPUtvXaN8J5FFUvx6VPYnGKXPGE3HZQF5vw2Nj1ILp7G6SQoSPtb56AQcrkjJ1 vs0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796375809; x=1796980609; 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=1RR8Vimy36qW6LJy5Qh4FyHVSshx341j1F+xDGqVPbU=; b=PZQkwXINb0lJyE5zr9GvzS3brgM83Gk0CPU0O2sQvxk/LCQQNIPJ1Hk5/G+7n35Wil Y+sLHDgPuLi74Zq2OhG+tQPRrzVw7QbNSaSE0GKkrWgMRJrDp3HoRuYcsU8k0YX4GY86 +xuGyzZGqeNjAIKBkrVDzkRtfoL2tRjKvHLaUNZ9/QQ0BGqQwajNjHD3P5b9ANGvy7TG 9lXKf7Iyvrz5BADUMEuhZwqtSVcxnmB30k9ZtR1rl23s7og3VPC0j/lD+xTv9ogI+qKl DGO2/DSBaw0D5hQhObZwSiZ3/13BdE1u6crDQwsDLNQzFQoto6XppHboigFMAKJE+9XU /Jhw== X-Forwarded-Encrypted: i=1; AL+hErHY9RsVkgkrAFcySokO0yqmrydJeaNaS/RXK0IBypkVwSTTPlV9oVIvbG3HQIf4glWqLraAfq6wg8z/Dv4=@vger.kernel.org X-Gm-Message-State: APuU4Z+MKpY2PuDxHhij9D0qmhLyI5ZWHg9AQirx/haYJmN8VHfS2nEp eZ9GODJ9bPuS/nLr1q4FgUOpIw30ynZWs5NubuqsVnCnDuMOr/L+ZLqZ X-Gm-Gg: AZFdTZfcMleUVMiEjZ8KzLuT2GPEPle2jI01eh9WSyUE/HaLVP3a6txPZQegaFFCAYH bduQuUY2TaxIu80anEj+Kns7xzqYWx4gsrhHbuxVgbEy6DU2lQxoCykO3oweGesaZswmhB6tVNE JMgieJUf/dAUa6EQAgvCdvhge1c+1712OnWfEr99Rmb69l7wrMduCK3YfxqmxIlldClhxxRBuGG eZDAfJ3C4hESAw3dtfMTM9glPRCVUAaZkD9JawaxrDRCJz1+6pHXnADBBbp0/TbfZsitmISJ+sq A3GZpbSmj5LQOMzEx2WFCKqxujcT7DqervQP0RJhw8k/YN19S8o+Eaq+vHZ4seEuOeiOrb5zEoo zNGFfBzFGdbC034i0mQ9syAjq38DHx4Ks+lOqewSr3uyM3xEYTzegmZDJR0t0CMX7XCs8VfXW6x Jz6AE++QOfsNFC4wC471XOaI6GU7CoHhtNcAn93sJJqVifo/llNDh1J+ONb/d+MwS9LpXdZ7Kl3 2Na7ludywwQspP8WuO9A2zv/70HjDYE3b8BPz9JYSZzzCxUnQsWH09Lsp85QEEcvb+4JaE53Syx rvAXJ/5cUHcRcjR/Mal+NTb/T4PUIDeDvg== X-Received: by 2002:a17:90b:2784:b0:3ac:929e:691 with SMTP id 98e67ed59e1d1-3b5e061cd8amr1118370a91.17.1796375809002; Fri, 04 Dec 2026 01:16:49 -0800 (PST) Received: from server225.. ([39.170.101.209]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3b5ddc54888sm2885970a91.11.2026.12.04.01.16.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Dec 2026 01:16:48 -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 1/4] mtd: core: add per-device bounce buffer allocation helper Date: Fri, 4 Dec 2026 17:16:37 +0800 Message-Id: <20261204091640.444442-2-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 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. Signed-off-by: Ping Li --- drivers/mtd/mtdcore.c | 45 +++++++++++++++++++++++++++++++++++++++++ drivers/mtd/mtdpart.c | 1 + include/linux/mtd/mtd.h | 9 +++++++++ 3 files changed, 55 insertions(+) diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c index b170c3144ca8f..a96eb69407758 100644 --- a/drivers/mtd/mtdcore.c +++ b/drivers/mtd/mtdcore.c @@ -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 /*====================================================================*/ 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..e746c9d490041 100644 --- a/include/linux/mtd/mtd.h +++ b/include/linux/mtd/mtd.h @@ -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 -- 2.34.1