public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] x86/mtrr: Remove unnecessary strlen() in mtrr_write()
@ 2025-02-25 13:16 Thorsten Blum
  2025-02-25 13:35 ` Ingo Molnar
  2025-02-25 20:00 ` [tip: x86/mm] " tip-bot2 for Thorsten Blum
  0 siblings, 2 replies; 5+ messages in thread
From: Thorsten Blum @ 2025-02-25 13:16 UTC (permalink / raw)
  To: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
	H. Peter Anvin, Thorsten Blum
  Cc: linux-kernel

The local variable length already holds the string length after calling
strncpy_from_user(). Using another local variable linlen and calling
strlen() is therefore unnecessary and can be removed. Remove linlen
and strlen() and use length instead.

Compile-tested only.

Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
---
 arch/x86/kernel/cpu/mtrr/if.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/arch/x86/kernel/cpu/mtrr/if.c b/arch/x86/kernel/cpu/mtrr/if.c
index a5c506f6da7f..4049235b1bfe 100644
--- a/arch/x86/kernel/cpu/mtrr/if.c
+++ b/arch/x86/kernel/cpu/mtrr/if.c
@@ -99,7 +99,6 @@ mtrr_write(struct file *file, const char __user *buf, size_t len, loff_t * ppos)
 	char *ptr;
 	char line[LINE_SIZE];
 	int length;
-	size_t linelen;
 
 	memset(line, 0, LINE_SIZE);
 
@@ -108,9 +107,8 @@ mtrr_write(struct file *file, const char __user *buf, size_t len, loff_t * ppos)
 	if (length < 0)
 		return length;
 
