Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp135062pxb; Fri, 15 Jan 2021 09:12:55 -0800 (PST) X-Google-Smtp-Source: ABdhPJznbyE6O6FoVmCMtmOM2wFhvHsrWa0vLt41Rtgucd00uNrWkvja6v8lSO3Vi+892SZV7NVo X-Received: by 2002:a50:8a62:: with SMTP id i89mr10266264edi.388.1610730774813; Fri, 15 Jan 2021 09:12:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610730774; cv=none; d=google.com; s=arc-20160816; b=lZELj5PoOXmfHFSdgUesMGjW/mHV0w2A/5rlawJXf4D4IfoljzErnAyKTkPs7t2cDG +CHO3Th6Ph0TeqPVOiRzmTW4lXyCi9aIuzVagvVTq9RnwZtJT5eaWXCvkKj9bDDnLl9y +I8Gy7XtyopYZPwqcDdej5vxPvGnML3tr4F/35eCuxNiCrLwlC4yaN5OpdieIcLMqEpk XzfMEKXrOYBmFIXbbsb6X0Pae6d2auQy9gzNPf2Eb4Ae5zl9eFs9KW2Yd5TzEWidHgZK bM4Ez5nT6aTrF6u+oHyT/C3/8+lf79q7sZWCUIRx8ar7b17vFcv3TQwUk+vPJ49RTyy5 gZXQ== 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=dvgubUamx9bzZRkUGwNzgfOj+y21NKvpsf3CqKI90Qc=; b=GP+pJDWk5sVAWw/x5im3YXtI6cICi6u8IqLm7kBQg/id8KFBnI0Hnp1el86OpqGyri HxQyg1e75ld3zXqVQ8vPl1GrJgxiXoONqWfpfksMkD+ebqOamtjaygAHXZshdPMj2HDy oMzbZbhOnO/yspaQyV5UUTn6+WGh7t+FUwcqnFAvPjx/0E1Otob8O4Dv8vipN3EvRR50 5vHzg8x1D7DZa+Rv4FyNQEfgny1Cgwu62iGeyZDJJiIIf14SESVE3IdLIaRZcqcJRF5C Dzj1unJZR88p764zN6128om0troRJK7n6vH1dBzDNUIQQOCN9U+E9g2OXFi2bFRNuq1t LGHw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=RNE4hbkW; 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 hs7si1097112ejc.147.2021.01.15.09.12.30; Fri, 15 Jan 2021 09:12:54 -0800 (PST) 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=RNE4hbkW; 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 S1733296AbhAORLW (ORCPT + 99 others); Fri, 15 Jan 2021 12:11:22 -0500 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:47319 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1733179AbhAORLI (ORCPT ); Fri, 15 Jan 2021 12:11:08 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1610730582; 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=dvgubUamx9bzZRkUGwNzgfOj+y21NKvpsf3CqKI90Qc=; b=RNE4hbkWVHlbkriKXu7UED7K8SFHO13jbmx3Iify+410q3CnGabYSlKh6n9uIgRG70Z6yr Ll1QkhvD2tQVJ3+wew1DL8FRZkzlAsoytQudPaNwnvOZrJDIqQA2Imc4ECq3w/lLX9hBFl SIsYpwLGreIzvSaYfUavX+Lm5yZjPaw= Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-255-B8ElwNVOOHq5pWQKVciMuQ-1; Fri, 15 Jan 2021 12:09:40 -0500 X-MC-Unique: B8ElwNVOOHq5pWQKVciMuQ-1 Received: by mail-qv1-f70.google.com with SMTP id h1so8238667qvr.7 for ; Fri, 15 Jan 2021 09:09:40 -0800 (PST) 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=dvgubUamx9bzZRkUGwNzgfOj+y21NKvpsf3CqKI90Qc=; b=rCkcdBam5bAH67wwrHJ071fVobim4BVqiCqoWH4Q/mjfCl4eZV5NLs/6pSEfrKA4NF W93jmxwpz2M0nXEZjIdKTxl4FxNwEghHExeJI1Je9baC3plPazAI5ioViROTG3bs5Ngl urbu5zhS3bYJgEQMo9nA6mxI0DFQwnYzE5TvGd8PHbt9JIaqBgLjXC9mZNmkFKR3QUPF uurUugkSGgTdhXmnzQHdNOIvXjHVnXcnr9JWks1ea5yLm4T/Fcdh5w9tMW3z8EiloHH7 k00j3ysoJzjXIgBsqXo0KnCKqX2dfu3NbipvJaY0dmjrsocCtO3VtxoFdFss5u7Rlh5C pCow== X-Gm-Message-State: AOAM533Gos/k1+Wtf4jc7s28OFR2Rm98qGoWA0fiUzTS6oI3TeXDhL4f WPqzL/4iQmqW5x6ktyhXVdCoBBwxXIVjYQx9+2Eng618fTa2nVkYCWM9xMDEfmNB/2JXcr1i0+R /bvlZG7b9A5GkbBkmNmp54hYRwR8ID2YfA9azRsUxvvWQBwNTQeMquSMUwXnikqPQmYbslNbPTQ == X-Received: by 2002:a37:aa57:: with SMTP id t84mr13905370qke.348.1610730579848; Fri, 15 Jan 2021 09:09:39 -0800 (PST) X-Received: by 2002:a37:aa57:: with SMTP id t84mr13905333qke.348.1610730579535; Fri, 15 Jan 2021 09:09:39 -0800 (PST) Received: from localhost.localdomain ([142.126.83.202]) by smtp.gmail.com with ESMTPSA id d123sm5187840qke.95.2021.01.15.09.09.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Jan 2021 09:09:38 -0800 (PST) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Mike Rapoport , Mike Kravetz , peterx@redhat.com, Jerome Glisse , "Kirill A . Shutemov" , Hugh Dickins , Axel Rasmussen , Matthew Wilcox , Andrew Morton , Andrea Arcangeli , Nadav Amit Subject: [PATCH RFC 15/30] shmem/userfaultfd: Allows file-back mem to be uffd wr-protected on thps Date: Fri, 15 Jan 2021 12:08:52 -0500 Message-Id: <20210115170907.24498-16-peterx@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210115170907.24498-1-peterx@redhat.com> References: <20210115170907.24498-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 c9390fd673fe..055871322007 100644 --- a/mm/mprotect.c +++ b/mm/mprotect.c @@ -296,8 +296,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.26.2