mm/vdso: make offset_in_page() usable without linux/mm.h
Subsystems (5)
v2 cover mm/vdso: make offset_in_page() usable without linux/mm.h
offset_in_page() is a small page-arithmetic helper that has been around for 20+ years. However, page-offset calculations are still open-coded in many places and in different ways:
(unsigned long)p & ~PAGE_MASK (unsigned long)p & (PAGE_SIZE - 1) (long)p & (PAGE_SIZE - 1) ...
Some of these open-coded instances may be due to offset_in_page() being buried 3000+ lines into linux/mm.h; others may have avoided including linux/mm.h, which is a large header that pulls in many others.
Patch 1 moves offset_in_page() from linux/mm.h to vdso/page.h, which keeps the helper with other low-level page definitions and allows users that only need offset_in_page() to avoid including linux/mm.h.
Patch 2 shows a concrete example where including vdso/page.h is sufficient and including the large linux/mm.h would be unnecessary.
Existing users of offset_in_page() do not need to change because patch 1 also includes vdso/page.h from linux/mm.h.
This series is based on akpm/mm.git mm-nonmm-unstable commit d067a83c8063 ("string: use min in sized_strscpy"), which is also in linux-next.
Changes in v2:
- Add a cover letter and drop the bytes_to_page_end() helper
- Move offset_in_page() to vdso/page.h as suggested by David and Lorenzo
- Use offset_in_page() in lib/string.c as an example since bitmap_print_to_pagebuf() in lib/bitmap-str.c is being removed [1]
- v1: https://lore.kernel.org/lkml/[email protected]/
[1] https://lore.kernel.org/lkml/[email protected]/
---
Thorsten Blum (2):
vdso: move offset_in_page() from linux/mm.h to vdso/page.h
string: use offset_in_page() in sized_strscpy()
include/linux/mm.h | 2 +- include/vdso/page.h | 2 ++ lib/string.c | 3 ++- 3 files changed, 5 insertions(+), 2 deletions(-)
base-commit: d067a83c8063d1bdcbd9af8e1326d846f85138b8
PATCH v2 1/2 vdso: move offset_in_page() from linux/mm.h to vdso/page.h
Move offset_in_page() out of linux/mm.h so users that only need page offset calculations can include the lightweight vdso/page.h header instead of pulling in the large linux/mm.h.
Include vdso/page.h from linux/mm.h so existing users of offset_in_page() continue to build.
--- include/linux/mm.h | 2 +- include/vdso/page.h | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-)
include/linux/mm.h +1 -1
@@ -37,6 +37,7 @@#include <linux/bitmap.h> #include <linux/bitops.h> #include <linux/iommu-debug-pagealloc.h> +#include <vdso/page.h> struct mempolicy ;struct anon_vma ;
@@ -3023,7 +3024,6 @@ static inline void clear_page_pfmemalloc(struct page *page)*/ extern void pagefault_out_of_memory (void ); -#define offset_in_page (p) ((unsigned long)(p) & ~PAGE_MASK)#define offset_in_folio (folio, p) ((unsigned long)(p) & (folio_size(folio) - 1))/*
include/vdso/page.h +2 -0
@@ -28,4 +28,6 @@#define PAGE_MASK (~(PAGE_SIZE - 1))#endif +#define offset_in_page (p) ((unsigned long)(p) & ~PAGE_MASK) +#endif /* __VDSO_PAGE_H */
Re: [PATCH v2 1/2] vdso: move offset_in_page() from linux/mm.h to vdso/page.h
Quoted 18 lines · On Thu, 21 May 2026 11:06:57 +0200 Thorsten Blum <[email protected]>…
Move
offset_in_page()out oflinux/mm.hso users that only need page offset calculations can include the lightweightvdso/page.hheader instead of pulling in the largelinux/mm.h.Include
vdso/page.hfromlinux/mm.hso existing users ofoffset_in_page()continue to build.Signed-off-by Thorsten Blum <[email protected]>--- include/linux/mm.h | 2 +- include/vdso/page.h | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-)include/linux/mm.h +0
-0
include/linux/mm.h line 40
Quoted 4 lines
include/linux/mm.h +1
-0@@ -37,6 +37,7 @@#include <linux/bitmap.h> #include <linux/bitops.h> #include <linux/iommu-debug-pagealloc.h> +#include <vdso/page.h>
That is already picked up otherwise PAGE_MASK would be undefined.
The whole vdso headers is a mess.
I moved one of the definitions (to do with bitmasks) back into its main header. The vdso compiled fine because all the 'normal' kernel headers get included in vdso builds.
-- David
Quoted 22 lines
include/linux/mm.h +0
-1struct mempolicy ;struct anon_vma ;@@ -3023,7 +3024,6 @@ static inline void clear_page_pfmemalloc(struct page *page)*/ extern void pagefault_out_of_memory (void ); -#define offset_in_page (p) ((unsigned long)(p) & ~PAGE_MASK)#define offset_in_folio (folio, p) ((unsigned long)(p) & (folio_size(folio) - 1))/* include/vdso/page.h +2
-0@@ -28,4 +28,6 @@#define PAGE_MASK (~(PAGE_SIZE - 1))#endif +#define offset_in_page (p) ((unsigned long)(p) & ~PAGE_MASK) +#endif /* __VDSO_PAGE_H */
Thomas Gleixner
maintainerRe: [PATCH v2 1/2] vdso: move offset_in_page() from linux/mm.h to vdso/page.h
Quoted 10 lines · David Laight
Quoted 1 line · On Thu, 21 May 2026 11:06:57 +0200 Thorsten Blum <[email protected]>…
+#include <
vdso/page.h>That is already picked up otherwise
PAGE_MASKwould be undefined.The whole vdso headers is a mess.
I moved one of the definitions (to do with bitmasks) back into its main header. The vdso compiled fine because all the 'normal' kernel headers get included in vdso builds.
Which is just wrong and we had subtle issues due to that in the past.
VDSO is a pure user space library and just because it compiles does not mean it is correct.
Thanks,
tglx
Re: [PATCH v2 1/2] vdso: move offset_in_page() from linux/mm.h to vdso/page.h
In reply to Thorsten BlumQuoted 8 lines · Thorsten Blum
Move
offset_in_page()out oflinux/mm.hso users that only need page offset calculations can include the lightweightvdso/page.hheader instead of pulling in the largelinux/mm.h.Include
vdso/page.hfromlinux/mm.hso existing users ofoffset_in_page()continue to build.Signed-off-by Thorsten Blum <[email protected]>
LGTM other than dropping the include below (maybe Andrew can do it for us?), so:
Quoted 10 lines
--- include/linux/mm.h | 2 +- include/vdso/page.h | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-)include/linux/mm.h +0
-0
include/linux/mm.h line 40
Quoted 4 lines
include/linux/mm.h +1
-0@@ -37,6 +37,7 @@#include <linux/bitmap.h> #include <linux/bitops.h> #include <linux/iommu-debug-pagealloc.h> +#include <vdso/page.h>
Yeah as per David Laight, we don't need this.
Quoted 22 lines
include/linux/mm.h +0
-1struct mempolicy ;struct anon_vma ;@@ -3023,7 +3024,6 @@ static inline void clear_page_pfmemalloc(struct page *page)*/ extern void pagefault_out_of_memory (void ); -#define offset_in_page (p) ((unsigned long)(p) & ~PAGE_MASK)#define offset_in_folio (folio, p) ((unsigned long)(p) & (folio_size(folio) - 1))/* include/vdso/page.h +2
-0@@ -28,4 +28,6 @@#define PAGE_MASK (~(PAGE_SIZE - 1))#endif +#define offset_in_page (p) ((unsigned long)(p) & ~PAGE_MASK) +#endif /* __VDSO_PAGE_H */
Lorenzo Stoakes
reviewerRe: [PATCH v2 1/2] vdso: move offset_in_page() from linux/mm.h to vdso/page.h
Quoted 8 lines · Lorenzo Stoakes
Quoted 7 lines · Thorsten Blum
Move
offset_in_page()out oflinux/mm.hso users that only need page offset calculations can include the lightweightvdso/page.hheader instead of pulling in the largelinux/mm.h.Include
vdso/page.hfromlinux/mm.hso existing users ofoffset_in_page()continue to build.
Quoted 6 lines
Quoted 1 line
Signed-off-by Thorsten Blum <[email protected]>LGTM other than dropping the include below (maybe Andrew can do it for us?), so:
Reviewed-by Lorenzo Stoakes <[email protected]>
TBH I'd like to withdraw this at this point.
Unless a series can be put forwards that sensibly justifies this, not some random change somewhere, I'd rather we not take it.
Also vdso/page.h is a VDSO-specific header by MAINTAINERS, offset_in_page() is really an mm thing so that's another reason not to move it.
(A justifying change would show actually build time/binary bloat/etc. numbers + involve actual substantive changes).
Thanks, Lorenzo
Quoted 40 lines
Quoted 11 lines
--- include/linux/mm.h | 2 +- include/vdso/page.h | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-)include/linux/mm.h +0
-0include/linux/mm.h line 40
Quoted 4 lines
include/linux/mm.h +1
-0@@ -37,6 +37,7 @@#include <linux/bitmap.h> #include <linux/bitops.h> #include <linux/iommu-debug-pagealloc.h> +#include <vdso/page.h> Yeah as per David Laight, we don't need this.
Quoted 22 lines
include/linux/mm.h +0
-1struct mempolicy ;struct anon_vma ;@@ -3023,7 +3024,6 @@ static inline void clear_page_pfmemalloc(struct page *page)*/ extern void pagefault_out_of_memory (void ); -#define offset_in_page (p) ((unsigned long)(p) & ~PAGE_MASK)#define offset_in_folio (folio, p) ((unsigned long)(p) & (folio_size(folio) - 1))/* include/vdso/page.h +2
-0@@ -28,4 +28,6 @@#define PAGE_MASK (~(PAGE_SIZE - 1))#endif +#define offset_in_page (p) ((unsigned long)(p) & ~PAGE_MASK) +#endif /* __VDSO_PAGE_H */
Re: [PATCH v2 1/2] vdso: move offset_in_page() from linux/mm.h to vdso/page.h
Quoted 2 lines · Lorenzo Stoakes
Quoted 1 line · Lorenzo Stoakes
On Thu, May 21, 2026 at 11:06:57AM +0200, Thorsten Blum wrote:
...
Quoted 2 lines
Unless a series can be put forwards that sensibly justifies this, not some random change somewhere, I'd rather we not take it.
The problem is not only a compile time but more generic - the mess in the Linux headers. People who want to use this match need to include mm.h which is a bloated header with *tons* of unneeded dependencies (for those pure users) and increases chaos in how the headers are split.
If we don't care about it, then provide an "INCLUDE EVERYTHING" header and let the driver just include that. But of course, we don't do that, we do modular headers for a reason. Currently I see no reason to have that simple helper to be mm.h as its use is much wider in the cases where the whole hell of mm.h is not needed.
So, the best course of actions I see now is leave mm people alone and simply duplicate what we need in the headers we want to see (util_macros.h as a rough approximation, but maybe something better).
Quoted 5 lines
Also
vdso/page.his a VDSO-specific header byMAINTAINERS,offset_in_page()is really an mm thing so that's another reason not to move it.(A justifying change would show actually build time/binary bloat/etc. numbers + involve actual substantive changes).
Signature · 2 lines
With Best Regards, Andy Shevchenko
Lorenzo Stoakes
reviewerRe: [PATCH v2 1/2] vdso: move offset_in_page() from linux/mm.h to vdso/page.h
Quoted 6 lines · Andy Shevchenko
Quoted 2 lines · Lorenzo Stoakes
Quoted 1 line · Lorenzo Stoakes
On Thu, May 21, 2026 at 11:06:57AM +0200, Thorsten Blum wrote:
...
Quoted 8 lines
Quoted 2 lines
Unless a series can be put forwards that sensibly justifies this, not some random change somewhere, I'd rather we not take it.
The problem is not only a compile time but more generic - the mess in the Linux headers. People who want to use this match need to include
mm.hwhich is a bloated header with *tons* of unneeded dependencies (for those pure users) and increases chaos in how the headers are split.
No question the headers are a bit of a mess, and I've had absolute headaches with dependency chain stuff there.
Quoted 1 line
If we don't care about it, then provide an "INCLUDE EVERYTHING" header
That's a false dichotomy - the series isn't upstreamable as-is. I've provided feedback that's not been addressed so we won't take it.
Quoted 4 lines
and let the driver just include that. But of course, we don't do that, we do modular headers for a reason. Currently I see no reason to have that simple helper to be
mm.has its use is much wider in the cases where the whole hell ofmm.his not needed.
We also don't want to have wildly divergent overly-specific individual headers, and series have to be upstreamable.
Quoted 4 lines
So, the best course of actions I see now is leave mm people alone and simply duplicate what we need in the headers we want to see (
util_macros.has a rough approximation, but maybe something better).
I mean I feel engaging civilly and constructively with people who have taken their time out to provide feedback is a better approach, but obviously what you do elsewhere in the kernel is up to you.
Quoted 5 lines
Quoted 5 lines
Also
vdso/page.his a VDSO-specific header byMAINTAINERS,offset_in_page()is really an mm thing so that's another reason not to move it.
Quoted 8 lines
Quoted 2 lines
(A justifying change would show actually build time/binary bloat/etc. numbers + involve actual substantive changes).
--
With Best Regards,
Andy Shevchenko
Overall this all feels like a storm in a teapot, this is a very trivial macro and the use case seems nebulous at best.
A series actually addressing header issues with _a justifying reason provided_ (I am astonished that it still hasn't!), would be more useful, as long as it was upstreamable and didn't cause other issues.
We in mm are friendly and accepting of good series, engaging positively will get a positive reception :)
Thanks, Lorenzo
Re: [PATCH v2 1/2] vdso: move offset_in_page() from linux/mm.h to vdso/page.h
Quoted 4 lines · Lorenzo Stoakes
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Lorenzo Stoakes
Quoted 1 line · Lorenzo Stoakes
On Thu, May 21, 2026 at 11:06:57AM +0200, Thorsten Blum wrote:
...
Quoted 17 lines
Quoted 8 lines
Quoted 2 lines
Unless a series can be put forwards that sensibly justifies this, not some random change somewhere, I'd rather we not take it.
The problem is not only a compile time but more generic - the mess in the Linux headers. People who want to use this match need to include
mm.hwhich is a bloated header with *tons* of unneeded dependencies (for those pure users) and increases chaos in how the headers are split.No question the headers are a bit of a mess, and I've had absolute headaches with dependency chain stuff there.
Quoted 1 line
If we don't care about it, then provide an "INCLUDE EVERYTHING" header
That's a false dichotomy - the series isn't upstreamable as-is. I've provided feedback that's not been addressed so we won't take it.
Quoted 7 lines
Quoted 4 lines
and let the driver just include that. But of course, we don't do that, we do modular headers for a reason. Currently I see no reason to have that simple helper to be
mm.has its use is much wider in the cases where the whole hell ofmm.his not needed.We also don't want to have wildly divergent overly-specific individual headers, and series have to be upstreamable.
No objections on this.
Quoted 7 lines
Quoted 3 lines
So, the best course of actions I see now is leave mm people alone and simply duplicate what we need in the headers we want to see (
util_macros.has a rough approximation, but maybe something better).I mean I feel engaging civilly and constructively with people who have taken their time out to provide feedback is a better approach, but obviously what you do elsewhere in the kernel is up to you.
Yes, but it's may be due to my blindness to see how the whole mm.h machinery is useful for just simple macros that use PAGE_* ones beneath. I'm for whatever (much) smaller and that has much lesser dependencies, so people can use it.
Quoted 8 lines
Quoted 5 lines
Quoted 5 lines
Also
vdso/page.his a VDSO-specific header byMAINTAINERS,offset_in_page()is really an mm thing so that's another reason not to move it.(A justifying change would show actually build time/binary bloat/etc. numbers + involve actual substantive changes).
Overall this all feels like a storm in a teapot, this is a very trivial macro and the use case seems nebulous at best.
It might be more users found, if we start looking into that with tools like coccinelle. But I don't know.
Quoted 3 lines
A series actually addressing header issues with _a justifying reason
provided_(I am astonished that it still hasn't!), would be more useful, as long as it was upstreamable and didn't cause other issues.
Sure, justifying reason (one of?) is a very baby step to clean up the header dependency hell we have. I know that some people want the problem to be solved once for all, but it's impossible to achieve (remember Ingo's 2000+ patch series).
Quoted 2 lines
We in mm are friendly and accepting of good series, engaging positively will get a positive reception :)
I believe this is a standard for maintainers in the kernel independently on the subsystem. But thanks for confirming!
Signature · 2 lines
With Best Regards, Andy Shevchenko
Lorenzo Stoakes (ARM)
reviewerRe: [PATCH v2 1/2] vdso: move offset_in_page() from linux/mm.h to vdso/page.h
Quoted 30 lines · Andy Shevchenko
Quoted 4 lines · Lorenzo Stoakes
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Lorenzo Stoakes
Quoted 1 line · Lorenzo Stoakes
On Thu, May 21, 2026 at 11:06:57AM +0200, Thorsten Blum wrote:
...
Quoted 22 lines
Quoted 8 lines
Quoted 2 lines
Unless a series can be put forwards that sensibly justifies this, not some random change somewhere, I'd rather we not take it.
The problem is not only a compile time but more generic - the mess in the Linux headers. People who want to use this match need to include
mm.hwhich is a bloated header with *tons* of unneeded dependencies (for those pure users) and increases chaos in how the headers are split.No question the headers are a bit of a mess, and I've had absolute headaches with dependency chain stuff there.
Quoted 1 line
If we don't care about it, then provide an "INCLUDE EVERYTHING" header
That's a false dichotomy - the series isn't upstreamable as-is. I've provided feedback that's not been addressed so we won't take it.
Quoted 4 lines
and let the driver just include that. But of course, we don't do that, we do modular headers for a reason. Currently I see no reason to have that simple helper to be
mm.has its use is much wider in the cases where the whole hell ofmm.his not needed.
Quoted 4 lines
Quoted 2 lines
We also don't want to have wildly divergent overly-specific individual headers, and series have to be upstreamable.
No objections on this.
Yeah I think in this case this was the main issue.
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
So, the best course of actions I see now is leave mm people alone and simply duplicate what we need in the headers we want to see (
util_macros.has a rough approximation, but maybe something better).
Quoted 7 lines
Quoted 3 lines
I mean I feel engaging civilly and constructively with people who have taken their time out to provide feedback is a better approach, but obviously what you do elsewhere in the kernel is up to you.
Yes, but it's may be due to my blindness to see how the whole
mm.hmachinery is useful for just simple macros that use PAGE_* ones beneath. I'm for whatever (much) smaller and that has much lesser dependencies, so people can use it.
Yup agreed, but it's all very catch-22
Quoted 13 lines
Quoted 9 lines
Quoted 6 lines
Quoted 6 lines
Also
vdso/page.his a VDSO-specific header byMAINTAINERS,offset_in_page()is really an mm thing so that's another reason not to move it.(A justifying change would show actually build time/binary bloat/etc. numbers + involve actual substantive changes).
Overall this all feels like a storm in a teapot, this is a very trivial macro and the use case seems nebulous at best.
It might be more users found, if we start looking into that with tools like coccinelle. But I don't know.
Quoted 7 lines
Quoted 3 lines
A series actually addressing header issues with _a justifying reason
provided_(I am astonished that it still hasn't!), would be more useful, as long as it was upstreamable and didn't cause other issues.Sure, justifying reason (one of?) is a very baby step to clean up the header dependency hell we have. I know that some people want the problem to be solved once for all, but it's impossible to achieve (remember Ingo's 2000+ patch series).
I know Matthew is looking at doing some work in this area actually!
But yeah it's horrendous and I'm not sure we're going to make all that much headway, esp. as it's all very bikesheddable (perhaps I was demonstrating that here ;)
I hate headers in general as a concept they invite the kind of horrific insanity we have in the kernel.
Have made some small contribution myself too:
https://lore.kernel.org/all/[email protected]/
But that's just symptomatic of the mess we have in general :)
Quoted 6 lines
Quoted 3 lines
We in mm are friendly and accepting of good series, engaging positively will get a positive reception :)
I believe this is a standard for maintainers in the kernel independently on the subsystem. But thanks for confirming!
:>)
Quoted 6 lines
--
With Best Regards,
Andy Shevchenko
--
Cheers, Lorenzo
Re: [PATCH v2 1/2] vdso: move offset_in_page() from linux/mm.h to vdso/page.h
In reply to Lorenzo StoakesQuoted 9 lines · Lorenzo Stoakes
Quoted 8 lines · Lorenzo Stoakes
Quoted 7 lines · Thorsten Blum
Move
offset_in_page()out oflinux/mm.hso users that only need page offset calculations can include the lightweightvdso/page.hheader instead of pulling in the largelinux/mm.h.Include
vdso/page.hfromlinux/mm.hso existing users ofoffset_in_page()continue to build.
Quoted 8 lines
Quoted 6 lines
Quoted 1 line
Signed-off-by Thorsten Blum <[email protected]>LGTM other than dropping the include below (maybe Andrew can do it for us?), so:
Reviewed-by Lorenzo Stoakes <[email protected]>TBH I'd like to withdraw this at this point.
OK
Quoted 2 lines
Unless a series can be put forwards that sensibly justifies this, not some random change somewhere, I'd rather we not take it.
Using lib/bitmap-str.c as an example was apparently fine, but since that is being removed, converting lib/string.c instead to use the same idiomatic PAGE_SIZE - offset_in_page() calculation and include a lightweight header is a "random change somewhere"?
Quoted 2 lines
Also
vdso/page.his a VDSO-specific header byMAINTAINERS,offset_in_page()is really an mm thing so that's another reason not to move it.
Adding a new header was rejected (ok), but moving offset_in_page() to vdso/page.h was your and David's suggestion, not mine.
Let's just drop this series as I don't think this is going anywhere.
Thanks,
Thorsten
Re: [PATCH v2 1/2] vdso: move offset_in_page() from linux/mm.h to vdso/page.h
In reply to Thorsten BlumQuoted 6 lines · Thorsten Blum
Move
offset_in_page()out oflinux/mm.hso users that only need page offset calculations can include the lightweightvdso/page.hheader instead of pulling in the largelinux/mm.h.Include
vdso/page.hfromlinux/mm.hso existing users ofoffset_in_page()continue to build.
lib/string.c doesn't look like a vdso, and AFAIK there's no vdso candidates for using the macro. So that please don't bloat the header.
Quoted 37 lines
Signed-off-by Thorsten Blum <[email protected]>--- include/linux/mm.h | 2 +- include/vdso/page.h | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-)include/linux/mm.h +1
-1@@ -37,6 +37,7 @@#include <linux/bitmap.h> #include <linux/bitops.h> #include <linux/iommu-debug-pagealloc.h> +#include <vdso/page.h> struct mempolicy ;struct anon_vma ;@@ -3023,7 +3024,6 @@ static inline void clear_page_pfmemalloc(struct page *page)*/ extern void pagefault_out_of_memory (void ); -#define offset_in_page (p) ((unsigned long)(p) & ~PAGE_MASK)#define offset_in_folio (folio, p) ((unsigned long)(p) & (folio_size(folio) - 1))/* include/vdso/page.h +2
-0@@ -28,4 +28,6 @@#define PAGE_MASK (~(PAGE_SIZE - 1))#endif +#define offset_in_page (p) ((unsigned long)(p) & ~PAGE_MASK) +#endif /* __VDSO_PAGE_H */
PATCH v2 2/2 string: use offset_in_page() in sized_strscpy()
In reply to Thorsten BlumReplace the open-coded implementation with offset_in_page() to simplify sized_strscpy().
--- lib/string.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
lib/string.c +2 -1
@@ -25,6 +25,7 @@#include <linux/stddef.h> #include <linux/string.h> #include <linux/types.h> +#include <vdso/page.h> #include <asm/page.h> #include <asm/rwonce.h>
@@ -127,7 +128,7 @@ ssize_t sized_strscpy(char *dest, const char *src, size_t count)* since we don't know if the next page is mapped. */ if ((long )src & (sizeof (long ) -1 )) - max =min (PAGE_SIZE - ((long )src & (PAGE_SIZE -1 )), max); + max =min (PAGE_SIZE -offset_in_page (src), max);#else /* If src or dest is unaligned, don't do word-at-a-time. */ if (((long ) dest | (long ) src) & (sizeof (long ) -1 ))
Lorenzo Stoakes
reviewerRe: [PATCH v2 2/2] string: use offset_in_page() in sized_strscpy()
Quoted 4 lines · Thorsten Blum
Replace the open-coded implementation with
offset_in_page()to simplifysized_strscpy().Signed-off-by Thorsten Blum <[email protected]>
I feel this patch actually makes sized_strscpy() more difficult to understand unfortunately, so not really in favour of us taking it.
Quoted 17 lines
--- lib/string.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)lib/string.c +1
-0@@ -25,6 +25,7 @@#include <linux/stddef.h> #include <linux/string.h> #include <linux/types.h> +#include <vdso/page.h> #include <asm/page.h> #include <asm/rwonce.h>
lib/string.c ssize_t sized_strscpy(char *dest, const char *src, size_t count) · line 131
Quoted 5 lines
lib/string.c +1
-1@@ -127,7 +128,7 @@ ssize_t sized_strscpy(char *dest, const char *src, size_t count)* since we don't know if the next page is mapped. */ if ((long )src & (sizeof (long ) -1 )) - max =min (PAGE_SIZE - ((long )src & (PAGE_SIZE -1 )), max); + max =min (PAGE_SIZE -offset_in_page (src), max);
This isn't strictly the same code, offset_in_page() is:
#define offset_in_page(p) ((unsigned long)(p) & ~PAGE_MASK)
So this is now, effectively:
- max = min(PAGE_SIZE - ((long)src & (PAGE_SIZE - 1)), max);
+ max = min(PAGE_SIZE - ((unsigned long)src & (PAGE_SIZE -1)), max);
So there could be some issues here with type conversions at least in theory.
But in any case you're now making the logic inconsistent (the next line uses bitwise operations directly).
So I'd rather we didn't make this change.
lib/string.c ssize_t sized_strscpy(char *dest, const char *src, size_t count) · line 134
Quoted 3 lines
lib/string.c +0
-0#else /* If src or dest is unaligned, don't do word-at-a-time. */ if (((long ) dest | (long ) src) & (sizeof (long ) -1 ))
I'm generally not hugely fond of this 'do a sample case to demonstrate that mm.h doesn't need to be imported' approach.
Either make the change properly and consistently, or not at all.
Anyway, I'm fine with you just sending a v3 as a single patch moving offset_in_page() to vdso/page.h then you can defer the use cases if you like?
Cheers, Lorenzo
Re: [PATCH v2 2/2] string: use offset_in_page() in sized_strscpy()
Quoted 41 lines · Lorenzo Stoakes
Quoted 4 lines · Thorsten Blum
Replace the open-coded implementation with
offset_in_page()to simplifysized_strscpy().Signed-off-by Thorsten Blum <[email protected]>I feel this patch actually makes
sized_strscpy()more difficult to understand unfortunately, so not really in favour of us taking it.Quoted 17 lines
--- lib/string.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)lib/string.c +1
-0@@ -25,6 +25,7 @@#include <linux/stddef.h> #include <linux/string.h> #include <linux/types.h> +#include <vdso/page.h> #include <asm/page.h> #include <asm/rwonce.h> lib/string.c ssize_t sized_strscpy(char *dest, const char *src, size_t count) · line 131
Quoted 5 lines
lib/string.c +1
-1@@ -127,7 +128,7 @@ ssize_t sized_strscpy(char *dest, const char *src, size_t count)* since we don't know if the next page is mapped. */ if ((long )src & (sizeof (long ) -1 )) - max =min (PAGE_SIZE - ((long )src & (PAGE_SIZE -1 )), max); + max =min (PAGE_SIZE -offset_in_page (src), max);This isn't strictly the same code,
offset_in_page()is:#define
offset_in_page(p)((unsigned long)(p) & ~PAGE_MASK)So this is now, effectively:
(quoted diff) +1
-1- max = min(PAGE_SIZE - ((long)src & (PAGE_SIZE - 1)), max); + max = min(PAGE_SIZE - ((unsigned long)src & (PAGE_SIZE -1)), max);
Quoted 1 line
So there could be some issues here with type conversions at least in theory.
I think this should have used unsigned long from the start, as long seems a bit odd for a pointer, and PAGE_SIZE - offset_in_page() is a common kernel idiom for this exact calculation.
Andrew also commented [1] on this line recently.
Quoted 2 lines
But in any case you're now making the logic inconsistent (the next line uses bitwise operations directly).
Agreed, maybe there are other helpers we can use here too, but that should probably be a follow-up patch.
Quoted 1 line
So I'd rather we didn't make this change.
FWIW, using offset_in_page() in lib/string.c (as suggested by Andy [2]) was the initial motivation for moving it out of mm.h, so I'd prefer to keep the use-site in this series if possible.
Thanks,
Thorsten
[1] https://lore.kernel.org/lkml/[email protected]/
[2] https://lore.kernel.org/lkml/CAHp75VfQNkqEYsO4Uup0c-uiYuVyAWit=tmCz2BsYLp-sjXsZw@mail.gmail.com/
Re: [PATCH v2 2/2] string: use offset_in_page() in sized_strscpy()
NAK.
I guess I wasn't clear before :)
Quoted 42 lines · Thorsten Blum
Quoted 41 lines · Lorenzo Stoakes
Quoted 4 lines · Thorsten Blum
Replace the open-coded implementation with
offset_in_page()to simplifysized_strscpy().Signed-off-by Thorsten Blum <[email protected]>I feel this patch actually makes
sized_strscpy()more difficult to understand unfortunately, so not really in favour of us taking it.Quoted 17 lines
--- lib/string.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)lib/string.c +1
-0@@ -25,6 +25,7 @@#include <linux/stddef.h> #include <linux/string.h> #include <linux/types.h> +#include <vdso/page.h> #include <asm/page.h> #include <asm/rwonce.h> lib/string.c ssize_t sized_strscpy(char *dest, const char *src, size_t count) · line 131
Quoted 5 lines
lib/string.c +1
-1@@ -127,7 +128,7 @@ ssize_t sized_strscpy(char *dest, const char *src, size_t count)* since we don't know if the next page is mapped. */ if ((long )src & (sizeof (long ) -1 )) - max =min (PAGE_SIZE - ((long )src & (PAGE_SIZE -1 )), max); + max =min (PAGE_SIZE -offset_in_page (src), max);This isn't strictly the same code,
offset_in_page()is:#define
offset_in_page(p)((unsigned long)(p) & ~PAGE_MASK)So this is now, effectively:
(quoted diff) +1
-1- max = min(PAGE_SIZE - ((long)src & (PAGE_SIZE - 1)), max); + max = min(PAGE_SIZE - ((unsigned long)src & (PAGE_SIZE -1)), max);
Quoted 7 lines
Quoted 1 line
So there could be some issues here with type conversions at least in theory.
I think this should have used unsigned long from the start, as long seems a bit odd for a pointer, and
PAGE_SIZE-offset_in_page()is a common kernel idiom for this exact calculation.Andrew also commented [1] on this line recently.
Yup, but it makes no sense as a singular change, it actively worsens the code.
Hypotheticals about what should or should not be with you not changing anything isn't useful.
Quoted 6 lines
Quoted 3 lines
But in any case you're now making the logic inconsistent (the next line uses bitwise operations directly).
Agreed, maybe there are other helpers we can use here too, but that should probably be a follow-up patch.
No, this patch is bad, we're not taking it.
Quoted 6 lines
Quoted 2 lines
So I'd rather we didn't make this change.
FWIW, using
offset_in_page()inlib/string.c(as suggested by Andy [2]) was the initial motivation for moving it out ofmm.h, so I'd prefer to keep the use-site in this series if possible.
Nope.
Quoted 6 lines
Thanks,
Thorsten[1] https://lore.kernel.org/lkml/[email protected]/
[2] https://lore.kernel.org/lkml/CAHp75VfQNkqEYsO4Uup0c-uiYuVyAWit=tmCz2BsYLp-sjXsZw@mail.gmail.com/
Thanks, Lorenzo
Lorenzo Stoakes
reviewerQuoted 4 lines · Thorsten Blum
offset_in_page()is a small page-arithmetic helper that has been around for 20+ years. However, page-offset calculations are still open-coded in many places and in different ways:
Quoted 8 lines
(unsigned long)p & ~PAGE_MASK (unsigned long)p & (PAGE_SIZE - 1) (long)p & (PAGE_SIZE - 1) ...Some of these open-coded instances may be due to
offset_in_page()being buried 3000+ lines intolinux/mm.h; others may have avoided includinglinux/mm.h, which is a large header that pulls in many others.
This is good but I think you're still missing the 'why' here (as to why files don't import mm.h, and thus what this series addreses), which presumably, is compile time.
Quoted 35 lines
Patch 1 moves
offset_in_page()fromlinux/mm.htovdso/page.h, which keeps the helper with other low-level page definitions and allows users that only needoffset_in_page()to avoid includinglinux/mm.h.Patch 2 shows a concrete example where including
vdso/page.his sufficient and including the largelinux/mm.hwould be unnecessary.Existing users of
offset_in_page()do not need to change because patch 1 also includesvdso/page.hfromlinux/mm.h.This series is based on akpm/mm.git mm-nonmm-unstable commit
d067a83c8063("string: use min insized_strscpy"), which is also in linux-next.Changes in v2:
- Add a cover letter and drop thebytes_to_page_end()helper
- Moveoffset_in_page()tovdso/page.has suggested by David and Lorenzo
- Useoffset_in_page()inlib/string.cas an example sincebitmap_print_to_pagebuf()inlib/bitmap-str.cis being removed [1]
- v1: https://lore.kernel.org/lkml/[email protected]/[1] https://lore.kernel.org/lkml/[email protected]/
---
Thorsten Blum (2):
vdso: moveoffset_in_page()fromlinux/mm.htovdso/page.h
string: useoffset_in_page()insized_strscpy()include/linux/mm.h | 2 +- include/vdso/page.h | 2 ++ lib/string.c | 3 ++- 3 files changed, 5 insertions(+), 2 deletions(-)base-commit:
d067a83c8063d1bdcbd9af8e1326d846f85138b8
Quoted 13 lines · Thorsten Blum
offset_in_page()is a small page-arithmetic helper that has been around for 20+ years. However, page-offset calculations are still open-coded in many places and in different ways:(unsigned long)p & ~PAGE_MASK (unsigned long)p & (PAGE_SIZE - 1) (long)p & (PAGE_SIZE - 1) ...Some of these open-coded instances may be due to
offset_in_page()being buried 3000+ lines intolinux/mm.h; others may have avoided includinglinux/mm.h, which is a large header that pulls in many others.
Quoted 6 lines
Patch 1 moves
offset_in_page()fromlinux/mm.htovdso/page.h, which keeps the helper with other low-level page definitions and allows users that only needoffset_in_page()to avoid includinglinux/mm.h.Patch 2 shows a concrete example where including
vdso/page.his sufficient and including the largelinux/mm.hwould be unnecessary.
mm.h is already included there indirectly, otherwise it would be a compile error, isn't?
--
Example for who? Are you expecting somebody to pick it up from you and cleanup the whole kernel?
I'm not interested in 'example', I'm interested in a broad picture and an estimate of how realistically would it be to switch kernel to consistently use the helper.
So this is the broad picture for you:
- 660 uses of offset_in_page()
- 570 uses of & ~PAGE_MASK
- 176 uses of & (PAGE_SIZE - 1)
- 135 high-confidence candidates
Those 135 candidates are found with:
$ git grep -n -E \ '\b(offset|off|ofs|poff|pg_off|pg_ofs|page_offset|page_offs|start_offset|dest_off|src_off|from|to|remainder)\b[^=]*= [^;]*&[[:space:]]*(~PAGE_MASK|\(?PAGE_SIZE[[:space:]]*-[[:space:]]*1\)?)' \ -- ':(exclude)drivers/gpu/drm/amd/include/asic_reg/**' \ ':(exclude)include/trace/events/**'
If you want to go ahead and convert at least those good candidates - it would be about a couple dozens of patches. And it would look like a meaningful commitment.
If you want to move the macro out of linux/mm.h and drop the header from some inclusion paths - it would be another meaningful commitment.
In this series you move the macro to a questionable vdso header, and convert just one user, which leads to *adding* another dependency, instead of removing some. This doesn't look like a valuable commitment, sorry.
Thanks,
Yury
Quoted 27 lines
Existing users of
offset_in_page()do not need to change because patch 1 also includesvdso/page.hfromlinux/mm.h.This series is based on akpm/mm.git mm-nonmm-unstable commit
d067a83c8063("string: use min insized_strscpy"), which is also in linux-next.Changes in v2:
- Add a cover letter and drop thebytes_to_page_end()helper
- Moveoffset_in_page()tovdso/page.has suggested by David and Lorenzo
- Useoffset_in_page()inlib/string.cas an example sincebitmap_print_to_pagebuf()inlib/bitmap-str.cis being removed [1]
- v1: https://lore.kernel.org/lkml/[email protected]/[1] https://lore.kernel.org/lkml/[email protected]/
---
Thorsten Blum (2):
vdso: moveoffset_in_page()fromlinux/mm.htovdso/page.h
string: useoffset_in_page()insized_strscpy()include/linux/mm.h | 2 +- include/vdso/page.h | 2 ++ lib/string.c | 3 ++- 3 files changed, 5 insertions(+), 2 deletions(-)base-commit:
d067a83c8063d1bdcbd9af8e1326d846f85138b8