From patchwork Mon Nov 15 08:03:10 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12618895 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3C59FC433F5 for ; Mon, 15 Nov 2021 08:03:28 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id E1A4263219 for ; Mon, 15 Nov 2021 08:03:27 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org E1A4263219 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 7E52F6B007D; Mon, 15 Nov 2021 03:03:27 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7952F6B0080; Mon, 15 Nov 2021 03:03:27 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 684DD6B009A; Mon, 15 Nov 2021 03:03:27 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0102.hostedemail.com [216.40.44.102]) by kanga.kvack.org (Postfix) with ESMTP id 593396B007D for ; Mon, 15 Nov 2021 03:03:27 -0500 (EST) Received: from smtpin09.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 220F48249980 for ; Mon, 15 Nov 2021 08:03:27 +0000 (UTC) X-FDA: 78810424854.09.2C8F101 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf31.hostedemail.com (Postfix) with ESMTP id A009B10512E1 for ; Mon, 15 Nov 2021 08:03:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1636963406; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ILY47EUAX8YWGrwtSg12WPG1Xh7JfzVTNjA+1Egv88U=; b=ExQISPt43LnooPvo0p1Jj+Xe/JZwJueHnSfhvPkW4YXtwYvvJqdbQlcVC9tb2Sczk0LqcV 7UVhpOlQ6PyXjphHslmaWYFbAMlvEZPQEXKBNNkZy/7c7WNlO1H+UPt+Ua8xOQFHUO1svy cihVPByD8HnWNtsiKLSppggCGbZ3PG4= Received: from mail-pl1-f200.google.com (mail-pl1-f200.google.com [209.85.214.200]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-186-ZkIm_ZBTMcicvwfDpEKQoQ-1; Mon, 15 Nov 2021 03:03:25 -0500 X-MC-Unique: ZkIm_ZBTMcicvwfDpEKQoQ-1 Received: by mail-pl1-f200.google.com with SMTP id m17-20020a170902db1100b001421cb34857so5809253plx.15 for ; Mon, 15 Nov 2021 00:03:25 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ILY47EUAX8YWGrwtSg12WPG1Xh7JfzVTNjA+1Egv88U=; b=BXKF+4jrPCsuxOH66lNQ1Ulw5RV9AoOsyynQADMokoIsmGdj56cpWsHeDuBnhxbsIQ 1tUOZdbONURZeWdUqkMtdEKH7xBUxu1/cvl3zrxIFbR5qNUuWpY3LXD/R8GtIYEDVPGN 5wq5+M4UxaQd15Tg1qc35usGUgVuH1KSGshNYIiRVPu3+oQVxK7tZ9n9r4VXwn6Ul6Xo nxv5uG3Xs1LqlWc0CxHJ5MA45J66JNEoKibUHxsPI/i8g7p62zo8ua+241wdijBX1AVn grxa0WoSprxFiG+gjyADxFEjoSNYoA08QmfuV3LLMiQa9pcCip3xslhw0PoBF0mNJJW9 3AeQ== X-Gm-Message-State: AOAM530nZxH1P9lqOA6vbAT013p0Bo5eW8f8FqLlnF9xbOtbKqBoGSjf g2yRhxDQ0VHey+QkjHUUzeHUttE0qWLNm4B9LWC6duzldOutZUFbd8m1H33cqNzUW0/V7c9lHAF 6FGFPvLB9RXqh2mvoVL0zw6cWPGOJn2llpERxdvXmK5TmFcdJnGNuY3r0MdP3 X-Received: by 2002:a17:90b:1d0e:: with SMTP id on14mr62720952pjb.119.1636963403676; Mon, 15 Nov 2021 00:03:23 -0800 (PST) X-Google-Smtp-Source: ABdhPJxn0Rp/rkm5Ee0ybXP2u5WW8WzuvnOkz9ms/eTShUG9S2uyIUVwsqBwYx3b+PT9/ChdhtyCSA== X-Received: by 2002:a17:90b:1d0e:: with SMTP id on14mr62720896pjb.119.1636963403271; Mon, 15 Nov 2021 00:03:23 -0800 (PST) Received: from localhost.localdomain ([94.177.118.89]) by smtp.gmail.com with ESMTPSA id rm10sm12783901pjb.29.2021.11.15.00.03.14 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 15 Nov 2021 00:03:22 -0800 (PST) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Nadav Amit , peterx@redhat.com, Alistair Popple , Andrew Morton , Mike Kravetz , Mike Rapoport , Matthew Wilcox , Jerome Glisse , Axel Rasmussen , "Kirill A . Shutemov" , David Hildenbrand , Andrea Arcangeli , Hugh Dickins Subject: [PATCH v6 19/23] mm/khugepaged: Don't recycle vma pgtable if uffd-wp registered Date: Mon, 15 Nov 2021 16:03:10 +0800 Message-Id: <20211115080310.75154-1-peterx@redhat.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211115075522.73795-1-peterx@redhat.com> References: <20211115075522.73795-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: A009B10512E1 X-Stat-Signature: 7ruuho7hhzwia8maqigdhgpg7f8jaqhi Authentication-Results: imf31.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=ExQISPt4; dmarc=pass (policy=none) header.from=redhat.com; spf=none (imf31.hostedemail.com: domain of peterx@redhat.com has no SPF policy when checking 170.10.133.124) smtp.mailfrom=peterx@redhat.com X-HE-Tag: 1636963389-162652 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: When we're trying to collapse a 2M huge shmem page, don't retract pgtable pmd page if it's registered with uffd-wp, because that pgtable could have pte markers installed. Recycling of that pgtable means we'll lose the pte markers. That could cause data loss for an uffd-wp enabled application on shmem. Instead of disabling khugepaged on these files, simply skip retracting these special VMAs, then the page cache can still be merged into a huge thp, and other mm/vma can still map the range of file with a huge thp when proper. Note that checking VM_UFFD_WP needs to be done with mmap_sem held for write, that avoids race like: khugepaged user thread ========== =========== check VM_UFFD_WP, not set UFFDIO_REGISTER with uffd-wp on shmem wr-protect some pages (install markers) take mmap_sem write lock erase pmd and free pmd page --> pte markers are dropped unnoticed! Signed-off-by: Peter Xu --- mm/khugepaged.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index e99101162f1a..9c75153a36de 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -1454,6 +1454,10 @@ void collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr) if (!hugepage_vma_check(vma, vma->vm_flags | VM_HUGEPAGE)) return; + /* Keep pmd pgtable for uffd-wp; see comment in retract_page_tables() */ + if (userfaultfd_wp(vma)) + return; + hpage = find_lock_page(vma->vm_file->f_mapping, linear_page_index(vma, haddr)); if (!hpage) @@ -1594,7 +1598,15 @@ static void retract_page_tables(struct address_space *mapping, pgoff_t pgoff) * reverse order. Trylock is a way to avoid deadlock. */ if (mmap_write_trylock(mm)) { - if (!khugepaged_test_exit(mm)) { + /* + * When a vma is registered with uffd-wp, we can't + * recycle the pmd pgtable because there can be pte + * markers installed. Skip it only, so the rest mm/vma + * can still have the same file mapped hugely, however + * it'll always mapped in small page size for uffd-wp + * registered ranges. + */ + if (!khugepaged_test_exit(mm) && !userfaultfd_wp(vma)) { spinlock_t *ptl = pmd_lock(mm, pmd); /* assume page table is clear */ _pmd = pmdp_collapse_flush(vma, addr, pmd);