Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp2898289rdh; Mon, 27 Nov 2023 01:24:42 -0800 (PST) X-Google-Smtp-Source: AGHT+IHMaO+mcfEms6X1O3cKGmLqszpaVvEZWkm1Uk8AWJFeOv1YKzkyAI1qlhIgJzJQLz+B0276 X-Received: by 2002:a17:903:22c5:b0:1cc:2a23:cbb4 with SMTP id y5-20020a17090322c500b001cc2a23cbb4mr10745843plg.35.1701077082439; Mon, 27 Nov 2023 01:24:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701077082; cv=none; d=google.com; s=arc-20160816; b=nX+tGVg7edc8e49JUjhSrbDPr5itqE4kqKLzac925zZjAvAsKpTkLTKPG3G4Ra6mh7 gz6U9PorD4kvPxZi/UxNOscabibqnnXL9NSQR5rwCEk8pFJI5d6ltiWIvM+Ma6fPP8AO +mFPgb4A78yMtjdqh368ywlwoJoh7oUmAKE/fRIhwTU1mKxoeXocJEOxpm1B60yjwwNI 6daQKP35Ex6aLkaQ0u6AJH2neAw7oQI7Fq7EAz7SmSZ5WcyO+nLeB9jrtk9OjQCkMvjk JtbtF+2f6QdchkcqQwl/QwjRei98udNw4e80/wKKyU6uFJId1/XWGCA7QceQLDj5W+le 82tQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id; bh=FCYY/SYvwN0Gr8pGKZISrO0rCGWM8fs0vtdrHNaiAV8=; fh=Iq90pYtK3oOdLRS6wcOYtCi0+G8Np5/ZJK5ks93k25I=; b=wuecAJ+q9rNGoxghtRpO/sVixWSyBqIRZgdXg5N9GR2MPZm0DdfzqsC/rypmEystV1 7NsO7KpPi9UoJfknZsSwLr+kydhxXuhxQ2YjaKayeVRXdSNR1UX7pWyBjT8RGVLEObTZ F6Ms1wV6PdWzh+m+Xc/itN5O5f1WmXdHFTBy5WDa2iTeJNZyqgBrRiCQd/dQhdWUIqqZ HuvkDMGvrQuDSiNeOr5uKtZBnN6rkWqHVbJpYDZMwG+IGZoWaiEGN66MzjsN/R7KmFLN mMYhgdCj+pdccDtvOVm4uLCta8FKU0m/Xr0OdULU9vvA5kbguLwMvSdb7PoitPu6eWqq FCLw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id t8-20020a17090340c800b001cf9a4c9c90si8642842pld.307.2023.11.27.01.24.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Nov 2023 01:24:42 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id DF9A08092DB2; Mon, 27 Nov 2023 01:24:40 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232686AbjK0JYb (ORCPT + 99 others); Mon, 27 Nov 2023 04:24:31 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41986 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231339AbjK0JYa (ORCPT ); Mon, 27 Nov 2023 04:24:30 -0500 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id D8354CB for ; Mon, 27 Nov 2023 01:24:36 -0800 (PST) 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 EE1882F4; Mon, 27 Nov 2023 01:25:23 -0800 (PST) Received: from [10.57.73.191] (unknown [10.57.73.191]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 1D3083F6C4; Mon, 27 Nov 2023 01:24:32 -0800 (PST) Message-ID: <755343a1-ce94-4d38-8317-0925e2dae3bc@arm.com> Date: Mon, 27 Nov 2023 09:24:31 +0000 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2 01/14] mm: Batch-copy PTE ranges during fork() Content-Language: en-GB To: Barry Song <21cnbao@gmail.com> Cc: akpm@linux-foundation.org, andreyknvl@gmail.com, anshuman.khandual@arm.com, ardb@kernel.org, catalin.marinas@arm.com, david@redhat.com, dvyukov@google.com, glider@google.com, james.morse@arm.com, jhubbard@nvidia.com, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, mark.rutland@arm.com, maz@kernel.org, oliver.upton@linux.dev, ryabinin.a.a@gmail.com, suzuki.poulose@arm.com, vincenzo.frascino@arm.com, wangkefeng.wang@huawei.com, will@kernel.org, willy@infradead.org, yuzenghui@huawei.com, yuzhao@google.com, ziy@nvidia.com References: <20231115163018.1303287-2-ryan.roberts@arm.com> <20231127055414.9015-1-v-songbaohua@oppo.com> From: Ryan Roberts In-Reply-To: <20231127055414.9015-1-v-songbaohua@oppo.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=0.6 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_BLOCKED, SORTED_RECIPS,SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Mon, 27 Nov 2023 01:24:41 -0800 (PST) On 27/11/2023 05:54, Barry Song wrote: >> +copy_present_ptes(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, >> + pte_t *dst_pte, pte_t *src_pte, >> + unsigned long addr, unsigned long end, >> + int *rss, struct folio **prealloc) >> { >> struct mm_struct *src_mm = src_vma->vm_mm; >> unsigned long vm_flags = src_vma->vm_flags; >> pte_t pte = ptep_get(src_pte); >> struct page *page; >> struct folio *folio; >> + int nr = 1; >> + bool anon; >> + bool any_dirty = pte_dirty(pte); >> + int i; >> >> page = vm_normal_page(src_vma, addr, pte); >> - if (page) >> + if (page) { >> folio = page_folio(page); >> - if (page && folio_test_anon(folio)) { >> - /* >> - * If this page may have been pinned by the parent process, >> - * copy the page immediately for the child so that we'll always >> - * guarantee the pinned page won't be randomly replaced in the >> - * future. >> - */ >> - folio_get(folio); >> - if (unlikely(page_try_dup_anon_rmap(page, false, src_vma))) { >> - /* Page may be pinned, we have to copy. */ >> - folio_put(folio); >> - return copy_present_page(dst_vma, src_vma, dst_pte, src_pte, >> - addr, rss, prealloc, page); >> + anon = folio_test_anon(folio); >> + nr = folio_nr_pages_cont_mapped(folio, page, src_pte, addr, >> + end, pte, &any_dirty); > > in case we have a large folio with 16 CONTPTE basepages, and userspace > do madvise(addr + 4KB * 5, DONTNEED); nit: if you are offsetting by 5 pages from addr, then below I think you mean page0~page4 and page6~15? > > thus, the 4th basepage of PTE becomes PTE_NONE and folio_nr_pages_cont_mapped() > will return 15. in this case, we should copy page0~page3 and page5~page15. No I don't think folio_nr_pages_cont_mapped() will return 15; that's certainly not how its intended to work. The function is scanning forwards from the current pte until it finds the first pte that does not fit in the batch - either because it maps a PFN that is not contiguous, or because the permissions are different (although this is being relaxed a bit; see conversation with DavidH against this same patch). So the first time through this loop, folio_nr_pages_cont_mapped() will return 5, (page0~page4) then the next time through the loop we will go through the !present path and process the single swap marker. Then the 3rd time through the loop folio_nr_pages_cont_mapped() will return 10. Thanks, Ryan > > but the current code is copying page0~page14, right? unless we are immediatly > split_folio to basepages in zap_pte_range(), we will have problems? > >> + >> + for (i = 0; i < nr; i++, page++) { >> + if (anon) { >> + /* >> + * If this page may have been pinned by the >> + * parent process, copy the page immediately for >> + * the child so that we'll always guarantee the >> + * pinned page won't be randomly replaced in the >> + * future. >> + */ >> + if (unlikely(page_try_dup_anon_rmap( >> + page, false, src_vma))) { >> + if (i != 0) >> + break; >> + /* Page may be pinned, we have to copy. */ >> + return copy_present_page( >> + dst_vma, src_vma, dst_pte, >> + src_pte, addr, rss, prealloc, >> + page); >> + } >> + rss[MM_ANONPAGES]++; >> + VM_BUG_ON(PageAnonExclusive(page)); >> + } else { >> + page_dup_file_rmap(page, false); >> + rss[mm_counter_file(page)]++; >> + } > > Thanks > Barry >