Use unified pte_bitop helper to manipulate bits in pte/pgoff bitfield,
and convert pte_to_pgoff/pgoff_to_pte to inlines.

Signed-off-by: Cyrill Gorcunov <gorcu...@openvz.org>
Cc: Andrew Morton <a...@linux-foundation.org>
Cc: Pavel Emelyanov <xe...@parallels.com>
Cc: Andy Lutomirski <l...@amacapital.net>
Cc: Ingo Molnar <mi...@elte.hu>
Cc: "H. Peter Anvin" <h...@zytor.com>
Cc: Thomas Gleixner <t...@linutronix.de>
---
 arch/x86/include/asm/pgtable-2level.h |  100 ++++++++++++++++++++--------------
 1 file changed, 59 insertions(+), 41 deletions(-)

Index: linux-2.6.git/arch/x86/include/asm/pgtable-2level.h
===================================================================
--- linux-2.6.git.orig/arch/x86/include/asm/pgtable-2level.h
+++ linux-2.6.git/arch/x86/include/asm/pgtable-2level.h
@@ -55,6 +55,13 @@ static inline pmd_t native_pmdp_get_and_
 #define native_pmdp_get_and_clear(xp) native_local_pmdp_get_and_clear(xp)
 #endif
 
+/* Bit manipulation helper on pte/pgoff entry */
+static inline unsigned long pte_bitop(unsigned long value, unsigned int 
rightshift,
+                                     unsigned long mask, unsigned int 
leftshift)
+{
+       return ((value >> rightshift) & mask) << leftshift;
+}
+
 #ifdef CONFIG_MEM_SOFT_DIRTY
 
 /*
@@ -71,31 +78,34 @@ static inline pmd_t native_pmdp_get_and_
 #define PTE_FILE_BITS2         (PTE_FILE_SHIFT3 - PTE_FILE_SHIFT2 - 1)
 #define PTE_FILE_BITS3         (PTE_FILE_SHIFT4 - PTE_FILE_SHIFT3 - 1)
 
-#define pte_to_pgoff(pte)                                              \
-       ((((pte).pte_low >> (PTE_FILE_SHIFT1))                          \
-         & ((1U << PTE_FILE_BITS1) - 1)))                              \
-       + ((((pte).pte_low >> (PTE_FILE_SHIFT2))                        \
-           & ((1U << PTE_FILE_BITS2) - 1))                             \
-          << (PTE_FILE_BITS1))                                         \
-       + ((((pte).pte_low >> (PTE_FILE_SHIFT3))                        \
-           & ((1U << PTE_FILE_BITS3) - 1))                             \
-          << (PTE_FILE_BITS1 + PTE_FILE_BITS2))                        \
-       + ((((pte).pte_low >> (PTE_FILE_SHIFT4)))                       \
-           << (PTE_FILE_BITS1 + PTE_FILE_BITS2 + PTE_FILE_BITS3))
-
-#define pgoff_to_pte(off)                                              \
-       ((pte_t) { .pte_low =                                           \
-        ((((off)) & ((1U << PTE_FILE_BITS1) - 1)) << PTE_FILE_SHIFT1)  \
-        + ((((off) >> PTE_FILE_BITS1)                                  \
-            & ((1U << PTE_FILE_BITS2) - 1))                            \
-           << PTE_FILE_SHIFT2)                                         \
-        + ((((off) >> (PTE_FILE_BITS1 + PTE_FILE_BITS2))               \
-            & ((1U << PTE_FILE_BITS3) - 1))                            \
-           << PTE_FILE_SHIFT3)                                         \
-        + ((((off) >>                                                  \
-             (PTE_FILE_BITS1 + PTE_FILE_BITS2 + PTE_FILE_BITS3)))      \
-           << PTE_FILE_SHIFT4)                                         \
-        + _PAGE_FILE })
+#define PTE_FILE_MASK1         ((1U << PTE_FILE_BITS1) - 1)
+#define PTE_FILE_MASK2         ((1U << PTE_FILE_BITS2) - 1)
+#define PTE_FILE_MASK3         ((1U << PTE_FILE_BITS3) - 1)
+
+#define PTE_FILE_LSHIFT2       (PTE_FILE_BITS1)
+#define PTE_FILE_LSHIFT3       (PTE_FILE_BITS1 + PTE_FILE_BITS2)
+#define PTE_FILE_LSHIFT4       (PTE_FILE_BITS1 + PTE_FILE_BITS2 + 
PTE_FILE_BITS3)
+
+static __always_inline pgoff_t pte_to_pgoff(pte_t pte)
+{
+       return (pgoff_t)
+               (pte_bitop(pte.pte_low, PTE_FILE_SHIFT1, PTE_FILE_MASK1,  0)    
            +
+                pte_bitop(pte.pte_low, PTE_FILE_SHIFT2, PTE_FILE_MASK2,  
PTE_FILE_LSHIFT2) +
+                pte_bitop(pte.pte_low, PTE_FILE_SHIFT3, PTE_FILE_MASK3,  
PTE_FILE_LSHIFT3) +
+                pte_bitop(pte.pte_low, PTE_FILE_SHIFT4,           -1UL,  
PTE_FILE_LSHIFT4));
+}
+
+static __always_inline pte_t pgoff_to_pte(pgoff_t off)
+{
+       return (pte_t){
+               .pte_low =
+                       pte_bitop(off,                0, PTE_FILE_MASK1,  
PTE_FILE_SHIFT1) +
+                       pte_bitop(off, PTE_FILE_LSHIFT2, PTE_FILE_MASK2,  
PTE_FILE_SHIFT2) +
+                       pte_bitop(off, PTE_FILE_LSHIFT3, PTE_FILE_MASK3,  
PTE_FILE_SHIFT3) +
+                       pte_bitop(off, PTE_FILE_LSHIFT4,           -1UL,  
PTE_FILE_SHIFT4) +
+                       _PAGE_FILE,
+       };
+}
 
 #else /* CONFIG_MEM_SOFT_DIRTY */
 
