Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp7909pxv; Wed, 14 Jul 2021 18:12:37 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwfyh7Appf69ruVLjYYr13hlbvPdyJ4kV/YsWstEBfXdKDp+s5qox38YizxMxOB5bdXYj+f X-Received: by 2002:a02:380b:: with SMTP id b11mr940881jaa.83.1626311557305; Wed, 14 Jul 2021 18:12:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626311557; cv=none; d=google.com; s=arc-20160816; b=j2TxXY52L4xllxdnWQLamU5wIUbzdokjmUiDypSd924BWMmVp5TB47cWf61Q3dPj+i fu7Ue+UlUKp82d1i/u9HJVG4EpybDItFfwO8UCkGZOgmG7lambs0kh3izVvmV/tXct3i zdiN0QuYgHNHvlj9Fx2bIlU5/kfuYgv8q8esLL75vcyxG64tnACgayu3Pwm8dNQFtBH5 lI4Q7I74C5FT0kSRNQve7XPcNW8qJaLqIqrbEYoU376vgQRK3SbcZoS/rJ190PAvT504 Z1epac439eohL2PvOf+kWbFRltRZ0xaBTThb7N6yt5RAqTRCiohwdRgMHwCzFQYmMs7K A0HQ== 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=PBLwD+aJiPHDM6ljv8cUQu75+rM6kEYOChQIqgtIBOt/wcH/XBdGhcbtsAfQPFLUEg kB1ggAC/iOHYp+r85Nvfy5uHdX1uaV2WHdmXPsjFY0klULMLsWwl9GorczIZjBmTXOs/ E1EcD4ymqgiqB21sqgOu9LDoRWCllP4MNI/QTwlKX6k1p1n11vOaEJNdqH1zqLjT3/29 JADfX5TENmUx31JTmJj5vLa0lxCw8X0aNGE0ub3v9ATMGqz9gIbz1OTtsjQRIAcAkIn7 n61nVAa7QM0Xb4DygQcEC8EOq0PDbDsH1i9EhUaA2lNUh65cZ9Rx1Rk/wFYZkCsKTgnB VwVA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Xr4q4WUD; 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 u12si4256239ior.23.2021.07.14.18.12.13; Wed, 14 Jul 2021 18:12:37 -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=Xr4q4WUD; 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 S234221AbhGNW1f (ORCPT + 99 others); Wed, 14 Jul 2021 18:27:35 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:50744 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229735AbhGNW1f (ORCPT ); Wed, 14 Jul 2021 18:27:35 -0400 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ltWxXgdsERQEx3E6ySA6em4pDoRaSyrvcOj0w7z20l8=; b=Xr4q4WUDDs58U085Cw9HVjcHln0cFs428rPvLi6gd7EEkS41fflcE1TAu7eMbscbtG8Q98 bZu1NElR0o2Sf3J7iM9D0X/gme9aLWrpfq0nx/raU/iaiP8aqGPxbViUSkooFeRpO34lEd TQgi6udmWP/zjwn8PU4XjsD81OhIfX8= Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-440-a5KZbveKMP-989Wr7Co_UA-1; Wed, 14 Jul 2021 18:24:42 -0400 X-MC-Unique: a5KZbveKMP-989Wr7Co_UA-1 Received: by mail-qt1-f198.google.com with SMTP id e9-20020ac859890000b0290250be770d0fso2801455qte.15 for ; Wed, 14 Jul 2021 15:24:41 -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=HhQZE0aMebz+VkqQ8e/Lhzy5CWMNEBc/8Kwxv3nxTpObjV6/GfobyXnZYHLb7IbcwU YTRE1GS1NwrGEiZ49QnP6cw9und2Z3m06rqKbfavMJvDeC5gMGA3GCmqdk9207MEK20P /bmAYLsSNKFbHqPeW6muDSaFvbiMx5prsj/Mo1sLxuwRuBdpRdbOSdAILLvZV/gDylZ/ bOTUp78EPHpB58wTgRUOsMrW4VdJ7qp/RC2uu8WUj/TDhg4/ou/uInTCDq6OyP8Ao1dX Y7MSuXqX8e2qXQE+jj3W45P5rVy2tAEyVSBmjGCHkJeY5+q37g7fB8NK65TJBH1/6DSU QX5Q== X-Gm-Message-State: AOAM530e2QNHJJBkBThPBNVN3Bxdz1UZAr0dbqMmcAfb+M1Uf9whAgrs rDj4VPhU5VQmbgjmDv9KKnWvVXK5rkKiizvHH2hyB6pvD2ENQ54c3cKIsAf5tDV90qSncx1B1wn /dmqmSpJskC9Y7YcydfcFJuX7 X-Received: by 2002:ac8:44c2:: with SMTP id b2mr348914qto.216.1626301481568; Wed, 14 Jul 2021 15:24:41 -0700 (PDT) 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 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