summaryrefslogtreecommitdiffstats
path: root/mm/swapfile.c
diff options
context:
space:
mode:
authorAndi Kleen <ak@linux.intel.com>2018-06-13 18:48:28 -0400
committerThomas Gleixner <tglx@linutronix.de>2018-06-20 13:10:01 -0400
commit377eeaa8e11fe815b1d07c81c4a0e2843a8c15eb (patch)
tree05848bf81eb28216c7a95870d8658d6f0151cccf /mm/swapfile.c
parent42e4089c7890725fcd329999252dc489b72f2921 (diff)
x86/speculation/l1tf: Limit swap file size to MAX_PA/2
For the L1TF workaround its necessary to limit the swap file size to below MAX_PA/2, so that the higher bits of the swap offset inverted never point to valid memory. Add a mechanism for the architecture to override the swap file size check in swapfile.c and add a x86 specific max swapfile check function that enforces that limit. The check is only enabled if the CPU is vulnerable to L1TF. In VMs with 42bit MAX_PA the typical limit is 2TB now, on a native system with 46bit PA it is 32TB. The limit is only per individual swap file, so it's always possible to exceed these limits with multiple swap files or partitions. Signed-off-by: Andi Kleen <ak@linux.intel.com> Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Reviewed-by: Josh Poimboeuf <jpoimboe@redhat.com> Acked-by: Michal Hocko <mhocko@suse.com> Acked-by: Dave Hansen <dave.hansen@intel.com>
Diffstat (limited to 'mm/swapfile.c')
-rw-r--r--mm/swapfile.c46
1 files changed, 30 insertions, 16 deletions
diff --git a/mm/swapfile.c b/mm/swapfile.c
index 2cc2972eedaf..18185ae4f223 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -2909,6 +2909,35 @@ static int claim_swapfile(struct swap_info_struct *p, struct inode *inode)
2909 return 0; 2909 return 0;
2910} 2910}
2911 2911
2912
2913/*
2914 * Find out how many pages are allowed for a single swap device. There
2915 * are two limiting factors:
2916 * 1) the number of bits for the swap offset in the swp_entry_t type, and
2917 * 2) the number of bits in the swap pte, as defined by the different
2918 * architectures.
2919 *
2920 * In order to find the largest possible bit mask, a swap entry with
2921 * swap type 0 and swap offset ~0UL is created, encoded to a swap pte,
2922 * decoded to a swp_entry_t again, and finally the swap offset is
2923 * extracted.
2924 *
2925 * This will mask all the bits from the initial ~0UL mask that can't
2926 * be encoded in either the swp_entry_t or the architecture definition
2927 * of a swap pte.
2928 */
2929unsigned long generic_max_swapfile_size(void)
2930{
2931 return swp_offset(pte_to_swp_entry(
2932 swp_entry_to_pte(swp_entry(0, ~0UL)))) + 1;
2933}
2934
2935/* Can be overridden by an architecture for additional checks. */
2936__weak unsigned long max_swapfile_size(void)
2937{
2938 return generic_max_swapfile_size();
2939}
2940
2912static unsigned long read_swap_header(struct swap_info_struct *p, 2941static unsigned long read_swap_header(struct swap_info_struct *p,
2913 union swap_header *swap_header, 2942 union swap_header *swap_header,
2914 struct inode *inode) 2943 struct inode *inode)
@@ -2944,22 +2973,7 @@ static unsigned long read_swap_header(struct swap_info_struct *p,
2944 p->cluster_next = 1; 2973 p->cluster_next = 1;
2945 p->cluster_nr = 0; 2974 p->cluster_nr = 0;
2946 2975
2947 /* 2976 maxpages = max_swapfile_size();
2948 * Find out how many pages are allowed for a single swap
2949 * device. There are two limiting factors: 1) the number
2950 * of bits for the swap offset in the swp_entry_t type, and
2951 * 2) the number of bits in the swap pte as defined by the
2952 * different architectures. In order to find the
2953 * largest possible bit mask, a swap entry with swap type 0
2954 * and swap offset ~0UL is created, encoded to a swap pte,
2955 * decoded to a swp_entry_t again, and finally the swap
2956 * offset is extracted. This will mask all the bits from
2957 * the initial ~0UL mask that can't be encoded in either
2958 * the swp_entry_t or the architecture definition of a
2959 * swap pte.
2960 */
2961 maxpages = swp_offset(pte_to_swp_entry(
2962 swp_entry_to_pte(swp_entry(0, ~0UL)))) + 1;
2963 last_page = swap_header->info.last_page; 2977 last_page = swap_header->info.last_page;
2964 if (!last_page) { 2978 if (!last_page) {
2965 pr_warn("Empty swap-file\n"); 2979 pr_warn("Empty swap-file\n");