[PATCH 4/8] core: use rest_of_page() macro where appropriate

Yury Norov
2026-03-04T01:27:47Z

Switch core and library code to using the macro. No functional
changes intended.

Signed-off-by: Yury Norov <ynorov@nvidia.com>
---
include/crypto/scatterwalk.h | 2 +-
include/linux/highmem.h | 24 ++++++++++--------------
include/linux/iomap.h | 2 +-
include/linux/iov_iter.h | 3 +--
kernel/events/ring_buffer.c | 2 +-
lib/bitmap-str.c | 2 +-
lib/iov_iter.c | 5 ++---
7 files changed, 17 insertions(+), 23 deletions(-)

diff --git a/include/crypto/scatterwalk.h b/include/crypto/scatterwalk.h
index 624fab589c2c..c671d5383c12 100644
--- a/include/crypto/scatterwalk.h
+++ b/include/crypto/scatterwalk.h
@@ -73,7 +73,7 @@ static inline unsigned int scatterwalk_clamp(struct scatter_walk *walk,
* page due to the data not being aligned to the algorithm's alignmask.
*/
if (IS_ENABLED(CONFIG_HIGHMEM))
- limit = PAGE_SIZE - offset_in_page(walk->offset);
+ limit = rest_of_page(walk->offset);
else
limit = PAGE_SIZE;

diff --git a/include/linux/highmem.h b/include/linux/highmem.h
index af03db851a1d..05528ba886fb 100644
--- a/include/linux/highmem.h
+++ b/include/linux/highmem.h
@@ -507,12 +507,10 @@ static inline void memcpy_folio(struct folio *dst_folio, size_t dst_off,
const char *src = kmap_local_folio(src_folio, src_off);
size_t chunk = len;

- if (folio_test_highmem(dst_folio) &&
- chunk > PAGE_SIZE - offset_in_page(dst_off))
- chunk = PAGE_SIZE - offset_in_page(dst_off);
- if (folio_test_highmem(src_folio) &&
- chunk > PAGE_SIZE - offset_in_page(src_off))
- chunk = PAGE_SIZE - offset_in_page(src_off);
+ if (folio_test_highmem(dst_folio) && chunk > rest_of_page(dst_off))
+ chunk = rest_of_page(dst_off);
+ if (folio_test_highmem(src_folio) && chunk > rest_of_page(src_off))
+ chunk = rest_of_page(src_off);
memcpy(dst, src, chunk);
kunmap_local(src);
kunmap_local(dst);
@@ -580,9 +578,8 @@ static inline void memcpy_from_folio(char *to, struct folio *folio,
const char *from = kmap_local_folio(folio, offset);
size_t chunk = len;

- if (folio_test_partial_kmap(folio) &&
- chunk > PAGE_SIZE - offset_in_page(offset))
- chunk = PAGE_SIZE - offset_in_page(offset);
+ if (folio_test_partial_kmap(folio) && chunk > rest_of_page(offset))
+ chunk = rest_of_page(offset);
memcpy(to, from, chunk);
kunmap_local(from);

@@ -608,9 +605,8 @@ static inline void memcpy_to_folio(struct folio *folio, size_t offset,
char *to = kmap_local_folio(folio, offset);
size_t chunk = len;

- if (folio_test_partial_kmap(folio) &&
- chunk > PAGE_SIZE - offset_in_page(offset))
- chunk = PAGE_SIZE - offset_in_page(offset);
+ if (folio_test_partial_kmap(folio) && chunk > rest_of_page(offset))
+ chunk = rest_of_page(offset);
memcpy(to, from, chunk);
kunmap_local(to);

@@ -642,7 +638,7 @@ static inline __must_check void *folio_zero_tail(struct folio *folio,
size_t len = folio_size(folio) - offset;

if (folio_test_partial_kmap(folio)) {
- size_t max = PAGE_SIZE - offset_in_page(offset);
+ size_t max = rest_of_page(offset);

while (len > max) {
memset(kaddr, 0, max);
@@ -680,7 +676,7 @@ static inline void folio_fill_tail(struct folio *folio, size_t offset,
VM_BUG_ON(offset + len > folio_size(folio));

if (folio_test_partial_kmap(folio)) {
- size_t max = PAGE_SIZE - offset_in_page(offset);
+ size_t max = rest_of_page(offset);

while (len > max) {
memcpy(to, from, max);
diff --git a/include/linux/iomap.h b/include/linux/iomap.h
index 99b7209dabd7..6ae549192adb 100644
--- a/include/linux/iomap.h
+++ b/include/linux/iomap.h
@@ -142,7 +142,7 @@ static inline void *iomap_inline_data(const struct iomap *iomap, loff_t pos)
*/
static inline bool iomap_inline_data_valid(const struct iomap *iomap)
{
- return iomap->length <= PAGE_SIZE - offset_in_page(iomap->inline_data);
+ return iomap->length <= rest_of_page(iomap->inline_data);
}

/*
diff --git a/include/linux/iov_iter.h b/include/linux/iov_iter.h
index f9a17fbbd398..13a9ee653ef8 100644
--- a/include/linux/iov_iter.h
+++ b/include/linux/iov_iter.h
@@ -227,8 +227,7 @@ size_t iterate_xarray(struct iov_iter *iter, size_t len, void *priv, void *priv2
while (flen) {
void *base = kmap_local_folio(folio, offset);

- part = min_t(size_t, flen,
- PAGE_SIZE - offset_in_page(offset));
+ part = min_t(size_t, flen, rest_of_page(offset));
remain = step(base, progress, part, priv, priv2);
kunmap_local(base);

diff --git a/kernel/events/ring_buffer.c b/kernel/events/ring_buffer.c
index 3e7de2661417..1db2868b90c9 100644
--- a/kernel/events/ring_buffer.c
+++ b/kernel/events/ring_buffer.c
@@ -590,7 +590,7 @@ long perf_output_copy_aux(struct perf_output_handle *aux_handle,
to &= (rb->aux_nr_pages << PAGE_SHIFT) - 1;

do {
- tocopy = PAGE_SIZE - offset_in_page(from);
+ tocopy = rest_of_page(from);
if (to > from)
tocopy = min(tocopy, to - from);
if (!tocopy)
diff --git a/lib/bitmap-str.c b/lib/bitmap-str.c
index be745209507a..a357342d5d6c 100644
--- a/lib/bitmap-str.c
+++ b/lib/bitmap-str.c
@@ -58,7 +58,7 @@ EXPORT_SYMBOL(bitmap_parse_user);
int bitmap_print_to_pagebuf(bool list, char *buf, const unsigned long *maskp,
int nmaskbits)
{
- ptrdiff_t len = PAGE_SIZE - offset_in_page(buf);
+ ptrdiff_t len = rest_of_page(buf);

return list ? scnprintf(buf, len, "%*pbl\n", nmaskbits, maskp) :
scnprintf(buf, len, "%*pb\n", nmaskbits, maskp);
diff --git a/lib/iov_iter.c b/lib/iov_iter.c
index 0a63c7fba313..c7e812349ca2 100644
--- a/lib/iov_iter.c
+++ b/lib/iov_iter.c
@@ -483,9 +483,8 @@ size_t copy_folio_from_iter_atomic(struct folio *folio, size_t offset,
char *to = kmap_local_folio(folio, offset);

n = bytes - copied;
- if (folio_test_partial_kmap(folio) &&
- n > PAGE_SIZE - offset_in_page(offset))
- n = PAGE_SIZE - offset_in_page(offset);
+ if (folio_test_partial_kmap(folio) && n > rest_of_page(offset))
+ n = rest_of_page(offset);

pagefault_disable();
n = __copy_from_iter(to, n, i);
--
2.43.0