linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] ext4: use kmemdup_nul() instead of kstrndup()
@ 2020-08-08 13:45 Xianting Tian
  2020-08-17 17:08 ` Jan Kara
  0 siblings, 1 reply; 2+ messages in thread
From: Xianting Tian @ 2020-08-08 13:45 UTC (permalink / raw)
  To: tytso, adilger.kernel; +Cc: linux-ext4, linux-kernel, clang-built-linux

kmemdup_nul() is more efficient than kmemdup_nul() if
the size is known exactly.

The description of kstrndup() already suggested:
Note: Use kmemdup_nul() instead if the size is known exactly.

Signed-off-by: Xianting Tian <xianting_tian@126.com>
---
 fs/ext4/super.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fs/ext4/super.c b/fs/ext4/super.c
index 330957e..be37556 100644
--- a/fs/ext4/super.c
+++ b/fs/ext4/super.c
@@ -4016,7 +4016,7 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent)
 	}
 
 	if (sbi->s_es->s_mount_opts[0]) {
-		char *s_mount_opts = kstrndup(sbi->s_es->s_mount_opts,
+		char *s_mount_opts = kmemdup_nul(sbi->s_es->s_mount_opts,
 					      sizeof(sbi->s_es->s_mount_opts),
 					      GFP_KERNEL);
 		if (!s_mount_opts)
-- 
1.8.3.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] ext4: use kmemdup_nul() instead of kstrndup()
  2020-08-08 13:45 [PATCH] ext4: use kmemdup_nul() instead of kstrndup() Xianting Tian
@ 2020-08-17 17:08 ` Jan Kara
  0 siblings, 0 replies; 2+ messages in thread
From: Jan Kara @ 2020-08-17 17:08 UTC (permalink / raw)
  To: Xianting Tian
  Cc: tytso, adilger.kernel, linux-ext4, linux-kernel, clang-built-linux

On Sat 08-08-20 09:45:36, Xianting Tian wrote:
> kmemdup_nul() is more efficient than kmemdup_nul() if
> the size is known exactly.
> 
> The description of kstrndup() already suggested:
> Note: Use kmemdup_nul() instead if the size is known exactly.
> 
> Signed-off-by: Xianting Tian <xianting_tian@126.com>

Thanks for the patch but I think it is not ideal. The thing is that
s_mount_opt is actually a NUL terminated string (we are just careful and
don't rely on that to avoid problems when the fs is corrupted). So it is
possibly shorter than those 64 bytes reserved for it in the superblock and
using kmemdup_nul() will allocate and copy more memory than strictly
necessary.

								Honza

> ---
>  fs/ext4/super.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/fs/ext4/super.c b/fs/ext4/super.c
> index 330957e..be37556 100644
> --- a/fs/ext4/super.c
> +++ b/fs/ext4/super.c
> @@ -4016,7 +4016,7 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent)
>  	}
>  
>  	if (sbi->s_es->s_mount_opts[0]) {
> -		char *s_mount_opts = kstrndup(sbi->s_es->s_mount_opts,
> +		char *s_mount_opts = kmemdup_nul(sbi->s_es->s_mount_opts,
>  					      sizeof(sbi->s_es->s_mount_opts),
>  					      GFP_KERNEL);
>  		if (!s_mount_opts)
> -- 
> 1.8.3.1
> 
-- 
Jan Kara <jack@suse.com>
SUSE Labs, CR

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-08-17 17:29 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-08 13:45 [PATCH] ext4: use kmemdup_nul() instead of kstrndup() Xianting Tian
2020-08-17 17:08 ` Jan Kara

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).