Skip to content

Commit 631c111

Browse files
Lorenzo Stoakes (Oracle)akpm00
authored andcommitted
mm/zswap: add missing kunmap_local()
Commit e2c3b6b ("mm: zswap: use SG list decompression APIs from zsmalloc") updated zswap_decompress() to use the scatterwalk API to copy data for uncompressed pages. In doing so, it mapped kernel memory locally for 32-bit kernels using kmap_local_folio(), however it never unmapped this memory. This resulted in the linked syzbot report where a BUG_ON() is triggered due to leaking the kmap slot. This patch fixes the issue by explicitly unmapping the established kmap. Also, add flush_dcache_folio() after the kunmap_local() call I had assumed that a new folio here combined with the flush that is done at the point of setting the PTE would suffice, but it doesn't seem that's actually the case, as update_mmu_cache() will in many archtectures only actually flush entries where a dcache flush was done on a range previously. I had also wondered whether kunmap_local() might suffice, but it doesn't seem to be the case. Some arches do seem to actually dcache flush on unmap, parisc does it if CONFIG_HIGHMEM is not set by setting ARCH_HAS_FLUSH_ON_KUNMAP and calling kunmap_flush_on_unmap() from __kunmap_local(), otherwise non-CONFIG_HIGHMEM callers do nothing here. Otherwise arch_kmap_local_pre_unmap() is called which does: * sparc - flush_cache_all() * arm - if VIVT, __cpuc_flush_dcache_area() * otherwise - nothing Also arch_kmap_local_post_unmap() is called which does: * arm - local_flush_tlb_kernel_page() * csky - kmap_flush_tlb() * microblaze, ppc - local_flush_tlb_page() * mips - local_flush_tlb_one() * sparc - flush_tlb_all() (again) * x86 - arch_flush_lazy_mmu_mode() * otherwise - nothing But this is only if it's high memory, and doesn't cover all architectures, so is presumably intended to handle other cache consistency concerns. In any case, VIPT is problematic here whether low or high memory (in spite of what the documentation claims, see [0] - 'the kernel did write to a page that is in the page cache page and / or in high memory'), because dirty cache lines may exist at the set indexed by the kernel direct mapping, which won't exist in the set indexed by any subsequent userland mapping, meaning userland might read stale data from L2 cache. Even if the documentation is correct and low memory is fine not to be flushed here, we can't be sure as to whether the memory is low or high (kmap_local_folio() will be a no-op if low), and this call should be harmless if it is low. VIVT would require more work if the memory were shared and already mapped, but this isn't the case here, and would anyway be handled by the dcache flush call. In any case, we definitely need this flush as far as I can tell. And we should probably consider updating the documentation unless it turns out there's somehow dcache synchronisation that happens for low memory/64-bit kernels elsewhere? [[email protected]: add flush_dcache_folio() after the kunmap_local() call] Link: https://lkml.kernel.org/r/[email protected] Link: https://lkml.kernel.org/r/[email protected] Link: https://docs.kernel.org/core-api/cachetlb.html [0] Fixes: e2c3b6b ("mm: zswap: use SG list decompression APIs from zsmalloc") Signed-off-by: Lorenzo Stoakes (Oracle) <[email protected]> Reported-by: [email protected] Closes: https://lore.kernel.org/all/[email protected] Acked-by: Yosry Ahmed <[email protected]> Acked-by: Johannes Weiner <[email protected]> Reviewed-by: SeongJae Park <[email protected]> Acked-by: Yosry Ahmed <[email protected]> Acked-by: Nhat Pham <[email protected]> Cc: Chengming Zhou <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 38dfd29 commit 631c111

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

mm/zswap.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -942,9 +942,15 @@ static bool zswap_decompress(struct zswap_entry *entry, struct folio *folio)
942942

943943
/* zswap entries of length PAGE_SIZE are not compressed. */
944944
if (entry->length == PAGE_SIZE) {
945+
void *dst;
946+
945947
WARN_ON_ONCE(input->length != PAGE_SIZE);
946-
memcpy_from_sglist(kmap_local_folio(folio, 0), input, 0, PAGE_SIZE);
948+
949+
dst = kmap_local_folio(folio, 0);
950+
memcpy_from_sglist(dst, input, 0, PAGE_SIZE);
947951
dlen = PAGE_SIZE;
952+
kunmap_local(dst);
953+
flush_dcache_folio(folio);
948954
} else {
949955
sg_init_table(&output, 1);
950956
sg_set_folio(&output, folio, PAGE_SIZE, 0);

0 commit comments

Comments
 (0)