#define NR_RESERVED_PKEYS_4K   27 /* pkey-0, pkey-1, exec-only-pkey
                                      and 24 other keys that cannot be
                                      represented in the PTE */
-#define NR_RESERVED_PKEYS_64K  3  /* pkey-0, pkey-1 and exec-only-pkey */
+#define NR_RESERVED_PKEYS_64K_3KEYS    3 /* PowerNV and KVM: pkey-0,
+                                            pkey-1 and exec-only key */
+#define NR_RESERVED_PKEYS_64K_4KEYS    4 /* PowerVM: pkey-0, pkey-1,
+                                            pkey-31 and exec-only key */
 #define PKEY_BITS_PER_PKEY     2
 #define HPAGE_SIZE             (1UL << 24)
 #define PAGE_SIZE              (1UL << 16)
        return 1;
 }
 
+static inline bool arch_is_powervm()
+{
+       struct stat buf;
+
+       if ((stat("/sys/firmware/devicetree/base/ibm,partition-name", &buf) == 0) &&
+           (stat("/sys/firmware/devicetree/base/hmc-managed?", &buf) == 0) &&
+           (stat("/sys/firmware/devicetree/base/chosen/qemu,graphic-width", &buf) == -1) )
+               return true;
+
+       return false;
+}
+
 static inline int get_arch_reserved_keys(void)
 {
        if (sysconf(_SC_PAGESIZE) == 4096)
                return NR_RESERVED_PKEYS_4K;
        else
-               return NR_RESERVED_PKEYS_64K;
+               if (arch_is_powervm())
+                       return NR_RESERVED_PKEYS_64K_4KEYS;
+               else
+                       return NR_RESERVED_PKEYS_64K_3KEYS;
 }
 
 void expect_fault_on_read_execonly_key(void *p1, int pkey)