Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp861510pxj; Thu, 27 May 2021 13:24:47 -0700 (PDT) X-Google-Smtp-Source: ABdhPJztlWujRDT2Ig4Ayo9CG55y/PS58s4lxPekRqu/WFBwwiUgCopFgl2ztTGMw+2uP1gi4su7 X-Received: by 2002:a92:d58a:: with SMTP id a10mr4559333iln.234.1622147086936; Thu, 27 May 2021 13:24:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1622147086; cv=none; d=google.com; s=arc-20160816; b=bXA++UvdbIcYNENa1fZZrhGXBMdV/y6IPOa/2vd4QD6KKWUb55Xcv4kvhsBXI7nwVs o0iCowQgQe+nVHXwEMTuDO6/5GawE+1VdE8rZkYjP6PXlS61JYMoIXn4zFVnfuVTBX6A 4mLUk8JmFLNqlCnNID17yUBenm6dLTZPrI9sW2hILG9R9WifGZg2kISTblXlhSzJl2Ca ZryEEAAsDe953vbU+4whDpzjiDOrVb0fNx64Jsb/NdW+fj5+q0ciaamGUbGhDLtMC9U0 Ao7VRhyqyXqMhGa+9c8n9UoyXM/tzu2oyrQb3VhOumBo1CHakdQoudo5Htcwrgs1iJjv Ix8w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=ltWxXgdsERQEx3E6ySA6em4pDoRaSyrvcOj0w7z20l8=; b=Zl+wZCEJrzfvzvbTuj93GnwjIoeJSYBk5D/duMc/OH+duRCppX7VcSE6tYp3Nh8FiU dQUGAMhqv+/hDhBrjt1yhZYQGX5lthZaXsJZUgyXn9Dz+zfeG72cNb0FayTuZjPHNzlc qEUgynM8nm4jW1VhHKNOsCbuZBhJy+2qt0UgiuDJK15ni0Mcur+B0insfYKR4iL0JnMI +Isjwpp22rLgfZGOtyMyOt5mQnEixeZRN/bLdkJF5EaLaI5r+dKykbgYxnMXf8D97O9k NAloltDy7j9/Zwnfsd4EEdG2L1hV/OZ/+dCpFw0/nND6p1ytUbbJhXKKjIhcH4naKHDs xxNA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Ls0R+yoZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id t15si3820627ilu.79.2021.05.27.13.24.09; Thu, 27 May 2021 13:24:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Ls0R+yoZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236097AbhE0UYc (ORCPT + 99 others); Thu, 27 May 2021 16:24:32 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:55261 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235646AbhE0UYa (ORCPT ); Thu, 27 May 2021 16:24:30 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1622146977; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ltWxXgdsERQEx3E6ySA6em4pDoRaSyrvcOj0w7z20l8=; b=Ls0R+yoZpBqON1bDorRpWubXB/9QSD0LWLW3CgcpoQAW5hw5FZsMvdiHb4av8/I3XMZFad 4nCDimyA2VSQ+crUGfc9pbtXmiyRvqWXa5J75lVrd7SAU/L7V3Bfd4hXZoz+Bf91u6eON4 sMqT0iFh+GLIxLhH/JgkA3+JumdEuAs= Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-169-tfbcyXBgMm-Avfu6kVqinA-1; Thu, 27 May 2021 16:22:55 -0400 X-MC-Unique: tfbcyXBgMm-Avfu6kVqinA-1 Received: by mail-qk1-f199.google.com with SMTP id c126-20020ae9ed840000b02903a69f28eea6so1368988qkg.2 for ; Thu, 27 May 2021 13:22:55 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ltWxXgdsERQEx3E6ySA6em4pDoRaSyrvcOj0w7z20l8=; b=fWAbor2DVavdo261MUcyIt9jV3lCWxowk3jB/IeMqaXSUYWAvtjAEEPZJ0T/p0aHPm mchnN8fNSLhp9GxPiY4oMZZA9sw+isKQ8w3wCbuMnLZS7yjzqdi0wu6X2CFLpvGvkB8Z WmkYkCYt8QWN+lv5siUgXagOEvs7JjXDAVXYFIsP7aCxkbFgXBxcDCG0kw4VLchatKhY hQXMSNd7zEUc+7VckHtjlyyG3+iLg4qlYmdZzF1fJq28YE1EyDoZuMkZ2XI3G0tEnDcJ 4xsHkpmul2RaoSoZTxJ7NYpSHItrjxAeRNGlUpEPwoUTr/dSX+hh26e6YWkgRER7/obj VFWg== X-Gm-Message-State: AOAM531WsVjSqOgWP+iHZm6hfcZK320fBZsL/G+4F6vjPAqfwZ7Zf2L/ MZra5JgO26TOKgVW39eIiEti2ZbrVj+FKQaSuA0kV5JRsXNs2IZV4xpiKzqxr4gFpvS40Unb36u kNJy7eElLFtsiuMxberMKkUzaGHRlGs8HCyQKKZzddQE8e7pgHeEPyMG8vCEMfD3Nc+/3H9hROQ == X-Received: by 2002:ac8:5784:: with SMTP id v4mr352670qta.264.1622146974787; Thu, 27 May 2021 13:22:54 -0700 (PDT) X-Received: by 2002:ac8:5784:: with SMTP id v4mr352632qta.264.1622146974465; Thu, 27 May 2021 13:22:54 -0700 (PDT) Received: from localhost.localdomain (bras-base-toroon474qw-grc-72-184-145-4-219.dsl.bell.ca. [184.145.4.219]) by smtp.gmail.com with ESMTPSA id j62sm2075122qkf.125.2021.05.27.13.22.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 May 2021 13:22:53 -0700 (PDT) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Mike Rapoport , peterx@redhat.com, Andrew Morton , Mike Kravetz , Jerome Glisse , Miaohe Lin , Nadav Amit , Hugh Dickins , Matthew Wilcox , Jason Gunthorpe , "Kirill A . Shutemov" , Andrea Arcangeli , Axel Rasmussen Subject: [PATCH v3 13/27] shmem/userfaultfd: Allows file-back mem to be uffd wr-protected on thps Date: Thu, 27 May 2021 16:22:50 -0400 Message-Id: <20210527202250.31663-1-peterx@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210527201927.29586-1-peterx@redhat.com> References: <20210527201927.29586-1-peterx@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org We don't have "huge" version of PTE_SWP_UFFD_WP_SPECIAL, instead when necessary we split the thp if the huge page is uffd wr-protected previously. However split the thp is not enough, because file-backed thp is handled totally differently comparing to anonymous thps - rather than doing a real split, the thp pmd will simply got dropped in __split_huge_pmd_locked(). That is definitely not enough if e.g. when there is a thp covers range [0, 2M) but we want to wr-protect small page resides in [4K, 8K) range, because after __split_huge_pmd() returns, there will be a none pmd. Here we leverage the previously introduced change_protection_prepare() macro so that we'll populate the pmd with a pgtable page. Then change_pte_range() will do all the rest for us, e.g., install the uffd-wp swap special pte marker at any pte that we'd like to wr-protect, under the protection of pgtable lock. Signed-off-by: Peter Xu --- mm/mprotect.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/mm/mprotect.c b/mm/mprotect.c index 8ec85b276975..3fcb87b59696 100644 --- a/mm/mprotect.c +++ b/mm/mprotect.c @@ -306,8 +306,16 @@ static inline unsigned long change_pmd_range(struct vm_area_struct *vma, } if (is_swap_pmd(*pmd) || pmd_trans_huge(*pmd) || pmd_devmap(*pmd)) { - if (next - addr != HPAGE_PMD_SIZE) { + if (next - addr != HPAGE_PMD_SIZE || + /* Uffd wr-protecting a file-backed memory range */ + unlikely(!vma_is_anonymous(vma) && + (cp_flags & MM_CP_UFFD_WP))) { __split_huge_pmd(vma, pmd, addr, false, NULL); + /* + * For file-backed, the pmd could have been + * gone; still provide a pte pgtable if needed. + */ + change_protection_prepare(vma, pmd, addr, cp_flags); } else { int nr_ptes = change_huge_pmd(vma, pmd, addr, newprot, cp_flags); -- 2.31.1