Skip to content

Commit c51a4f1

Browse files
x-y-zakpm00
authored andcommitted
mm: use clear_user_(high)page() for arch with special user folio handling
Some architectures have special handling after clearing user folios: architectures, which set cpu_dcache_is_aliasing() to true, require flushing dcache; arc, which sets cpu_icache_is_aliasing() to true, changes folio->flags to make icache coherent to dcache. So __GFP_ZERO using only clear_page() is not enough to zero user folios and clear_user_(high)page() must be used. Otherwise, user data will be corrupted. Fix it by always clearing user folios with clear_user_(high)page() when cpu_dcache_is_aliasing() is true or cpu_icache_is_aliasing() is true. Rename alloc_zeroed() to user_alloc_needs_zeroing() and invert the logic to clarify its intend. Link: https://lkml.kernel.org/r/[email protected] Fixes: 5708d96 ("mm: avoid zeroing user movable page twice with init_on_alloc=1") Signed-off-by: Zi Yan <[email protected]> Reported-by: Geert Uytterhoeven <[email protected]> Closes: https://lore.kernel.org/linux-mm/CAMuHMdV1hRp_NtR5YnJo=HsfgKQeH91J537Gh4gKk3PFZhSkbA@mail.gmail.com/ Tested-by: Geert Uytterhoeven <[email protected]> Acked-by: Vlastimil Babka <[email protected]> Cc: Alexander Potapenko <[email protected]> Cc: David Hildenbrand <[email protected]> Cc: John Hubbard <[email protected]> Cc: Kees Cook <[email protected]> Cc: Kefeng Wang <[email protected]> Cc: Mathieu Desnoyers <[email protected]> Cc: Matthew Wilcox <[email protected]> Cc: Miaohe Lin <[email protected]> Cc: Ryan Roberts <[email protected]> Cc: Vineet Gupta <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 5c0541e commit c51a4f1

File tree

5 files changed

+35
-16
lines changed

5 files changed

+35
-16
lines changed

include/linux/highmem.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,13 @@ static inline
224224
struct folio *vma_alloc_zeroed_movable_folio(struct vm_area_struct *vma,
225225
unsigned long vaddr)
226226
{
227-
return vma_alloc_folio(GFP_HIGHUSER_MOVABLE | __GFP_ZERO, 0, vma, vaddr);
227+
struct folio *folio;
228+
229+
folio = vma_alloc_folio(GFP_HIGHUSER_MOVABLE, 0, vma, vaddr);
230+
if (folio && user_alloc_needs_zeroing())
231+
clear_user_highpage(&folio->page, vaddr);
232+
233+
return folio;
228234
}
229235
#endif
230236

include/linux/mm.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <linux/kasan.h>
3232
#include <linux/memremap.h>
3333
#include <linux/slab.h>
34+
#include <linux/cacheinfo.h>
3435

3536
struct mempolicy;
3637
struct anon_vma;
@@ -4184,6 +4185,23 @@ static inline int do_mseal(unsigned long start, size_t len_in, unsigned long fla
41844185
}
41854186
#endif
41864187

4188+
/*
4189+
* user_alloc_needs_zeroing checks if a user folio from page allocator needs to
4190+
* be zeroed or not.
4191+
*/
4192+
static inline bool user_alloc_needs_zeroing(void)
4193+
{
4194+
/*
4195+
* for user folios, arch with cache aliasing requires cache flush and
4196+
* arc changes folio->flags to make icache coherent with dcache, so
4197+
* always return false to make caller use
4198+
* clear_user_page()/clear_user_highpage().
4199+
*/
4200+
return cpu_dcache_is_aliasing() || cpu_icache_is_aliasing() ||
4201+
!static_branch_maybe(CONFIG_INIT_ON_ALLOC_DEFAULT_ON,
4202+
&init_on_alloc);
4203+
}
4204+
41874205
int arch_get_shadow_stack_status(struct task_struct *t, unsigned long __user *status);
41884206
int arch_set_shadow_stack_status(struct task_struct *t, unsigned long status);
41894207
int arch_lock_shadow_stack_status(struct task_struct *t, unsigned long status);

mm/huge_memory.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,11 +1176,12 @@ static struct folio *vma_alloc_anon_folio_pmd(struct vm_area_struct *vma,
11761176
folio_throttle_swaprate(folio, gfp);
11771177

11781178
/*
1179-
* When a folio is not zeroed during allocation (__GFP_ZERO not used),
1180-
* folio_zero_user() is used to make sure that the page corresponding
1181-
* to the faulting address will be hot in the cache after zeroing.
1179+
* When a folio is not zeroed during allocation (__GFP_ZERO not used)
1180+
* or user folios require special handling, folio_zero_user() is used to
1181+
* make sure that the page corresponding to the faulting address will be
1182+
* hot in the cache after zeroing.
11821183
*/
1183-
if (!alloc_zeroed())
1184+
if (user_alloc_needs_zeroing())
11841185
folio_zero_user(folio, addr);
11851186
/*
11861187
* The memory barrier inside __folio_mark_uptodate makes sure that

mm/internal.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,12 +1285,6 @@ void touch_pud(struct vm_area_struct *vma, unsigned long addr,
12851285
void touch_pmd(struct vm_area_struct *vma, unsigned long addr,
12861286
pmd_t *pmd, bool write);
12871287

1288-
static inline bool alloc_zeroed(void)
1289-
{
1290-
return static_branch_maybe(CONFIG_INIT_ON_ALLOC_DEFAULT_ON,
1291-
&init_on_alloc);
1292-
}
1293-
12941288
/*
12951289
* Parses a string with mem suffixes into its order. Useful to parse kernel
12961290
* parameters.

mm/memory.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4733,12 +4733,12 @@ static struct folio *alloc_anon_folio(struct vm_fault *vmf)
47334733
folio_throttle_swaprate(folio, gfp);
47344734
/*
47354735
* When a folio is not zeroed during allocation
4736-
* (__GFP_ZERO not used), folio_zero_user() is used
4737-
* to make sure that the page corresponding to the
4738-
* faulting address will be hot in the cache after
4739-
* zeroing.
4736+
* (__GFP_ZERO not used) or user folios require special
4737+
* handling, folio_zero_user() is used to make sure
4738+
* that the page corresponding to the faulting address
4739+
* will be hot in the cache after zeroing.
47404740
*/
4741-
if (!alloc_zeroed())
4741+
if (user_alloc_needs_zeroing())
47424742
folio_zero_user(folio, vmf->address);
47434743
return folio;
47444744
}

0 commit comments

Comments
 (0)