From patchwork Tue Apr 5 01:49:21 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12800975 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 AC08FC433F5 for ; Tue, 5 Apr 2022 01:57:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A08476B008C; Mon, 4 Apr 2022 21:49:36 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9B7996B0092; Mon, 4 Apr 2022 21:49:36 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 858496B0093; Mon, 4 Apr 2022 21:49:36 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.hostedemail.com [64.99.140.27]) by kanga.kvack.org (Postfix) with ESMTP id 790C16B008C for ; Mon, 4 Apr 2022 21:49:36 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 4676AB08 for ; Tue, 5 Apr 2022 01:49:26 +0000 (UTC) X-FDA: 79321143132.03.F522F0B Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf27.hostedemail.com (Postfix) with ESMTP id C4FC640020 for ; Tue, 5 Apr 2022 01:49:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1649123365; 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=bWfhkJFq1BCdKqisyymaNspfijHKW5rO8LQgLYY4Yk0=; b=bR5JfXtgBKqNHxNxgEslZ5nxiYhg6oyPfyrM2bCTjxbGKEkA/mV7lJWwHDMecvdFcI4bKM maunlIwtkebVHiIrZ2fLPToEEitUc+h+UYiFcV5Ra0wC1/jlqRblXZDedfK95ThtnTxiZ4 d5cx654q2ARfC0POkG3pXYOshxkq8j8= Received: from mail-il1-f200.google.com (mail-il1-f200.google.com [209.85.166.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-669-8fNBt3XJM-OwqTZdecq6Fg-1; Mon, 04 Apr 2022 21:49:24 -0400 X-MC-Unique: 8fNBt3XJM-OwqTZdecq6Fg-1 Received: by mail-il1-f200.google.com with SMTP id q11-20020a056e02106b00b002ca4677e013so2311905ilj.10 for ; Mon, 04 Apr 2022 18:49:24 -0700 (PDT) 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=bWfhkJFq1BCdKqisyymaNspfijHKW5rO8LQgLYY4Yk0=; b=Hmyt4Gb6+J+NXkEsjJalwgrxSmYjjzM5lPC5XmLuEhiELF3C2BKDSOPH7oCvd+O23u Cne06Y5N/YQCPeyal6whEU94ujQOaa6rNCCX20+v3Yt26f7vUT9LHrDh5WRBVBWbh/cV YydSw4F3RKNO938jlm31uDao8jR5rS3aOAVcVYzbg5HQ2Z99BVianUBUl2/2ER9XwfMn 0m/JsxdHkoTtLZ3WBjXP2UwsPfFYHHInkmgHQU8K3hsSRp8D5JsxpH1PPYcmV2mCQnZj B9eoS7RFCBWooM9VOLIwkvpNhpeYl6tGJ/ywNP2jNuryZvzi5Y/yehfB6rFaMdXGdXze LAcA== X-Gm-Message-State: AOAM5317FEb7H1P2mM4y5VjNsm6BQa9FSgEL8Ewwq1kGlzG34WY6Z2j1 lngGJIITc1TuRfLzoqKhD94jKxCe85iPYGWnBp76Da0I+ey8DSytVFO1kJBLmN3UO/P4fUMbxGC rzgZSPfbrUaU= X-Received: by 2002:a02:cd12:0:b0:321:29bd:b5ae with SMTP id g18-20020a02cd12000000b0032129bdb5aemr659608jaq.83.1649123363626; Mon, 04 Apr 2022 18:49:23 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwFo84QPKUst3NxwRFnpoGwYsq1cNnk6B7cOmPSPQoeut9h5XdJGOgIWmVZYMgQ4peNHpQKkw== X-Received: by 2002:a02:cd12:0:b0:321:29bd:b5ae with SMTP id g18-20020a02cd12000000b0032129bdb5aemr659588jaq.83.1649123363425; Mon, 04 Apr 2022 18:49:23 -0700 (PDT) Received: from localhost.localdomain (cpec09435e3e0ee-cmc09435e3e0ec.cpe.net.cable.rogers.com. [99.241.198.116]) by smtp.gmail.com with ESMTPSA id z12-20020a92d18c000000b002ca3ac378e2sm2852863ilz.76.2022.04.04.18.49.22 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 04 Apr 2022 18:49:23 -0700 (PDT) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Mike Kravetz , Nadav Amit , Matthew Wilcox , Mike Rapoport , David Hildenbrand , Hugh Dickins , Jerome Glisse , "Kirill A . Shutemov" , Andrea Arcangeli , Andrew Morton , Axel Rasmussen , Alistair Popple , peterx@redhat.com Subject: [PATCH v8 19/23] mm/khugepaged: Don't recycle vma pgtable if uffd-wp registered Date: Mon, 4 Apr 2022 21:49:21 -0400 Message-Id: <20220405014921.14994-1-peterx@redhat.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220405014646.13522-1-peterx@redhat.com> References: <20220405014646.13522-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rspam-User: Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=bR5JfXtg; spf=none (imf27.hostedemail.com: domain of peterx@redhat.com has no SPF policy when checking 170.10.129.124) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: C4FC640020 X-Stat-Signature: 4jxqiucdytanou6kiphqaxjz7fdajgfz X-HE-Tag: 1649123365-458905 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 04a972259136..d7c5bb9fd1fb 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -1464,6 +1464,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) @@ -1599,7 +1603,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)) collapse_and_free_pmd(mm, vma, addr, pmd); mmap_write_unlock(mm); } else {