@@ -115,22 +125,30 @@ static inline pmd_t native_pmdp_get_and_
 #define PTE_FILE_BITS1         (PTE_FILE_SHIFT2 - PTE_FILE_SHIFT1 - 1)
 #define PTE_FILE_BITS2         (PTE_FILE_SHIFT3 - PTE_FILE_SHIFT2 - 1)
 
-#define pte_to_pgoff(pte)                                              \
-       ((((pte).pte_low >> PTE_FILE_SHIFT1)                            \
-         & ((1U << PTE_FILE_BITS1) - 1))                               \
-        + ((((pte).pte_low >> PTE_FILE_SHIFT2)                         \
-            & ((1U << PTE_FILE_BITS2) - 1)) << PTE_FILE_BITS1)         \
-        + (((pte).pte_low >> PTE_FILE_SHIFT3)                          \
-           << (PTE_FILE_BITS1 + PTE_FILE_BITS2)))
-
-#define pgoff_to_pte(off)                                              \
-       ((pte_t) { .pte_low =                                           \
-        (((off) & ((1U << PTE_FILE_BITS1) - 1)) << PTE_FILE_SHIFT1)    \
-        + ((((off) >> PTE_FILE_BITS1) & ((1U << PTE_FILE_BITS2) - 1))  \
-           << PTE_FILE_SHIFT2)                                         \
-        + (((off) >> (PTE_FILE_BITS1 + PTE_FILE_BITS2))                \
-           << PTE_FILE_SHIFT3)                                         \
-        + _PAGE_FILE })
+#define PTE_FILE_MASK1         ((1U << PTE_FILE_BITS1) - 1)
+#define PTE_FILE_MASK2         ((1U << PTE_FILE_BITS2) - 1)
+
+#define PTE_FILE_LSHIFT2       (PTE_FILE_BITS1)
+#define PTE_FILE_LSHIFT3       (PTE_FILE_BITS1 + PTE_FILE_BITS2)
+
+static __always_inline pgoff_t pte_to_pgoff(pte_t pte)
+{
+       return (pgoff_t)
+               (pte_bitop(pte.pte_low, PTE_FILE_SHIFT1, PTE_FILE_MASK1,  0)    
            +
+                pte_bitop(pte.pte_low, PTE_FILE_SHIFT2, PTE_FILE_MASK2,  
PTE_FILE_LSHIFT2) +
+                pte_bitop(pte.pte_low, PTE_FILE_SHIFT3,           -1UL,  
PTE_FILE_LSHIFT3));
+}
+
+static __always_inline pte_t pgoff_to_pte(pgoff_t off)
+{
+       return (pte_t){
+               .pte_low =
+                       pte_bitop(off,                0, PTE_FILE_MASK1,  
PTE_FILE_SHIFT1) +
+                       pte_bitop(off, PTE_FILE_LSHIFT2, PTE_FILE_MASK2,  
PTE_FILE_SHIFT2) +
+                       pte_bitop(off, PTE_FILE_LSHIFT3,           -1UL,  
PTE_FILE_SHIFT3) +
+                       _PAGE_FILE,
+       };
+}
 
 #endif /* CONFIG_MEM_SOFT_DIRTY */
 
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Reply via email to