From patchwork Wed Jul 14 22:24:37 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12377963 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 92B9BC12002 for ; Wed, 14 Jul 2021 22:24:45 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 3CB9E613C9 for ; Wed, 14 Jul 2021 22:24:45 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 3CB9E613C9 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 903EE6B00BF; Wed, 14 Jul 2021 18:24:45 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8DA696B00C0; Wed, 14 Jul 2021 18:24:45 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 77AFC6B00C1; Wed, 14 Jul 2021 18:24:45 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0039.hostedemail.com [216.40.44.39]) by kanga.kvack.org (Postfix) with ESMTP id 4DCB86B00BF for ; Wed, 14 Jul 2021 18:24:45 -0400 (EDT) Received: from smtpin05.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 3A94E1837DCAE for ; Wed, 14 Jul 2021 22:24:44 +0000 (UTC) X-FDA: 78362624088.05.189866D Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf14.hostedemail.com (Postfix) with ESMTP id D1AEC6001A90 for ; Wed, 14 Jul 2021 22:24:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1626301483; 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=ltWxXgdsERQEx3E6ySA6em4pDoRaSyrvcOj0w7z20l8=; b=RwHKUGCGdjuNICP9+Tf+23LWsdsfZtref41n6psOCiEI9KaEb8Ke9KZK4cz6y08IYx/sXU cx2mFs58uBR3YzImA3Z1Kzb8qaFmDj2hv7JUUJJYj9YNL3xnr9DOgssDJfKZO/WEZAR0y9 tsAtJFvCHPlrs518gbkGaqvRWhQ9Hbw= Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-172-I_xluNMkMsqURc7jm9Le-Q-1; Wed, 14 Jul 2021 18:24:42 -0400 X-MC-Unique: I_xluNMkMsqURc7jm9Le-Q-1 Received: by mail-qk1-f198.google.com with SMTP id o14-20020a05620a0d4eb02903a5eee61155so2315122qkl.9 for ; Wed, 14 Jul 2021 15:24:42 -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=n4f1a/CyhxHsHEg0TVcCNEcJMVvy7STTHFV33l89g779rYhhylbKI5WiAFPa9iCDgz +stKrly7sAP3kDY4drPLHqpmhuxMgrO7Od2OlnOpXOgXABLMatm5oAvmEyltaC8nEsew m4WaG7bF6S2VFHmRJ9uyvvKKusR8DO3gsHEfT1p1gbASF4OBeW6z50HHqdFC7MPuPQ23 mS6D8qkiBqP0B3wPIr/oTfcxwU+jNT2C5GdiMWrY9FVvxjQwSmQi7BoWouNuJ+0JHER9 D0NmbeyZQr04N4roytj2NFh+nDx4acalxphQIvp+1o/3xtb8oBYGeY+e0/w5qKOxOfp9 ICSQ== X-Gm-Message-State: AOAM532R155iJzQGYw22lQG6LJkcFxppmNgY/4HsIJxaOTbdUP6e6Sbi T94SjgNWg0JZxFzX3tDs39SKTRnnEqoZVqAeDjTjTtiBNTpkAGjKRsAws/HCgFRyGPdTeYJNUy3 R78yRuX+C7WuE9vocVs9Zkw8Ip1SUH7EAZ+G6iT8T5MgnOZX6ozHdJ45K7YhS X-Received: by 2002:ac8:44c2:: with SMTP id b2mr348930qto.216.1626301481624; Wed, 14 Jul 2021 15:24:41 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxltkXhESK8mTiLGdx//VedRH0qopTtyIpRgcuMJZv4e/oNyE6Nw/k7eAZhcln38G+Gz1hD3g== X-Received: by 2002:ac8:44c2:: with SMTP id b2mr348896qto.216.1626301481364; Wed, 14 Jul 2021 15:24:41 -0700 (PDT) Received: from localhost.localdomain (bras-base-toroon474qw-grc-65-184-144-111-238.dsl.bell.ca. [184.144.111.238]) by smtp.gmail.com with ESMTPSA id t125sm1645040qkf.41.2021.07.14.15.24.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Jul 2021 15:24:40 -0700 (PDT) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Andrew Morton , Mike Kravetz , Axel Rasmussen , Miaohe Lin , "Kirill A . Shutemov" , Hugh Dickins , Jason Gunthorpe , Alistair Popple , Matthew Wilcox , peterx@redhat.com, Jerome Glisse , Andrea Arcangeli , Mike Rapoport , Nadav Amit , David Hildenbrand Subject: [PATCH v4 12/26] shmem/userfaultfd: Allows file-back mem to be uffd wr-protected on thps Date: Wed, 14 Jul 2021 18:24:37 -0400 Message-Id: <20210714222437.48687-1-peterx@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210714222117.47648-1-peterx@redhat.com> References: <20210714222117.47648-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=RwHKUGCG; spf=none (imf14.hostedemail.com: domain of peterx@redhat.com has no SPF policy when checking 170.10.133.124) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Rspamd-Server: rspam02 X-Stat-Signature: e53hfakxpyf8k354fdexn8ecu5t3fj7j X-Rspamd-Queue-Id: D1AEC6001A90 X-HE-Tag: 1626301483-160719 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_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);