diff options
-rw-r--r-- | arch/x86/mm/init.c | 15 | ||||
-rw-r--r-- | include/linux/swapfile.h | 2 | ||||
-rw-r--r-- | mm/swapfile.c | 46 |
3 files changed, 47 insertions, 16 deletions
diff --git a/arch/x86/mm/init.c b/arch/x86/mm/init.c index cee58a972cb2..1b530197d114 100644 --- a/arch/x86/mm/init.c +++ b/arch/x86/mm/init.c | |||
@@ -4,6 +4,8 @@ | |||
4 | #include <linux/swap.h> | 4 | #include <linux/swap.h> |
5 | #include <linux/memblock.h> | 5 | #include <linux/memblock.h> |
6 | #include <linux/bootmem.h> /* for max_low_pfn */ | 6 | #include <linux/bootmem.h> /* for max_low_pfn */ |
7 | #include <linux/swapfile.h> | ||
8 | #include <linux/swapops.h> | ||
7 | 9 | ||
8 | #include <asm/set_memory.h> | 10 | #include <asm/set_memory.h> |
9 | #include <asm/e820/api.h> | 11 | #include <asm/e820/api.h> |
@@ -880,3 +882,16 @@ void update_cache_mode_entry(unsigned entry, enum page_cache_mode cache) | |||
880 | __cachemode2pte_tbl[cache] = __cm_idx2pte(entry); | 882 | __cachemode2pte_tbl[cache] = __cm_idx2pte(entry); |
881 | __pte2cachemode_tbl[entry] = cache; | 883 | __pte2cachemode_tbl[entry] = cache; |
882 | } | 884 | } |
885 | |||
886 | unsigned long max_swapfile_size(void) | ||
887 | { | ||
888 | unsigned long pages; | ||
889 | |||
890 | pages = generic_max_swapfile_size(); | ||
891 | |||
892 | if (boot_cpu_has_bug(X86_BUG_L1TF)) { | ||
893 | /* Limit the swap file size to MAX_PA/2 for L1TF workaround */ | ||
894 | pages = min_t(unsigned long, l1tf_pfn_limit() + 1, pages); | ||
895 | } | ||
896 | return pages; | ||
897 | } | ||
diff --git a/include/linux/swapfile.h b/include/linux/swapfile.h index 06bd7b096167..e06febf62978 100644 --- a/include/linux/swapfile.h +++ b/include/linux/swapfile.h | |||
@@ -10,5 +10,7 @@ extern spinlock_t swap_lock; | |||
10 | extern struct plist_head swap_active_head; | 10 | extern struct plist_head swap_active_head; |
11 | extern struct swap_info_struct *swap_info[]; | 11 | extern struct swap_info_struct *swap_info[]; |
12 | extern int try_to_unuse(unsigned int, bool, unsigned long); | 12 | extern int try_to_unuse(unsigned int, bool, unsigned long); |
13 | extern unsigned long generic_max_swapfile_size(void); | ||
14 | extern unsigned long max_swapfile_size(void); | ||
13 | 15 | ||
14 | #endif /* _LINUX_SWAPFILE_H */ | 16 | #endif /* _LINUX_SWAPFILE_H */ |
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 | */ | ||
2929 | unsigned 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 | |||
2912 | static unsigned long read_swap_header(struct swap_info_struct *p, | 2941 | static 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"); |