-	linelen = strlen(line);
-	ptr = line + linelen - 1;
-	if (linelen && *ptr == '\n')
+	ptr = line + length - 1;
+	if (length && *ptr == '\n')
 		*ptr = '\0';
 
 	if (!strncmp(line, "disable=", 8)) {
-- 
2.48.1


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

* Re: [PATCH] x86/mtrr: Remove unnecessary strlen() in mtrr_write()
  2025-02-25 13:16 [PATCH] x86/mtrr: Remove unnecessary strlen() in mtrr_write() Thorsten Blum
@ 2025-02-25 13:35 ` Ingo Molnar
  2025-02-25 19:17   ` Thorsten Blum
  2025-02-25 20:00 ` [tip: x86/mm] " tip-bot2 for Thorsten Blum
  1 sibling, 1 reply; 5+ messages in thread
From: Ingo Molnar @ 2025-02-25 13:35 UTC (permalink / raw)
  To: Thorsten Blum
  Cc: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
	H. Peter Anvin, linux-kernel


* Thorsten Blum <thorsten.blum@linux.dev> wrote:

> The local variable length already holds the string length after calling
> strncpy_from_user(). Using another local variable linlen and calling
> strlen() is therefore unnecessary and can be removed. Remove linlen
> and strlen() and use length instead.
> 
> Compile-tested only.
> 
> Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
> ---
>  arch/x86/kernel/cpu/mtrr/if.c | 6 ++----
>  1 file changed, 2 insertions(+), 4 deletions(-)
> 
> diff --git a/arch/x86/kernel/cpu/mtrr/if.c b/arch/x86/kernel/cpu/mtrr/if.c
> index a5c506f6da7f..4049235b1bfe 100644
> --- a/arch/x86/kernel/cpu/mtrr/if.c
> +++ b/arch/x86/kernel/cpu/mtrr/if.c
> @@ -99,7 +99,6 @@ mtrr_write(struct file *file, const char __user *buf, size_t len, loff_t * ppos)
>  	char *ptr;
>  	char line[LINE_SIZE];
>  	int length;
> -	size_t linelen;
>  
>  	memset(line, 0, LINE_SIZE);
>  
> @@ -108,9 +107,8 @@ mtrr_write(struct file *file, const char __user *buf, size_t len, loff_t * ppos)
>  	if (length < 0)
>  		return length;
>  
> -	linelen = strlen(line);
> -	ptr = line + linelen - 1;
> -	if (linelen && *ptr == '\n')
> +	ptr = line + length - 1;
> +	if (length && *ptr == '\n')
>  		*ptr = '\0';

I see no corner-case analysis in the changelog about what may happen if 
the copy fails partially.

Thanks,

	Ingo

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

* Re: [PATCH] x86/mtrr: Remove unnecessary strlen() in mtrr_write()
  2025-02-25 13:35 ` Ingo Molnar
@ 2025-02-25 19:17   ` Thorsten Blum
  2025-02-25 19:50     ` Ingo Molnar
  0 siblings, 1 reply; 5+ messages in thread
From: Thorsten Blum @ 2025-02-25 19:17 UTC (permalink / raw)
  To: Ingo Molnar
  Cc: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
	H. Peter Anvin, linux-kernel

On 25. Feb 2025, at 14:35, Ingo Molnar wrote:
> * Thorsten Blum <thorsten.blum@linux.dev> wrote:
> 
>> The local variable length already holds the string length after calling
>> strncpy_from_user(). Using another local variable linlen and calling
>> strlen() is therefore unnecessary and can be removed. Remove linlen
>> and strlen() and use length instead.
>> 
>> Compile-tested only.
>> 
>> Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
>> ---
> 
> I see no corner-case analysis in the changelog about what may happen if 
> the copy fails partially.

Hi Ingo,

I'm not sure what you mean. Why would I describe something I didn't
change? This patch only removes an unnecessary string (line) length
calculation and shouldn't affect the logic in any way.

If strncpy_from_user() fails, we immediately return length from
mtrr_write(), but my patch doesn't change this - unless I'm missing
something?

Thanks,
Thorsten


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

* Re: [PATCH] x86/mtrr: Remove unnecessary strlen() in mtrr_write()
  2025-02-25 19:17   ` Thorsten Blum
@ 2025-02-25 19:50     ` Ingo Molnar
  0 siblings, 0 replies; 5+ messages in thread
From: Ingo Molnar @ 2025-02-25 19:50 UTC (permalink / raw)
  To: Thorsten Blum
  Cc: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
	H. Peter Anvin, linux-kernel


* Thorsten Blum <thorsten.blum@linux.dev> wrote:

> On 25. Feb 2025, at 14:35, Ingo Molnar wrote:
> > * Thorsten Blum <thorsten.blum@linux.dev> wrote:
> > 
> >> The local variable length already holds the string length after calling
> >> strncpy_from_user(). Using another local variable linlen and calling
> >> strlen() is therefore unnecessary and can be removed. Remove linlen
> >> and strlen() and use length instead.
> >> 
> >> Compile-tested only.
> >> 
> >> Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
> >> ---
> > 
> > I see no corner-case analysis in the changelog about what may happen if 
> > the copy fails partially.
> 
> Hi Ingo,
> 
> I'm not sure what you mean. Why would I describe something I didn't
> change? This patch only removes an unnecessary string (line) length
> calculation and shouldn't affect the logic in any way.
> 
> If strncpy_from_user() fails, we immediately return length from
> mtrr_write(), but my patch doesn't change this - unless I'm missing
> something?

My bad: I was fixated on this 'some data may have been copied' language 
in the strncpy_from_user() documentation:

 * If access to userspace fails, returns -EFAULT (some data may have been
 * copied).

which would make the strlen() result ambiguous. But this doesn't matter 
here - if there was a fault then we get an unconditional -EFAULT from 
strncpy_from_user(), so your patch should be equivalent to the previous 
logic.

Thanks,

	Ingo

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

* [tip: x86/mm] x86/mtrr: Remove unnecessary strlen() in mtrr_write()
  2025-02-25 13:16 [PATCH] x86/mtrr: Remove unnecessary strlen() in mtrr_write() Thorsten Blum
  2025-02-25 13:35 ` Ingo Molnar
@ 2025-02-25 20:00 ` tip-bot2 for Thorsten Blum
  1 sibling, 0 replies; 5+ messages in thread
From: tip-bot2 for Thorsten Blum @ 2025-02-25 20:00 UTC (permalink / raw)
  To: linux-tip-commits; +Cc: Thorsten Blum, Ingo Molnar, x86, linux-kernel

The following commit has been merged into the x86/mm branch of tip:

Commit-ID:     8e8f0306497dea58fb4e8e2558949daae5eeac5c
Gitweb:        https://git.kernel.org/tip/8e8f0306497dea58fb4e8e2558949daae5eeac5c
Author:        Thorsten Blum <thorsten.blum@linux.dev>
AuthorDate:    Tue, 25 Feb 2025 14:16:19 +01:00
Committer:     Ingo Molnar <mingo@kernel.org>
CommitterDate: Tue, 25 Feb 2025 20:50:55 +01:00

x86/mtrr: Remove unnecessary strlen() in mtrr_write()

The local variable length already holds the string length after calling
strncpy_from_user(). Using another local variable linlen and calling
strlen() is therefore unnecessary and can be removed. Remove linlen
and strlen() and use length instead.

No change in functionality intended.

Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Link: https://lore.kernel.org/r/20250225131621.329699-2-thorsten.blum@linux.dev
---
 arch/x86/kernel/cpu/mtrr/if.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/arch/x86/kernel/cpu/mtrr/if.c b/arch/x86/kernel/cpu/mtrr/if.c
index a5c506f..4049235 100644
--- a/arch/x86/kernel/cpu/mtrr/if.c
+++ b/arch/x86/kernel/cpu/mtrr/if.c
@@ -99,7 +99,6 @@ mtrr_write(struct file *file, const char __user *buf, size_t len, loff_t * ppos)
 	char *ptr;
 	char line[LINE_SIZE];
 	int length;
-	size_t linelen;
 
 	memset(line, 0, LINE_SIZE);
 
@@ -108,9 +107,8 @@ mtrr_write(struct file *file, const char __user *buf, size_t len, loff_t * ppos)
 	if (length < 0)
 		return length;
 
-	linelen = strlen(line);
-	ptr = line + linelen - 1;
-	if (linelen && *ptr == '\n')
+	ptr = line + length - 1;
+	if (length && *ptr == '\n')
 		*ptr = '\0';
 
 	if (!strncmp(line, "disable=", 8)) {

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

end of thread, other threads:[~2025-02-25 20:00 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-02-25 13:16 [PATCH] x86/mtrr: Remove unnecessary strlen() in mtrr_write() Thorsten Blum
2025-02-25 13:35 ` Ingo Molnar
2025-02-25 19:17   ` Thorsten Blum
2025-02-25 19:50     ` Ingo Molnar
2025-02-25 20:00 ` [tip: x86/mm] " tip-bot2 for Thorsten Blum

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox