From patchwork Mon Jan 22 19:42:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13526143 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 89207C47DD9 for ; Mon, 22 Jan 2024 19:43:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EEC3C6B009F; Mon, 22 Jan 2024 14:43:26 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E9B578D000D; Mon, 22 Jan 2024 14:43:26 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D15448D0001; Mon, 22 Jan 2024 14:43:26 -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 BFFF16B009F for ; Mon, 22 Jan 2024 14:43:26 -0500 (EST) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 6807CC05CE for ; Mon, 22 Jan 2024 19:43:26 +0000 (UTC) X-FDA: 81707971212.01.0B4D00C Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf29.hostedemail.com (Postfix) with ESMTP id B432C12001F for ; Mon, 22 Jan 2024 19:43:24 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=epISbfNM; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf29.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1705952604; 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=tPsvEkVgYnC21FVdKrk6yuEGSmSqLr856lIR4lg3IVs=; b=uJCEWTZ7JoQo4a1rtT8X9gXW/lvZhM1Kz5oxfBHOHUg4sSAI7o67pRPt5zNEuiIiJYS3cC gOKpuLbhJjhNMjcT+FhfcVnh0MSHrS57OLqj2mP2BlSC8EbrVOBFURpL5Z+mSsSQUk45hS Qsh3HccaMNf+bdJkiSBU9Pz390MjvTM= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=epISbfNM; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf29.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1705952604; a=rsa-sha256; cv=none; b=mI0YJCwrtWlTYXSdk8mJij6L9MJJXwn9SqbjXT7/mpoFne8rQl1K/YoGgw15PCitqay36h GRWciGNLBOeda5Bqdrj2Qf2Ywx7L7EQTWyD4KP3yGzzKcr7ooWUBe4Td3jgnNp+rRuJ7ve h/qcNayQvz+e4dmZLaA7qWxKB9j7CeY= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1705952604; 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=tPsvEkVgYnC21FVdKrk6yuEGSmSqLr856lIR4lg3IVs=; b=epISbfNMsFymD8bEd6Gm/XAxGGp/qvluvDYWJZKLuaRZlKTUbkJc9plqovX5P5OAsFL5bP VNAdgmM9xuyQRlzHWEhXmStfBWVItleAfwUyeUgIPZRTTKG5SoFrVJ0BYCdxeOiFfCIdmR F8bHg61fXVmOLKZxn7f1KTHPJio/pfI= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-678-YpW0JN5UPxiQRu3cMsFdBw-1; Mon, 22 Jan 2024 14:43:18 -0500 X-MC-Unique: YpW0JN5UPxiQRu3cMsFdBw-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 2508C87A38D; Mon, 22 Jan 2024 19:43:15 +0000 (UTC) Received: from t14s.redhat.com (unknown [10.39.195.114]) by smtp.corp.redhat.com (Postfix) with ESMTP id 192C124ED; Mon, 22 Jan 2024 19:43:08 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, David Hildenbrand , Andrew Morton , Matthew Wilcox , Ryan Roberts , Russell King , Catalin Marinas , Will Deacon , Dinh Nguyen , Michael Ellerman , Nicholas Piggin , Christophe Leroy , "Aneesh Kumar K.V" , "Naveen N. Rao" , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexander Gordeev , Gerald Schaefer , Heiko Carstens , Vasily Gorbik , Christian Borntraeger , Sven Schnelle , "David S. Miller" , linux-arm-kernel@lists.infradead.org, linuxppc-dev@lists.ozlabs.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, sparclinux@vger.kernel.org Subject: [PATCH v1 11/11] mm/memory: ignore writable bit in folio_pte_batch() Date: Mon, 22 Jan 2024 20:42:00 +0100 Message-ID: <20240122194200.381241-12-david@redhat.com> In-Reply-To: <20240122194200.381241-1-david@redhat.com> References: <20240122194200.381241-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.1 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: B432C12001F X-Stat-Signature: m6i7e4n88tqa5ypt5gaeodr9ckczfim9 X-HE-Tag: 1705952604-44604 X-HE-Meta: U2FsdGVkX1/j/8PuveOARv9h8BiSXVknmcpT95fiVcA7BqxeL43Eoh+Jp9TwglzlP55OwDbG7o4wQZpCOu25eihAFY9Zm2Xkpl7oJAuWjZ+naogHoPA9XbJFQ414ipWxbn/bsxh5kl/UwlQl4XJ1gSK6lxhlKQMeQ8qvhuwuqYjrZ1vbeHi2zFNLGdolLFEB9ERX2SMCSNPfrwft4UKeZ4lSYKKDFnLxcNGmqOaYBw1cMJVDa8FGldYLFP/t/AG9Hh+UrkiNzoGNJb+ZiH9FhivKzQv1HzF379q6/W71cAaVQFW/SB6L5f15Sxr3ayqsCKmc+lmI3oVNBZvX1yvzhZau/GnBUTqr6DkSPxsb06iZStoevlmsLjJslj4tNo12S0sKRodaD84AO6OZwVseFbUinGPERTZiAt5MIw+zaFBvpQc6BIg+wN5vWrxFwKhMzOQG0oOdRigTajQB+YHVGyDvra1cUt4EAfkaeWC+r/OIrQvH3oOj9F12ZyzGSXhFWzQUTRhIBDLTG8jgu3pNonh/bDesgVJ4DeSRkjLnHpq+rOh8CeIq3caDbTwL60wrg1c2Gn4nhDCltBx8DvuzORV5avDulXDaxvJ5FGPc2tQiFlEq8aP/Cb4IpNxKLBIe48+XOCI9Ndx/RYzMvt8JpxtUteAg82XjyyM+iBXQr1j84Y3r4kCJXB92V0lvUpeIEdZUJ3uM0o2F+Fxfb5TJYipsWLVEUKv+ADc7P9i02TIb7INdJTSuzjXRPiaBYh4Mp2VztJAykVFKKORKlS9ccNm9N8mPHnxg5U1KsQXoamqdFq+AENd4LaoFhg/52aBDtbhRlb9KUHxKMmezeqRrHKRxkdWwLZBVWrAEfmaDqIt65P1xLLjzZQbaez0e4tekS2897syxZkxFEdPscbCvLYQcOsoZoAzpd2B5L0etAMu5I314asNGFqD5KJ+9uwhZ2zc6DYmK6f9uOb612fV A+ztQ6xP u2qfUNJQ5UgV3JGvSrSsjx4kqRFLiDxM40OKxa9W2pGsNK7c16o76GmzXXreiOGVE9rmKk3SXeBmy2FFq6GcUNcgStUaPyNSXlqsZWVETeyIrbpVp83eMQU24N46w3dD7TnT4Oxh0m7CErK439lLp0OwX97ksIB1ILUurJZthA0IYxRF26MLNfW2AdGM88qsICZaU76VchwPPn1KqvDvk3jE6nJFBOmSbPYNx 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: List-Subscribe: List-Unsubscribe: ... and conditionally return to the caller if any pte except the first one is writable. fork() has to make sure to properly write-protect in case any PTE is writable. Other users (e.g., page unmaping) won't care. Signed-off-by: David Hildenbrand Reviewed-by: Ryan Roberts --- mm/memory.c | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index 341b2be845b6e..a26fd0669016b 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -955,7 +955,7 @@ static __always_inline void __copy_present_ptes(struct vm_area_struct *dst_vma, static inline pte_t __pte_batch_clear_ignored(pte_t pte) { - return pte_clear_soft_dirty(pte_mkclean(pte_mkold(pte))); + return pte_wrprotect(pte_clear_soft_dirty(pte_mkclean(pte_mkold(pte)))); } /* @@ -963,20 +963,29 @@ static inline pte_t __pte_batch_clear_ignored(pte_t pte) * pages of the same folio. * * All PTEs inside a PTE batch have the same PTE bits set, excluding the PFN. - * the accessed bit, dirty bit and soft-dirty bit. + * the accessed bit, dirty bit, soft-dirty bit and writable bit. + . If "any_writable" is set, it will indicate if any other PTE besides the + * first (given) PTE is writable. */ static inline int folio_pte_batch(struct folio *folio, unsigned long addr, - pte_t *start_ptep, pte_t pte, int max_nr) + pte_t *start_ptep, pte_t pte, int max_nr, bool *any_writable) { unsigned long folio_end_pfn = folio_pfn(folio) + folio_nr_pages(folio); const pte_t *end_ptep = start_ptep + max_nr; pte_t expected_pte = __pte_batch_clear_ignored(pte_next_pfn(pte)); pte_t *ptep = start_ptep + 1; + bool writable; + + if (any_writable) + *any_writable = false; VM_WARN_ON_FOLIO(!pte_present(pte), folio); while (ptep != end_ptep) { - pte = __pte_batch_clear_ignored(ptep_get(ptep)); + pte = ptep_get(ptep); + if (any_writable) + writable = !!pte_write(pte); + pte = __pte_batch_clear_ignored(pte); if (!pte_same(pte, expected_pte)) break; @@ -989,6 +998,9 @@ static inline int folio_pte_batch(struct folio *folio, unsigned long addr, if (pte_pfn(pte) == folio_end_pfn) break; + if (any_writable) + *any_writable |= writable; + expected_pte = pte_next_pfn(expected_pte); ptep++; } @@ -1010,6 +1022,7 @@ copy_present_ptes(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma { struct page *page; struct folio *folio; + bool any_writable; int err, nr; page = vm_normal_page(src_vma, addr, pte); @@ -1024,7 +1037,8 @@ copy_present_ptes(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma * by keeping the batching logic separate. */ if (unlikely(!*prealloc && folio_test_large(folio) && max_nr != 1)) { - nr = folio_pte_batch(folio, addr, src_pte, pte, max_nr); + nr = folio_pte_batch(folio, addr, src_pte, pte, max_nr, + &any_writable); if (folio_test_anon(folio)) { folio_ref_add(folio, nr); if (unlikely(folio_try_dup_anon_rmap_ptes(folio, page, @@ -1039,6 +1053,8 @@ copy_present_ptes(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma folio_dup_file_rmap_ptes(folio, page, nr); rss[mm_counter_file(page)] += nr; } + if (any_writable) + pte = pte_mkwrite(pte, src_vma); __copy_present_ptes(dst_vma, src_vma, dst_pte, src_pte, pte, addr, nr); return nr;