From patchwork Tue Apr 5 01:49:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12800972 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 5EA46C433F5 for ; Tue, 5 Apr 2022 01:56:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 54E846B0088; Mon, 4 Apr 2022 21:49:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4FD706B0089; Mon, 4 Apr 2022 21:49:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 39DE26B008A; Mon, 4 Apr 2022 21:49:28 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0018.hostedemail.com [216.40.44.18]) by kanga.kvack.org (Postfix) with ESMTP id 2C1AE6B0088 for ; Mon, 4 Apr 2022 21:49:28 -0400 (EDT) Received: from smtpin22.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id E6FE7ACB7F for ; Tue, 5 Apr 2022 01:49:17 +0000 (UTC) X-FDA: 79321142754.22.3E5BE52 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf03.hostedemail.com (Postfix) with ESMTP id 5EF782000D for ; Tue, 5 Apr 2022 01:49:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1649123356; 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=tYxpsQItjDK7cACelYDmbYGqmACKurZeMNuIXszIjeQ=; b=h0LNy9N8EhOh4XVEDtl4ewWSzzzvYmUFBSqwqOVcz1QtfWCXQD7sSWQLnGYgY13GPMdkog XkbpMRe3G73vCdf2ikaD9BDY4ddsb2GJh6MUG01T3Sy1Iju4G0s9gvoRPxs+NnF3wjJii4 sNQOVPVY/CZJ8O/XqFva1XIDP/m+Xao= Received: from mail-il1-f197.google.com (mail-il1-f197.google.com [209.85.166.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-651-XSmM5VsUNU-3CK4YyezSow-1; Mon, 04 Apr 2022 21:49:15 -0400 X-MC-Unique: XSmM5VsUNU-3CK4YyezSow-1 Received: by mail-il1-f197.google.com with SMTP id q6-20020a056e0215c600b002c2c4091914so7202988ilu.14 for ; Mon, 04 Apr 2022 18:49:15 -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=tYxpsQItjDK7cACelYDmbYGqmACKurZeMNuIXszIjeQ=; b=Wbr2Gz8MAfGfUSwQ5R0L6051Qq6PF+TK+sn1i7RMxzJYYwzksN44Z0pC4m2x9NqpdL jyPb97cYX0DotxYC74/F/FNAFmKEJ8P2MO1eUhO/m6oDTtiycClbXlBCDr6VuOewUFX4 iDSL7W9UxnS1lR8RrLNONQysg2zMnJezSbGai7S3VJn3bd+Bg9uI3f+sgnox/YUL9B2e dUXEGXkC8WN1+wq07HO4jgYzxt+Ghx/Z3BGuwG6QnNBr+5VZrF/QmSbRG3O7z+LqszET eKFA7Yw67siGwaBIia+ffMye89HHOihtM3fwfCaj6HZcm1GHz80XclN7BrKskqv/p+Rh QlWA== X-Gm-Message-State: AOAM533wRjfCLmnw+0HnYZ2tl2fvthZhO4Gr6Y0kIvDms06jqy/asea9 eK7w3LGZis2KU80GJcyafNaT1lYqEKCLq1h6xrfcp4ftnXWKwa8FQZgk5OmFuhtM95/4+wB08ml qmmrpvT7ZHFY= X-Received: by 2002:a92:c545:0:b0:2ca:1066:3d6 with SMTP id a5-20020a92c545000000b002ca106603d6mr616127ilj.229.1649123355268; Mon, 04 Apr 2022 18:49:15 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzDsITZBgZ19aGlVu4ZTn7lfD33BDzVrxkkBzyP8LrQwVfSiH2pEhd9gNDZ7UGnRWBIoTtlXw== X-Received: by 2002:a92:c545:0:b0:2ca:1066:3d6 with SMTP id a5-20020a92c545000000b002ca106603d6mr616119ilj.229.1649123355058; Mon, 04 Apr 2022 18:49:15 -0700 (PDT) Received: from localhost.localdomain (cpec09435e3e0ee-cmc09435e3e0ec.cpe.net.cable.rogers.com. [99.241.198.116]) by smtp.gmail.com with ESMTPSA id m5-20020a92c525000000b002ca19cc6e43sm5578396ili.20.2022.04.04.18.49.13 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 04 Apr 2022 18:49:14 -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 16/23] mm/hugetlb: Allow uffd wr-protect none ptes Date: Mon, 4 Apr 2022 21:49:12 -0400 Message-Id: <20220405014912.14815-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: rspam04 X-Rspamd-Queue-Id: 5EF782000D X-Stat-Signature: msphj3wfna495i7qj4n6qjzbd5937u4o Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=h0LNy9N8; dmarc=pass (policy=none) header.from=redhat.com; spf=none (imf03.hostedemail.com: domain of peterx@redhat.com has no SPF policy when checking 170.10.129.124) smtp.mailfrom=peterx@redhat.com X-Rspam-User: X-HE-Tag: 1649123357-67956 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: 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 | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 9317b790161d..578c48ef931a 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -6225,7 +6225,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; @@ -6245,13 +6245,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; @@ -6281,12 +6287,20 @@ 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(pte_marker_uffd_wp(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; unsigned int shift = huge_page_shift(hstate_vma(vma)); @@ -6300,6 +6314,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, + make_pte_marker(PTE_MARKER_UFFD_WP)); } spin_unlock(ptl); }