[RFC PATCH 1/3] vmcore: simplify read_from_olemem

From: Kairui Song
Date: Wed Sep 09 2020 - 03:50:56 EST


Simplify the code logic, also helps reduce object size and stack usage.

Stack usage:
Before: fs/proc/vmcore.c:106:9:read_from_oldmem.part.0 80 static
fs/proc/vmcore.c:106:9:read_from_oldmem 16 static
After: fs/proc/vmcore.c:106:9:read_from_oldmem 80 static

Size of vmcore.o:
text data bss dec hex filename
Before: 7677 109 88 7874 1ec2 fs/proc/vmcore.o
After: 7669 109 88 7866 1eba fs/proc/vmcore.o

Signed-off-by: Kairui Song <kasong@xxxxxxxxxx>
---
fs/proc/vmcore.c | 27 ++++++++++-----------------
1 file changed, 10 insertions(+), 17 deletions(-)

diff --git a/fs/proc/vmcore.c b/fs/proc/vmcore.c
index c3a345c28a93..124c2066f3e5 100644
--- a/fs/proc/vmcore.c
+++ b/fs/proc/vmcore.c
@@ -108,25 +108,19 @@ ssize_t read_from_oldmem(char *buf, size_t count,
bool encrypted)
{
unsigned long pfn, offset;
- size_t nr_bytes;
- ssize_t read = 0, tmp;
+ size_t nr_bytes, to_copy = count;
+ ssize_t tmp;

- if (!count)
- return 0;
-
- offset = (unsigned long)(*ppos % PAGE_SIZE);
+ offset = (unsigned long)(*ppos & (PAGE_SIZE - 1));
pfn = (unsigned long)(*ppos / PAGE_SIZE);

- do {
- if (count > (PAGE_SIZE - offset))
- nr_bytes = PAGE_SIZE - offset;
- else
- nr_bytes = count;
+ while (to_copy) {
+ nr_bytes = min(to_copy, PAGE_SIZE - offset);

/* If pfn is not ram, return zeros for sparse dump files */
- if (pfn_is_ram(pfn) == 0)
+ if (pfn_is_ram(pfn) == 0) {
memset(buf, 0, nr_bytes);
- else {
+ } else {
if (encrypted)
tmp = copy_oldmem_page_encrypted(pfn, buf,
nr_bytes,
@@ -140,14 +134,13 @@ ssize_t read_from_oldmem(char *buf, size_t count,
return tmp;
}
*ppos += nr_bytes;
- count -= nr_bytes;
buf += nr_bytes;
- read += nr_bytes;
+ to_copy -= nr_bytes;
++pfn;
offset = 0;
- } while (count);
+ }

- return read;
+ return count;
}

/*
--
2.26.2