Received: by 2002:a05:7412:5112:b0:fa:6e18:a558 with SMTP id fm18csp500050rdb; Tue, 23 Jan 2024 06:15:29 -0800 (PST) X-Google-Smtp-Source: AGHT+IGCYUaQjUsHxHvKxEmbBDiUCg/nmpjXVDcVMeXRoSGmLQiQDMt1CjfO9PmoQeKT8ok8tH34 X-Received: by 2002:a17:90a:9dca:b0:290:4e40:5adc with SMTP id x10-20020a17090a9dca00b002904e405adcmr8441565pjv.45.1706019328951; Tue, 23 Jan 2024 06:15:28 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706019328; cv=pass; d=google.com; s=arc-20160816; b=Hn1Lh5v05TmGgEV8mqaYEYZia1fD1YpBvPPmcj9fGIod8+jZip2kC/EXDAFA4+erKp F9L6WkipjFdtNle2uehiFRUlHzUC+dPM+GXtawTkMnliz1fnU5/9+lKg0ocp//iTwfwf 0Z003NVQP9OR8PWXE5VDboMbUOdk0c9TGioh607N9HmLdYWaMxgBuNi2xpyvG2kJ/k+W ipd8/1bt6vcO9d58JERB4rI5wQjQjQnuK/p6hHwmuBcAlzlf0Kbml93j/ybm+Lp4fEXr EDhm74CPe7gZPGrP7UAWuuI92hfS61nmw9y3m52/Ps2PtOu8K5is45V3VxO+sK+VyICD nd6g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id; bh=ijAG7htsP2sT9AsDoi4BzAFWEz8FwY5yVfOsaWOwYog=; fh=cLjkAanmB8aobrGIK/HTftjty0FQAaQ+aRvIv8PHcYU=; b=rosmL08XAgtGRlEgGW4Cp3BCw9T6ZjAoXejWexUUFPU+JrX3+tmvdorTmRg6TZpike BDfTtZU8BFj1zilhmGrXZsoP8OcakOXgBB/Vgv/sFX3YVPOmmmrrgeN2QCu+YHhmhzP6 PEscH6QCpEbp0ayS3v20FHw8UcaCs1mMNEB1xeqYploZhaBE6zJYYeckXSjnaesyQZTr kKz+tiZ3JaR7flqFmVoqCEbcqS+wnxjspOgsJh9MWHQ/+ckjjkqhMwXCHjKy18ubyaw5 8J+R5p0svhsj7yaU4g8Wdr7OfhtDdO+1G75mYnEhqWVvlFXsmSAf7ZcF+aTTZ3NZ1sHW e4Sw== ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-35395-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-35395-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id bv10-20020a632e0a000000b005d45c4f4749si83028pgb.181.2024.01.23.06.15.28 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jan 2024 06:15:28 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-35395-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-35395-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-35395-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id B8716B226A3 for ; Tue, 23 Jan 2024 13:42:46 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4640C5F54E; Tue, 23 Jan 2024 13:42:38 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 997B25F542; Tue, 23 Jan 2024 13:42:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706017357; cv=none; b=jGBUy1xufhgc7//h6zETeWYq9PFsLrCASE1EFMNK8fvXz6+d6QE+OlmUg8+NiqtJQh2bGsoaLywttHmp2aQmPXgwjzMisp8x0zofBRBK5q+vP5GYU3erPFsx6yk/J+dHPjKAkjC9ivpNVB9oOFsKH0lY8FqL/RuSUvMhKcGV794= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706017357; c=relaxed/simple; bh=uUJ2HO+3UqHTTrvrYPI/QJXNZTXarf00MBuJxNlTReI=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=DIE5RD/2XfE6wfD1em51zp9HV9D1+EnBEkx2ektx7dWcQb8hfDQd61iE/vQpFyxmz7ZfpgY0XhoCa4k9hrLu9TzOjioiX0d4TQNcc1eOx1Y+y6RbyAITLSaLh8rFr2BWbnhZzJfPIUCy6Vz8tIw7CUM+2yu+iQSH/xjnDAfGAic= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 5D631FEC; Tue, 23 Jan 2024 05:43:20 -0800 (PST) Received: from [10.57.77.165] (unknown [10.57.77.165]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id A8BB63F762; Tue, 23 Jan 2024 05:42:30 -0800 (PST) Message-ID: <94d33a07-c59a-4315-9c64-8b4d959ca1f4@arm.com> Date: Tue, 23 Jan 2024 13:42:29 +0000 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v1 10/11] mm/memory: ignore dirty/accessed/soft-dirty bits in folio_pte_batch() Content-Language: en-GB To: David Hildenbrand , linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, Andrew Morton , Matthew Wilcox , 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 References: <20240122194200.381241-1-david@redhat.com> <20240122194200.381241-11-david@redhat.com> <59592b50-fe89-4b32-8490-2e6c296f972f@arm.com> <76740e33-9b52-4e23-b407-8ae38bac15ec@redhat.com> From: Ryan Roberts In-Reply-To: <76740e33-9b52-4e23-b407-8ae38bac15ec@redhat.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On 23/01/2024 13:06, David Hildenbrand wrote: > On 23.01.24 13:25, Ryan Roberts wrote: >> On 22/01/2024 19:41, David Hildenbrand wrote: >>> Let's ignore these bits: they are irrelevant for fork, and will likely >>> be irrelevant for upcoming users such as page unmapping. >>> >>> Signed-off-by: David Hildenbrand >>> --- >>>   mm/memory.c | 10 ++++++++-- >>>   1 file changed, 8 insertions(+), 2 deletions(-) >>> >>> diff --git a/mm/memory.c b/mm/memory.c >>> index f563aec85b2a8..341b2be845b6e 100644 >>> --- a/mm/memory.c >>> +++ b/mm/memory.c >>> @@ -953,24 +953,30 @@ static __always_inline void __copy_present_ptes(struct >>> vm_area_struct *dst_vma, >>>       set_ptes(dst_vma->vm_mm, addr, dst_pte, pte, nr); >>>   } >>>   +static inline pte_t __pte_batch_clear_ignored(pte_t pte) >>> +{ >>> +    return pte_clear_soft_dirty(pte_mkclean(pte_mkold(pte))); >>> +} >>> + >>>   /* >>>    * Detect a PTE batch: consecutive (present) PTEs that map consecutive >>>    * pages of the same folio. >>>    * >>>    * All PTEs inside a PTE batch have the same PTE bits set, excluding the PFN. >> >> nit: last char should be a comma (,) not a full stop (.) >> >>> + * the accessed bit, dirty bit and soft-dirty bit. >>>    */ >>>   static inline int folio_pte_batch(struct folio *folio, unsigned long addr, >>>           pte_t *start_ptep, pte_t pte, int max_nr) >>>   { >>>       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_next_pfn(pte); >>> +    pte_t expected_pte = __pte_batch_clear_ignored(pte_next_pfn(pte)); >>>       pte_t *ptep = start_ptep + 1; >>>         VM_WARN_ON_FOLIO(!pte_present(pte), folio); >>>         while (ptep != end_ptep) { >>> -        pte = ptep_get(ptep); >>> +        pte = __pte_batch_clear_ignored(ptep_get(ptep)); >>>             if (!pte_same(pte, expected_pte)) >>>               break; >> >> I think you'll lose dirty information in the child for private mappings? If the >> first pte in a batch is clean, but a subsequent page is dirty, you will end up >> setting all the pages in the batch as clean in the child. Previous behavior >> would preserve dirty bit for private mappings. >> >> In my version (v3) that did arbitrary batching, I had some fun and games >> tracking dirty, write and uffd_wp: >> https://lore.kernel.org/linux-arm-kernel/20231204105440.61448-2-ryan.roberts@arm.com/ >> >> Also, I think you will currently either set soft dirty on all or none of the >> pages in the batch, depending on the value of the first. I previously convinced >> myself that the state was unimportant so always cleared it in the child to >> provide consistency. > > Good points regarding dirty and soft-dirty. I wanted to avoid passing flags to > folio_pte_batch(), but maybe that's just what we need to not change behavior. I think you could not bother with the enforce_uffd_wp - just always enforce uffd-wp. So that's one simplification vs mine. Then you just need an any_dirty flag following the same pattern as your any_writable. Then just set dirty on the whole batch in the child if any were dirty in the parent. Although now I'm wondering if there is a race here... What happens if a page in the parent becomes dirty after you have checked it but before you write protect it? Isn't that already a problem with the current non-batched version? Why do we even to preserve dirty in the child for private mappings?