Skip to content

Commit

Permalink
PAGE_ALIGN(): correctly handle 64-bit values on 32-bit architectures
Browse files Browse the repository at this point in the history
On 32-bit architectures PAGE_ALIGN() truncates 64-bit values to the 32-bit
boundary. For example:

	u64 val = PAGE_ALIGN(size);

always returns a value < 4GB even if size is greater than 4GB.

The problem resides in PAGE_MASK definition (from include/asm-x86/page.h for
example):

#define PAGE_SHIFT      12
#define PAGE_SIZE       (_AC(1,UL) << PAGE_SHIFT)
#define PAGE_MASK       (~(PAGE_SIZE-1))
...
#define PAGE_ALIGN(addr)       (((addr)+PAGE_SIZE-1)&PAGE_MASK)

The "~" is performed on a 32-bit value, so everything in "and" with
PAGE_MASK greater than 4GB will be truncated to the 32-bit boundary.
Using the ALIGN() macro seems to be the right way, because it uses
typeof(addr) for the mask.

Also move the PAGE_ALIGN() definitions out of include/asm-*/page.h in
include/linux/mm.h.

See also lkml discussion: http://lkml.org/lkml/2008/6/11/237

[[email protected]: fix drivers/media/video/uvc/uvc_queue.c]
[[email protected]: fix v850]
[[email protected]: fix powerpc]
[[email protected]: fix arm]
[[email protected]: fix mips]
[[email protected]: fix drivers/media/video/pvrusb2/pvrusb2-dvb.c]
[[email protected]: fix drivers/mtd/maps/uclinux.c]
[[email protected]: fix powerpc]
Signed-off-by: Andrea Righi <[email protected]>
Cc: <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
arighi authored and torvalds committed Jul 24, 2008
1 parent d92bc31 commit 27ac792
Show file tree
Hide file tree
Showing 57 changed files with 36 additions and 74 deletions.
1 change: 1 addition & 0 deletions arch/arm/kernel/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <linux/module.h>
#include <linux/moduleloader.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/elf.h>
#include <linux/vmalloc.h>
#include <linux/slab.h>
Expand Down
1 change: 1 addition & 0 deletions arch/arm/plat-omap/fb.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/bootmem.h>
Expand Down
1 change: 1 addition & 0 deletions arch/avr32/mm/ioremap.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* published by the Free Software Foundation.
*/
#include <linux/vmalloc.h>
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/io.h>

Expand Down
1 change: 1 addition & 0 deletions arch/h8300/kernel/setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <linux/sched.h>
#include <linux/delay.h>
#include <linux/interrupt.h>
#include <linux/mm.h>
#include <linux/fs.h>
#include <linux/fb.h>
#include <linux/console.h>
Expand Down
1 change: 1 addition & 0 deletions arch/m68k/amiga/chipram.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/init.h>
#include <linux/ioport.h>
#include <linux/slab.h>
Expand Down
1 change: 1 addition & 0 deletions arch/m68knommu/kernel/setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <linux/interrupt.h>
#include <linux/fb.h>
#include <linux/module.h>
#include <linux/mm.h>
#include <linux/console.h>
#include <linux/errno.h>
#include <linux/string.h>
Expand Down
1 change: 1 addition & 0 deletions arch/mips/kernel/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include <linux/moduleloader.h>
#include <linux/elf.h>
#include <linux/mm.h>
#include <linux/vmalloc.h>
#include <linux/slab.h>
#include <linux/fs.h>
Expand Down
1 change: 1 addition & 0 deletions arch/mips/sgi-ip27/ip27-klnuma.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Copyright 2000 - 2001 Kanoj Sarcar ([email protected])
*/
#include <linux/init.h>
#include <linux/mm.h>
#include <linux/mmzone.h>
#include <linux/kernel.h>
#include <linux/nodemask.h>
Expand Down
1 change: 1 addition & 0 deletions arch/powerpc/kernel/suspend.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Copyright (c) 2001 Patrick Mochel <[email protected]>
*/

#include <linux/mm.h>
#include <asm/page.h>

/* References to section boundaries */
Expand Down
1 change: 1 addition & 0 deletions arch/powerpc/lib/code-patching.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <linux/kernel.h>
#include <linux/vmalloc.h>
#include <linux/init.h>
#include <linux/mm.h>
#include <asm/page.h>
#include <asm/code-patching.h>

