From patchwork Wed Oct 4 03:25:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rik van Riel X-Patchwork-Id: 13408259 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0D8DAE8FDC6 for ; Wed, 4 Oct 2023 03:28:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0CEAA8D00A7; Tue, 3 Oct 2023 23:28:37 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EAC898D0002; Tue, 3 Oct 2023 23:28:36 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C61B38D009A; Tue, 3 Oct 2023 23:28:36 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id B58D68D0002 for ; Tue, 3 Oct 2023 23:28:36 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 826F5160564 for ; Wed, 4 Oct 2023 03:28:36 +0000 (UTC) X-FDA: 81306346632.17.D56FB63 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf24.hostedemail.com (Postfix) with ESMTP id E1322180018 for ; Wed, 4 Oct 2023 03:28:34 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=none; dmarc=none; spf=none (imf24.hostedemail.com: domain of riel@shelob.surriel.com has no SPF policy when checking 96.67.55.147) smtp.mailfrom=riel@shelob.surriel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1696390115; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=d/UtUPZcqAmB8qgZ0HCBdpt5ojQpP+60u6FhGM8uWFY=; b=Qoj+dtgVSTLPQ1fFzeE8hlpacaWPFkh24fLgVgDRB1cJaT6HiLV75KcaRZ6G12LzangqtC Lt2hb84333P+g4a0uiL8zoRFZAs5hLAfZ/CSC2pfYjN+QBT4kLS84OS1uu4+kzL63P9Gbp Q9jWsE7vGPcueYN6QSwc3AJ1x74xmhI= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=none; dmarc=none; spf=none (imf24.hostedemail.com: domain of riel@shelob.surriel.com has no SPF policy when checking 96.67.55.147) smtp.mailfrom=riel@shelob.surriel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1696390115; a=rsa-sha256; cv=none; b=o0dZh7fqogEuOcg8PP1WDHSsgy9Q+1hQzil4sh+y0kJrVIQqeFZVNm3nQ8Xcd5rLVAL+Td EKKPqdY2bMGoW2ho8sP+6ux1wlutLzvkkNML4cPuc1vnCDt2E3hdR1347QWuynK1w2nSTx EZisn27+qX5VQGSqo+eaIzVZ7c7XrUc= Received: from imladris.home.surriel.com ([10.0.13.28] helo=imladris.surriel.com) by shelob.surriel.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1qnsYb-0005Bb-0b; Tue, 03 Oct 2023 23:28:17 -0400 From: riel@surriel.com To: linux-kernel@vger.kernel.org Cc: kernel-team@meta.com, linux-mm@kvack.org, akpm@linux-foundation.org, muchun.song@linux.dev, mike.kravetz@oracle.com, leit@meta.com, willy@infradead.org, Rik van Riel , stable@kernel.org Subject: [PATCH 2/3] hugetlbfs: close race between MADV_DONTNEED and page fault Date: Tue, 3 Oct 2023 23:25:04 -0400 Message-ID: <20231004032814.3108383-3-riel@surriel.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231004032814.3108383-1-riel@surriel.com> References: <20231004032814.3108383-1-riel@surriel.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: E1322180018 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: cgbsy35ot5earc8wzc87ign9edbcmod8 X-HE-Tag: 1696390114-296107 X-HE-Meta: U2FsdGVkX180vuIpH3hh3qkKhBPgJTPv+/8slDroqIV8YJmoNOrxiTfTCzyIraiJhthEmetslt5q9NgejFN9BQsvvzbxVfFnWOL8cJimlphYFdJWnwDoVEwz+1nryHZm2vTjbYTmslSpSMAIwJR5NEp+/wycegAPKbC4IrHzsHbH3Nhx27Ug4/gQ8s6k/U2o9EKxaBcHfFtBnkcyKn9i09p6ely0S7QLC/V2nkDjAPZ9GSEYiisAa1I2Bb0HJOVev8fD40F3NksI4AVsm62VWoYy+OomgHR+YPg1ZxTN5avypCdhY46BC27aoI+l2p1mSLb/G4UWg26Eb778HiC9Joe7+ABpcxi/JAmpIl96ECtMw6OcXehE6ZRWN7KOqRtW4mfrheEuI3ZiWxxycT3ttD2kxRMs/TYXS09r8p9ItgQYPaUvEPYBsbEDKOVvyZ84Hmp7YLpRxEc4HyQV89GM/gK32FMmH7E6ua1w5g2ZzthcaGmnK2gGxgzSqRTCrP8yZQ6Kqt1Rg/5CclTKkWnBiV2gDZQ922ldW5lMlOtxTlC+JwbODbH25WDfFHyz0RslLRRONSHZf5Z/AKNXEMfA4sQ/7lAGGawJvCY6dejZPAwqva2wY+JhPe/GlS2XRrM9jA/C4groDrn6oZQ7AXHX6QcBDjLScW9Lv6wp90sRFsZhLzP9377sQh3JWr5zuBgJpLfOqblpCLFD4aRtNxHrjKb36CHV+AGrMgphyEOPcf9j67DX9ayleb1L412bmcV4QJH6muLxVhlz7XRYBjaNE4ssXeZp5lgxcbfP1KRfqcrsS9G+FPozE2EXdFRBg5Gwgf6kTT/NpHKahJSTHwnF2vv9MHK/uX2JAeEXiKWjj0jxx0uFodVdqaawPiVHYglOYW8wSKfdaxE47Pss41WlwsawkJJERWjL+KmiV7wxXq9jnrD0zYdlVqePDl73SITghuZz0IIIhJ62dDTa1Eq 3ySPxFC/ blJkylxWF1NXdLzNOWMSJV3Bm93wrc5tZkYZHBWNVQBhLTGQptz4u80MlQbm4JHwqjK9s7VqRlZiRZ4Yylk8oeq5po/zC1IvtEe5+SynT9Db2H6TqRp5RrP/BR0EikFikSHrXuK8wlUIhipmw4WCHCu6/h1ByENobHeuwiQwU/X3F6cUfyY5EURhNEdpuURDWV+VrGOryGaerNL7ZtWrdFSdJew/QAqmBtvJcZ3DXRLrV/URqJsGF5RyD7eLzvkpI9rbH9iKiFI4cWILqYmmmipjTJQ2UNCO3TAYmD2Nry6s9xcWA+E+mbJ74cHSzHz7RbliwkFRTvU9y0Ltenv8QevIrSdNFYIWOQON0zFv+sy4mvORgQcuavRIF0YGWLWXndGswdJyJ6liNL9l5sW6grrpnqg== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Rik van Riel Malloc libraries, like jemalloc and tcalloc, take decisions on when to call madvise independently from the code in the main application. This sometimes results in the application page faulting on an address, right after the malloc library has shot down the backing memory with MADV_DONTNEED. Usually this is harmless, because we always have some 4kB pages sitting around to satisfy a page fault. However, with hugetlbfs systems often allocate only the exact number of huge pages that the application wants. Due to TLB batching, hugetlbfs MADV_DONTNEED will free pages outside of any lock taken on the page fault path, which can open up the following race condition: CPU 1 CPU 2 MADV_DONTNEED unmap page shoot down TLB entry page fault fail to allocate a huge page killed with SIGBUS free page Fix that race by pulling the locking from __unmap_hugepage_final_range into helper functions called from zap_page_range_single. This ensures page faults stay locked out of the MADV_DONTNEED VMA until the huge pages have actually been freed. Signed-off-by: Rik van Riel Cc: stable@kernel.org Fixes: 04ada095dcfc ("hugetlb: don't delete vma_lock in hugetlb MADV_DONTNEED processing") --- include/linux/hugetlb.h | 35 +++++++++++++++++++++++++++++++++-- mm/hugetlb.c | 28 ++++++++++++++++------------ mm/memory.c | 13 ++++++++----- 3 files changed, 57 insertions(+), 19 deletions(-) diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h index 694928fa06a3..d9ec500cfef9 100644 --- a/include/linux/hugetlb.h +++ b/include/linux/hugetlb.h @@ -139,7 +139,7 @@ struct page *hugetlb_follow_page_mask(struct vm_area_struct *vma, void unmap_hugepage_range(struct vm_area_struct *, unsigned long, unsigned long, struct page *, zap_flags_t); -void __unmap_hugepage_range_final(struct mmu_gather *tlb, +void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma, unsigned long start, unsigned long end, struct page *ref_page, zap_flags_t zap_flags); @@ -246,6 +246,25 @@ int huge_pmd_unshare(struct mm_struct *mm, struct vm_area_struct *vma, void adjust_range_if_pmd_sharing_possible(struct vm_area_struct *vma, unsigned long *start, unsigned long *end); +extern void __hugetlb_zap_begin(struct vm_area_struct *vma, + unsigned long *begin, unsigned long *end); +extern void __hugetlb_zap_end(struct vm_area_struct *vma, + struct zap_details *details); + +static inline void hugetlb_zap_begin(struct vm_area_struct *vma, + unsigned long *start, unsigned long *end) +{ + if (is_vm_hugetlb_page(vma)) + __hugetlb_zap_begin(vma, start, end); +} + +static inline void hugetlb_zap_end(struct vm_area_struct *vma, + struct zap_details *details) +{ + if (is_vm_hugetlb_page(vma)) + __hugetlb_zap_end(vma, details); +} + void hugetlb_vma_lock_read(struct vm_area_struct *vma); void hugetlb_vma_unlock_read(struct vm_area_struct *vma); void hugetlb_vma_lock_write(struct vm_area_struct *vma); @@ -297,6 +316,18 @@ static inline void adjust_range_if_pmd_sharing_possible( { } +static inline void hugetlb_zap_begin( + struct vm_area_struct *vma, + unsigned long *start, unsigned long *end) +{ +} + +static inline void hugetlb_zap_end( + struct vm_area_struct *vma, + struct zap_details *details) +{ +} + static inline struct page *hugetlb_follow_page_mask( struct vm_area_struct *vma, unsigned long address, unsigned int flags, unsigned int *page_mask) @@ -442,7 +473,7 @@ static inline long hugetlb_change_protection( return 0; } -static inline void __unmap_hugepage_range_final(struct mmu_gather *tlb, +static inline void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma, unsigned long start, unsigned long end, struct page *ref_page, zap_flags_t zap_flags) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index ee7497f37098..424bb8da9519 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -5306,9 +5306,9 @@ int move_hugetlb_page_tables(struct vm_area_struct *vma, return len + old_addr - old_end; } -static void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma, - unsigned long start, unsigned long end, - struct page *ref_page, zap_flags_t zap_flags) +void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma, + unsigned long start, unsigned long end, + struct page *ref_page, zap_flags_t zap_flags) { struct mm_struct *mm = vma->vm_mm; unsigned long address; @@ -5435,16 +5435,19 @@ static void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct tlb_flush_mmu_tlbonly(tlb); } -void __unmap_hugepage_range_final(struct mmu_gather *tlb, - struct vm_area_struct *vma, unsigned long start, - unsigned long end, struct page *ref_page, - zap_flags_t zap_flags) +void __hugetlb_zap_begin(struct vm_area_struct *vma, + unsigned long *start, unsigned long *end) { + adjust_range_if_pmd_sharing_possible(vma, start, end); hugetlb_vma_lock_write(vma); - i_mmap_lock_write(vma->vm_file->f_mapping); + if (vma->vm_file) + i_mmap_lock_write(vma->vm_file->f_mapping); +} - /* mmu notification performed in caller */ - __unmap_hugepage_range(tlb, vma, start, end, ref_page, zap_flags); +void __hugetlb_zap_end(struct vm_area_struct *vma, + struct zap_details *details) +{ + zap_flags_t zap_flags = details ? details->zap_flags : 0; if (zap_flags & ZAP_FLAG_UNMAP) { /* final unmap */ /* @@ -5457,11 +5460,12 @@ void __unmap_hugepage_range_final(struct mmu_gather *tlb, * someone else. */ __hugetlb_vma_unlock_write_free(vma); - i_mmap_unlock_write(vma->vm_file->f_mapping); } else { - i_mmap_unlock_write(vma->vm_file->f_mapping); hugetlb_vma_unlock_write(vma); } + + if (vma->vm_file) + i_mmap_unlock_write(vma->vm_file->f_mapping); } void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start, diff --git a/mm/memory.c b/mm/memory.c index 6c264d2f969c..517221f01303 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -1683,7 +1683,7 @@ static void unmap_single_vma(struct mmu_gather *tlb, if (vma->vm_file) { zap_flags_t zap_flags = details ? details->zap_flags : 0; - __unmap_hugepage_range_final(tlb, vma, start, end, + __unmap_hugepage_range(tlb, vma, start, end, NULL, zap_flags); } } else @@ -1728,8 +1728,12 @@ void unmap_vmas(struct mmu_gather *tlb, struct ma_state *mas, start_addr, end_addr); mmu_notifier_invalidate_range_start(&range); do { - unmap_single_vma(tlb, vma, start_addr, end_addr, &details, + unsigned long start = start_addr; + unsigned long end = end_addr; + hugetlb_zap_begin(vma, &start, &end); + unmap_single_vma(tlb, vma, start, end, &details, mm_wr_locked); + hugetlb_zap_end(vma, &details); } while ((vma = mas_find(mas, tree_end - 1)) != NULL); mmu_notifier_invalidate_range_end(&range); } @@ -1753,9 +1757,7 @@ void zap_page_range_single(struct vm_area_struct *vma, unsigned long address, lru_add_drain(); mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma->vm_mm, address, end); - if (is_vm_hugetlb_page(vma)) - adjust_range_if_pmd_sharing_possible(vma, &range.start, - &range.end); + hugetlb_zap_begin(vma, &range.start, &range.end); tlb_gather_mmu(&tlb, vma->vm_mm); update_hiwater_rss(vma->vm_mm); mmu_notifier_invalidate_range_start(&range); @@ -1766,6 +1768,7 @@ void zap_page_range_single(struct vm_area_struct *vma, unsigned long address, unmap_single_vma(&tlb, vma, address, end, details, false); mmu_notifier_invalidate_range_end(&range); tlb_finish_mmu(&tlb); + hugetlb_zap_end(vma, details); } /**