From patchwork Mon Jan 29 12:46:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13535529 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 6316BC47DA9 for ; Mon, 29 Jan 2024 12:48:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F33A28D0006; Mon, 29 Jan 2024 07:48:32 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id EE3848D0001; Mon, 29 Jan 2024 07:48:32 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DAB938D0006; Mon, 29 Jan 2024 07:48:32 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id C5ADD8D0001 for ; Mon, 29 Jan 2024 07:48:32 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 9D5C9160577 for ; Mon, 29 Jan 2024 12:48:32 +0000 (UTC) X-FDA: 81732327264.19.1AB1E78 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf29.hostedemail.com (Postfix) with ESMTP id E06ED12001B for ; Mon, 29 Jan 2024 12:48:30 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=KjeRfVvG; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf29.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=1706532510; 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=kPofd6LlL0FntZsVxpMLUzci6vAQKc1aG/cPn5S11O0=; b=qaM2KzSCzyhm3hDTXcQXKYWCQ15XG4L12rsRzrskwqUuqBGKXHTZ+vO9cKxWLeD36ze9FB KLeP42SHu/+QUO0+mNfWx8lgEC12wRrh5PK418bbaXOl0dqFz30MyjM0oaW8VlLEkwKhv2 5+ivAuP0mIf0RPV9VCb2wglrnWWUvlw= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=KjeRfVvG; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf29.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=1706532510; a=rsa-sha256; cv=none; b=uzo6sqoI30A+nZflPokjq2kgozpIp5hQ4egV7uP/07v/7aBl0NPZBXOH2Op8X5ZXEW6Fav 0lmv8Jn6Av0TTLezT/YU61OExzQ+MDHx2DifMYi47ju9lM8njYduPDzWU9IIdhUzy5s+O3 Ge+c/6FH3QVx0VlzqHb2sDTs5IigA9s= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1706532510; 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=kPofd6LlL0FntZsVxpMLUzci6vAQKc1aG/cPn5S11O0=; b=KjeRfVvG+5QpX2CYiPT2SE6634umI5mDUdNheSMhDv0jw9AOx1JJXHZTWJwuSTQXpFzU+L 8BUmSoVQ8cOop6J49klZIrAlGwqjDZlLKVZbiyoXzrlWP9B7XCUA0JOxmuZ9WWyC2q/Wmf 8Ec0Eb/dlzQUNIwNa/Lic4sRwMRwQk0= 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-413-PehoyRVrMfeAXOFdLIlxVA-1; Mon, 29 Jan 2024 07:48:26 -0500 X-MC-Unique: PehoyRVrMfeAXOFdLIlxVA-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (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 898A186F129; Mon, 29 Jan 2024 12:48:25 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.194.46]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4CC6D8B; Mon, 29 Jan 2024 12:48:20 +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 v3 15/15] mm/memory: ignore writable bit in folio_pte_batch() Date: Mon, 29 Jan 2024 13:46:49 +0100 Message-ID: <20240129124649.189745-16-david@redhat.com> In-Reply-To: <20240129124649.189745-1-david@redhat.com> References: <20240129124649.189745-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.5 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: E06ED12001B X-Stat-Signature: p53bpx78g3e9urrkrsuwn3yih5wxzbd8 X-Rspam-User: X-HE-Tag: 1706532510-456813 X-HE-Meta: U2FsdGVkX1+lhIdQtbTOmYHiUmR5rbFhD+XiQk83jM0Vikp4z971vhN4RDHy9G99Ysp55NyNYNFznCtzg0wYUfmv2l+Ls/ZvP78CGWi/cqdj7YB/jpppG+JTMOKQ6kETIYyi4GC4S4Qu8Rq5u0+n7JxBdojRYgMjmo9AXl2N53lo1NMvvImXg/Gp6c/k6AzqKiCebpQWPUh3sYpC1/mRb1B/Tol98g1XqPxidbms8lGVlyLI/rOpnbAF0jXfLjvRY20rj1HK8SM3XxOOFp6RKXIufa9QMlYB3DK/soLXn6An2tMKLJB4537uHVuUM6Xtxxj58sfrEL/tfRLwbSEZWv+mZBi+aw833iT9e6xXhMCvtONwWhqfhxZRTQtCUSBDSFiHJZlvHwUDSEaZDj+/VjwVp0ct98Fkp2dK9fepORUpyQgbxNeWIR4Oqa/Y9AGlBMnfDdGzIA5GW6msWser8LK7sYAgxGgHuhjaBeT4blip7lH9LZVvSJxZKDnH2jnQKnZpH4N9Xp76/TOFEWGdZJ0F8fI967NYN7lLbu+5Dy9nOcUY/dch+AOwNIXwaBpZxhAk07OKrRa2P3TI53f1b0+hWcraN0tPuhTLY7KqgWWwVkPAaZDCtjx0oufuyfEJJ/tkK2rzTstJL24LCjhBP4mTb6L6ZmFNZpmNiUw/YonracsYycbEEmhrXoqdOkDdgynlzMBOY6S5YqgUj9kPK+vgNDd/8zNzsVqqlW4yqPxScEQTrt3M2smMoRF/WBcqeTpe22fmyr/gycFDe5B6jQypEiTZVlpOR2vO/oUfuQynHmk5VIhGKcQU1WVnNcUJgKx8m3hdsXaIi4ccy2YyWzo1Lgjku9KY2+ansHqLHB8POf6svIHOXx9s5rq+iYxVhV8uFqEKNL5oK4MEddCuIw2VeuAEhLSaHoisoOp0AxHhpiCfgmXu845BpJIpkPdnuAaS34QHkw8a4VNUHLm OoiumphK xtickqf+77vRhdwFK8FhAiooPgVwVbYEsSqcO3OALbvTr8ckQOO+m/9vFXHmKNF90F7qc0ejmUQ3VCGl+vlwaXtxTaWF7s6RKy+McyP/3f107Cc3bd7OifE0aHckaTa+UyQUm96jBYFOei3MBI0jtY07N38NgYwqUJQ8iShvGMH5hM4lL+Gx101dG7hcqu2vFQE6NgTpsZCUwc3f5EV7Evk/NARdIj2qEEOwfxpmv5xm24ZV/mq3NfXNxsnEey31ZX41bgOjwbonKxKETnGvST9tgPw== 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 b2ec2b6b54c7..b05fd28dbce1 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(folio)] += 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;