Expand Down
2 changes: 1 addition & 1 deletion arch/sparc64/kernel/iommu_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#define IO_PAGE_SHIFT 13
#define IO_PAGE_SIZE (1UL << IO_PAGE_SHIFT)
#define IO_PAGE_MASK (~(IO_PAGE_SIZE-1))
#define IO_PAGE_ALIGN(addr) (((addr)+IO_PAGE_SIZE-1)&IO_PAGE_MASK)
#define IO_PAGE_ALIGN(addr) ALIGN(addr, IO_PAGE_SIZE)

#define IO_TSB_ENTRIES (128*1024)
#define IO_TSB_SIZE (IO_TSB_ENTRIES * 8)
Expand Down
1 change: 1 addition & 0 deletions arch/x86/kernel/module_64.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <linux/fs.h>
#include <linux/string.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/slab.h>
#include <linux/bug.h>

Expand Down
1 change: 1 addition & 0 deletions arch/xtensa/kernel/setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include <linux/errno.h>
#include <linux/init.h>
#include <linux/mm.h>
#include <linux/proc_fs.h>
#include <linux/screen_info.h>
#include <linux/bootmem.h>
Expand Down
1 change: 1 addition & 0 deletions drivers/char/random.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@
#include <linux/fs.h>
#include <linux/genhd.h>
#include <linux/interrupt.h>
#include <linux/mm.h>
#include <linux/spinlock.h>
#include <linux/percpu.h>
#include <linux/cryptohash.h>
Expand Down
1 change: 1 addition & 0 deletions drivers/ieee1394/iso.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include <linux/pci.h>
#include <linux/sched.h>
#include <linux/mm.h>
#include <linux/slab.h>

#include "hosts.h"
Expand Down
1 change: 1 addition & 0 deletions drivers/media/video/pvrusb2/pvrusb2-dvb.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include <linux/kthread.h>
#include <linux/freezer.h>
#include <linux/mm.h>
#include "dvbdev.h"
#include "pvrusb2-debug.h"
#include "pvrusb2-hdw-internal.h"
Expand Down
1 change: 1 addition & 0 deletions drivers/media/video/pvrusb2/pvrusb2-ioread.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "pvrusb2-debug.h"
#include <linux/errno.h>
#include <linux/string.h>
#include <linux/mm.h>
#include <linux/slab.h>
#include <linux/mutex.h>
#include <asm/uaccess.h>
Expand Down
1 change: 1 addition & 0 deletions drivers/media/video/uvc/uvc_queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include <linux/kernel.h>
#include <linux/version.h>
#include <linux/mm.h>
#include <linux/list.h>
#include <linux/module.h>
#include <linux/usb.h>
Expand Down
1 change: 1 addition & 0 deletions drivers/media/video/videobuf-core.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <linux/init.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/mm.h>
#include <linux/slab.h>
#include <linux/interrupt.h>

Expand Down
1 change: 1 addition & 0 deletions drivers/mtd/maps/uclinux.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include <linux/major.h>
#include <linux/mtd/mtd.h>
#include <linux/mtd/map.h>
Expand Down
1 change: 1 addition & 0 deletions drivers/net/mlx4/eq.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/mm.h>
#include <linux/dma-mapping.h>

#include <linux/mlx4/cmd.h>
Expand Down
1 change: 1 addition & 0 deletions drivers/pcmcia/electra_cf.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/interrupt.h>
#include <linux/mm.h>
#include <linux/vmalloc.h>
#include <linux/of_platform.h>

Expand Down
1 change: 1 addition & 0 deletions drivers/scsi/sun_esp.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <linux/types.h>
#include <linux/delay.h>
#include <linux/module.h>
#include <linux/mm.h>
#include <linux/init.h>

#include <asm/irq.h>
Expand Down
1 change: 1 addition & 0 deletions drivers/video/acornfb.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <linux/string.h>
#include <linux/ctype.h>
#include <linux/slab.h>
#include <linux/mm.h>
#include <linux/init.h>
#include <linux/fb.h>
#include <linux/platform_device.h>
Expand Down
1 change: 1 addition & 0 deletions drivers/video/imxfb.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <linux/string.h>
#include <linux/interrupt.h>
#include <linux/slab.h>
#include <linux/mm.h>
#include <linux/fb.h>
#include <linux/delay.h>
#include <linux/init.h>
Expand Down
1 change: 1 addition & 0 deletions drivers/video/omap/dispc.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/
#include <linux/kernel.h>
#include <linux/dma-mapping.h>
#include <linux/mm.h>
#include <linux/vmalloc.h>
#include <linux/clk.h>
#include <linux/io.h>
Expand Down
1 change: 1 addition & 0 deletions drivers/video/omap/omapfb_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <linux/platform_device.h>
#include <linux/mm.h>
#include <linux/uaccess.h>

