Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/s390/linux

Pull s390 updates from Martin Schwidefsky:
"One additional new feature for 4.1, a new PRNG based on SHA-512 for
the zcrypt driver.

Two memory management related changes, the page table reallocation for
KVM is removed, and with file ptes gone the encoding of page table
entries is improved.

And three bug fixes"

* 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/s390/linux:
s390/zcrypt: Introduce new SHA-512 based Pseudo Random Generator.
s390/mm: change swap pte encoding and pgtable cleanup
s390/mm: correct transfer of dirty & young bits in __pmd_to_pte
s390/bpf: add dependency to z196 features
s390/3215: free memory in error path
s390/kvm: remove delayed reallocation of page tables for KVM
kexec: allocate the kexec control page with KEXEC_CONTROL_MEMORY_GFP

+1043 -333
+1 -1
arch/s390/Kconfig
··· 115 115 select HAVE_ARCH_SECCOMP_FILTER 116 116 select HAVE_ARCH_TRACEHOOK 117 117 select HAVE_ARCH_TRANSPARENT_HUGEPAGE 118 - select HAVE_BPF_JIT if PACK_STACK && HAVE_MARCH_Z9_109_FEATURES 118 + select HAVE_BPF_JIT if PACK_STACK && HAVE_MARCH_Z196_FEATURES 119 119 select HAVE_CMPXCHG_DOUBLE 120 120 select HAVE_CMPXCHG_LOCAL 121 121 select HAVE_DEBUG_KMEMLEAK
+91 -31
arch/s390/crypto/crypt_s390.h
··· 3 3 * 4 4 * Support for s390 cryptographic instructions. 5 5 * 6 - * Copyright IBM Corp. 2003, 2007 6 + * Copyright IBM Corp. 2003, 2015 7 7 * Author(s): Thomas Spatzier 8 8 * Jan Glauber (jan.glauber@de.ibm.com) 9 + * Harald Freudenberger (freude@de.ibm.com) 9 10 * 10 11 * This program is free software; you can redistribute it and/or modify it 11 12 * under the terms of the GNU General Public License as published by the Free ··· 29 28 #define CRYPT_S390_MSA 0x1 30 29 #define CRYPT_S390_MSA3 0x2 31 30 #define CRYPT_S390_MSA4 0x4 31 + #define CRYPT_S390_MSA5 0x8 32 32 33 33 /* s390 cryptographic operations */ 34 34 enum crypt_s390_operations { 35 - CRYPT_S390_KM = 0x0100, 36 - CRYPT_S390_KMC = 0x0200, 37 - CRYPT_S390_KIMD = 0x0300, 38 - CRYPT_S390_KLMD = 0x0400, 39 - CRYPT_S390_KMAC = 0x0500, 40 - CRYPT_S390_KMCTR = 0x0600 35 + CRYPT_S390_KM = 0x0100, 36 + CRYPT_S390_KMC = 0x0200, 37 + CRYPT_S390_KIMD = 0x0300, 38 + CRYPT_S390_KLMD = 0x0400, 39 + CRYPT_S390_KMAC = 0x0500, 40 + CRYPT_S390_KMCTR = 0x0600, 41 + CRYPT_S390_PPNO = 0x0700 41 42 }; 42 43 43 44 /* ··· 141 138 KMAC_TDEA_192 = CRYPT_S390_KMAC | 3 142 139 }; 143 140 141 + /* 142 + * function codes for PPNO (PERFORM PSEUDORANDOM NUMBER 143 + * OPERATION) instruction 144 + */ 145 + enum crypt_s390_ppno_func { 146 + PPNO_QUERY = CRYPT_S390_PPNO | 0, 147 + PPNO_SHA512_DRNG_GEN = CRYPT_S390_PPNO | 3, 148 + PPNO_SHA512_DRNG_SEED = CRYPT_S390_PPNO | 0x83 149 + }; 150 + 144 151 /** 145 152 * crypt_s390_km: 146 153 * @func: the function code passed to KM; see crypt_s390_km_func ··· 175 162 int ret; 176 163 177 164 asm volatile( 178 - "0: .insn rre,0xb92e0000,%3,%1 \n" /* KM opcode */ 179 - "1: brc 1,0b \n" /* handle partial completion */ 165 + "0: .insn rre,0xb92e0000,%3,%1\n" /* KM opcode */ 166 + "1: brc 1,0b\n" /* handle partial completion */ 180 167 " la %0,0\n" 181 168 "2:\n" 182 - EX_TABLE(0b,2b) EX_TABLE(1b,2b) 169 + EX_TABLE(0b, 2b) EX_TABLE(1b, 2b) 183 170 : "=d" (ret), "+a" (__src), "+d" (__src_len), "+a" (__dest) 184 171 : "d" (__func), "a" (__param), "0" (-1) : "cc", "memory"); 185 172 if (ret < 0) ··· 211 198 int ret; 212 199 213 200 asm volatile( 214 - "0: .insn rre,0xb92f0000,%3,%1 \n" /* KMC opcode */ 215 - "1: brc 1,0b \n" /* handle partial completion */ 201 + "0: .insn rre,0xb92f0000,%3,%1\n" /* KMC opcode */ 202 + "1: brc 1,0b\n" /* handle partial completion */ 216 203 " la %0,0\n" 217 204 "2:\n" 218 - EX_TABLE(0b,2b) EX_TABLE(1b,2b) 205 + EX_TABLE(0b, 2b) EX_TABLE(1b, 2b) 219 206 : "=d" (ret), "+a" (__src), "+d" (__src_len), "+a" (__dest) 220 207 : "d" (__func), "a" (__param), "0" (-1) : "cc", "memory"); 221 208 if (ret < 0) ··· 246 233 int ret; 247 234 248 235 asm volatile( 249 - "0: .insn rre,0xb93e0000,%1,%1 \n" /* KIMD opcode */ 250 - "1: brc 1,0b \n" /* handle partial completion */ 236 + "0: .insn rre,0xb93e0000,%1,%1\n" /* KIMD opcode */ 237 + "1: brc 1,0b\n" /* handle partial completion */ 251 238 " la %0,0\n" 252 239 "2:\n" 253 - EX_TABLE(0b,2b) EX_TABLE(1b,2b) 240 + EX_TABLE(0b, 2b) EX_TABLE(1b, 2b) 254 241 : "=d" (ret), "+a" (__src), "+d" (__src_len) 255 242 : "d" (__func), "a" (__param), "0" (-1) : "cc", "memory"); 256 243 if (ret < 0) ··· 280 267 int ret; 281 268 282 269 asm volatile( 283 - "0: .insn rre,0xb93f0000,%1,%1 \n" /* KLMD opcode */ 284 - "1: brc 1,0b \n" /* handle partial completion */ 270 + "0: .insn rre,0xb93f0000,%1,%1\n" /* KLMD opcode */ 271 + "1: brc 1,0b\n" /* handle partial completion */ 285 272 " la %0,0\n" 286 273 "2:\n" 287 - EX_TABLE(0b,2b) EX_TABLE(1b,2b) 274 + EX_TABLE(0b, 2b) EX_TABLE(1b, 2b) 288 275 : "=d" (ret), "+a" (__src), "+d" (__src_len) 289 276 : "d" (__func), "a" (__param), "0" (-1) : "cc", "memory"); 290 277 if (ret < 0) ··· 315 302 int ret; 316 303 317 304 asm volatile( 318 - "0: .insn rre,0xb91e0000,%1,%1 \n" /* KLAC opcode */ 319 - "1: brc 1,0b \n" /* handle partial completion */ 305 + "0: .insn rre,0xb91e0000,%1,%1\n" /* KLAC opcode */ 306 + "1: brc 1,0b\n" /* handle partial completion */ 320 307 " la %0,0\n" 321 308 "2:\n" 322 - EX_TABLE(0b,2b) EX_TABLE(1b,2b) 309 + EX_TABLE(0b, 2b) EX_TABLE(1b, 2b) 323 310 : "=d" (ret), "+a" (__src), "+d" (__src_len) 324 311 : "d" (__func), "a" (__param), "0" (-1) : "cc", "memory"); 325 312 if (ret < 0) ··· 353 340 int ret = -1; 354 341 355 342 asm volatile( 356 - "0: .insn rrf,0xb92d0000,%3,%1,%4,0 \n" /* KMCTR opcode */ 357 - "1: brc 1,0b \n" /* handle partial completion */ 343 + "0: .insn rrf,0xb92d0000,%3,%1,%4,0\n" /* KMCTR opcode */ 344 + "1: brc 1,0b\n" /* handle partial completion */ 358 345 " la %0,0\n" 359 346 "2:\n" 360 - EX_TABLE(0b,2b) EX_TABLE(1b,2b) 347 + EX_TABLE(0b, 2b) EX_TABLE(1b, 2b) 361 348 : "+d" (ret), "+a" (__src), "+d" (__src_len), "+a" (__dest), 362 349 "+a" (__ctr) 363 350 : "d" (__func), "a" (__param) : "cc", "memory"); 364 351 if (ret < 0) 365 352 return ret; 366 353 return (func & CRYPT_S390_FUNC_MASK) ? src_len - __src_len : __src_len; 354 + } 355 + 356 + /** 357 + * crypt_s390_ppno: 358 + * @func: the function code passed to PPNO; see crypt_s390_ppno_func 359 + * @param: address of parameter block; see POP for details on each func 360 + * @dest: address of destination memory area 361 + * @dest_len: size of destination memory area in bytes 362 + * @seed: address of seed data 363 + * @seed_len: size of seed data in bytes 364 + * 365 + * Executes the PPNO (PERFORM PSEUDORANDOM NUMBER OPERATION) 366 + * operation of the CPU. 367 + * 368 + * Returns -1 for failure, 0 for the query func, number of random 369 + * bytes stored in dest buffer for generate function 370 + */ 371 + static inline int crypt_s390_ppno(long func, void *param, 372 + u8 *dest, long dest_len, 373 + const u8 *seed, long seed_len) 374 + { 375 + register long __func asm("0") = func & CRYPT_S390_FUNC_MASK; 376 + register void *__param asm("1") = param; /* param block (240 bytes) */ 377 + register u8 *__dest asm("2") = dest; /* buf for recv random bytes */ 378 + register long __dest_len asm("3") = dest_len; /* requested random bytes */ 379 + register const u8 *__seed asm("4") = seed; /* buf with seed data */ 380 + register long __seed_len asm("5") = seed_len; /* bytes in seed buf */ 381 + int ret = -1; 382 + 383 + asm volatile ( 384 + "0: .insn rre,0xb93c0000,%1,%5\n" /* PPNO opcode */ 385 + "1: brc 1,0b\n" /* handle partial completion */ 386 + " la %0,0\n" 387 + "2:\n" 388 + EX_TABLE(0b, 2b) EX_TABLE(1b, 2b) 389 + : "+d" (ret), "+a"(__dest), "+d"(__dest_len) 390 + : "d"(__func), "a"(__param), "a"(__seed), "d"(__seed_len) 391 + : "cc", "memory"); 392 + if (ret < 0) 393 + return ret; 394 + return (func & CRYPT_S390_FUNC_MASK) ? dest_len - __dest_len : 0; 367 395 } 368 396 369 397 /** ··· 427 373 return 0; 428 374 if (facility_mask & CRYPT_S390_MSA4 && !test_facility(77)) 429 375 return 0; 376 + if (facility_mask & CRYPT_S390_MSA5 && !test_facility(57)) 377 + return 0; 378 + 430 379 switch (func & CRYPT_S390_OP_MASK) { 431 380 case CRYPT_S390_KM: 432 381 ret = crypt_s390_km(KM_QUERY, &status, NULL, NULL, 0); ··· 447 390 ret = crypt_s390_kmac(KMAC_QUERY, &status, NULL, 0); 448 391 break; 449 392 case CRYPT_S390_KMCTR: 450 - ret = crypt_s390_kmctr(KMCTR_QUERY, &status, NULL, NULL, 0, 451 - NULL); 393 + ret = crypt_s390_kmctr(KMCTR_QUERY, &status, 394 + NULL, NULL, 0, NULL); 395 + break; 396 + case CRYPT_S390_PPNO: 397 + ret = crypt_s390_ppno(PPNO_QUERY, &status, 398 + NULL, 0, NULL, 0); 452 399 break; 453 400 default: 454 401 return 0; ··· 480 419 int ret = -1; 481 420 482 421 asm volatile( 483 - "0: .insn rre,0xb92c0000,0,0 \n" /* PCC opcode */ 484 - "1: brc 1,0b \n" /* handle partial completion */ 422 + "0: .insn rre,0xb92c0000,0,0\n" /* PCC opcode */ 423 + "1: brc 1,0b\n" /* handle partial completion */ 485 424 " la %0,0\n" 486 425 "2:\n" 487 - EX_TABLE(0b,2b) EX_TABLE(1b,2b) 426 + EX_TABLE(0b, 2b) EX_TABLE(1b, 2b) 488 427 : "+d" (ret) 489 428 : "d" (__func), "a" (__param) : "cc", "memory"); 490 429 return ret; 491 430 } 492 - 493 431 494 432 #endif /* _CRYPTO_ARCH_S390_CRYPT_S390_H */
+783 -75
arch/s390/crypto/prng.c
··· 1 1 /* 2 - * Copyright IBM Corp. 2006, 2007 2 + * Copyright IBM Corp. 2006, 2015 3 3 * Author(s): Jan Glauber <jan.glauber@de.ibm.com> 4 + * Harald Freudenberger <freude@de.ibm.com> 4 5 * Driver for the s390 pseudo random number generator 5 6 */ 7 + 8 + #define KMSG_COMPONENT "prng" 9 + #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt 10 + 6 11 #include <linux/fs.h> 12 + #include <linux/fips.h> 7 13 #include <linux/init.h> 8 14 #include <linux/kernel.h> 15 + #include <linux/device.h> 9 16 #include <linux/miscdevice.h> 10 17 #include <linux/module.h> 11 18 #include <linux/moduleparam.h> 19 + #include <linux/mutex.h> 12 20 #include <linux/random.h> 13 21 #include <linux/slab.h> 14 22 #include <asm/debug.h> 15 23 #include <asm/uaccess.h> 24 + #include <asm/timex.h> 16 25 17 26 #include "crypt_s390.h" 18 27 19 28 MODULE_LICENSE("GPL"); 20 - MODULE_AUTHOR("Jan Glauber <jan.glauber@de.ibm.com>"); 29 + MODULE_AUTHOR("IBM Corporation"); 21 30 MODULE_DESCRIPTION("s390 PRNG interface"); 22 31 23 - static int prng_chunk_size = 256; 24 - module_param(prng_chunk_size, int, S_IRUSR | S_IRGRP | S_IROTH); 32 + 33 + #define PRNG_MODE_AUTO 0 34 + #define PRNG_MODE_TDES 1 35 + #define PRNG_MODE_SHA512 2 36 + 37 + static unsigned int prng_mode = PRNG_MODE_AUTO; 38 + module_param_named(mode, prng_mode, int, 0); 39 + MODULE_PARM_DESC(prng_mode, "PRNG mode: 0 - auto, 1 - TDES, 2 - SHA512"); 40 + 41 + 42 + #define PRNG_CHUNKSIZE_TDES_MIN 8 43 + #define PRNG_CHUNKSIZE_TDES_MAX (64*1024) 44 + #define PRNG_CHUNKSIZE_SHA512_MIN 64 45 + #define PRNG_CHUNKSIZE_SHA512_MAX (64*1024) 46 + 47 + static unsigned int prng_chunk_size = 256; 48 + module_param_named(chunksize, prng_chunk_size, int, 0); 25 49 MODULE_PARM_DESC(prng_chunk_size, "PRNG read chunk size in bytes"); 26 50 27 - static int prng_entropy_limit = 4096; 28 - module_param(prng_entropy_limit, int, S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR); 29 - MODULE_PARM_DESC(prng_entropy_limit, 30 - "PRNG add entropy after that much bytes were produced"); 51 + 52 + #define PRNG_RESEED_LIMIT_TDES 4096 53 + #define PRNG_RESEED_LIMIT_TDES_LOWER 4096 54 + #define PRNG_RESEED_LIMIT_SHA512 100000 55 + #define PRNG_RESEED_LIMIT_SHA512_LOWER 10000 56 + 57 + static unsigned int prng_reseed_limit; 58 + module_param_named(reseed_limit, prng_reseed_limit, int, 0); 59 + MODULE_PARM_DESC(prng_reseed_limit, "PRNG reseed limit"); 60 + 31 61 32 62 /* 33 63 * Any one who considers arithmetical methods of producing random digits is, 34 64 * of course, in a state of sin. -- John von Neumann 35 65 */ 36 66 37 - struct s390_prng_data { 38 - unsigned long count; /* how many bytes were produced */ 39 - char *buf; 67 + static int prng_errorflag; 68 + 69 + #define PRNG_GEN_ENTROPY_FAILED 1 70 + #define PRNG_SELFTEST_FAILED 2 71 + #define PRNG_INSTANTIATE_FAILED 3 72 + #define PRNG_SEED_FAILED 4 73 + #define PRNG_RESEED_FAILED 5 74 + #define PRNG_GEN_FAILED 6 75 + 76 + struct prng_ws_s { 77 + u8 parm_block[32]; 78 + u32 reseed_counter; 79 + u64 byte_counter; 40 80 }; 41 81 42 - static struct s390_prng_data *p; 43 - 44 - /* copied from libica, use a non-zero initial parameter block */ 45 - static unsigned char parm_block[32] = { 46 - 0x0F,0x2B,0x8E,0x63,0x8C,0x8E,0xD2,0x52,0x64,0xB7,0xA0,0x7B,0x75,0x28,0xB8,0xF4, 47 - 0x75,0x5F,0xD2,0xA6,0x8D,0x97,0x11,0xFF,0x49,0xD8,0x23,0xF3,0x7E,0x21,0xEC,0xA0, 82 + struct ppno_ws_s { 83 + u32 res; 84 + u32 reseed_counter; 85 + u64 stream_bytes; 86 + u8 V[112]; 87 + u8 C[112]; 48 88 }; 49 89 50 - static int prng_open(struct inode *inode, struct file *file) 90 + struct prng_data_s { 91 + struct mutex mutex; 92 + union { 93 + struct prng_ws_s prngws; 94 + struct ppno_ws_s ppnows; 95 + }; 96 + u8 *buf; 97 + u32 rest; 98 + u8 *prev; 99 + }; 100 + 101 + static struct prng_data_s *prng_data; 102 + 103 + /* initial parameter block for tdes mode, copied from libica */ 104 + static const u8 initial_parm_block[32] __initconst = { 105 + 0x0F, 0x2B, 0x8E, 0x63, 0x8C, 0x8E, 0xD2, 0x52, 106 + 0x64, 0xB7, 0xA0, 0x7B, 0x75, 0x28, 0xB8, 0xF4, 107 + 0x75, 0x5F, 0xD2, 0xA6, 0x8D, 0x97, 0x11, 0xFF, 108 + 0x49, 0xD8, 0x23, 0xF3, 0x7E, 0x21, 0xEC, 0xA0 }; 109 + 110 + 111 + /*** helper functions ***/ 112 + 113 + static int generate_entropy(u8 *ebuf, size_t nbytes) 51 114 { 52 - return nonseekable_open(inode, file); 115 + int n, ret = 0; 116 + u8 *pg, *h, hash[32]; 117 + 118 + pg = (u8 *) __get_free_page(GFP_KERNEL); 119 + if (!pg) { 120 + prng_errorflag = PRNG_GEN_ENTROPY_FAILED; 121 + return -ENOMEM; 122 + } 123 + 124 + while (nbytes) { 125 + /* fill page with urandom bytes */ 126 + get_random_bytes(pg, PAGE_SIZE); 127 + /* exor page with stckf values */ 128 + for (n = 0; n < sizeof(PAGE_SIZE/sizeof(u64)); n++) { 129 + u64 *p = ((u64 *)pg) + n; 130 + *p ^= get_tod_clock_fast(); 131 + } 132 + n = (nbytes < sizeof(hash)) ? nbytes : sizeof(hash); 133 + if (n < sizeof(hash)) 134 + h = hash; 135 + else 136 + h = ebuf; 137 + /* generate sha256 from this page */ 138 + if (crypt_s390_kimd(KIMD_SHA_256, h, 139 + pg, PAGE_SIZE) != PAGE_SIZE) { 140 + prng_errorflag = PRNG_GEN_ENTROPY_FAILED; 141 + ret = -EIO; 142 + goto out; 143 + } 144 + if (n < sizeof(hash)) 145 + memcpy(ebuf, hash, n); 146 + ret += n; 147 + ebuf += n; 148 + nbytes -= n; 149 + } 150 + 151 + out: 152 + free_page((unsigned long)pg); 153 + return ret; 53 154 } 54 155 55 - static void prng_add_entropy(void) 156 + 157 + /*** tdes functions ***/ 158 + 159 + static void prng_tdes_add_entropy(void) 56 160 { 57 161 __u64 entropy[4]; 58 162 unsigned int i; 59 163 int ret; 60 164 61 165 for (i = 0; i < 16; i++) { 62 - ret = crypt_s390_kmc(KMC_PRNG, parm_block, (char *)entropy, 63 - (char *)entropy, sizeof(entropy)); 166 + ret = crypt_s390_kmc(KMC_PRNG, prng_data->prngws.parm_block, 167 + (char *)entropy, (char *)entropy, 168 + sizeof(entropy)); 64 169 BUG_ON(ret < 0 || ret != sizeof(entropy)); 65 - memcpy(parm_block, entropy, sizeof(entropy)); 170 + memcpy(prng_data->prngws.parm_block, entropy, sizeof(entropy)); 66 171 } 67 172 } 68 173 69 - static void prng_seed(int nbytes) 174 + 175 + static void prng_tdes_seed(int nbytes) 70 176 { 71 177 char buf[16]; 72 178 int i = 0; 73 179 74 - BUG_ON(nbytes > 16); 180 + BUG_ON(nbytes > sizeof(buf)); 181 + 75 182 get_random_bytes(buf, nbytes); 76 183 77 184 /* Add the entropy */ 78 185 while (nbytes >= 8) { 79 - *((__u64 *)parm_block) ^= *((__u64 *)(buf+i)); 80 - prng_add_entropy(); 186 + *((__u64 *)prng_data->prngws.parm_block) ^= *((__u64 *)(buf+i)); 187 + prng_tdes_add_entropy(); 81 188 i += 8; 82 189 nbytes -= 8; 83 190 } 84 - prng_add_entropy(); 191 + prng_tdes_add_entropy(); 192 + prng_data->prngws.reseed_counter = 0; 85 193 } 86 194 87 - static ssize_t prng_read(struct file *file, char __user *ubuf, size_t nbytes, 88 - loff_t *ppos) 89 - { 90 - int chunk, n; 91 - int ret = 0; 92 - int tmp; 93 195 94 - /* nbytes can be arbitrary length, we split it into chunks */ 196 + static int __init prng_tdes_instantiate(void) 197 + { 198 + int datalen; 199 + 200 + pr_debug("prng runs in TDES mode with " 201 + "chunksize=%d and reseed_limit=%u\n", 202 + prng_chunk_size, prng_reseed_limit); 203 + 204 + /* memory allocation, prng_data struct init, mutex init */ 205 + datalen = sizeof(struct prng_data_s) + prng_chunk_size; 206 + prng_data = kzalloc(datalen, GFP_KERNEL); 207 + if (!prng_data) { 208 + prng_errorflag = PRNG_INSTANTIATE_FAILED; 209 + return -ENOMEM; 210 + } 211 + mutex_init(&prng_data->mutex); 212 + prng_data->buf = ((u8 *)prng_data) + sizeof(struct prng_data_s); 213 + memcpy(prng_data->prngws.parm_block, initial_parm_block, 32); 214 + 215 + /* initialize the PRNG, add 128 bits of entropy */ 216 + prng_tdes_seed(16); 217 + 218 + return 0; 219 + } 220 + 221 + 222 + static void prng_tdes_deinstantiate(void) 223 + { 224 + pr_debug("The prng module stopped " 225 + "after running in triple DES mode\n"); 226 + kzfree(prng_data); 227 + } 228 + 229 + 230 + /*** sha512 functions ***/ 231 + 232 + static int __init prng_sha512_selftest(void) 233 + { 234 + /* NIST DRBG testvector for Hash Drbg, Sha-512, Count #0 */ 235 + static const u8 seed[] __initconst = { 236 + 0x6b, 0x50, 0xa7, 0xd8, 0xf8, 0xa5, 0x5d, 0x7a, 237 + 0x3d, 0xf8, 0xbb, 0x40, 0xbc, 0xc3, 0xb7, 0x22, 238 + 0xd8, 0x70, 0x8d, 0xe6, 0x7f, 0xda, 0x01, 0x0b, 239 + 0x03, 0xc4, 0xc8, 0x4d, 0x72, 0x09, 0x6f, 0x8c, 240 + 0x3e, 0xc6, 0x49, 0xcc, 0x62, 0x56, 0xd9, 0xfa, 241 + 0x31, 0xdb, 0x7a, 0x29, 0x04, 0xaa, 0xf0, 0x25 }; 242 + static const u8 V0[] __initconst = { 243 + 0x00, 0xad, 0xe3, 0x6f, 0x9a, 0x01, 0xc7, 0x76, 244 + 0x61, 0x34, 0x35, 0xf5, 0x4e, 0x24, 0x74, 0x22, 245 + 0x21, 0x9a, 0x29, 0x89, 0xc7, 0x93, 0x2e, 0x60, 246 + 0x1e, 0xe8, 0x14, 0x24, 0x8d, 0xd5, 0x03, 0xf1, 247 + 0x65, 0x5d, 0x08, 0x22, 0x72, 0xd5, 0xad, 0x95, 248 + 0xe1, 0x23, 0x1e, 0x8a, 0xa7, 0x13, 0xd9, 0x2b, 249 + 0x5e, 0xbc, 0xbb, 0x80, 0xab, 0x8d, 0xe5, 0x79, 250 + 0xab, 0x5b, 0x47, 0x4e, 0xdd, 0xee, 0x6b, 0x03, 251 + 0x8f, 0x0f, 0x5c, 0x5e, 0xa9, 0x1a, 0x83, 0xdd, 252 + 0xd3, 0x88, 0xb2, 0x75, 0x4b, 0xce, 0x83, 0x36, 253 + 0x57, 0x4b, 0xf1, 0x5c, 0xca, 0x7e, 0x09, 0xc0, 254 + 0xd3, 0x89, 0xc6, 0xe0, 0xda, 0xc4, 0x81, 0x7e, 255 + 0x5b, 0xf9, 0xe1, 0x01, 0xc1, 0x92, 0x05, 0xea, 256 + 0xf5, 0x2f, 0xc6, 0xc6, 0xc7, 0x8f, 0xbc, 0xf4 }; 257 + static const u8 C0[] __initconst = { 258 + 0x00, 0xf4, 0xa3, 0xe5, 0xa0, 0x72, 0x63, 0x95, 259 + 0xc6, 0x4f, 0x48, 0xd0, 0x8b, 0x5b, 0x5f, 0x8e, 260 + 0x6b, 0x96, 0x1f, 0x16, 0xed, 0xbc, 0x66, 0x94, 261 + 0x45, 0x31, 0xd7, 0x47, 0x73, 0x22, 0xa5, 0x86, 262 + 0xce, 0xc0, 0x4c, 0xac, 0x63, 0xb8, 0x39, 0x50, 263 + 0xbf, 0xe6, 0x59, 0x6c, 0x38, 0x58, 0x99, 0x1f, 264 + 0x27, 0xa7, 0x9d, 0x71, 0x2a, 0xb3, 0x7b, 0xf9, 265 + 0xfb, 0x17, 0x86, 0xaa, 0x99, 0x81, 0xaa, 0x43, 266 + 0xe4, 0x37, 0xd3, 0x1e, 0x6e, 0xe5, 0xe6, 0xee, 267 + 0xc2, 0xed, 0x95, 0x4f, 0x53, 0x0e, 0x46, 0x8a, 268 + 0xcc, 0x45, 0xa5, 0xdb, 0x69, 0x0d, 0x81, 0xc9, 269 + 0x32, 0x92, 0xbc, 0x8f, 0x33, 0xe6, 0xf6, 0x09, 270 + 0x7c, 0x8e, 0x05, 0x19, 0x0d, 0xf1, 0xb6, 0xcc, 271 + 0xf3, 0x02, 0x21, 0x90, 0x25, 0xec, 0xed, 0x0e }; 272 + static const u8 random[] __initconst = { 273 + 0x95, 0xb7, 0xf1, 0x7e, 0x98, 0x02, 0xd3, 0x57, 274 + 0x73, 0x92, 0xc6, 0xa9, 0xc0, 0x80, 0x83, 0xb6, 275 + 0x7d, 0xd1, 0x29, 0x22, 0x65, 0xb5, 0xf4, 0x2d, 276 + 0x23, 0x7f, 0x1c, 0x55, 0xbb, 0x9b, 0x10, 0xbf, 277 + 0xcf, 0xd8, 0x2c, 0x77, 0xa3, 0x78, 0xb8, 0x26, 278 + 0x6a, 0x00, 0x99, 0x14, 0x3b, 0x3c, 0x2d, 0x64, 279 + 0x61, 0x1e, 0xee, 0xb6, 0x9a, 0xcd, 0xc0, 0x55, 280 + 0x95, 0x7c, 0x13, 0x9e, 0x8b, 0x19, 0x0c, 0x7a, 281 + 0x06, 0x95, 0x5f, 0x2c, 0x79, 0x7c, 0x27, 0x78, 282 + 0xde, 0x94, 0x03, 0x96, 0xa5, 0x01, 0xf4, 0x0e, 283 + 0x91, 0x39, 0x6a, 0xcf, 0x8d, 0x7e, 0x45, 0xeb, 284 + 0xdb, 0xb5, 0x3b, 0xbf, 0x8c, 0x97, 0x52, 0x30, 285 + 0xd2, 0xf0, 0xff, 0x91, 0x06, 0xc7, 0x61, 0x19, 286 + 0xae, 0x49, 0x8e, 0x7f, 0xbc, 0x03, 0xd9, 0x0f, 287 + 0x8e, 0x4c, 0x51, 0x62, 0x7a, 0xed, 0x5c, 0x8d, 288 + 0x42, 0x63, 0xd5, 0xd2, 0xb9, 0x78, 0x87, 0x3a, 289 + 0x0d, 0xe5, 0x96, 0xee, 0x6d, 0xc7, 0xf7, 0xc2, 290 + 0x9e, 0x37, 0xee, 0xe8, 0xb3, 0x4c, 0x90, 0xdd, 291 + 0x1c, 0xf6, 0xa9, 0xdd, 0xb2, 0x2b, 0x4c, 0xbd, 292 + 0x08, 0x6b, 0x14, 0xb3, 0x5d, 0xe9, 0x3d, 0xa2, 293 + 0xd5, 0xcb, 0x18, 0x06, 0x69, 0x8c, 0xbd, 0x7b, 294 + 0xbb, 0x67, 0xbf, 0xe3, 0xd3, 0x1f, 0xd2, 0xd1, 295 + 0xdb, 0xd2, 0xa1, 0xe0, 0x58, 0xa3, 0xeb, 0x99, 296 + 0xd7, 0xe5, 0x1f, 0x1a, 0x93, 0x8e, 0xed, 0x5e, 297 + 0x1c, 0x1d, 0xe2, 0x3a, 0x6b, 0x43, 0x45, 0xd3, 298 + 0x19, 0x14, 0x09, 0xf9, 0x2f, 0x39, 0xb3, 0x67, 299 + 0x0d, 0x8d, 0xbf, 0xb6, 0x35, 0xd8, 0xe6, 0xa3, 300 + 0x69, 0x32, 0xd8, 0x10, 0x33, 0xd1, 0x44, 0x8d, 301 + 0x63, 0xb4, 0x03, 0xdd, 0xf8, 0x8e, 0x12, 0x1b, 302 + 0x6e, 0x81, 0x9a, 0xc3, 0x81, 0x22, 0x6c, 0x13, 303 + 0x21, 0xe4, 0xb0, 0x86, 0x44, 0xf6, 0x72, 0x7c, 304 + 0x36, 0x8c, 0x5a, 0x9f, 0x7a, 0x4b, 0x3e, 0xe2 }; 305 + 306 + int ret = 0; 307 + u8 buf[sizeof(random)]; 308 + struct ppno_ws_s ws; 309 + 310 + memset(&ws, 0, sizeof(ws)); 311 + 312 + /* initial seed */ 313 + ret = crypt_s390_ppno(PPNO_SHA512_DRNG_SEED, 314 + &ws, NULL, 0, 315 + seed, sizeof(seed)); 316 + if (ret < 0) { 317 + pr_err("The prng self test seed operation for the " 318 + "SHA-512 mode failed with rc=%d\n", ret); 319 + prng_errorflag = PRNG_SELFTEST_FAILED; 320 + return -EIO; 321 + } 322 + 323 + /* check working states V and C */ 324 + if (memcmp(ws.V, V0, sizeof(V0)) != 0 325 + || memcmp(ws.C, C0, sizeof(C0)) != 0) { 326 + pr_err("The prng self test state test " 327 + "for the SHA-512 mode failed\n"); 328 + prng_errorflag = PRNG_SELFTEST_FAILED; 329 + return -EIO; 330 + } 331 + 332 + /* generate random bytes */ 333 + ret = crypt_s390_ppno(PPNO_SHA512_DRNG_GEN, 334 + &ws, buf, sizeof(buf), 335 + NULL, 0); 336 + if (ret < 0) { 337 + pr_err("The prng self test generate operation for " 338 + "the SHA-512 mode failed with rc=%d\n", ret); 339 + prng_errorflag = PRNG_SELFTEST_FAILED; 340 + return -EIO; 341 + } 342 + ret = crypt_s390_ppno(PPNO_SHA512_DRNG_GEN, 343 + &ws, buf, sizeof(buf), 344 + NULL, 0); 345 + if (ret < 0) { 346 + pr_err("The prng self test generate operation for " 347 + "the SHA-512 mode failed with rc=%d\n", ret); 348 + prng_errorflag = PRNG_SELFTEST_FAILED; 349 + return -EIO; 350 + } 351 + 352 + /* check against expected data */ 353 + if (memcmp(buf, random, sizeof(random)) != 0) { 354 + pr_err("The prng self test data test " 355 + "for the SHA-512 mode failed\n"); 356 + prng_errorflag = PRNG_SELFTEST_FAILED; 357 + return -EIO; 358 + } 359 + 360 + return 0; 361 + } 362 + 363 + 364 + static int __init prng_sha512_instantiate(void) 365 + { 366 + int ret, datalen; 367 + u8 seed[64]; 368 + 369 + pr_debug("prng runs in SHA-512 mode " 370 + "with chunksize=%d and reseed_limit=%u\n", 371 + prng_chunk_size, prng_reseed_limit); 372 + 373 + /* memory allocation, prng_data struct init, mutex init */ 374 + datalen = sizeof(struct prng_data_s) + prng_chunk_size; 375 + if (fips_enabled) 376 + datalen += prng_chunk_size; 377 + prng_data = kzalloc(datalen, GFP_KERNEL); 378 + if (!prng_data) { 379 + prng_errorflag = PRNG_INSTANTIATE_FAILED; 380 + return -ENOMEM; 381 + } 382 + mutex_init(&prng_data->mutex); 383 + prng_data->buf = ((u8 *)prng_data) + sizeof(struct prng_data_s); 384 + 385 + /* selftest */ 386 + ret = prng_sha512_selftest(); 387 + if (ret) 388 + goto outfree; 389 + 390 + /* generate initial seed bytestring, first 48 bytes of entropy */ 391 + ret = generate_entropy(seed, 48); 392 + if (ret != 48) 393 + goto outfree; 394 + /* followed by 16 bytes of unique nonce */ 395 + get_tod_clock_ext(seed + 48); 396 + 397 + /* initial seed of the ppno drng */ 398 + ret = crypt_s390_ppno(PPNO_SHA512_DRNG_SEED, 399 + &prng_data->ppnows, NULL, 0, 400 + seed, sizeof(seed)); 401 + if (ret < 0) { 402 + prng_errorflag = PRNG_SEED_FAILED; 403 + ret = -EIO; 404 + goto outfree; 405 + } 406 + 407 + /* if fips mode is enabled, generate a first block of random 408 + bytes for the FIPS 140-2 Conditional Self Test */ 409 + if (fips_enabled) { 410 + prng_data->prev = prng_data->buf + prng_chunk_size; 411 + ret = crypt_s390_ppno(PPNO_SHA512_DRNG_GEN, 412 + &prng_data->ppnows, 413 + prng_data->prev, 414 + prng_chunk_size, 415 + NULL, 0); 416 + if (ret < 0 || ret != prng_chunk_size) { 417 + prng_errorflag = PRNG_GEN_FAILED; 418 + ret = -EIO; 419 + goto outfree; 420 + } 421 + } 422 + 423 + return 0; 424 + 425 + outfree: 426 + kfree(prng_data); 427 + return ret; 428 + } 429 + 430 + 431 + static void prng_sha512_deinstantiate(void) 432 + { 433 + pr_debug("The prng module stopped after running in SHA-512 mode\n"); 434 + kzfree(prng_data); 435 + } 436 + 437 + 438 + static int prng_sha512_reseed(void) 439 + { 440 + int ret; 441 + u8 seed[32]; 442 + 443 + /* generate 32 bytes of fresh entropy */ 444 + ret = generate_entropy(seed, sizeof(seed)); 445 + if (ret != sizeof(seed)) 446 + return ret; 447 + 448 + /* do a reseed of the ppno drng with this bytestring */ 449 + ret = crypt_s390_ppno(PPNO_SHA512_DRNG_SEED, 450 + &prng_data->ppnows, NULL, 0, 451 + seed, sizeof(seed)); 452 + if (ret) { 453 + prng_errorflag = PRNG_RESEED_FAILED; 454 + return -EIO; 455 + } 456 + 457 + return 0; 458 + } 459 + 460 + 461 + static int prng_sha512_generate(u8 *buf, size_t nbytes) 462 + { 463 + int ret; 464 + 465 + /* reseed needed ? */ 466 + if (prng_data->ppnows.reseed_counter > prng_reseed_limit) { 467 + ret = prng_sha512_reseed(); 468 + if (ret) 469 + return ret; 470 + } 471 + 472 + /* PPNO generate */ 473 + ret = crypt_s390_ppno(PPNO_SHA512_DRNG_GEN, 474 + &prng_data->ppnows, buf, nbytes, 475 + NULL, 0); 476 + if (ret < 0 || ret != nbytes) { 477 + prng_errorflag = PRNG_GEN_FAILED; 478 + return -EIO; 479 + } 480 + 481 + /* FIPS 140-2 Conditional Self Test */ 482 + if (fips_enabled) { 483 + if (!memcmp(prng_data->prev, buf, nbytes)) { 484 + prng_errorflag = PRNG_GEN_FAILED; 485 + return -EILSEQ; 486 + } 487 + memcpy(prng_data->prev, buf, nbytes); 488 + } 489 + 490 + return ret; 491 + } 492 + 493 + 494 + /*** file io functions ***/ 495 + 496 + static int prng_open(struct inode *inode, struct file *file) 497 + { 498 + return nonseekable_open(inode, file); 499 + } 500 + 501 + 502 + static ssize_t prng_tdes_read(struct file *file, char __user *ubuf, 503 + size_t nbytes, loff_t *ppos) 504 + { 505 + int chunk, n, tmp, ret = 0; 506 + 507 + /* lock prng_data struct */ 508 + if (mutex_lock_interruptible(&prng_data->mutex)) 509 + return -ERESTARTSYS; 510 + 95 511 while (nbytes) { 96 - /* same as in extract_entropy_user in random.c */ 97 512 if (need_resched()) { 98 513 if (signal_pending(current)) { 99 514 if (ret == 0) 100 515 ret = -ERESTARTSYS; 101 516 break; 102 517 } 518 + /* give mutex free before calling schedule() */ 519 + mutex_unlock(&prng_data->mutex); 103 520 schedule(); 521 + /* occopy mutex again */ 522 + if (mutex_lock_interruptible(&prng_data->mutex)) { 523 + if (ret == 0) 524 + ret = -ERESTARTSYS; 525 + return ret; 526 + } 104 527 } 105 528 106 529 /* ··· 535 112 /* PRNG only likes multiples of 8 bytes */ 536 113 n = (chunk + 7) & -8; 537 114 538 - if (p->count > prng_entropy_limit) 539 - prng_seed(8); 115 + if (prng_data->prngws.reseed_counter > prng_reseed_limit) 116 + prng_tdes_seed(8); 540 117 541 118 /* if the CPU supports PRNG stckf is present too */ 542 - asm volatile(".insn s,0xb27c0000,%0" 543 - : "=m" (*((unsigned long long *)p->buf)) : : "cc"); 119 + *((unsigned long long *)prng_data->buf) = get_tod_clock_fast(); 544 120 545 121 /* 546 122 * Beside the STCKF the input for the TDES-EDE is the output ··· 554 132 * Note: you can still get strict X9.17 conformity by setting 555 133 * prng_chunk_size to 8 bytes. 556 134 */ 557 - tmp = crypt_s390_kmc(KMC_PRNG, parm_block, p->buf, p->buf, n); 558 - BUG_ON((tmp < 0) || (tmp != n)); 135 + tmp = crypt_s390_kmc(KMC_PRNG, prng_data->prngws.parm_block, 136 + prng_data->buf, prng_data->buf, n); 137 + if (tmp < 0 || tmp != n) { 138 + ret = -EIO; 139 + break; 140 + } 559 141 560 - p->count += n; 142 + prng_data->prngws.byte_counter += n; 143 + prng_data->prngws.reseed_counter += n; 561 144 562 - if (copy_to_user(ubuf, p->buf, chunk)) 145 + if (copy_to_user(ubuf, prng_data->buf, chunk)) 563 146 return -EFAULT; 564 147 565 148 nbytes -= chunk; 566 149 ret += chunk; 567 150 ubuf += chunk; 568 151 } 152 + 153 + /* unlock prng_data struct */ 154 + mutex_unlock(&prng_data->mutex); 155 + 569 156 return ret; 570 157 } 571 158 572 - static const struct file_operations prng_fops = { 159 + 160 + static ssize_t prng_sha512_read(struct file *file, char __user *ubuf, 161 + size_t nbytes, loff_t *ppos) 162 + { 163 + int n, ret = 0; 164 + u8 *p; 165 + 166 + /* if errorflag is set do nothing and return 'broken pipe' */ 167 + if (prng_errorflag) 168 + return -EPIPE; 169 + 170 + /* lock prng_data struct */ 171 + if (mutex_lock_interruptible(&prng_data->mutex)) 172 + return -ERESTARTSYS; 173 + 174 + while (nbytes) { 175 + if (need_resched()) { 176 + if (signal_pending(current)) { 177 + if (ret == 0) 178 + ret = -ERESTARTSYS; 179 + break; 180 + } 181 + /* give mutex free before calling schedule() */ 182 + mutex_unlock(&prng_data->mutex); 183 + schedule(); 184 + /* occopy mutex again */ 185 + if (mutex_lock_interruptible(&prng_data->mutex)) { 186 + if (ret == 0) 187 + ret = -ERESTARTSYS; 188 + return ret; 189 + } 190 + } 191 + if (prng_data->rest) { 192 + /* push left over random bytes from the previous read */ 193 + p = prng_data->buf + prng_chunk_size - prng_data->rest; 194 + n = (nbytes < prng_data->rest) ? 195 + nbytes : prng_data->rest; 196 + prng_data->rest -= n; 197 + } else { 198 + /* generate one chunk of random bytes into read buf */ 199 + p = prng_data->buf; 200 + n = prng_sha512_generate(p, prng_chunk_size); 201 + if (n < 0) { 202 + ret = n; 203 + break; 204 + } 205 + if (nbytes < prng_chunk_size) { 206 + n = nbytes; 207 + prng_data->rest = prng_chunk_size - n; 208 + } else { 209 + n = prng_chunk_size; 210 + prng_data->rest = 0; 211 + } 212 + } 213 + if (copy_to_user(ubuf, p, n)) { 214 + ret = -EFAULT; 215 + break; 216 + } 217 + ubuf += n; 218 + nbytes -= n; 219 + ret += n; 220 + } 221 + 222 + /* unlock prng_data struct */ 223 + mutex_unlock(&prng_data->mutex); 224 + 225 + return ret; 226 + } 227 + 228 + 229 + /*** sysfs stuff ***/ 230 + 231 + static const struct file_operations prng_sha512_fops = { 573 232 .owner = THIS_MODULE, 574 233 .open = &prng_open, 575 234 .release = NULL, 576 - .read = &prng_read, 235 + .read = &prng_sha512_read, 236 + .llseek = noop_llseek, 237 + }; 238 + static const struct file_operations prng_tdes_fops = { 239 + .owner = THIS_MODULE, 240 + .open = &prng_open, 241 + .release = NULL, 242 + .read = &prng_tdes_read, 577 243 .llseek = noop_llseek, 578 244 }; 579 245 580 - static struct miscdevice prng_dev = { 246 + static struct miscdevice prng_sha512_dev = { 581 247 .name = "prandom", 582 248 .minor = MISC_DYNAMIC_MINOR, 583 - .fops = &prng_fops, 249 + .fops = &prng_sha512_fops, 584 250 }; 251 + static struct miscdevice prng_tdes_dev = { 252 + .name = "prandom", 253 + .minor = MISC_DYNAMIC_MINOR, 254 + .fops = &prng_tdes_fops, 255 + }; 256 + 257 + 258 + /* chunksize attribute (ro) */ 259 + static ssize_t prng_chunksize_show(struct device *dev, 260 + struct device_attribute *attr, 261 + char *buf) 262 + { 263 + return snprintf(buf, PAGE_SIZE, "%u\n", prng_chunk_size); 264 + } 265 + static DEVICE_ATTR(chunksize, 0444, prng_chunksize_show, NULL); 266 + 267 + /* counter attribute (ro) */ 268 + static ssize_t prng_counter_show(struct device *dev, 269 + struct device_attribute *attr, 270 + char *buf) 271 + { 272 + u64 counter; 273 + 274 + if (mutex_lock_interruptible(&prng_data->mutex)) 275 + return -ERESTARTSYS; 276 + if (prng_mode == PRNG_MODE_SHA512) 277 + counter = prng_data->ppnows.stream_bytes; 278 + else 279 + counter = prng_data->prngws.byte_counter; 280 + mutex_unlock(&prng_data->mutex); 281 + 282 + return snprintf(buf, PAGE_SIZE, "%llu\n", counter); 283 + } 284 + static DEVICE_ATTR(byte_counter, 0444, prng_counter_show, NULL); 285 + 286 + /* errorflag attribute (ro) */ 287 + static ssize_t prng_errorflag_show(struct device *dev, 288 + struct device_attribute *attr, 289 + char *buf) 290 + { 291 + return snprintf(buf, PAGE_SIZE, "%d\n", prng_errorflag); 292 + } 293 + static DEVICE_ATTR(errorflag, 0444, prng_errorflag_show, NULL); 294 + 295 + /* mode attribute (ro) */ 296 + static ssize_t prng_mode_show(struct device *dev, 297 + struct device_attribute *attr, 298 + char *buf) 299 + { 300 + if (prng_mode == PRNG_MODE_TDES) 301 + return snprintf(buf, PAGE_SIZE, "TDES\n"); 302 + else 303 + return snprintf(buf, PAGE_SIZE, "SHA512\n"); 304 + } 305 + static DEVICE_ATTR(mode, 0444, prng_mode_show, NULL); 306 + 307 + /* reseed attribute (w) */ 308 + static ssize_t prng_reseed_store(struct device *dev, 309 + struct device_attribute *attr, 310 + const char *buf, size_t count) 311 + { 312 + if (mutex_lock_interruptible(&prng_data->mutex)) 313 + return -ERESTARTSYS; 314 + prng_sha512_reseed(); 315 + mutex_unlock(&prng_data->mutex); 316 + 317 + return count; 318 + } 319 + static DEVICE_ATTR(reseed, 0200, NULL, prng_reseed_store); 320 + 321 + /* reseed limit attribute (rw) */ 322 + static ssize_t prng_reseed_limit_show(struct device *dev, 323 + struct device_attribute *attr, 324 + char *buf) 325 + { 326 + return snprintf(buf, PAGE_SIZE, "%u\n", prng_reseed_limit); 327 + } 328 + static ssize_t prng_reseed_limit_store(struct device *dev, 329 + struct device_attribute *attr, 330 + const char *buf, size_t count) 331 + { 332 + unsigned limit; 333 + 334 + if (sscanf(buf, "%u\n", &limit) != 1) 335 + return -EINVAL; 336 + 337 + if (prng_mode == PRNG_MODE_SHA512) { 338 + if (limit < PRNG_RESEED_LIMIT_SHA512_LOWER) 339 + return -EINVAL; 340 + } else { 341 + if (limit < PRNG_RESEED_LIMIT_TDES_LOWER) 342 + return -EINVAL; 343 + } 344 + 345 + prng_reseed_limit = limit; 346 + 347 + return count; 348 + } 349 + static DEVICE_ATTR(reseed_limit, 0644, 350 + prng_reseed_limit_show, prng_reseed_limit_store); 351 + 352 + /* strength attribute (ro) */ 353 + static ssize_t prng_strength_show(struct device *dev, 354 + struct device_attribute *attr, 355 + char *buf) 356 + { 357 + return snprintf(buf, PAGE_SIZE, "256\n"); 358 + } 359 + static DEVICE_ATTR(strength, 0444, prng_strength_show, NULL); 360 + 361 + static struct attribute *prng_sha512_dev_attrs[] = { 362 + &dev_attr_errorflag.attr, 363 + &dev_attr_chunksize.attr, 364 + &dev_attr_byte_counter.attr, 365 + &dev_attr_mode.attr, 366 + &dev_attr_reseed.attr, 367 + &dev_attr_reseed_limit.attr, 368 + &dev_attr_strength.attr, 369 + NULL 370 + }; 371 + static struct attribute *prng_tdes_dev_attrs[] = { 372 + &dev_attr_chunksize.attr, 373 + &dev_attr_byte_counter.attr, 374 + &dev_attr_mode.attr, 375 + NULL 376 + }; 377 + 378 + static struct attribute_group prng_sha512_dev_attr_group = { 379 + .attrs = prng_sha512_dev_attrs 380 + }; 381 + static struct attribute_group prng_tdes_dev_attr_group = { 382 + .attrs = prng_tdes_dev_attrs 383 + }; 384 + 385 + 386 + /*** module init and exit ***/ 585 387 586 388 static int __init prng_init(void) 587 389 { ··· 815 169 if (!crypt_s390_func_available(KMC_PRNG, CRYPT_S390_MSA)) 816 170 return -EOPNOTSUPP; 817 171 818 - if (prng_chunk_size < 8) 819 - return -EINVAL; 820 - 821 - p = kmalloc(sizeof(struct s390_prng_data), GFP_KERNEL); 822 - if (!p) 823 - return -ENOMEM; 824 - p->count = 0; 825 - 826 - p->buf = kmalloc(prng_chunk_size, GFP_KERNEL); 827 - if (!p->buf) { 828 - ret = -ENOMEM; 829 - goto out_free; 172 + /* choose prng mode */ 173 + if (prng_mode != PRNG_MODE_TDES) { 174 + /* check for MSA5 support for PPNO operations */ 175 + if (!crypt_s390_func_available(PPNO_SHA512_DRNG_GEN, 176 + CRYPT_S390_MSA5)) { 177 + if (prng_mode == PRNG_MODE_SHA512) { 178 + pr_err("The prng module cannot " 179 + "start in SHA-512 mode\n"); 180 + return -EOPNOTSUPP; 181 + } 182 + prng_mode = PRNG_MODE_TDES; 183 + } else 184 + prng_mode = PRNG_MODE_SHA512; 830 185 } 831 186 832 - /* initialize the PRNG, add 128 bits of entropy */ 833 - prng_seed(16); 187 + if (prng_mode == PRNG_MODE_SHA512) { 834 188 835 - ret = misc_register(&prng_dev); 836 - if (ret) 837 - goto out_buf; 838 - return 0; 189 + /* SHA512 mode */ 839 190 840 - out_buf: 841 - kfree(p->buf); 842 - out_free: 843 - kfree(p); 191 + if (prng_chunk_size < PRNG_CHUNKSIZE_SHA512_MIN 192 + || prng_chunk_size > PRNG_CHUNKSIZE_SHA512_MAX) 193 + return -EINVAL; 194 + prng_chunk_size = (prng_chunk_size + 0x3f) & ~0x3f; 195 + 196 + if (prng_reseed_limit == 0) 197 + prng_reseed_limit = PRNG_RESEED_LIMIT_SHA512; 198 + else if (prng_reseed_limit < PRNG_RESEED_LIMIT_SHA512_LOWER) 199 + return -EINVAL; 200 + 201 + ret = prng_sha512_instantiate(); 202 + if (ret) 203 + goto out; 204 + 205 + ret = misc_register(&prng_sha512_dev); 206 + if (ret) { 207 + prng_sha512_deinstantiate(); 208 + goto out; 209 + } 210 + ret = sysfs_create_group(&prng_sha512_dev.this_device->kobj, 211 + &prng_sha512_dev_attr_group); 212 + if (ret) { 213 + misc_deregister(&prng_sha512_dev); 214 + prng_sha512_deinstantiate(); 215 + goto out; 216 + } 217 + 218 + } else { 219 + 220 + /* TDES mode */ 221 + 222 + if (prng_chunk_size < PRNG_CHUNKSIZE_TDES_MIN 223 + || prng_chunk_size > PRNG_CHUNKSIZE_TDES_MAX) 224 + return -EINVAL; 225 + prng_chunk_size = (prng_chunk_size + 0x07) & ~0x07; 226 + 227 + if (prng_reseed_limit == 0) 228 + prng_reseed_limit = PRNG_RESEED_LIMIT_TDES; 229 + else if (prng_reseed_limit < PRNG_RESEED_LIMIT_TDES_LOWER) 230 + return -EINVAL; 231 + 232 + ret = prng_tdes_instantiate(); 233 + if (ret) 234 + goto out; 235 + 236 + ret = misc_register(&prng_tdes_dev); 237 + if (ret) { 238 + prng_tdes_deinstantiate(); 239 + goto out; 240 + } 241 + ret = sysfs_create_group(&prng_tdes_dev.this_device->kobj, 242 + &prng_tdes_dev_attr_group); 243 + if (ret) { 244 + misc_deregister(&prng_tdes_dev); 245 + prng_tdes_deinstantiate(); 246 + goto out; 247 + } 248 + 249 + } 250 + 251 + out: 844 252 return ret; 845 253 } 846 254 255 + 847 256 static void __exit prng_exit(void) 848 257 { 849 - /* wipe me */ 850 - kzfree(p->buf); 851 - kfree(p); 852 - 853 - misc_deregister(&prng_dev); 258 + if (prng_mode == PRNG_MODE_SHA512) { 259 + sysfs_remove_group(&prng_sha512_dev.this_device->kobj, 260 + &prng_sha512_dev_attr_group); 261 + misc_deregister(&prng_sha512_dev); 262 + prng_sha512_deinstantiate(); 263 + } else { 264 + sysfs_remove_group(&prng_tdes_dev.this_device->kobj, 265 + &prng_tdes_dev_attr_group); 266 + misc_deregister(&prng_tdes_dev); 267 + prng_tdes_deinstantiate(); 268 + } 854 269 } 270 + 855 271 856 272 module_init(prng_init); 857 273 module_exit(prng_exit);
+3
arch/s390/include/asm/kexec.h
··· 26 26 /* Not more than 2GB */ 27 27 #define KEXEC_CONTROL_MEMORY_LIMIT (1UL<<31) 28 28 29 + /* Allocate control page with GFP_DMA */ 30 + #define KEXEC_CONTROL_MEMORY_GFP GFP_DMA 31 + 29 32 /* Maximum address we can use for the crash control pages */ 30 33 #define KEXEC_CRASH_CONTROL_MEMORY_LIMIT (-1UL) 31 34
+3 -1
arch/s390/include/asm/mmu.h
··· 14 14 unsigned long asce_bits; 15 15 unsigned long asce_limit; 16 16 unsigned long vdso_base; 17 - /* The mmu context has extended page tables. */ 17 + /* The mmu context allocates 4K page tables. */ 18 + unsigned int alloc_pgste:1; 19 + /* The mmu context uses extended page tables. */ 18 20 unsigned int has_pgste:1; 19 21 /* The mmu context uses storage keys. */ 20 22 unsigned int use_skey:1;
+3
arch/s390/include/asm/mmu_context.h
··· 20 20 mm->context.flush_mm = 0; 21 21 mm->context.asce_bits = _ASCE_TABLE_LENGTH | _ASCE_USER_BITS; 22 22 mm->context.asce_bits |= _ASCE_TYPE_REGION3; 23 + #ifdef CONFIG_PGSTE 24 + mm->context.alloc_pgste = page_table_allocate_pgste; 23 25 mm->context.has_pgste = 0; 24 26 mm->context.use_skey = 0; 27 + #endif 25 28 mm->context.asce_limit = STACK_TOP_MAX; 26 29 crst_table_init((unsigned long *) mm->pgd, pgd_entry_type(mm)); 27 30 return 0;
+1
arch/s390/include/asm/pgalloc.h
··· 21 21 unsigned long *page_table_alloc(struct mm_struct *); 22 22 void page_table_free(struct mm_struct *, unsigned long *); 23 23 void page_table_free_rcu(struct mmu_gather *, unsigned long *, unsigned long); 24 + extern int page_table_allocate_pgste; 24 25 25 26 int set_guest_storage_key(struct mm_struct *mm, unsigned long addr, 26 27 unsigned long key, bool nq);
+72 -95
arch/s390/include/asm/pgtable.h
··· 12 12 #define _ASM_S390_PGTABLE_H 13 13 14 14 /* 15 - * The Linux memory management assumes a three-level page table setup. For 16 - * s390 31 bit we "fold" the mid level into the top-level page table, so 17 - * that we physically have the same two-level page table as the s390 mmu 18 - * expects in 31 bit mode. For s390 64 bit we use three of the five levels 19 - * the hardware provides (region first and region second tables are not 20 - * used). 15 + * The Linux memory management assumes a three-level page table setup. 16 + * For s390 64 bit we use up to four of the five levels the hardware 17 + * provides (region first tables are not used). 21 18 * 22 19 * The "pgd_xxx()" functions are trivial for a folded two-level 23 20 * setup: the pgd is never bad, and a pmd always exists (as it's folded ··· 98 101 99 102 #ifndef __ASSEMBLY__ 100 103 /* 101 - * The vmalloc and module area will always be on the topmost area of the kernel 102 - * mapping. We reserve 96MB (31bit) / 128GB (64bit) for vmalloc and modules. 104 + * The vmalloc and module area will always be on the topmost area of the 105 + * kernel mapping. We reserve 128GB (64bit) for vmalloc and modules. 103 106 * On 64 bit kernels we have a 2GB area at the top of the vmalloc area where 104 107 * modules will reside. That makes sure that inter module branches always 105 108 * happen without trampolines and in addition the placement within a 2GB frame ··· 128 131 } 129 132 130 133 /* 131 - * A 31 bit pagetable entry of S390 has following format: 132 - * | PFRA | | OS | 133 - * 0 0IP0 134 - * 00000000001111111111222222222233 135 - * 01234567890123456789012345678901 136 - * 137 - * I Page-Invalid Bit: Page is not available for address-translation 138 - * P Page-Protection Bit: Store access not possible for page 139 - * 140 - * A 31 bit segmenttable entry of S390 has following format: 141 - * | P-table origin | |PTL 142 - * 0 IC 143 - * 00000000001111111111222222222233 144 - * 01234567890123456789012345678901 145 - * 146 - * I Segment-Invalid Bit: Segment is not available for address-translation 147 - * C Common-Segment Bit: Segment is not private (PoP 3-30) 148 - * PTL Page-Table-Length: Page-table length (PTL+1*16 entries -> up to 256) 149 - * 150 - * The 31 bit segmenttable origin of S390 has following format: 151 - * 152 - * |S-table origin | | STL | 153 - * X **GPS 154 - * 00000000001111111111222222222233 155 - * 01234567890123456789012345678901 156 - * 157 - * X Space-Switch event: 158 - * G Segment-Invalid Bit: * 159 - * P Private-Space Bit: Segment is not private (PoP 3-30) 160 - * S Storage-Alteration: 161 - * STL Segment-Table-Length: Segment-table length (STL+1*16 entries -> up to 2048) 162 - * 163 134 * A 64 bit pagetable entry of S390 has following format: 164 135 * | PFRA |0IPC| OS | 165 136 * 0000000000111111111122222222223333333333444444444455555555556666 ··· 185 220 186 221 /* Software bits in the page table entry */ 187 222 #define _PAGE_PRESENT 0x001 /* SW pte present bit */ 188 - #define _PAGE_TYPE 0x002 /* SW pte type bit */ 189 223 #define _PAGE_YOUNG 0x004 /* SW pte young bit */ 190 224 #define _PAGE_DIRTY 0x008 /* SW pte dirty bit */ 191 225 #define _PAGE_READ 0x010 /* SW pte read bit */ ··· 204 240 * table lock held. 205 241 * 206 242 * The following table gives the different possible bit combinations for 207 - * the pte hardware and software bits in the last 12 bits of a pte: 243 + * the pte hardware and software bits in the last 12 bits of a pte 244 + * (. unassigned bit, x don't care, t swap type): 208 245 * 209 246 * 842100000000 210 247 * 000084210000 211 248 * 000000008421 212 - * .IR...wrdytp 213 - * empty .10...000000 214 - * swap .10...xxxx10 215 - * file .11...xxxxx0 216 - * prot-none, clean, old .11...000001 217 - * prot-none, clean, young .11...000101 218 - * prot-none, dirty, old .10...001001 219 - * prot-none, dirty, young .10...001101 220 - * read-only, clean, old .11...010001 221 - * read-only, clean, young .01...010101 222 - * read-only, dirty, old .11...011001 223 - * read-only, dirty, young .01...011101 224 - * read-write, clean, old .11...110001 225 - * read-write, clean, young .01...110101 226 - * read-write, dirty, old .10...111001 227 - * read-write, dirty, young .00...111101 249 + * .IR.uswrdy.p 250 + * empty .10.00000000 251 + * swap .11..ttttt.0 252 + * prot-none, clean, old .11.xx0000.1 253 + * prot-none, clean, young .11.xx0001.1 254 + * prot-none, dirty, old .10.xx0010.1 255 + * prot-none, dirty, young .10.xx0011.1 256 + * read-only, clean, old .11.xx0100.1 257 + * read-only, clean, young .01.xx0101.1 258 + * read-only, dirty, old .11.xx0110.1 259 + * read-only, dirty, young .01.xx0111.1 260 + * read-write, clean, old .11.xx1100.1 261 + * read-write, clean, young .01.xx1101.1 262 + * read-write, dirty, old .10.xx1110.1 263 + * read-write, dirty, young .00.xx1111.1 264 + * HW-bits: R read-only, I invalid 265 + * SW-bits: p present, y young, d dirty, r read, w write, s special, 266 + * u unused, l large 228 267 * 229 - * pte_present is true for the bit pattern .xx...xxxxx1, (pte & 0x001) == 0x001 230 - * pte_none is true for the bit pattern .10...xxxx00, (pte & 0x603) == 0x400 231 - * pte_swap is true for the bit pattern .10...xxxx10, (pte & 0x603) == 0x402 268 + * pte_none is true for the bit pattern .10.00000000, pte == 0x400 269 + * pte_swap is true for the bit pattern .11..ooooo.0, (pte & 0x201) == 0x200 270 + * pte_present is true for the bit pattern .xx.xxxxxx.1, (pte & 0x001) == 0x001 232 271 */ 233 272 234 273 /* Bits in the segment/region table address-space-control-element */ ··· 302 335 * read-write, dirty, young 11..0...0...11 303 336 * The segment table origin is used to distinguish empty (origin==0) from 304 337 * read-write, old segment table entries (origin!=0) 338 + * HW-bits: R read-only, I invalid 339 + * SW-bits: y young, d dirty, r read, w write 305 340 */ 306 341 307 342 #define _SEGMENT_ENTRY_SPLIT_BIT 11 /* THP splitting bit number */ ··· 387 418 { 388 419 #ifdef CONFIG_PGSTE 389 420 if (unlikely(mm->context.has_pgste)) 421 + return 1; 422 + #endif 423 + return 0; 424 + } 425 + 426 + static inline int mm_alloc_pgste(struct mm_struct *mm) 427 + { 428 + #ifdef CONFIG_PGSTE 429 + if (unlikely(mm->context.alloc_pgste)) 390 430 return 1; 391 431 #endif 392 432 return 0; ··· 560 582 561 583 static inline int pte_swap(pte_t pte) 562 584 { 563 - /* Bit pattern: (pte & 0x603) == 0x402 */ 564 - return (pte_val(pte) & (_PAGE_INVALID | _PAGE_PROTECT | 565 - _PAGE_TYPE | _PAGE_PRESENT)) 566 - == (_PAGE_INVALID | _PAGE_TYPE); 585 + /* Bit pattern: (pte & 0x201) == 0x200 */ 586 + return (pte_val(pte) & (_PAGE_PROTECT | _PAGE_PRESENT)) 587 + == _PAGE_PROTECT; 567 588 } 568 589 569 590 static inline int pte_special(pte_t pte) ··· 1563 1586 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 1564 1587 1565 1588 /* 1566 - * 31 bit swap entry format: 1567 - * A page-table entry has some bits we have to treat in a special way. 1568 - * Bits 0, 20 and bit 23 have to be zero, otherwise an specification 1569 - * exception will occur instead of a page translation exception. The 1570 - * specifiation exception has the bad habit not to store necessary 1571 - * information in the lowcore. 1572 - * Bits 21, 22, 30 and 31 are used to indicate the page type. 1573 - * A swap pte is indicated by bit pattern (pte & 0x603) == 0x402 1574 - * This leaves the bits 1-19 and bits 24-29 to store type and offset. 1575 - * We use the 5 bits from 25-29 for the type and the 20 bits from 1-19 1576 - * plus 24 for the offset. 1577 - * 0| offset |0110|o|type |00| 1578 - * 0 0000000001111111111 2222 2 22222 33 1579 - * 0 1234567890123456789 0123 4 56789 01 1580 - * 1581 1589 * 64 bit swap entry format: 1582 1590 * A page-table entry has some bits we have to treat in a special way. 1583 1591 * Bits 52 and bit 55 have to be zero, otherwise an specification 1584 1592 * exception will occur instead of a page translation exception. The 1585 1593 * specifiation exception has the bad habit not to store necessary 1586 1594 * information in the lowcore. 1587 - * Bits 53, 54, 62 and 63 are used to indicate the page type. 1588 - * A swap pte is indicated by bit pattern (pte & 0x603) == 0x402 1589 - * This leaves the bits 0-51 and bits 56-61 to store type and offset. 1590 - * We use the 5 bits from 57-61 for the type and the 53 bits from 0-51 1591 - * plus 56 for the offset. 1592 - * | offset |0110|o|type |00| 1593 - * 0000000000111111111122222222223333333333444444444455 5555 5 55566 66 1594 - * 0123456789012345678901234567890123456789012345678901 2345 6 78901 23 1595 + * Bits 54 and 63 are used to indicate the page type. 1596 + * A swap pte is indicated by bit pattern (pte & 0x201) == 0x200 1597 + * This leaves the bits 0-51 and bits 56-62 to store type and offset. 1598 + * We use the 5 bits from 57-61 for the type and the 52 bits from 0-51 1599 + * for the offset. 1600 + * | offset |01100|type |00| 1601 + * |0000000000111111111122222222223333333333444444444455|55555|55566|66| 1602 + * |0123456789012345678901234567890123456789012345678901|23456|78901|23| 1595 1603 */ 1596 1604 1597 - #define __SWP_OFFSET_MASK (~0UL >> 11) 1605 + #define __SWP_OFFSET_MASK ((1UL << 52) - 1) 1606 + #define __SWP_OFFSET_SHIFT 12 1607 + #define __SWP_TYPE_MASK ((1UL << 5) - 1) 1608 + #define __SWP_TYPE_SHIFT 2 1598 1609 1599 1610 static inline pte_t mk_swap_pte(unsigned long type, unsigned long offset) 1600 1611 { 1601 1612 pte_t pte; 1602 - offset &= __SWP_OFFSET_MASK; 1603 - pte_val(pte) = _PAGE_INVALID | _PAGE_TYPE | ((type & 0x1f) << 2) | 1604 - ((offset & 1UL) << 7) | ((offset & ~1UL) << 11); 1613 + 1614 + pte_val(pte) = _PAGE_INVALID | _PAGE_PROTECT; 1615 + pte_val(pte) |= (offset & __SWP_OFFSET_MASK) << __SWP_OFFSET_SHIFT; 1616 + pte_val(pte) |= (type & __SWP_TYPE_MASK) << __SWP_TYPE_SHIFT; 1605 1617 return pte; 1606 1618 } 1607 1619 1608 - #define __swp_type(entry) (((entry).val >> 2) & 0x1f) 1609 - #define __swp_offset(entry) (((entry).val >> 11) | (((entry).val >> 7) & 1)) 1610 - #define __swp_entry(type,offset) ((swp_entry_t) { pte_val(mk_swap_pte((type),(offset))) }) 1620 + static inline unsigned long __swp_type(swp_entry_t entry) 1621 + { 1622 + return (entry.val >> __SWP_TYPE_SHIFT) & __SWP_TYPE_MASK; 1623 + } 1624 + 1625 + static inline unsigned long __swp_offset(swp_entry_t entry) 1626 + { 1627 + return (entry.val >> __SWP_OFFSET_SHIFT) & __SWP_OFFSET_MASK; 1628 + } 1629 + 1630 + static inline swp_entry_t __swp_entry(unsigned long type, unsigned long offset) 1631 + { 1632 + return (swp_entry_t) { pte_val(mk_swap_pte(type, offset)) }; 1633 + } 1611 1634 1612 1635 #define __pte_to_swp_entry(pte) ((swp_entry_t) { pte_val(pte) }) 1613 1636 #define __swp_entry_to_pte(x) ((pte_t) { (x).val })
+36 -30
arch/s390/mm/hugetlbpage.c
··· 14 14 15 15 /* 16 16 * Convert encoding pte bits pmd bits 17 - * .IR...wrdytp dy..R...I...wr 18 - * empty .10...000000 -> 00..0...1...00 19 - * prot-none, clean, old .11...000001 -> 00..1...1...00 20 - * prot-none, clean, young .11...000101 -> 01..1...1...00 21 - * prot-none, dirty, old .10...001001 -> 10..1...1...00 22 - * prot-none, dirty, young .10...001101 -> 11..1...1...00 23 - * read-only, clean, old .11...010001 -> 00..1...1...01 24 - * read-only, clean, young .01...010101 -> 01..1...0...01 25 - * read-only, dirty, old .11...011001 -> 10..1...1...01 26 - * read-only, dirty, young .01...011101 -> 11..1...0...01 27 - * read-write, clean, old .11...110001 -> 00..0...1...11 28 - * read-write, clean, young .01...110101 -> 01..0...0...11 29 - * read-write, dirty, old .10...111001 -> 10..0...1...11 30 - * read-write, dirty, young .00...111101 -> 11..0...0...11 17 + * lIR.uswrdy.p dy..R...I...wr 18 + * empty 010.000000.0 -> 00..0...1...00 19 + * prot-none, clean, old 111.000000.1 -> 00..1...1...00 20 + * prot-none, clean, young 111.000001.1 -> 01..1...1...00 21 + * prot-none, dirty, old 111.000010.1 -> 10..1...1...00 22 + * prot-none, dirty, young 111.000011.1 -> 11..1...1...00 23 + * read-only, clean, old 111.000100.1 -> 00..1...1...01 24 + * read-only, clean, young 101.000101.1 -> 01..1...0...01 25 + * read-only, dirty, old 111.000110.1 -> 10..1...1...01 26 + * read-only, dirty, young 101.000111.1 -> 11..1...0...01 27 + * read-write, clean, old 111.001100.1 -> 00..1...1...11 28 + * read-write, clean, young 101.001101.1 -> 01..1...0...11 29 + * read-write, dirty, old 110.001110.1 -> 10..0...1...11 30 + * read-write, dirty, young 100.001111.1 -> 11..0...0...11 31 + * HW-bits: R read-only, I invalid 32 + * SW-bits: p present, y young, d dirty, r read, w write, s special, 33 + * u unused, l large 31 34 */ 32 35 if (pte_present(pte)) { 33 36 pmd_val(pmd) = pte_val(pte) & PAGE_MASK; ··· 51 48 52 49 /* 53 50 * Convert encoding pmd bits pte bits 54 - * dy..R...I...wr .IR...wrdytp 55 - * empty 00..0...1...00 -> .10...001100 56 - * prot-none, clean, old 00..0...1...00 -> .10...000001 57 - * prot-none, clean, young 01..0...1...00 -> .10...000101 58 - * prot-none, dirty, old 10..0...1...00 -> .10...001001 59 - * prot-none, dirty, young 11..0...1...00 -> .10...001101 60 - * read-only, clean, old 00..1...1...01 -> .11...010001 61 - * read-only, clean, young 01..1...1...01 -> .11...010101 62 - * read-only, dirty, old 10..1...1...01 -> .11...011001 63 - * read-only, dirty, young 11..1...1...01 -> .11...011101 64 - * read-write, clean, old 00..0...1...11 -> .10...110001 65 - * read-write, clean, young 01..0...1...11 -> .10...110101 66 - * read-write, dirty, old 10..0...1...11 -> .10...111001 67 - * read-write, dirty, young 11..0...1...11 -> .10...111101 51 + * dy..R...I...wr lIR.uswrdy.p 52 + * empty 00..0...1...00 -> 010.000000.0 53 + * prot-none, clean, old 00..1...1...00 -> 111.000000.1 54 + * prot-none, clean, young 01..1...1...00 -> 111.000001.1 55 + * prot-none, dirty, old 10..1...1...00 -> 111.000010.1 56 + * prot-none, dirty, young 11..1...1...00 -> 111.000011.1 57 + * read-only, clean, old 00..1...1...01 -> 111.000100.1 58 + * read-only, clean, young 01..1...0...01 -> 101.000101.1 59 + * read-only, dirty, old 10..1...1...01 -> 111.000110.1 60 + * read-only, dirty, young 11..1...0...01 -> 101.000111.1 61 + * read-write, clean, old 00..1...1...11 -> 111.001100.1 62 + * read-write, clean, young 01..1...0...11 -> 101.001101.1 63 + * read-write, dirty, old 10..0...1...11 -> 110.001110.1 64 + * read-write, dirty, young 11..0...0...11 -> 100.001111.1 65 + * HW-bits: R read-only, I invalid 66 + * SW-bits: p present, y young, d dirty, r read, w write, s special, 67 + * u unused, l large 68 68 */ 69 69 if (pmd_present(pmd)) { 70 70 pte_val(pte) = pmd_val(pmd) & _SEGMENT_ENTRY_ORIGIN_LARGE; ··· 76 70 pte_val(pte) |= (pmd_val(pmd) & _SEGMENT_ENTRY_WRITE) << 4; 77 71 pte_val(pte) |= (pmd_val(pmd) & _SEGMENT_ENTRY_INVALID) << 5; 78 72 pte_val(pte) |= (pmd_val(pmd) & _SEGMENT_ENTRY_PROTECT); 79 - pmd_val(pmd) |= (pte_val(pte) & _PAGE_DIRTY) << 10; 80 - pmd_val(pmd) |= (pte_val(pte) & _PAGE_YOUNG) << 10; 73 + pte_val(pte) |= (pmd_val(pmd) & _SEGMENT_ENTRY_DIRTY) >> 10; 74 + pte_val(pte) |= (pmd_val(pmd) & _SEGMENT_ENTRY_YOUNG) >> 10; 81 75 } else 82 76 pte_val(pte) = _PAGE_INVALID; 83 77 return pte;
+43 -99
arch/s390/mm/pgtable.c
··· 18 18 #include <linux/rcupdate.h> 19 19 #include <linux/slab.h> 20 20 #include <linux/swapops.h> 21 + #include <linux/sysctl.h> 21 22 #include <linux/ksm.h> 22 23 #include <linux/mman.h> 23 24 ··· 921 920 } 922 921 EXPORT_SYMBOL(get_guest_storage_key); 923 922 923 + static int page_table_allocate_pgste_min = 0; 924 + static int page_table_allocate_pgste_max = 1; 925 + int page_table_allocate_pgste = 0; 926 + EXPORT_SYMBOL(page_table_allocate_pgste); 927 + 928 + static struct ctl_table page_table_sysctl[] = { 929 + { 930 + .procname = "allocate_pgste", 931 + .data = &page_table_allocate_pgste, 932 + .maxlen = sizeof(int), 933 + .mode = S_IRUGO | S_IWUSR, 934 + .proc_handler = proc_dointvec, 935 + .extra1 = &page_table_allocate_pgste_min, 936 + .extra2 = &page_table_allocate_pgste_max, 937 + }, 938 + { } 939 + }; 940 + 941 + static struct ctl_table page_table_sysctl_dir[] = { 942 + { 943 + .procname = "vm", 944 + .maxlen = 0, 945 + .mode = 0555, 946 + .child = page_table_sysctl, 947 + }, 948 + { } 949 + }; 950 + 951 + static int __init page_table_register_sysctl(void) 952 + { 953 + return register_sysctl_table(page_table_sysctl_dir) ? 0 : -ENOMEM; 954 + } 955 + __initcall(page_table_register_sysctl); 956 + 924 957 #else /* CONFIG_PGSTE */ 925 958 926 959 static inline int page_table_with_pgste(struct page *page) ··· 998 963 struct page *uninitialized_var(page); 999 964 unsigned int mask, bit; 1000 965 1001 - if (mm_has_pgste(mm)) 966 + if (mm_alloc_pgste(mm)) 1002 967 return page_table_alloc_pgste(mm); 1003 968 /* Allocate fragments of a 4K page as 1K/2K page table */ 1004 969 spin_lock_bh(&mm->context.list_lock); ··· 1200 1165 } 1201 1166 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 1202 1167 1203 - static unsigned long page_table_realloc_pmd(struct mmu_gather *tlb, 1204 - struct mm_struct *mm, pud_t *pud, 1205 - unsigned long addr, unsigned long end) 1206 - { 1207 - unsigned long next, *table, *new; 1208 - struct page *page; 1209 - spinlock_t *ptl; 1210 - pmd_t *pmd; 1211 - 1212 - pmd = pmd_offset(pud, addr); 1213 - do { 1214 - next = pmd_addr_end(addr, end); 1215 - again: 1216 - if (pmd_none_or_clear_bad(pmd)) 1217 - continue; 1218 - table = (unsigned long *) pmd_deref(*pmd); 1219 - page = pfn_to_page(__pa(table) >> PAGE_SHIFT); 1220 - if (page_table_with_pgste(page)) 1221 - continue; 1222 - /* Allocate new page table with pgstes */ 1223 - new = page_table_alloc_pgste(mm); 1224 - if (!new) 1225 - return -ENOMEM; 1226 - 1227 - ptl = pmd_lock(mm, pmd); 1228 - if (likely((unsigned long *) pmd_deref(*pmd) == table)) { 1229 - /* Nuke pmd entry pointing to the "short" page table */ 1230 - pmdp_flush_lazy(mm, addr, pmd); 1231 - pmd_clear(pmd); 1232 - /* Copy ptes from old table to new table */ 1233 - memcpy(new, table, PAGE_SIZE/2); 1234 - clear_table(table, _PAGE_INVALID, PAGE_SIZE/2); 1235 - /* Establish new table */ 1236 - pmd_populate(mm, pmd, (pte_t *) new); 1237 - /* Free old table with rcu, there might be a walker! */ 1238 - page_table_free_rcu(tlb, table, addr); 1239 - new = NULL; 1240 - } 1241 - spin_unlock(ptl); 1242 - if (new) { 1243 - page_table_free_pgste(new); 1244 - goto again; 1245 - } 1246 - } while (pmd++, addr = next, addr != end); 1247 - 1248 - return addr; 1249 - } 1250 - 1251 - static unsigned long page_table_realloc_pud(struct mmu_gather *tlb, 1252 - struct mm_struct *mm, pgd_t *pgd, 1253 - unsigned long addr, unsigned long end) 1254 - { 1255 - unsigned long next; 1256 - pud_t *pud; 1257 - 1258 - pud = pud_offset(pgd, addr); 1259 - do { 1260 - next = pud_addr_end(addr, end); 1261 - if (pud_none_or_clear_bad(pud)) 1262 - continue; 1263 - next = page_table_realloc_pmd(tlb, mm, pud, addr, next); 1264 - if (unlikely(IS_ERR_VALUE(next))) 1265 - return next; 1266 - } while (pud++, addr = next, addr != end); 1267 - 1268 - return addr; 1269 - } 1270 - 1271 - static unsigned long page_table_realloc(struct mmu_gather *tlb, struct mm_struct *mm, 1272 - unsigned long addr, unsigned long end) 1273 - { 1274 - unsigned long next; 1275 - pgd_t *pgd; 1276 - 1277 - pgd = pgd_offset(mm, addr); 1278 - do { 1279 - next = pgd_addr_end(addr, end); 1280 - if (pgd_none_or_clear_bad(pgd)) 1281 - continue; 1282 - next = page_table_realloc_pud(tlb, mm, pgd, addr, next); 1283 - if (unlikely(IS_ERR_VALUE(next))) 1284 - return next; 1285 - } while (pgd++, addr = next, addr != end); 1286 - 1287 - return 0; 1288 - } 1289 - 1290 1168 /* 1291 1169 * switch on pgstes for its userspace process (for kvm) 1292 1170 */ 1293 1171 int s390_enable_sie(void) 1294 1172 { 1295 - struct task_struct *tsk = current; 1296 - struct mm_struct *mm = tsk->mm; 1297 - struct mmu_gather tlb; 1173 + struct mm_struct *mm = current->mm; 1298 1174 1299 1175 /* Do we have pgstes? if yes, we are done */ 1300 - if (mm_has_pgste(tsk->mm)) 1176 + if (mm_has_pgste(mm)) 1301 1177 return 0; 1302 - 1178 + /* Fail if the page tables are 2K */ 1179 + if (!mm_alloc_pgste(mm)) 1180 + return -EINVAL; 1303 1181 down_write(&mm->mmap_sem); 1182 + mm->context.has_pgste = 1; 1304 1183 /* split thp mappings and disable thp for future mappings */ 1305 1184 thp_split_mm(mm); 1306 - /* Reallocate the page tables with pgstes */ 1307 - tlb_gather_mmu(&tlb, mm, 0, TASK_SIZE); 1308 - if (!page_table_realloc(&tlb, mm, 0, TASK_SIZE)) 1309 - mm->context.has_pgste = 1; 1310 - tlb_finish_mmu(&tlb, 0, TASK_SIZE); 1311 1185 up_write(&mm->mmap_sem); 1312 - return mm->context.has_pgste ? 0 : -ENOMEM; 1186 + return 0; 1313 1187 } 1314 1188 EXPORT_SYMBOL_GPL(s390_enable_sie); 1315 1189
+2
drivers/s390/char/con3215.c
··· 667 667 info->buffer = kzalloc(RAW3215_BUFFER_SIZE, GFP_KERNEL | GFP_DMA); 668 668 info->inbuf = kzalloc(RAW3215_INBUF_SIZE, GFP_KERNEL | GFP_DMA); 669 669 if (!info->buffer || !info->inbuf) { 670 + kfree(info->inbuf); 671 + kfree(info->buffer); 670 672 kfree(info); 671 673 return NULL; 672 674 }
+4
include/linux/kexec.h
··· 40 40 #error KEXEC_CONTROL_MEMORY_LIMIT not defined 41 41 #endif 42 42 43 + #ifndef KEXEC_CONTROL_MEMORY_GFP 44 + #define KEXEC_CONTROL_MEMORY_GFP GFP_KERNEL 45 + #endif 46 + 43 47 #ifndef KEXEC_CONTROL_PAGE_SIZE 44 48 #error KEXEC_CONTROL_PAGE_SIZE not defined 45 49 #endif
+1 -1
kernel/kexec.c
··· 707 707 do { 708 708 unsigned long pfn, epfn, addr, eaddr; 709 709 710 - pages = kimage_alloc_pages(GFP_KERNEL, order); 710 + pages = kimage_alloc_pages(KEXEC_CONTROL_MEMORY_GFP, order); 711 711 if (!pages) 712 712 break; 713 713 pfn = page_to_pfn(pages);