PAGE_SIZE Availability Inconsistency
Subsystems (2)
I was rtfc'ing the code one day and noticed somethings about the PAGE_SIZE define that is kinda inconsistent around its relative location to the __KERNEL__ define.
On some architectures the PAGE_SIZE is outside the __KERNEL__ define (i386 and x86_64) and on others its inside the define (ia64 and powerpc). I was wondering if this is because the powerpc and ia64 architectures have dynamic page sizes so that's why they can't export PAGE_SIZE outside __KERNEL__.
I'm kinda wondering how I'm supposed to write portable user-space code if I want to use the PAGE_SIZE define on different architectures.
Thanks,
- David Brown
Quoted 4 lines · David Brown
I was rtfc'ing the code one day and noticed somethings about the
PAGE_SIZEdefine that is kinda inconsistent around its relative location to the__KERNEL__define.
Quoted 8 lines
On some architectures the
PAGE_SIZEis outside the__KERNEL__define (i386 andx86_64) and on others its inside the define (ia64 and powerpc). I was wondering if this is because the powerpc and ia64 architectures have dynamic page sizes so that's why they can't exportPAGE_SIZEoutside__KERNEL__.I'm kinda wondering how I'm supposed to write portable user-space code if I want to use the
PAGE_SIZEdefine on different architectures.
The real question is : why do you need PAGE_SIZE from user-space code ?
If it's for mmap() use, you should use getpagesize()
From: "David Brown" <[email protected]>
Date: Mon, 5 Mar 2007 15:55:06 -0800
Quoted 2 lines
I'm kinda wondering how I'm supposed to write portable user-space code if I want to use the
PAGE_SIZEdefine on different architectures.
Call getpagesize().
Quoted 8 lines · David Miller
From: "David Brown" <[email protected]>
Date: Mon, 5 Mar 2007 15:55:06 -0800Quoted 2 lines
I'm kinda wondering how I'm supposed to write portable user-space code if I want to use the
PAGE_SIZEdefine on different architectures.Call
getpagesize().
Thanks, but that still leaves PAGE_SIZE available for some architectures and not for others shouldn't this be moved inside __KERNEL__ in i386 and x86_64 then?
- David Brown
From: "David Brown" <[email protected]>
Date: Mon, 5 Mar 2007 16:04:24 -0800
Quoted 7 lines · David Miller
Quoted 6 lines
From: "David Brown" <[email protected]>
Date: Mon, 5 Mar 2007 15:55:06 -0800Quoted 2 lines
I'm kinda wondering how I'm supposed to write portable user-space code if I want to use the
PAGE_SIZEdefine on different architectures.
Quoted 6 lines
Quoted 2 lines
Call
getpagesize().Thanks, but that still leaves
PAGE_SIZEavailable for some architectures and not for others shouldn't this be moved inside__KERNEL__in i386 andx86_64then?
I definitely think so.
Quoted 6 lines · David Miller
Quoted 4 lines
Quoted 1 line
Thanks, but that still leaves
PAGE_SIZEavailable for some architectures and not for others shouldn't this be moved inside__KERNEL__in i386 andx86_64then?I definitely think so.
It definitely should, especially on x86-64, where the page size isn't guaranteed by the ABI (on i386, the ABI guarantees a 4K page size; on x86-64 it can be up to 64K.)
-hpa
Quoted 10 lines · H. Peter Anvin
Quoted 6 lines · David Miller
Quoted 4 lines
Quoted 1 line
Thanks, but that still leaves
PAGE_SIZEavailable for some architectures and not for others shouldn't this be moved inside__KERNEL__in i386 andx86_64then?I definitely think so.
It definitely should, especially on x86-64, where the page size isn't guaranteed by the ABI (on i386, the ABI guarantees a 4K page size; on x86-64 it can be up to 64K.)
Wouldn't that be ia64?
Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.
Quoted 4 lines · Avi Kivity
Quoted 2 lines
It definitely should, especially on x86-64, where the page size isn't guaranteed by the ABI (on i386, the ABI guarantees a 4K page size; on x86-64 it can be up to 64K.)
Wouldn't that be ia64?
No, the x86-64 EFI ABI permits page sizes up to 64K. Currently, of course, the only page size in use is 4K, but unlike i386 that's not guaranteed by the ABI. At least AMD has indicated that they are considering introducing larger page size support in future hardware.
-hpa
Quoted 6 lines · H. Peter Anvin
Quoted 5 lines · Avi Kivity
Quoted 4 lines
It definitely should, especially on x86-64, where the page size isn't guaranteed by the ABI (on i386, the ABI guarantees a 4K page size; on x86-64 it can be up to 64K.)
Quoted 6 lines
Quoted 1 line
Wouldn't that be ia64?
No, the x86-64 EFI ABI permits page sizes up to 64K. Currently, of course, the only page size in use is 4K, but unlike i386 that's not guaranteed by the ABI. At least AMD has indicated that they are considering introducing larger page size support in future hardware.
EFI ABI? Don't you mean the SYSV ABI right.
That does seem to indicate that 64K is an option. And at a quick glance glibc is using 1M alignment on program segments so it looks like there is at least a reasonable chance of being able to make the transition to a bigger page size for user space.
Eric
Quoted 4 lines · On Mon, 5 Mar 2007 15:55:06 -0800 David Brown wrote:
I was rtfc'ing the code one day and noticed somethings about the
PAGE_SIZEdefine that is kinda inconsistent around its relative location to the__KERNEL__define.
Quoted 8 lines
On some architectures the
PAGE_SIZEis outside the__KERNEL__define (i386 andx86_64) and on others its inside the define (ia64 and powerpc). I was wondering if this is because the powerpc and ia64 architectures have dynamic page sizes so that's why they can't exportPAGE_SIZEoutside__KERNEL__.I'm kinda wondering how I'm supposed to write portable user-space code if I want to use the
PAGE_SIZEdefine on different architectures.
use 'getpagesize(2)'
---
~Randy
*** Remember to use Documentation/SubmitChecklist when testing your code ***
Quoted 4 lines · David Brown
I was rtfc'ing the code one day and noticed somethings about the
PAGE_SIZEdefine that is kinda inconsistent around its relative location to the__KERNEL__define.
Quoted 8 lines
On some architectures the
PAGE_SIZEis outside the__KERNEL__define (i386 andx86_64) and on others its inside the define (ia64 and powerpc). I was wondering if this is because the powerpc and ia64 architectures have dynamic page sizes so that's why they can't exportPAGE_SIZEoutside__KERNEL__.I'm kinda wondering how I'm supposed to write portable user-space code if I want to use the
PAGE_SIZEdefine on different architectures.
PAGE_SIZE should not be available at all. Please use getpagesize() instead.
Hi,
Quoted 2 lines · Christoph Hellwig
PAGE_SIZEshould not be available at all. Please usegetpagesize()instead.
While I agree, NBPG is a bit of a problem, although it's only needed for aout coredumps AFAICT, but still needed to compile e.g. gdb.
bye, Roman
Quoted 2 lines
While I agree, NBPG is a bit of a problem, although it's only needed for aout coredumps AFAICT, but still needed to compile e.g. gdb.
Well then how does gdb deal with ia64? because PAGE_SIZE and friends aren't available for that arch same with ppc.
Looking at the gdb code they do have places where they define a PAGE_SIZE but they even mention its a bug (gdb-6.6/libiberty/getpagesize.c:14) also grepped through their code looking for includes of page.h come up with nothing.
- David Brown
Quoted 6 lines · David Brown
Quoted 3 lines
While I agree, NBPG is a bit of a problem, although it's only needed for aout
coredumps AFAICT, but still needed to compile e.g. gdb.Well then how does gdb deal with ia64? because
PAGE_SIZEand friends aren't available for that arch same with ppc.
Neither of them support aout core dumps.
Quoted 9 lines · Roman Zippel
Hi,
Quoted 2 lines · Christoph Hellwig
PAGE_SIZEshould not be available at all. Please usegetpagesize()instead.While I agree, NBPG is a bit of a problem, although it's only needed for aout coredumps AFAICT, but still needed to compile e.g. gdb.
So we should export this one with an arbitrary value (on multiple page size architectures) and a warning, maybe even an __deprecated attached to it.
Quoted 8 lines · Christoph Hellwig
Quoted 7 lines · Roman Zippel
Hi,
Quoted 2 lines · Christoph Hellwig
PAGE_SIZEshould not be available at all. Please usegetpagesize()instead.
Quoted 6 lines
Quoted 2 lines
While I agree, NBPG is a bit of a problem, although it's only needed for aout coredumps AFAICT, but still needed to compile e.g. gdb.
So we should export this one with an arbitrary value (on multiple page size architectures) and a warning, maybe even an
__deprecatedattached to it.
if we think the kernel should export this one, we could do
#ifndef __KERNEL__
#define PAGE_SIZE getpagesize()
#endif
Signature · 2 lines
if you want to mail me at work (you don't), use arjan (at) linux.intel.com Test the interaction between Linux and your BIOS via http://www.linuxfirmwarekit.org
Quoted 10 lines · Arjan van de Ven
Quoted 9 lines
Quoted 5 lines
Quoted 2 lines
PAGE_SIZEshould not be available at all. Please usegetpagesize()instead.While I agree, NBPG is a bit of a problem, although it's only needed for aout coredumps AFAICT, but still needed to compile e.g. gdb.
So we should export this one with an arbitrary value (on multiple page size architectures) and a warning, maybe even an
__deprecatedattached to it.
Quoted 5 lines
if we think the kernel should export this one, we could do
#ifndef
__KERNEL__
#definePAGE_SIZEgetpagesize()
#endif
No, no no. We should never export PAGE_SIZE. We might export NBPG as deprecated symbol for gdb if it really needs it, but that should happen only on a.out systems, and it it should be a true constant, not depending on PAGE_SIZE.
I've Cc'ed the gdb list on whether they have any comments on this issue.
Quoted 7 lines · Christoph Hellwig
No, no no. We should never export
PAGE_SIZE. We might export NBPG as deprecated symbol for gdb if it really needs it, but that should happen only on a.out systems, and it it should be a true constant, not depending onPAGE_SIZE.I've Cc'ed the gdb list on whether they have any comments on this issue.
Sounds reasonable. I do not believe that GDB has any dependence on PAGE_SIZE; bfd (i.e. both gdb and binutils) use NBPG on a large number of systems. Looks like i386, alpha, m68k, s390, vax - but don't quote me on that, I had to guess from the configure script.
Signature · 2 lines
Daniel Jacobowitz CodeSourcery
Quoted 7 lines · Christoph Hellwig
No, no no. We should never export
PAGE_SIZE. We might export NBPG as deprecated symbol for gdb if it really needs it, but that should happen only on a.out systems, and it it should be a true constant, not depending onPAGE_SIZE.I've Cc'ed the gdb list on whether they have any comments on this issue.
By the way, it's a massive snafu that the swap area magic number is dependent on PAGE_SIZE. There is absolutely no good reason for that.
-hp
Quoted 11 lines · H. Peter Anvin
Quoted 7 lines · Christoph Hellwig
No, no no. We should never export
PAGE_SIZE. We might export NBPG as deprecated symbol for gdb if it really needs it, but that should happen only on a.out systems, and it it should be a true constant, not depending onPAGE_SIZE.I've Cc'ed the gdb list on whether they have any comments on this issue.
By the way, it's a massive snafu that the swap area magic number is dependent on
PAGE_SIZE. There is absolutely no good reason for that.
Yeah, now that you mention it I remember having problems with that in the past. We should probably create a new swap format that avoids this problem. I'll put it on my ever growing todo list.
Quoted 3 lines
By the way, it's a massive snafu that the swap area magic number is dependent on
PAGE_SIZE. There is absolutely no good reason for that.
Agreed, its been a big problem booting between 4kB and 64kB kernels on ppc64.
Anton
Quoted 4 lines · Anton Blanchard
Quoted 1 line
By the way, it's a massive snafu that the swap area magic number is dependent on
PAGE_SIZE. There is absolutely no good reason for that.Agreed, its been a big problem booting between 4kB and 64kB kernels on ppc64.
The easiest way to fix this would be to always park the swap magic at the offset of the smallest page size in use, which is 4K. This is analogous how the offset for the ext2/3 superblock got fixed at 1K -- for 1K blocks, it's the second block, but for larger blocks, it's part of the first block. If we fix the offset of the swap magic at 4096 minus the offset that's already there, it will always fall in the first page regardless of page size.
-hpa
Hi Peter,
Quoted 7 lines
The easiest way to fix this would be to always park the swap magic at the offset of the smallest page size in use, which is 4K. This is analogous how the offset for the ext2/3 superblock got fixed at 1K -- for 1K blocks, it's the second block, but for larger blocks, it's part of the first block. If we fix the offset of the swap magic at 4096 minus the offset that's already there, it will always fall in the first page regardless of page size.
Yeah that makes sense. I gave it a go by creating a MIN_PAGE_SIZE define, and allowing an architecture to override it if required.
A couple of issues:
1. Parts of the swap header are in PAGE_SIZE chunks so I made them MIN_PAGE_SIZE chunks too.
2. The badblocks stuff is PAGE_SIZEd too. Do we ever use it on modern disks? Maybe we can just remove this support.
3. This will unfortunately break machines currently running a 64kB kernel with swap space. We may just have to lump it and fix on upgade.
Anton
--
Our current swap layout has issues with variable page size kernels. Instead of using the page size at runtime, base it on the minimum page size the architecture supports.
---
include/linux/swap.h +8 -5
@@ -48,14 +48,17 @@* old reserved area - some extra information. Note that the first * kilobyte is reserved for boot loader or disk label stuff... * -* Having the magic at the end of the PAGE_SIZE makes detecting swap -* areas somewhat tricky on machines that support multiple page sizes. -* For 2.5 we'll probably want to move the magic to just beyond the -* bootbits... +* Version 1 and 2 swap headers store the magic at the end of the +* PAGE_SIZE which causes problems for architectures with multiple +* page sizes. An architecture can define MIN_PAGE_SIZE to be used +* regardless of the kernel page size to get around this. */ +#ifndef MIN_PAGE_SIZE +#define MIN_PAGE_SIZE PAGE_SIZE +#endif union swap_header {struct { -char reserved[PAGE_SIZE -10 ]; +char reserved[MIN_PAGE_SIZE -10 ];char magic[10 ];/* SWAP-SPACE or SWAPSPACE2 */ } magic;struct {
include/asm-powerpc/page.h +2 -0
@@ -24,8 +24,10 @@#else #define PAGE_SHIFT 12#endif +#define MIN_PAGE_SHIFT 12#define PAGE_SIZE (ASM_CONST(1) << PAGE_SHIFT) +#define MIN_PAGE_SIZE (ASM_CONST(1) << MIN_PAGE_SHIFT)/* We do define AT_SYSINFO_EHDR but don't use the gate mechanism */ #define __HAVE_ARCH_GATE_AREA 1
mm/swapfile.c +6 -0
@@ -1568,6 +1568,12 @@ p->cluster_next =1 ;/* +* last_page is in MIN_PAGE_SIZE chunks, scale to kernel +* page size. +*/ + swap_header->info.last_page >>= (PAGE_SHIFT -MIN_PAGE_SHIFT ); + +/* * Find out how many pages are allowed for a single swap * device. There are two limiting factors: 1) the number of * bits for the swap offset in the swp_entry_t type and
Hi,
Quoted 3 lines
Our current swap layout has issues with variable page size kernels. Instead of using the page size at runtime, base it on the minimum page size the architecture supports.
A hacked up patch to userspace utilities to test the kernel patch. BTW It looks like there are some real bugs here:
int pagesize = getpagesize();
int rd;
char buf[32768];
rd = pagesize;
if (rd < 8192)
rd = 8192;
if (rd > sizeof(buf))
rd = sizeof(buf);
if (lseek(fd, 0, SEEK_SET) != 0
|| read(fd, buf, rd) != rd)
goto io_error;
if (may_be_swap(buf+pagesize) ||
may_be_swap(buf+4096) || may_be_swap(buf+8192))
type = "swap";If page size == 64kB wont we read past the end of buf?
Anton
disk-utils/mkswap.c +4 -0
@@ -169,7 +158,11 @@#ifdef PAGE_SIZE defined_pagesize =PAGE_SIZE ;#endif +#ifdef __powerpc__ + kernel_pagesize =4096 ; +#else kernel_pagesize =getpagesize (); +#endif pagesize = kernel_pagesize;if (user_pagesize) {
mount/get_label_uuid.c +4 -0
@@ -79,7 +79,11 @@static int is_v1_swap_partition (int fd,char **label,char *uuid) { +#ifdef __powerpc__ +int n =4096 ; +#else int n =getpagesize (); +#endif char *buf =xmalloc (n);struct swap_header_v1_2 *p = (struct swap_header_v1_2 *) buf;
mount/mount_guess_fstype.c +4 -0
@@ -462,7 +462,11 @@if (!type) {/* perhaps the user tries to mount the swap space on a new disk; warn her before she does mke2fs on it */ +#ifdef __powerpc__ +int pagesize =4096 ; +#else int pagesize =getpagesize (); +#endif int rd;char buf[32768 ];
rescuept/rescuept.c +4 -0
@@ -510,7 +510,11 @@ size = s.st_size /512 ; } +#ifdef __powerpc__ + pagesize =4096 ; +#else pagesize =getpagesize (); +#endif if (pagesize <=0 ) pagesize =4096 ;else if (pagesize >MAXPAGESZ ) {
From: Anton Blanchard <[email protected]>
Date: Thu, 8 Mar 2007 15:42:36 -0600
Quoted 10 lines
Quoted 7 lines
The easiest way to fix this would be to always park the swap magic at the offset of the smallest page size in use, which is 4K. This is analogous how the offset for the ext2/3 superblock got fixed at 1K -- for 1K blocks, it's the second block, but for larger blocks, it's part of the first block. If we fix the offset of the swap magic at 4096 minus the offset that's already there, it will always fall in the first page regardless of page size.
Yeah that makes sense. I gave it a go by creating a
MIN_PAGE_SIZEdefine, and allowing an architecture to override it if required.
I might be missing something but doesn't this break every SWAP partition that was created with something other than MIN_PAGE_SIZE?
Hi,
Quoted 3 lines
I might be missing something but doesn't this break every SWAP partition that was created with something other than
MIN_PAGE_SIZE?
It does. I was thinking we could work around it in ppc64 (64kB is quite new), but I forgot there are options on sparc64 to change the page size :)
The other option is to create a v3 swap format that doesnt use any PAGE_SIZE parameters.
Anton
Quoted 6 lines · Anton Blanchard
Hi,
Quoted 3 lines
I might be missing something but doesn't this break every SWAP partition that was created with something other than
MIN_PAGE_SIZE?
Quoted 6 lines
It does. I was thinking we could work around it in ppc64 (64kB is quite new), but I forgot there are options on sparc64 to change the page size :)
The other option is to create a v3 swap format that doesnt use any
PAGE_SIZEparameters.
The best thing to do would be to look for the magic both at PAGE_SIZE (for compatibility) and MIN_PAGE_SIZE (for sanity.)
-hpa
From: "H. Peter Anvin" <[email protected]>
Date: Thu, 08 Mar 2007 20:18:28 -0800
Quoted 6 lines
Quoted 2 lines · Anton Blanchard
The other option is to create a v3 swap format that doesnt use any
PAGE_SIZEparameters.The best thing to do would be to look for the magic both at
PAGE_SIZE(for compatibility) andMIN_PAGE_SIZE(for sanity.)
That might work, but a large part of me says to go for v3 and do it cleanly.
Quoted 10 lines · David Miller
From: "H. Peter Anvin" <[email protected]>
Date: Thu, 08 Mar 2007 20:18:28 -0800Quoted 4 lines
Quoted 2 lines · Anton Blanchard
The other option is to create a v3 swap format that doesnt use any
PAGE_SIZEparameters.The best thing to do would be to look for the magic both at
PAGE_SIZE(for compatibility) andMIN_PAGE_SIZE(for sanity.)That might work, but a large part of me says to go for v3 and do it cleanly.
The advantage would be that it wouldn't require a v3 for platforms for which MIN_PAGE_SIZE == PAGE_SIZE, which accounts for a very large percentage of systems.
You still have to look for the darn magic in two places, so there is no reason for it to be different.
-hpa
From: "H. Peter Anvin" <[email protected]>
Date: Thu, 08 Mar 2007 20:31:05 -0800
Quoted 6 lines
The advantage would be that it wouldn't require a v3 for platforms for which
MIN_PAGE_SIZE==PAGE_SIZE, which accounts for a very large percentage of systems.You still have to look for the darn magic in two places, so there is no reason for it to be different.
Good point.
Hi,
Quoted 6 lines
The advantage would be that it wouldn't require a v3 for platforms for which
MIN_PAGE_SIZE==PAGE_SIZE, which accounts for a very large percentage of systems.You still have to look for the darn magic in two places, so there is no reason for it to be different.
The problem is if you can hit in two places then what PAGE_SIZE should you use to size the contents of the swap header while remaining backward compatible.
Im leaning towards Dave suggestion of creating a clean v3 swap header.
Anton
Quoted 4 lines · Anton Blanchard
Hi,
Quoted 2 lines
The advantage would be that it wouldn't require a v3 for platforms for which
MIN_PAGE_SIZE==PAGE_SIZE, which accounts for a very large percentage of systems.
Quoted 8 lines
Quoted 1 line
You still have to look for the darn magic in two places, so there is no reason for it to be different.
The problem is if you can hit in two places then what
PAGE_SIZEshould you use to size the contents of the swap header while remaining backward compatible.Im leaning towards Dave suggestion of creating a clean v3 swap header.
Changing the header format doesn't make *ANY* difference whatsoever.
You have to write two copies of the swap header, and the kernel should check for a header at MIN_PAGE_SIZE first and then at PAGE_SIZE.
If there are fields (other than position) in the v2 swap header that are dependent on PAGE_SIZE, then the copy at MIN_PAGE_SIZE should be sized using MIN_PAGE_SIZE, and the copy at PAGE_SIZE should be sized at PAGE_SIZE. It's that simple.
Creating a new format will not help that one iota, and will create gratuitous incompatiblity for the very common case of PAGE_SIZE == MIN_PAGE_SIZE.
-hpa
-hpa
Quoted 4 lines · Anton Blanchard
2. The badblocks stuff is
PAGE_SIZEdtoo. Do we ever use it on modern disks? Maybe we can just remove this support.
Badblocks is definitely still used in some configurations.
-hpa
Quoted 6 lines · Anton Blanchard
Quoted 3 lines
By the way, it's a massive snafu that the swap area magic number is dependent on
PAGE_SIZE. There is absolutely no good reason for that.Agreed, its been a big problem booting between 4kB and 64kB kernels on ppc64.
Okay this really seems like a couple of things need to happen, first change swap dependency on PAGE_SIZE, then move the __KERNEL__ define above the PAGE_SIZE and friends defines in the appropriate asm-*/page.h files.
Do these tasks need to happen in this order? I haven't really looked at the swap code at all...
Also, I'd be willing to help I've done kernel coding for experimental projects and such but nothing for kernel.org so I might need some shepherding.
- David Brown