#include <asm/mach-types.h>
Expand Down
1 change: 1 addition & 0 deletions drivers/video/pxafb.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <linux/string.h>
#include <linux/interrupt.h>
#include <linux/slab.h>
#include <linux/mm.h>
#include <linux/fb.h>
#include <linux/delay.h>
#include <linux/init.h>
Expand Down
1 change: 1 addition & 0 deletions drivers/video/sa1100fb.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@
#include <linux/string.h>
#include <linux/interrupt.h>
#include <linux/slab.h>
#include <linux/mm.h>
#include <linux/fb.h>
#include <linux/delay.h>
#include <linux/init.h>
Expand Down
3 changes: 0 additions & 3 deletions include/asm-alpha/page.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,6 @@ typedef struct page *pgtable_t;

#endif /* !__ASSEMBLY__ */

/* to align the pointer to the (next) page boundary */
#define PAGE_ALIGN(addr) (((addr)+PAGE_SIZE-1)&PAGE_MASK)

#define __pa(x) ((unsigned long) (x) - PAGE_OFFSET)
#define __va(x) ((void *)((unsigned long) (x) + PAGE_OFFSET))
#ifndef CONFIG_DISCONTIGMEM
Expand Down
4 changes: 1 addition & 3 deletions include/asm-arm/page-nommu.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/

#ifndef _ASMARM_PAGE_NOMMU_H
#define _ASMARM_PAGE_NOMMU_H

Expand Down Expand Up @@ -42,9 +43,6 @@ typedef unsigned long pgprot_t;
#define __pmd(x) (x)
#define __pgprot(x) (x)

/* to align the pointer to the (next) page boundary */
#define PAGE_ALIGN(addr) (((addr)+PAGE_SIZE-1)&PAGE_MASK)

extern unsigned long memory_start;
extern unsigned long memory_end;

Expand Down
3 changes: 0 additions & 3 deletions include/asm-arm/page.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
#define PAGE_SIZE (1UL << PAGE_SHIFT)
#define PAGE_MASK (~(PAGE_SIZE-1))

/* to align the pointer to the (next) page boundary */
#define PAGE_ALIGN(addr) (((addr)+PAGE_SIZE-1)&PAGE_MASK)

#ifndef __ASSEMBLY__

#ifndef CONFIG_MMU
Expand Down
3 changes: 0 additions & 3 deletions include/asm-avr32/page.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ static inline int get_order(unsigned long size)

#endif /* !__ASSEMBLY__ */

/* Align the pointer to the (next) page boundary */
#define PAGE_ALIGN(addr) (((addr) + PAGE_SIZE - 1) & PAGE_MASK)

/*
* The hardware maps the virtual addresses 0x80000000 -> 0x9fffffff
* permanently to the physical addresses 0x00000000 -> 0x1fffffff when
Expand Down
3 changes: 0 additions & 3 deletions include/asm-blackfin/page.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@ typedef struct page *pgtable_t;
#define __pgd(x) ((pgd_t) { (x) } )
#define __pgprot(x) ((pgprot_t) { (x) } )

/* to align the pointer to the (next) page boundary */
#define PAGE_ALIGN(addr) (((addr)+PAGE_SIZE-1)&PAGE_MASK)

extern unsigned long memory_start;
extern unsigned long memory_end;

Expand Down
3 changes: 0 additions & 3 deletions include/asm-cris/page.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@ typedef struct page *pgtable_t;

#define page_to_phys(page) __pa((((page) - mem_map) << PAGE_SHIFT) + PAGE_OFFSET)

/* to align the pointer to the (next) page boundary */
#define PAGE_ALIGN(addr) (((addr)+PAGE_SIZE-1)&PAGE_MASK)

#ifndef __ASSEMBLY__

#endif /* __ASSEMBLY__ */
Expand Down
3 changes: 0 additions & 3 deletions include/asm-frv/page.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ typedef struct page *pgtable_t;
#define __pgprot(x) ((pgprot_t) { (x) } )
#define PTE_MASK PAGE_MASK

/* to align the pointer to the (next) page boundary */
#define PAGE_ALIGN(addr) (((addr) + PAGE_SIZE - 1) & PAGE_MASK)

#define devmem_is_allowed(pfn) 1

#define __pa(vaddr) virt_to_phys((void *) (unsigned long) (vaddr))
Expand Down
3 changes: 0 additions & 3 deletions include/asm-h8300/page.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ typedef struct page *pgtable_t;
#define __pgd(x) ((pgd_t) { (x) } )
#define __pgprot(x) ((pgprot_t) { (x) } )

