From patchwork Tue Apr 5 01:48:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12800964 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 99E0DC433F5 for ; Tue, 5 Apr 2022 01:52:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B41F76B007D; Mon, 4 Apr 2022 21:49:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AF1B36B007E; Mon, 4 Apr 2022 21:49:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 992266B0080; Mon, 4 Apr 2022 21:49:10 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0195.hostedemail.com [216.40.44.195]) by kanga.kvack.org (Postfix) with ESMTP id 8BCF56B007D for ; Mon, 4 Apr 2022 21:49:10 -0400 (EDT) Received: from smtpin25.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 4E79B1828EE03 for ; Tue, 5 Apr 2022 01:49:00 +0000 (UTC) X-FDA: 79321142040.25.7BC0131 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf11.hostedemail.com (Postfix) with ESMTP id CC7AC40018 for ; Tue, 5 Apr 2022 01:48:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1649123339; 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=9Tho0jTi/mP2SxzzY9PiukQljtVmr59NjLeFBDKY3mg=; b=BK59SyfXaXC6GqaemoZ5y188v8MRE49skeFlJtJEj0THIbyXKfIUmD4JFZQ4D2pxpqhh3u vq+L4rxXK+9jNfhvKwYXNsrt4zRoXwOvu6SHtoqLPWR2fQFEdGQR/cHli/Uyb0TBKVSVfD etYEtTcV8mtTvO18i3rOiSvHs4Jcdqw= Received: from mail-il1-f198.google.com (mail-il1-f198.google.com [209.85.166.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-539-Or_uibUHPaa3q1g7I3lHSg-1; Mon, 04 Apr 2022 21:48:56 -0400 X-MC-Unique: Or_uibUHPaa3q1g7I3lHSg-1 Received: by mail-il1-f198.google.com with SMTP id a2-20020a056e020e0200b002ca4850353fso2209362ilk.5 for ; Mon, 04 Apr 2022 18:48:56 -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=9Tho0jTi/mP2SxzzY9PiukQljtVmr59NjLeFBDKY3mg=; b=EyDzA7ce5cRdJMwfbGwc2aD6A5V1g9LAZcdcCNrMaZyAGh1ikssHe8SB9Q0aDPyCL2 bEtBc6tZ3Iu3HqLkUkrpb6lgaI7dOyRu6g5TSA/ZG1/7OjpbODOasvCOtmf6qzpZFZ6P KFRg+bi+XkLXmp40bnZdUt3WpFD55opTlfkiy2Cvkzf65BzS5+5NrHstq7hxDRpyJwpb Ztj3tSyFZd2xyFZQqVzQgbcmRdGT4CQW3ved6aHk3c7DebzITG74PIM/qPLSokrHiugG 264eqsNyKOh7fjFLWddmTh6gSv8q1qV9gdN9WyKGIj7z0GnwM6Fnf//9mJpNPpCb2URR Mq+g== X-Gm-Message-State: AOAM530L0puDP3pHuXGTwi2OgCVD4sLTQ82YzyqfSty3cUdm062O2SBs FB7UqSlRxzlgElLZGRGYeGw3n+JyAhnU7nIZoZyU/Z6ztvW/HTK/+hNYlHp0+G6360PziKzHRL6 HV8J8iaSfbEA= X-Received: by 2002:a6b:e60a:0:b0:646:3e9e:172f with SMTP id g10-20020a6be60a000000b006463e9e172fmr604255ioh.1.1649123335714; Mon, 04 Apr 2022 18:48:55 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwEUfWYkT5v8wLM/eubINNRKFmNMGPSIHFsXbNqMBzFCqBGDzRGSMNc6UHIgjk9FfldR5FQwA== X-Received: by 2002:a6b:e60a:0:b0:646:3e9e:172f with SMTP id g10-20020a6be60a000000b006463e9e172fmr604247ioh.1.1649123335505; Mon, 04 Apr 2022 18:48:55 -0700 (PDT) Received: from localhost.localdomain (cpec09435e3e0ee-cmc09435e3e0ec.cpe.net.cable.rogers.com. [99.241.198.116]) by smtp.gmail.com with ESMTPSA id k6-20020a6b4006000000b00649d7111ebasm7563860ioa.0.2022.04.04.18.48.54 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 04 Apr 2022 18:48:55 -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 09/23] mm/shmem: Allows file-back mem to be uffd wr-protected on thps Date: Mon, 4 Apr 2022 21:48:52 -0400 Message-Id: <20220405014852.14413-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-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: CC7AC40018 X-Stat-Signature: kg9amhxhwgkongjo3he4skib616wy6ya X-Rspam-User: Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=BK59SyfX; dmarc=pass (policy=none) header.from=redhat.com; spf=none (imf11.hostedemail.com: domain of peterx@redhat.com has no SPF policy when checking 170.10.129.124) smtp.mailfrom=peterx@redhat.com X-HE-Tag: 1649123339-695344 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: We don't have "huge" version of pte markers, instead when necessary we split the thp. 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 cleared in __split_huge_pmd_locked(). That is 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, and change_pmd_range() will just skip it right after the split. Here we leverage the previously introduced change_pmd_prepare() macro so that we'll populate the pmd with a pgtable page after the pmd split (in which process the pmd will be cleared for cases like shmem). Then change_pte_range() will do all the rest for us by installing the uffd-wp pte marker at any none pte that we'd like to wr-protect. Signed-off-by: Peter Xu --- mm/mprotect.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/mm/mprotect.c b/mm/mprotect.c index bd62d5938c6c..e0a567b66d07 100644 --- a/mm/mprotect.c +++ b/mm/mprotect.c @@ -333,8 +333,15 @@ 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_wp_protect_file(vma, cp_flags)) { __split_huge_pmd(vma, pmd, addr, false, NULL); + /* + * For file-backed, the pmd could have been + * cleared; make sure pmd populated if + * necessary, then fall-through to pte level. + */ + change_pmd_prepare(vma, pmd, cp_flags); } else { int nr_ptes = change_huge_pmd(vma, pmd, addr, newprot, cp_flags);