From patchwork Wed Jan 14 22:56:13 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 1517 Return-Path: X-Original-To: u-boot-concept@u-boot.org Delivered-To: u-boot-concept@u-boot.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1768431512; bh=ctPMEcAKe9Ru0PaXzw/CV6BK4deMbNCqVsBvZ7U8AIA=; h=From:To:Date:In-Reply-To:References:CC:Subject:List-Id: List-Archive:List-Help:List-Owner:List-Post:List-Subscribe: List-Unsubscribe:From; b=Hp0Ff/ewXx7WkOf+bmoLwmL9hV9enYGvZxmYni77fqzau+Hh4a1xiLfrCgY+yj/w+ Awjp50lQmTypAyVf4M9vahdDV4HOwHDfAaVsTmFn4qhxGm9kGWddxy+euT5ZtylXNH sZbSw6FstVUnVemYC3lKervKo3HihcTSxGRA4v71pq8XYyFd9NbKt3EfU2f8JrbfBE BEbsR8fRBMR948PSKAsaYk0twTnHKgp+F71PSDYM0JbSvl/l2mRUod0mgT/sJ/tpVr peNbfCDjW++v62kHFSAJwK5Sd+Qb0waX3cFGQaJ4jEdMYCrADeFvE/Ykc1f4ARSvsQ D0EpKKWHRDepA== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 514E26935F for ; Wed, 14 Jan 2026 15:58:32 -0700 (MST) X-Virus-Scanned: Debian amavis at Received: from mail.u-boot.org ([127.0.0.1]) by localhost (mail.u-boot.org [127.0.0.1]) (amavis, port 10024) with ESMTP id HrqXHjAiBNEx for ; Wed, 14 Jan 2026 15:58:32 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1768431512; bh=ctPMEcAKe9Ru0PaXzw/CV6BK4deMbNCqVsBvZ7U8AIA=; h=From:To:Date:In-Reply-To:References:CC:Subject:List-Id: List-Archive:List-Help:List-Owner:List-Post:List-Subscribe: List-Unsubscribe:From; b=Hp0Ff/ewXx7WkOf+bmoLwmL9hV9enYGvZxmYni77fqzau+Hh4a1xiLfrCgY+yj/w+ Awjp50lQmTypAyVf4M9vahdDV4HOwHDfAaVsTmFn4qhxGm9kGWddxy+euT5ZtylXNH sZbSw6FstVUnVemYC3lKervKo3HihcTSxGRA4v71pq8XYyFd9NbKt3EfU2f8JrbfBE BEbsR8fRBMR948PSKAsaYk0twTnHKgp+F71PSDYM0JbSvl/l2mRUod0mgT/sJ/tpVr peNbfCDjW++v62kHFSAJwK5Sd+Qb0waX3cFGQaJ4jEdMYCrADeFvE/Ykc1f4ARSvsQ D0EpKKWHRDepA== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 401AB69345 for ; Wed, 14 Jan 2026 15:58:32 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1768431510; bh=DHIqpBv9TpdL3P+i/EXAljVChJYQ6LxYNO7GoHI7Rd0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lAFUQ9D/dzMtp3tDx3Ul9kYm3Y+rUEO+70jfasb/N/7ZT0gTYdz480NJhWoXnABAF 9UJuD4U4y1wwZ2aeb+tKpCC27dct/cRe5iqS0wHGrvSfBzs0CXQApXk3o2mEgVDy9K JcbS/RvK15ATckwag6/5UMLwmMm+ds4QulujGebkpypHqZIYTNjo08WcgE7TO2oacd r5KgtRVJF9vss6TBmp+9fCuttNKJRVmv4Qp4nJ8N66ynLGziZrKzxL/ctDVlrt2QmU YpNcWUFxZuj4uQmB91+t2HXqv5wrlTGpkgnjAX5RDs2s0mpjmQBbQCirSEX0Vq4bQa 3P72Il7sARQaQ== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id CCA6B6913F; Wed, 14 Jan 2026 15:58:30 -0700 (MST) X-Virus-Scanned: Debian amavis at Received: from mail.u-boot.org ([127.0.0.1]) by localhost (mail.u-boot.org [127.0.0.1]) (amavis, port 10026) with ESMTP id iVh3tfzptfGG; Wed, 14 Jan 2026 15:58:30 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1768431506; bh=XSWyl3O0ntFEa2YkiAhrSNH+HW9RDYJIraFwNZvKUJM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QxAXGwZO6BgeMxtAVyztoQxZ2M363eZ/3lW/SbegKUu9WGJklaFA7Rq823X+f/x+H 58wu00cEty6Km/sfsFM5liaJCMwQanaGgVWOxBfG98YrSzdffqVCGrZPCOtE6qQY2e ADhNyy0DwaBdkytmwcjvTuZ8xrDyqPkhLql08veYvphV+sumqjkOg3/wniAMUA9Mbq 42M0bhWYN/63i3GkbUmjzWEvxlt8m/qBzdamedgH98pQybTGHvgxbxsJ7iy9hRlvs8 I7MsMQ+5iWbCxLDLuEl8mE5UivDqnmXZnp7nOflBbFtaKer3OAHdp+M4mr92En6TPt 4ObTRZ3+Lqueg== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id 6697E692EF; Wed, 14 Jan 2026 15:58:26 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Wed, 14 Jan 2026 15:56:13 -0700 Message-ID: <20260114225635.3407989-24-sjg@u-boot.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260114225635.3407989-1-sjg@u-boot.org> References: <20260114225635.3407989-1-sjg@u-boot.org> MIME-Version: 1.0 Message-ID-Hash: J5H3XE3NVIEHAVKXVUCBSPWLIE5FPB5X X-Message-ID-Hash: J5H3XE3NVIEHAVKXVUCBSPWLIE5FPB5X X-MailFrom: sjg@u-boot.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Simon Glass , "Claude Opus 4 . 5" X-Mailman-Version: 3.3.10 Precedence: list Subject: [Concept] [PATCH 23/34] ext4l: Move string helpers to standard locations List-Id: Discussion and patches related to U-Boot Concept Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: From: Simon Glass The ext4l code defines string helper functions locally instead of in the standard Linux compatibility headers. Move these functions to their proper locations: - strtomem_pad(): macro added to linux/string.h - strscpy_pad(): macro added to linux/string.h - strreplace(): declaration in linux/string.h, implementation in lib/string.c - kmemdup_nul(): declaration in linux/slab.h (alongside kmemdup()), implementation in lib/string.c This makes these functions available to other parts of U-Boot and reduces duplication in ext4l. Co-developed-by: Claude Opus 4.5 Signed-off-by: Simon Glass --- fs/ext4l/ext4_uboot.h | 20 ++++---------------- fs/ext4l/stub.c | 32 ++++---------------------------- include/linux/slab.h | 13 +++++++++++++ include/linux/string.h | 41 +++++++++++++++++++++++++++++++++++++++++ lib/string.c | 25 +++++++++++++++++++++++++ 5 files changed, 87 insertions(+), 44 deletions(-) diff --git a/fs/ext4l/ext4_uboot.h b/fs/ext4l/ext4_uboot.h index ce40888879e..7b90276b2bc 100644 --- a/fs/ext4l/ext4_uboot.h +++ b/fs/ext4l/ext4_uboot.h @@ -934,16 +934,8 @@ struct dx_hash_info { /* pr_warn_once is in linux/printk.h */ /* lockdep_assert_held_read is in linux/lockdep.h */ - -/* strtomem_pad - copy string to fixed-size buffer with padding */ -#define strtomem_pad(dest, src, pad) do { \ - size_t _len = strlen(src); \ - if (_len >= sizeof(dest)) \ - _len = sizeof(dest); \ - memcpy(dest, src, _len); \ - if (_len < sizeof(dest)) \ - memset((char *)(dest) + _len, (pad), sizeof(dest) - _len); \ -} while (0) +/* strtomem_pad is in linux/string.h */ +/* strscpy_pad is in linux/string.h */ /* Memory weight - count set bits */ static inline unsigned long memweight(const void *ptr, size_t bytes) @@ -2075,10 +2067,7 @@ struct dentry *generic_fh_to_parent(struct super_block *sb, struct fid *fid, /* I/O priority - declaration for stub.c */ int IOPRIO_PRIO_VALUE(int class, int data); -/* String operations */ -char *kmemdup_nul(const char *s, size_t len, gfp_t gfp); -#define strscpy_pad(dst, src) strncpy(dst, src, sizeof(dst)) - +/* kmemdup_nul is in linux/slab.h */ /* fscrypt declarations are in ext4_fscrypt.h */ /* Memory allocation - declarations for stub.c */ @@ -2191,8 +2180,7 @@ struct mb_cache_entry { void generic_set_sb_d_ops(struct super_block *sb); struct dentry *d_make_root(struct inode *inode); -/* String operations - declarations for stub.c */ -char *strreplace(const char *str, char old, char new); +/* strreplace is in linux/string.h */ /* Ratelimit - declaration for stub.c */ void ratelimit_state_init(void *rs, int interval, int burst); diff --git a/fs/ext4l/stub.c b/fs/ext4l/stub.c index a0c21dfd14c..c14132815cd 100644 --- a/fs/ext4l/stub.c +++ b/fs/ext4l/stub.c @@ -373,32 +373,8 @@ int ext4_update_overhead(struct super_block *sb, bool force) return 0; } -/* String stubs */ -/* strtomem_pad is now a macro in ext4_uboot.h */ - -char *strreplace(const char *str, char old, char new) -{ - char *s = (char *)str; - - while (*s) { - if (*s == old) - *s = new; - s++; - } - return (char *)str; -} - -char *kmemdup_nul(const char *s, size_t len, gfp_t gfp) -{ - char *buf; - - buf = kmalloc(len + 1, gfp); - if (buf) { - memcpy(buf, s, len); - buf[len] = '\0'; - } - return buf; -} +/* strtomem_pad, strscpy_pad, strreplace are now in linux/string.h */ +/* kmemdup_nul is now in linux/slab.h, with implementation in lib/string.c */ /* Page allocation */ unsigned long get_zeroed_page(gfp_t gfp) @@ -506,8 +482,8 @@ int sb_set_blocksize(struct super_block *sb, int size) return size; } -/* strscpy_pad is now a macro in ext4_uboot.h */ -/* kmemdup_nul is defined earlier in this file */ +/* strscpy_pad is now a macro in linux/string.h */ +/* kmemdup_nul is now in lib/string.c */ /* Address check */ int generic_check_addressable(unsigned int blocksize_bits, u64 num_blocks) diff --git a/include/linux/slab.h b/include/linux/slab.h index f0c0add0cbd..4f413f93fa3 100644 --- a/include/linux/slab.h +++ b/include/linux/slab.h @@ -87,6 +87,19 @@ static inline void *krealloc(const void *p, size_t new_size, gfp_t flags) void *kmemdup(const void *src, size_t len, gfp_t gfp); +/** + * kmemdup_nul - Duplicate a string with null termination + * @s: Source string + * @len: Maximum length to copy + * @gfp: GFP flags for allocation + * + * Allocates len + 1 bytes, copies up to @len bytes from @s, and + * ensures the result is null-terminated. + * + * Return: pointer to new string, or NULL on allocation failure + */ +char *kmemdup_nul(const char *s, size_t len, gfp_t gfp); + /* kmem_cache stubs */ struct kmem_cache { int sz; diff --git a/include/linux/string.h b/include/linux/string.h index d943fcce690..591d99c46a1 100644 --- a/include/linux/string.h +++ b/include/linux/string.h @@ -147,6 +147,47 @@ char *memdup(const void *src, size_t len); unsigned long ustrtoul(const char *cp, char **endp, unsigned int base); unsigned long long ustrtoull(const char *cp, char **endp, unsigned int base); +/** + * strreplace() - Replace all occurrences of a character in a string + * @str: The string to operate on + * @old: The character being replaced + * @new: The character @old is replaced with + * + * Replaces all occurrences of character @old with character @new in + * the string @str in place. + * + * Return: pointer to the string @str itself + */ +char *strreplace(char *str, char old, char new); + +/** + * strtomem_pad - Copy string to fixed-size buffer with padding + * @dest: Destination buffer (must be an array, not a pointer) + * @src: Source string + * @pad: Padding character to fill remaining space + * + * Copy @src to @dest, truncating if necessary. If @src is shorter + * than @dest, fill the remaining bytes with @pad. + */ +#define strtomem_pad(dest, src, pad) do { \ + size_t _len = strlen(src); \ + if (_len >= sizeof(dest)) \ + _len = sizeof(dest); \ + memcpy(dest, src, _len); \ + if (_len < sizeof(dest)) \ + memset((char *)(dest) + _len, (pad), \ + sizeof(dest) - _len); \ +} while (0) + +/** + * strscpy_pad - Copy string to fixed-size buffer with padding + * @dest: Destination buffer (must be an array) + * @src: Source string + * + * Copy @src to @dest ensuring null termination and zero-padding. + */ +#define strscpy_pad(dest, src) strncpy(dest, src, sizeof(dest)) + #ifdef __cplusplus } #endif diff --git a/lib/string.c b/lib/string.c index d56f88d4a84..e297eb99df1 100644 --- a/lib/string.c +++ b/lib/string.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -802,3 +803,27 @@ void *memchr_inv(const void *start, int c, size_t bytes) return check_bytes8(start, value, bytes % 8); } #endif + +char *strreplace(char *str, char old, char new) +{ + char *s = str; + + while (*s) { + if (*s == old) + *s = new; + s++; + } + return str; +} + +char *kmemdup_nul(const char *s, size_t len, gfp_t gfp) +{ + char *buf; + + buf = kmalloc(len + 1, gfp); + if (buf) { + memcpy(buf, s, len); + buf[len] = '\0'; + } + return buf; +}