/* to align the pointer to the (next) page boundary */
#define PAGE_ALIGN(addr) (((addr)+PAGE_SIZE-1)&PAGE_MASK)

extern unsigned long memory_start;
extern unsigned long memory_end;

Expand Down
1 change: 0 additions & 1 deletion include/asm-ia64/page.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@

#define PAGE_SIZE (__IA64_UL_CONST(1) << PAGE_SHIFT)
#define PAGE_MASK (~(PAGE_SIZE - 1))
#define PAGE_ALIGN(addr) (((addr) + PAGE_SIZE - 1) & PAGE_MASK)

#define PERCPU_PAGE_SHIFT 16 /* log2() of max. size of per-CPU area */
#define PERCPU_PAGE_SIZE (__IA64_UL_CONST(1) << PERCPU_PAGE_SHIFT)
Expand Down
3 changes: 0 additions & 3 deletions include/asm-m32r/page.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ typedef struct page *pgtable_t;

#endif /* !__ASSEMBLY__ */

/* to align the pointer to the (next) page boundary */
#define PAGE_ALIGN(addr) (((addr) + PAGE_SIZE - 1) & PAGE_MASK)

/*
* This handles the memory map.. We could make this a config
* option, but too many people screw it up, and too few need
Expand Down
2 changes: 1 addition & 1 deletion include/asm-m68k/dvma.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#define DVMA_PAGE_SHIFT 13
#define DVMA_PAGE_SIZE (1UL << DVMA_PAGE_SHIFT)
#define DVMA_PAGE_MASK (~(DVMA_PAGE_SIZE-1))
#define DVMA_PAGE_ALIGN(addr) (((addr)+DVMA_PAGE_SIZE-1)&DVMA_PAGE_MASK)
#define DVMA_PAGE_ALIGN(addr) ALIGN(addr, DVMA_PAGE_SIZE)

extern void dvma_init(void);
extern int dvma_map_iommu(unsigned long kaddr, unsigned long baddr,
Expand Down
3 changes: 0 additions & 3 deletions include/asm-m68k/page.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,6 @@ typedef struct page *pgtable_t;
#define __pgd(x) ((pgd_t) { (x) } )
#define __pgprot(x) ((pgprot_t) { (x) } )

/* to align the pointer to the (next) page boundary */
#define PAGE_ALIGN(addr) (((addr)+PAGE_SIZE-1)&PAGE_MASK)

#endif /* !__ASSEMBLY__ */

#include <asm/page_offset.h>
Expand Down
3 changes: 0 additions & 3 deletions include/asm-m68knommu/page.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ typedef struct page *pgtable_t;
#define __pgd(x) ((pgd_t) { (x) } )
#define __pgprot(x) ((pgprot_t) { (x) } )

/* to align the pointer to the (next) page boundary */
#define PAGE_ALIGN(addr) (((addr)+PAGE_SIZE-1)&PAGE_MASK)

extern unsigned long memory_start;
extern unsigned long memory_end;

Expand Down
3 changes: 0 additions & 3 deletions include/asm-mips/page.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,6 @@ typedef struct { unsigned long pgprot; } pgprot_t;

#endif /* !__ASSEMBLY__ */

/* to align the pointer to the (next) page boundary */
#define PAGE_ALIGN(addr) (((addr) + PAGE_SIZE - 1) & PAGE_MASK)

/*
* __pa()/__va() should be used only during mem init.
*/
Expand Down
2 changes: 1 addition & 1 deletion include/asm-mips/processor.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ extern unsigned int vced_count, vcei_count;
* This decides where the kernel will search for a free chunk of vm
* space during mmap's.
*/
#define TASK_UNMAPPED_BASE (PAGE_ALIGN(TASK_SIZE / 3))
#define TASK_UNMAPPED_BASE ((TASK_SIZE / 3) & ~(PAGE_SIZE))
#endif

#ifdef CONFIG_64BIT
Expand Down
3 changes: 0 additions & 3 deletions include/asm-mn10300/page.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,6 @@ typedef struct page *pgtable_t;

#endif /* !__ASSEMBLY__ */

/* to align the pointer to the (next) page boundary */
#define PAGE_ALIGN(addr) (((addr) + PAGE_SIZE - 1) & PAGE_MASK)

/*
* This handles the memory map.. We could make this a config
* option, but too many people screw it up, and too few need
Expand Down
Loading

0 comments on commit 27ac792

Please sign in to comment.