All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/1] profile: account for partial /proc/profile reads
@ 2026-11-14  6:30 Andrew Morton
  2026-11-14 12:42 ` Bradley Morgan
  0 siblings, 1 reply; 2+ messages in thread
From: Andrew Morton @ 2026-11-14  6:30 UTC (permalink / raw)
  To: Bradley Morgan; +Cc: linux-kernel, Andrew Morton

From: Andrew Morton <akpm@linux-foundation.org>
Subject: profile: account for partial /proc/profile reads
Date: Tue, 20 Oct 2026 15:28:00 -0700

read_profile() returns -EFAULT without advancing the file position
when a user copy faults, even if earlier header bytes or a prefix of
the counters were successfully copied.  A retry can therefore copy
the same bytes again despite the previous read having delivered data.

Return the number of successfully copied bytes and advance *ppos by
that amount.  Return -EFAULT only if the fault occurs before any bytes
are copied.  Preserve the four-byte sampling-step header, raw counter
representation, and normal successful-read behavior.

Avi Weiss's "profiling: fix sampling step on 64-bit big-endian
systems" is unchanged.  This patch changes only short-read and fault
reporting, not the output format.

Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
---
Tested the combined three-patch series on x86-64 and i686 QEMU/KVM
guests with CONFIG_PROFILING=y and profile=12.
profile-read-test.c: PASS (0 failures) on both architectures.
Covered short and unaligned reads, header boundaries, EOF, partial
user-copy faults, return values, file positions and retry behavior.
The 64-bit big-endian sampling-step fix was not runtime tested.
No separate per-patch or unpatched-versus-patched run was reported.

 kernel/profile.c |   17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)

--- a/kernel/profile.c~profile-account-for-partial-proc-profile-reads
+++ a/kernel/profile.c
@@ -157,6 +157,7 @@ read_profile(struct file *file, char __u
 	ssize_t read;
 	char *pnt;
 	unsigned int sample_step = (unsigned int)(1UL << prof_shift);
+	size_t not_copied;
 
 	if (p >= (prof_len+1)*sizeof(unsigned int))
 		return 0;
@@ -166,15 +167,21 @@ read_profile(struct file *file, char __u
 
 	while (p < sizeof(unsigned int) && count > 0) {
 		if (put_user(*((char *)(&sample_step)+p), buf))
-			return -EFAULT;
+			goto out;
 		buf++; p++; count--; read++;
 	}
-	pnt = (char *)prof_buffer + p - sizeof(atomic_t);
-	if (copy_to_user(buf, (void *)pnt, count))
-		return -EFAULT;
-	read += count;
+	if (count) {
+		pnt = (char *)prof_buffer + p - sizeof(atomic_t);
+		not_copied = copy_to_user(buf, pnt, count);
+		read += count - not_copied;
+		if (not_copied)
+			goto out;
+	}
 	*ppos += read;
 	return read;
+out:
+	*ppos += read;
+	return read ? read : -EFAULT;
 }
 
 /* default is to not implement this call */
_

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

* Re: [PATCH 1/1] profile: account for partial /proc/profile reads
  2026-11-14  6:30 [PATCH 1/1] profile: account for partial /proc/profile reads Andrew Morton
@ 2026-11-14 12:42 ` Bradley Morgan
  0 siblings, 0 replies; 2+ messages in thread
From: Bradley Morgan @ 2026-11-14 12:42 UTC (permalink / raw)
  To: Andrew Morton; +Cc: linux-kernel

On 14 November 2026 06:30:37 GMT, Andrew Morton <akpm@linux-foundation.org>
wrote:
>From: Andrew Morton <akpm@linux-foundation.org>
>Subject: profile: account for partial /proc/profile reads
>Date: Tue, 20 Oct 2026 15:28:00 -0700
>
>read_profile() returns -EFAULT without advancing the file position
>when a user copy faults, even if earlier header bytes or a prefix of
>the counters were successfully copied.  A retry can therefore copy
>the same bytes again despite the previous read having delivered data.
>
>Return the number of successfully copied bytes and advance *ppos by
>that amount.  Return -EFAULT only if the fault occurs before any bytes
>are copied.  Preserve the four-byte sampling-step header, raw counter
>representation, and normal successful-read behavior.
>
>Avi Weiss's "profiling: fix sampling step on 64-bit big-endian
>systems" is unchanged.  This patch changes only short-read and fault
>reporting, not the output format.

Oh why not a series, please..

>
>Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
>---
>Tested the combined three-patch series on x86-64 and i686 QEMU/KVM
>guests with CONFIG_PROFILING=y and profile=12.
>profile-read-test.c: PASS (0 failures) on both architectures.
>Covered short and unaligned reads, header boundaries, EOF, partial
>user-copy faults, return values, file positions and retry behavior.
>The 64-bit big-endian sampling-step fix was not runtime tested.
>No separate per-patch or unpatched-versus-patched run was reported.
>
> kernel/profile.c |   17 ++++++++++++-----
> 1 file changed, 12 insertions(+), 5 deletions(-)
>
>--- a/kernel/profile.c~profile-account-for-partial-proc-profile-reads
>+++ a/kernel/profile.c
>@@ -157,6 +157,7 @@ read_profile(struct file *file, char __u
> 	ssize_t read;
> 	char *pnt;
> 	unsigned int sample_step = (unsigned int)(1UL << prof_shift);
>+	size_t not_copied;
> 
> 	if (p >= (prof_len+1)*sizeof(unsigned int))
> 		return 0;
>@@ -166,15 +167,21 @@ read_profile(struct file *file, char __u
> 
> 	while (p < sizeof(unsigned int) && count > 0) {
> 		if (put_user(*((char *)(&sample_step)+p), buf))
>-			return -EFAULT;
>+			goto out;
> 		buf++; p++; count--; read++;
> 	}
>-	pnt = (char *)prof_buffer + p - sizeof(atomic_t);
>-	if (copy_to_user(buf, (void *)pnt, count))
>-		return -EFAULT;
>-	read += count;
>+	if (count) {
>+		pnt = (char *)prof_buffer + p - sizeof(atomic_t);
>+		not_copied = copy_to_user(buf, pnt, count);
>+		read += count - not_copied;
>+		if (not_copied)
>+			goto out;
>+	}
> 	*ppos += read;
> 	return read;
>+out:
>+	*ppos += read;
>+	return read ? read : -EFAULT;
> }
> 
> /* default is to not implement this call */
>_

--- Thanks!

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

end of thread, other threads:[~2026-11-14 12:42 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-14  6:30 [PATCH 1/1] profile: account for partial /proc/profile reads Andrew Morton
2026-11-14 12:42 ` Bradley Morgan

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.