Skip to content

Commit a9ff696

Browse files
committed
ARM: mm: Make virt_to_pfn() a static inline
Making virt_to_pfn() a static inline taking a strongly typed (const void *) makes the contract of a passing a pointer of that type to the function explicit and exposes any misuse of the macro virt_to_pfn() acting polymorphic and accepting many types such as (void *), (unitptr_t) or (unsigned long) as arguments without warnings. Doing this is a bit intrusive: virt_to_pfn() requires PHYS_PFN_OFFSET and PAGE_SHIFT to be defined, and this is defined in <asm/page.h>, so this must be included *before* <asm/memory.h>. The use of macros were obscuring the unclear inclusion order here, as the macros would eventually be resolved, but a static inline like this cannot be compiled with unresolved macros. The naive solution to include <asm/page.h> at the top of <asm/memory.h> does not work, because <asm/memory.h> sometimes includes <asm/page.h> at the end of itself, which would create a confusing inclusion loop. So instead, take the approach to always unconditionally include <asm/page.h> at the end of <asm/memory.h> arch/arm uses <asm/memory.h> explicitly in a lot of places, however it turns out that if we just unconditionally include <asm/memory.h> into <asm/page.h> and switch all inclusions of <asm/memory.h> to <asm/page.h> instead, we enforce the right order and <asm/memory.h> will always have access to the definitions. Put an inclusion guard in place making it impossible to include <asm/memory.h> explicitly. Link: https://lore.kernel.org/linux-mm/20220701160004.2ffff4e5ab59a55499f4c736@linux-foundation.org/ Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
1 parent 2d78057 commit a9ff696

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+54
-55
lines changed

arch/arm/common/sharpsl_param.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include <linux/module.h>
1212
#include <linux/string.h>
1313
#include <asm/mach/sharpsl_param.h>
14-
#include <asm/memory.h>
14+
#include <asm/page.h>
1515

1616
/*
1717
* Certain hardware parameters determined at the time of device manufacture,

arch/arm/include/asm/delay.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#ifndef __ASM_ARM_DELAY_H
88
#define __ASM_ARM_DELAY_H
99

10-
#include <asm/memory.h>
10+
#include <asm/page.h>
1111
#include <asm/param.h> /* HZ */
1212

1313
/*

arch/arm/include/asm/io.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#include <linux/string.h>
2424
#include <linux/types.h>
2525
#include <asm/byteorder.h>
26-
#include <asm/memory.h>
26+
#include <asm/page.h>
2727
#include <asm-generic/pci_iomap.h>
2828

2929
/*

arch/arm/include/asm/memory.h

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@
55
* Copyright (C) 2000-2002 Russell King
66
* modification for nommu, Hyok S. Choi, 2004
77
*
8-
* Note: this file should not be included by non-asm/.h files
8+
* Note: this file should not be included explicitly, include <asm/page.h>
9+
* to get access to these definitions.
910
*/
1011
#ifndef __ASM_ARM_MEMORY_H
1112
#define __ASM_ARM_MEMORY_H
1213

14+
#ifndef _ASMARM_PAGE_H
15+
#error "Do not include <asm/memory.h> directly"
16+
#endif
17+
1318
#include <linux/compiler.h>
1419
#include <linux/const.h>
1520
#include <linux/types.h>
@@ -288,10 +293,12 @@ static inline unsigned long __phys_to_virt(phys_addr_t x)
288293

289294
#endif
290295

291-
#define virt_to_pfn(kaddr) \
292-
((((unsigned long)(kaddr) - PAGE_OFFSET) >> PAGE_SHIFT) + \
293-
PHYS_PFN_OFFSET)
294-
296+
static inline unsigned long virt_to_pfn(const void *p)
297+
{
298+
unsigned long kaddr = (unsigned long)p;
299+
return (((kaddr - PAGE_OFFSET) >> PAGE_SHIFT) +
300+
PHYS_PFN_OFFSET);
301+
}
295302
#define __pa_symbol_nodebug(x) __virt_to_phys_nodebug((x))
296303

297304
#ifdef CONFIG_DEBUG_VIRTUAL

arch/arm/include/asm/page.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,10 @@ extern int pfn_valid(unsigned long);
161161
#define pfn_valid pfn_valid
162162
#endif
163163

164-
#include <asm/memory.h>
165-
166164
#endif /* !__ASSEMBLY__ */
167165

166+
#include <asm/memory.h>
167+
168168
#define VM_DATA_DEFAULT_FLAGS VM_DATA_FLAGS_TSK_EXEC
169169

170170
#include <asm-generic/getorder.h>

arch/arm/include/asm/pgtable.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ extern struct page *empty_zero_page;
2727
#else
2828

2929
#include <asm-generic/pgtable-nopud.h>
30-
#include <asm/memory.h>
30+
#include <asm/page.h>
3131
#include <asm/pgtable-hwdef.h>
3232

3333

arch/arm/include/asm/proc-fns.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,6 @@ static inline void init_proc_vtable(const struct processor *p)
147147

148148
extern void cpu_resume(void);
149149

150-
#include <asm/memory.h>
151-
152150
#ifdef CONFIG_MMU
153151

154152
#define cpu_switch_mm(pgd,mm) cpu_do_switch_mm(virt_to_phys(pgd),mm)

arch/arm/include/asm/sparsemem.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#ifndef ASMARM_SPARSEMEM_H
33
#define ASMARM_SPARSEMEM_H
44

5-
#include <asm/memory.h>
5+
#include <asm/page.h>
66

77
/*
88
* Two definitions are required for sparsemem:

arch/arm/include/asm/uaccess-asm.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
#include <asm/asm-offsets.h>
77
#include <asm/domain.h>
8-
#include <asm/memory.h>
8+
#include <asm/page.h>
99
#include <asm/thread_info.h>
1010

1111
.macro csdb

arch/arm/include/asm/uaccess.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* User space memory access functions
1010
*/
1111
#include <linux/string.h>
12-
#include <asm/memory.h>
12+
#include <asm/page.h>
1313
#include <asm/domain.h>
1414
#include <asm/unaligned.h>
1515
#include <asm/unified.h>

0 commit comments

Comments
 (0)