From patchwork Tue Nov 8 17:46:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13036634 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 7342DC4332F for ; Tue, 8 Nov 2022 17:47:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0B9C38E0002; Tue, 8 Nov 2022 12:47:15 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id F38F28E0001; Tue, 8 Nov 2022 12:47:14 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D65ED8E0002; Tue, 8 Nov 2022 12:47:14 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id C56E48E0001 for ; Tue, 8 Nov 2022 12:47:14 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 9696F403E6 for ; Tue, 8 Nov 2022 17:47:14 +0000 (UTC) X-FDA: 80111006388.29.2573C03 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf10.hostedemail.com (Postfix) with ESMTP id 23E32C000A for ; Tue, 8 Nov 2022 17:47:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1667929633; 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=qF4AawT1CUSPT7cjLSeaKxf0IPbLFxKsvIIMhgx85ZA=; b=f/qc6/LIBCWEfPLJ1lo69/XQuKosAqCS1RtjYF7XVgt1X32pu2iyPtG2kLjEcovjbYiWqX NU/EeAZW5MWMAHgRUo5vSDluPcZpVL5e2N9+2XTCK96xh0qOkcfVbEaeBTWIkLRn6B4DCF Em2z90KNbyFpK6h8J+oMWSRmIHxdT/s= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-611-fLpLc06yPnybKLVzRDVTAg-1; Tue, 08 Nov 2022 12:47:09 -0500 X-MC-Unique: fLpLc06yPnybKLVzRDVTAg-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 85E9A3806738; Tue, 8 Nov 2022 17:47:08 +0000 (UTC) Received: from t480s.fritz.box (unknown [10.39.194.18]) by smtp.corp.redhat.com (Postfix) with ESMTP id 57D3FC15BB5; Tue, 8 Nov 2022 17:47:05 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, linuxppc-dev@lists.ozlabs.org, David Hildenbrand , Linus Torvalds , Andrew Morton , Mel Gorman , Dave Chinner , Nadav Amit , Peter Xu , Andrea Arcangeli , Hugh Dickins , Vlastimil Babka , Michael Ellerman , Nicholas Piggin , Mike Rapoport , Anshuman Khandual Subject: [PATCH v2 3/7] mm/huge_memory: try avoiding write faults when changing PMD protection Date: Tue, 8 Nov 2022 18:46:48 +0100 Message-Id: <20221108174652.198904-4-david@redhat.com> In-Reply-To: <20221108174652.198904-1-david@redhat.com> References: <20221108174652.198904-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1667929634; a=rsa-sha256; cv=none; b=QMGw9VdxQJdsThzyAiAWGYcZ2vKzwmww5Sp0LsFpnPhdVmgAfJ/ko6hkCsDzFAqzvWA17f DigLZKQQMsU85v2d+rCIbF9u/euwGnCVyN68K7/Q6uZyCt8lmP9IOLcSCkh/o5EZzzD9/g czJFv3xZqS0AVkPnjOsPSzbL1+zwVbE= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="f/qc6/LI"; spf=pass (imf10.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1667929634; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=qF4AawT1CUSPT7cjLSeaKxf0IPbLFxKsvIIMhgx85ZA=; b=y/5r79W3lEjqKiSyWtOcI2n2yOqqTUhnBMOHJ1Z785xizBr20YrxQZix+813hbsEbuSoXO F5/IS1cmrOnj2T2fG3OkmgZsIjiow85eUIyJ8W89CWgC2CDulQPWWkCNqbpp9bQA1CNrSf T87nv3UP8C5wHBsrRBRjy/CeBrJya3U= Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="f/qc6/LI"; spf=pass (imf10.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Rspam-User: X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 23E32C000A X-Stat-Signature: o6opgssdzguaefapd7uzshg1h3z7i7p5 X-HE-Tag: 1667929633-406866 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: Let's replicate what we have for PTEs in can_change_pte_writable() also for PMDs. While this might look like a pure performance improvement, we'll us this to get rid of savedwrite handling in do_huge_pmd_numa_page() next. Place do_huge_pmd_numa_page() strategically good for that purpose. Note that MM_CP_TRY_CHANGE_WRITABLE is currently only set when we come via mprotect_fixup(). Signed-off-by: David Hildenbrand --- mm/huge_memory.c | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 1d47b3f7b877..eeba9c00df62 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -1344,6 +1344,36 @@ vm_fault_t do_huge_pmd_wp_page(struct vm_fault *vmf) return VM_FAULT_FALLBACK; } +static inline bool can_change_pmd_writable(struct vm_area_struct *vma, + unsigned long addr, pmd_t pmd) +{ + struct page *page; + + if (WARN_ON_ONCE(!(vma->vm_flags & VM_WRITE))) + return false; + + /* Don't touch entries that are not even readable (NUMA hinting). */ + if (pmd_protnone(pmd)) + return false; + + /* Do we need write faults for softdirty tracking? */ + if (vma_soft_dirty_enabled(vma) && !pmd_soft_dirty(pmd)) + return false; + + /* Do we need write faults for uffd-wp tracking? */ + if (userfaultfd_huge_pmd_wp(vma, pmd)) + return false; + + if (!(vma->vm_flags & VM_SHARED)) { + /* See can_change_pte_writable(). */ + page = vm_normal_page_pmd(vma, addr, pmd); + return page && PageAnon(page) && PageAnonExclusive(page); + } + + /* See can_change_pte_writable(). */ + return pmd_dirty(pmd); +} + /* FOLL_FORCE can write to even unwritable PMDs in COW mappings. */ static inline bool can_follow_write_pmd(pmd_t pmd, struct page *page, struct vm_area_struct *vma, @@ -1847,13 +1877,17 @@ int change_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, */ entry = pmd_clear_uffd_wp(entry); } + + /* See change_pte_range(). */ + if ((cp_flags & MM_CP_TRY_CHANGE_WRITABLE) && !pmd_write(entry) && + can_change_pmd_writable(vma, addr, entry)) + entry = pmd_mkwrite(entry); + ret = HPAGE_PMD_NR; set_pmd_at(mm, addr, pmd, entry); if (huge_pmd_needs_flush(oldpmd, entry)) tlb_flush_pmd_range(tlb, addr, HPAGE_PMD_SIZE); - - BUG_ON(vma_is_anonymous(vma) && !preserve_write && pmd_write(entry)); unlock: spin_unlock(ptl); return ret;