Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp4644225pxy; Tue, 27 Apr 2021 09:26:26 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwqX3CEruq6brEBMtD8QSjRNf1+nrsteueRigWRAZl0SLG/oJ22OQjMtAH7VJUp3NTtq/Em X-Received: by 2002:a17:906:3e97:: with SMTP id a23mr24461349ejj.440.1619540786009; Tue, 27 Apr 2021 09:26:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619540786; cv=none; d=google.com; s=arc-20160816; b=Zdh4ue38zVBh69AtfjAj6nFmXR/F70wEeBnU/+PE7WN/3OkPySqDaXN1GCkYJWZqA1 aB2cPYAzSU9Qlt8zpYz+ndexxHsGorvzkvAP2eP9GvVBQuUVE9QYibX4VeANBYa2Rq6Z BN8dJOLH7va1WKPGHKhYHe+Zo1GUm0TXVktY7i6E7HZWwMwvvMcCGz+NTnlTCZNcGq5o 04JsZslH5AvJ3lggZc+G0NY8j6eCbE7in7r6eR5ltnLukTkHxc23WUSy55Uu+9gq6oSx ivWKKCCS6XhD7uv5DRPFXmJ37t9xaOiDYemNjk0kgqRUwrwiJ45H/iT3wIkVLgbotAZZ dFNg== 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=l1z1o5mbCVa78Hzla2EzzKhxkMDgZgiPiKG4mm1uLIY=; b=0Oe+wNgdMTi/nGqYnZvJFe2K+OTLB3SMFOR7Astbt6MmjU7r6M9ITPxU2UmtOWvwd4 cKEd1oNNchOIjxFLVou2oan+QxzzZDpNY4VFCJlRAbNxhcjoh99C+LhrGCK9PebZrwEC pq4WGttDt2JTTLbr6XHBR/zEJj0a4PFqrs4CXuJkOF+tGv6AueibH+aWJCg7uyx8hEKF nNrNqCx28/DNGi+h2wEBWVMpMqzxtn4xD3rhCeWXMgMJdt1V/ONtH4iVacu0FlSLoXZZ VI+bG7yzhEtcj5Px/3h2sF1lhehkhsAqe+RwYshthyKwe3nrbaXi9kyBCuXP+UoB4yq+ Bybg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=WEWIAYeN; 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 dk19si250752ejb.372.2021.04.27.09.26.01; Tue, 27 Apr 2021 09:26:25 -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=WEWIAYeN; 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 S238792AbhD0QZF (ORCPT + 99 others); Tue, 27 Apr 2021 12:25:05 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:53203 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237459AbhD0QQs (ORCPT ); Tue, 27 Apr 2021 12:16:48 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1619540164; 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=l1z1o5mbCVa78Hzla2EzzKhxkMDgZgiPiKG4mm1uLIY=; b=WEWIAYeNYs3WtK2dM6DjC0kvM9bmP7f8otW/T7SNXPxui18YVudy8nk5Y6PXmYPf+fsJjM qqsJy544774zJb0aFvT0GjIaFfT1C1mtvom7iMwnC5YRt4eCcN7VoLs/PZm9d0lZkbdKL3 o52az9gf3NGgPfO4uKhpW2AZGIVUlTQ= Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-581-Ulv9VXvnNc29x5z8IRGOKA-1; Tue, 27 Apr 2021 12:13:57 -0400 X-MC-Unique: Ulv9VXvnNc29x5z8IRGOKA-1 Received: by mail-qk1-f197.google.com with SMTP id s143-20020a3745950000b029028274263008so23356790qka.9 for ; Tue, 27 Apr 2021 09:13:57 -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=l1z1o5mbCVa78Hzla2EzzKhxkMDgZgiPiKG4mm1uLIY=; b=CS18JiLmLuEQWkakAcTHRZKXBXgcpGraCYW4iZpS3dTnY6FWYfECUW7VFFIhPISwSk mOnJHAfes95Gyto4R0tgIQD5Dit31NbwywOS6PH6+7HThi5TJ5ffjDjY9ipRHaJQ9VJg 9AlUEFxxWSMeayhlwV2qKp0c5z4YcO1J/5P0Bp3q+gF6mpHqGVzf8nfF4pBxCfSlMuId 8yMaIl/ahPo9rwOkS60k0B9LIB2Ww+ZltmtWDzR5D9+cckjw4nzryj3j15Tb2Q0WbgfS qwqTA2/llKe9uW3Ym4nUho83QGk7fgQ5FSWjXdGsnoz8WCPU68Anr1ZZ3wzuP6smBTzF nj+A== X-Gm-Message-State: AOAM530HcHuvuaf7hFAaDa5++gyNF0H6MiSvg7cfwtuQaR4ZTw978pak CTICDl2V7GBnedhwEp2tzxeFZrucX7/dgp5k6KD+v1XF6tCbw4ZERq1lfM6JkwewtleW0Y+8wxz Gxe4nAtSh1Kol2ky9fzNhq8T6 X-Received: by 2002:a05:620a:133c:: with SMTP id p28mr11069458qkj.3.1619540036830; Tue, 27 Apr 2021 09:13:56 -0700 (PDT) X-Received: by 2002:a05:620a:133c:: with SMTP id p28mr11069428qkj.3.1619540036590; Tue, 27 Apr 2021 09:13:56 -0700 (PDT) Received: from xz-x1.redhat.com (bras-base-toroon474qw-grc-77-184-145-104-227.dsl.bell.ca. [184.145.104.227]) by smtp.gmail.com with ESMTPSA id v66sm3103621qkd.113.2021.04.27.09.13.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Apr 2021 09:13:55 -0700 (PDT) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Nadav Amit , Miaohe Lin , Mike Rapoport , Andrea Arcangeli , Hugh Dickins , peterx@redhat.com, Jerome Glisse , Mike Kravetz , Jason Gunthorpe , Matthew Wilcox , Andrew Morton , Axel Rasmussen , "Kirill A . Shutemov" Subject: [PATCH v2 21/24] hugetlb/userfaultfd: Allow wr-protect none ptes Date: Tue, 27 Apr 2021 12:13:14 -0400 Message-Id: <20210427161317.50682-22-peterx@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210427161317.50682-1-peterx@redhat.com> References: <20210427161317.50682-1-peterx@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Teach hugetlbfs code to wr-protect none ptes just in case the page cache existed for that pte. Meanwhile we also need to be able to recognize a uffd-wp marker pte and remove it for uffd_wp_resolve. Since at it, introduce a variable "psize" to replace all references to the huge page size fetcher. Reviewed-by: Mike Kravetz Signed-off-by: Peter Xu --- mm/hugetlb.c | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index d9ff7db14175d..fa9af9c893512 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -5264,7 +5264,7 @@ unsigned long hugetlb_change_protection(struct vm_area_struct *vma, pte_t *ptep; pte_t pte; struct hstate *h = hstate_vma(vma); - unsigned long pages = 0; + unsigned long pages = 0, psize = huge_page_size(h); bool shared_pmd = false; struct mmu_notifier_range range; bool uffd_wp = cp_flags & MM_CP_UFFD_WP; @@ -5284,13 +5284,19 @@ unsigned long hugetlb_change_protection(struct vm_area_struct *vma, mmu_notifier_invalidate_range_start(&range); i_mmap_lock_write(vma->vm_file->f_mapping); - for (; address < end; address += huge_page_size(h)) { + for (; address < end; address += psize) { spinlock_t *ptl; - ptep = huge_pte_offset(mm, address, huge_page_size(h)); + ptep = huge_pte_offset(mm, address, psize); if (!ptep) continue; ptl = huge_pte_lock(h, mm, ptep); if (huge_pmd_unshare(mm, vma, &address, ptep)) { + /* + * When uffd-wp is enabled on the vma, unshare + * shouldn't happen at all. Warn about it if it + * happened due to some reason. + */ + WARN_ON_ONCE(uffd_wp || uffd_wp_resolve); pages++; spin_unlock(ptl); shared_pmd = true; @@ -5314,12 +5320,21 @@ unsigned long hugetlb_change_protection(struct vm_area_struct *vma, else if (uffd_wp_resolve) newpte = pte_swp_clear_uffd_wp(newpte); set_huge_swap_pte_at(mm, address, ptep, - newpte, huge_page_size(h)); + newpte, psize); pages++; } spin_unlock(ptl); continue; } + if (unlikely(is_swap_special_pte(pte))) { + WARN_ON_ONCE(!pte_swp_uffd_wp_special(pte)); + /* + * This is changing a non-present pte into a none pte, + * no need for huge_ptep_modify_prot_start/commit(). + */ + if (uffd_wp_resolve) + huge_pte_clear(mm, address, ptep, psize); + } if (!huge_pte_none(pte)) { pte_t old_pte; @@ -5332,6 +5347,12 @@ unsigned long hugetlb_change_protection(struct vm_area_struct *vma, pte = huge_pte_clear_uffd_wp(pte); huge_ptep_modify_prot_commit(vma, address, ptep, old_pte, pte); pages++; + } else { + /* None pte */ + if (unlikely(uffd_wp)) + /* Safe to modify directly (none->non-present). */ + set_huge_pte_at(mm, address, ptep, + pte_swp_mkuffd_wp_special(vma)); } spin_unlock(ptl); } -- 2.26.2