From patchwork Thu Jan 25 19:32:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13531563 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 32E04C48260 for ; Thu, 25 Jan 2024 19:34:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B8F53280011; Thu, 25 Jan 2024 14:34:13 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B4010280004; Thu, 25 Jan 2024 14:34:13 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9B995280011; Thu, 25 Jan 2024 14:34:13 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 84F0F280004 for ; Thu, 25 Jan 2024 14:34:13 -0500 (EST) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 66BA1C0415 for ; Thu, 25 Jan 2024 19:34:13 +0000 (UTC) X-FDA: 81718834386.11.1A1E491 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf16.hostedemail.com (Postfix) with ESMTP id A5DE518000B for ; Thu, 25 Jan 2024 19:34:11 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=AqkIzT97; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf16.hostedemail.com: domain of david@redhat.com designates 170.10.129.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=1706211251; 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=KHoljgu5TCYuagRFnizBrkwj14eAAX8FKS7MlgFMZ5g=; b=o/tGksI3DnQak2lJPntgHICVT4Mc9JU+WwgCsrCr8kSbzMd2pTcADDVkEk0s5+2EtNabtE Idix5073QDiQL8OC2Qnq/S+7j3dVFMZlgRLiMyY+GiWAZd6R+OumD93c8H2a3wawPxjq8Y kpoU4+ig7ws6UnhmiahAT54o1R24yR8= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=AqkIzT97; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf16.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706211251; a=rsa-sha256; cv=none; b=tOvOT05lK5YEfluiH6GQ2IRWicYbCdxSbUzDRBMSFt1yoVm+rJxM6Ce/umPpbbzZHREiQ9 qoyF+XXchZBzG0+2O0emdo515w/GWrVXmMZ7v//gLf/nc+7bQAayZsrfVqcPqc8sOeUNKH BPu1OPfFnS2d+bbYkYG5oyaccFHi7bs= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1706211251; 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=KHoljgu5TCYuagRFnizBrkwj14eAAX8FKS7MlgFMZ5g=; b=AqkIzT972mtUmvqxJwonY52hBQkn/bk5vgs7W+QppGFUUtxYWkPIkeRVzV7YqTyZUwl6Ur zu7FsCPsYalq+7+7mnn1btjT7mE5heol8nA7bM5M1pwYGm1Gu0cBMYmNv8GBIZCljViD0e repUviIOdiZA8+K4nm8R8eFhfR+KcNg= 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-403-CrPk6FszOsesuz_bqE2sxg-1; Thu, 25 Jan 2024 14:34:06 -0500 X-MC-Unique: CrPk6FszOsesuz_bqE2sxg-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (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 88F408432A2; Thu, 25 Jan 2024 19:34:05 +0000 (UTC) Received: from t14s.redhat.com (unknown [10.39.193.154]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3B6DA492BC6; Thu, 25 Jan 2024 19:34:00 +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 v2 15/15] mm/memory: ignore writable bit in folio_pte_batch() Date: Thu, 25 Jan 2024 20:32:27 +0100 Message-ID: <20240125193227.444072-16-david@redhat.com> In-Reply-To: <20240125193227.444072-1-david@redhat.com> References: <20240125193227.444072-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.9 X-Rspamd-Queue-Id: A5DE518000B X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: gxz7y73p7g9asbbq65z14ak5i1nncfop X-HE-Tag: 1706211251-835054 X-HE-Meta: U2FsdGVkX19giSVQqFKACf53yTQmljKfEpZr+JL4eW8fxpV7ap08qxF7ne7uMctaUba06HpL6+pMzb6IXyeZxkeiJpalwui2NgmYsEDKnQz/tDjygI8dVdFbd2JDnjIpwIRtJ1/4XHrqqJe0AFEfetMtfs7CJlcjWFk4BfLorNo7y1s3uIzY0jj+Bly+QwhbzdksluJBpd6CrKmM1pEr60FbUBB2GqlK8FyAAD3I9FDMsNUp3LRDMEI+kJlmmTI7v87zyIdAJ5iHf5J9z75OZ/83GGA9UnDnAelduBB4W/jMVKJDjY3MuCD0Y/vfvTSIT4cBuGsczC8+mLq/nEBd0n4cPWA6Vi1xMo8NNk45ciktym/mjkPE/dHjEOtPO2jrP2YuNJwmzLeS88CxZyji3RJST7+0KdoO/uX3wsoiX/GwgS11OnDo+SsSwjBossdruelplNWSwc8rgM/YtVKqDbLkocRDv6KZMmpiI3B9rsevzPnBWHqabRZoOinbXXY9Udt8iIcl0KvE8wuwJDhyFMuOLKUt6n7LvdMhEZakSi9foyhD5GbNGGgagLvmqUfRnrDrL9VOilVQsKElez3rY7Si4aeqSBYbfx8/5CMhcCAqUFZUbNIojeo2NVUG3IntGfGxWYwgt8d2LqWduhrRa4dmQ227JxAp5ortmg190VORjj7EN8sVB08Tk1j5TgEeTFT3xv4YhCvY22vFCnFPG9iEBEXWzHte1d55aKA/ppRsN5WTSnwN3ikFuzglFW5U6yfyjgeGX/d0SSNONJk/aTYlcSDh/eRw4EG9hxPOXvLJZX3g9Ko2ZwGioV7FFdKFnyxNX9fSItb7UjAeghvBNrTJSfpZO73GR8gSDicUpWzoG+wuSIJSF+S6xcXv+XGSRWweAJFQrEw/cBtWkE1tDK+sP1b7nvU1zxOFmxS8uArVsWzP0ljW7SmsVBa2Hwmymrf3OfqWAfbl6AYKij1 /rtk0YEc MeYAqKsKMh4QUjKfJLhUrjARABFJ2Zi/SmfDjTyMM+VnHgz0aSUlD0AybEjWmfNZk4s1eTfpCjctAoCGoS2UhZqHUrefdstJw1jJ9YAHtbudOK72Qayy9+xgg4V/1LSlJq5nkGBq4euDYeyU8SX+3pDRAYeTq5h/ca4+gS8zIZVf/dAztE9kOmMqLJr0GHhpSlQUb5PAvTuE2/R85ay+j/uGxR9lV9Eq5zZ0dXiFhCz1uIDFIyY3MiiHUzPnIdJGK/7+/V5Ph1liO2Zy0RkR3vIdkQA== 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) are expected to not care. Reviewed-by: Ryan Roberts Signed-off-by: David Hildenbrand --- mm/memory.c | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index b3f035fe54c8d..64442da3df6d4 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -968,7 +968,7 @@ static inline pte_t __pte_batch_clear_ignored(pte_t pte, fpb_t flags) pte = pte_mkclean(pte); if (likely(flags & FPB_IGNORE_SOFT_DIRTY)) pte = pte_clear_soft_dirty(pte); - return pte_mkold(pte); + return pte_wrprotect(pte_mkold(pte)); } /* @@ -976,21 +976,32 @@ static inline pte_t __pte_batch_clear_ignored(pte_t pte, fpb_t flags) * 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 (with FPB_IGNORE_DIRTY) and soft-dirty bit - * (with FPB_IGNORE_SOFT_DIRTY). + * the accessed bit, writable bit, dirty bit (with FPB_IGNORE_DIRTY) and + * soft-dirty bit (with FPB_IGNORE_SOFT_DIRTY). + * + * 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, fpb_t flags) + pte_t *start_ptep, pte_t pte, int max_nr, fpb_t flags, + 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), flags); 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), flags); + pte = ptep_get(ptep); + if (any_writable) + writable = !!pte_write(pte); + pte = __pte_batch_clear_ignored(pte, flags); if (!pte_same(pte, expected_pte)) break; @@ -1003,6 +1014,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++; } @@ -1024,6 +1038,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; fpb_t flags = 0; int err, nr; @@ -1044,7 +1059,8 @@ copy_present_ptes(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma if (!vma_soft_dirty_enabled(src_vma)) flags |= FPB_IGNORE_SOFT_DIRTY; - nr = folio_pte_batch(folio, addr, src_pte, pte, max_nr, flags); + nr = folio_pte_batch(folio, addr, src_pte, pte, max_nr, flags, + &any_writable); folio_ref_add(folio, nr); if (folio_test_anon(folio)) { if (unlikely(folio_try_dup_anon_rmap_ptes(folio, page, @@ -1058,6 +1074,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;