Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp2927424rdh; Mon, 27 Nov 2023 02:29:47 -0800 (PST) X-Google-Smtp-Source: AGHT+IF4WLtxPjWdaSe5NJM7HOUa6V/skz+RZB6y09YPXHRG6ketze5cHD5ogfAKXm8g+MA9QxE+ X-Received: by 2002:a05:6a00:1d09:b0:690:41a1:9b6a with SMTP id a9-20020a056a001d0900b0069041a19b6amr11607391pfx.5.1701080987378; Mon, 27 Nov 2023 02:29:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701080987; cv=none; d=google.com; s=arc-20160816; b=r2rKWjSxr83/fYfPiEBWdnL7dfo5vxupHZTDMjGDDGfUmh61SY/3asGnvvvIV1p+tn Ck3xDlKW8MLuTbOFBMoXDU2a5p8nFFbJ277Z+J8EwknkNS5I1lXPsHVHe4w9mELF0i7R fNXDlFrn9ufkU3OLH8kXkbuoXVxEUy013cmalEOMrkzWX5SRCrxUdbmWUcNtp1++e7cP kctGCC+48oV6D3BMWIBoXcCwp2AuZKsWbkPQv8gfovxMNE3+jtViET7SQnxVpWTwXV3q V19rxKI0SF4S8xL4IbD8YIGOcMXGRmHUjGgTAFVDmrmwe4xY/f7rRBj3Yt9h6p1+Oq81 NMqA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=shZQ+S7x4tTu8+8gjD68J2xWVVR//+1g56EcTBBB4d0=; fh=YFu1/ulTj8Z3PIqCnVWDSL0JORTqainAMpKIkzDAcGk=; b=VbrER9JkbiWqhXkAy/A25zv/kjTzF4eaNCeLiGfDKE3g10u5jJRZR99Xn8CCOkEZC2 xkWwCZjM7qx6HzJ1LA8Z3A8zhEWDjW/11dwPaNJ9yK7N7VkYhmr5dBCI0rW1tKvwD6yc yYkAGUdEydc1dDGj62UcNXfoQ1o7j2iv1r94a1iNDEhNNPOJJdr97Ul9OtjUE5id+5Ns zVchYXjb6xR5aayeP6u3My0A4FnHUGtTxIGmkml5eZ6mj4eb9FsSMVZ4J2yerUvLQkLj 6vhrja9alGhYQ0LZBVbfi8Jc14mBWEznXnELxwoVNvlcuIvWcPhxn4PRZaYAj0+DyDRs +Jkg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=YPCMJx6K; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id u27-20020a63141b000000b005897813624fsi9008034pgl.476.2023.11.27.02.29.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Nov 2023 02:29:47 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=YPCMJx6K; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 759128041ECD; Mon, 27 Nov 2023 02:29:44 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232981AbjK0K3H (ORCPT + 99 others); Mon, 27 Nov 2023 05:29:07 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38024 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232991AbjK0K3B (ORCPT ); Mon, 27 Nov 2023 05:29:01 -0500 Received: from mail-ua1-x933.google.com (mail-ua1-x933.google.com [IPv6:2607:f8b0:4864:20::933]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 32F4A10E6 for ; Mon, 27 Nov 2023 02:29:02 -0800 (PST) Received: by mail-ua1-x933.google.com with SMTP id a1e0cc1a2514c-7c46d224b45so926014241.3 for ; Mon, 27 Nov 2023 02:29:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701080941; x=1701685741; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=shZQ+S7x4tTu8+8gjD68J2xWVVR//+1g56EcTBBB4d0=; b=YPCMJx6KLFo/b+EPZZ0EBCTEwk+FgBCtQnf9j1eJhqXDVISNYrk/e6a88c3flwz2E2 HPXEg6Fjk/1kuYKWeU+gMs20xMGrMJrXzyCHZouHO+IrplXaZBVLV0+QZ7pmFbJSe0zk yQCiiDmmcOVktOLziArhvAEqdFczrvBzmAZ3PaqD7ddUgcPMGVuvtYgNzF0DKenSrHHD 1j7Ss/JIRHLsb8B91MrwehyFEIttOZKgLR+uL9xsZpZJg7oqGSoZI/xSdwv+2sztXXGn rQB2f9CE+HcMjmrmZWa/ETcHw/rYQn3lNUVNYPcZnG4oW+BT8OUZ17OzNGpRuKr7kXyp 9l/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701080941; x=1701685741; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=shZQ+S7x4tTu8+8gjD68J2xWVVR//+1g56EcTBBB4d0=; b=PecKbONihrj8Ci5/mRHvXOrPbMabFjbnNSx/2dmPlYub+RH3ljl4/hgrafGdGhFuL5 YeOasD68xmWyuu028PDmIdOHmqzMIpu69b1x/W8py5Y5gNT49IxEE8np0YjButQBmEB6 XmA2wvCLfXscH0LMpHp8MZxMpBQjfozk8gAdo7OP1o584rCXby8CqunEq2sUBHqU4zY4 /yBNHZVdFtgWurhcPrZzZDHKWoYu4ewHVeRZ6iimHUfu8aB8SpG9p9TiqqehY5eScNaF wWKu/ueqntEGKLnI/TgtpNY3wTl6K77N12XWxWBizIz3p0bxvz/Jc/DoQkd16+/Aed5A gvGw== X-Gm-Message-State: AOJu0YyRRHT9wkV6fZqphRBKt4TPLvDd7NHw8s3Hwwgwi7OwIFKKBvWP jWUK8QsYGruUiCAhFWhrmQdHONhioB7QJUz0zXo= X-Received: by 2002:a67:bd1a:0:b0:462:71ee:5ef6 with SMTP id y26-20020a67bd1a000000b0046271ee5ef6mr5438891vsq.17.1701080941077; Mon, 27 Nov 2023 02:29:01 -0800 (PST) MIME-Version: 1.0 References: <271f1e98-6217-4b40-bae0-0ac9fe5851cb@redhat.com> <20231127084217.13110-1-v-songbaohua@oppo.com> In-Reply-To: From: Barry Song <21cnbao@gmail.com> Date: Mon, 27 Nov 2023 23:28:49 +1300 Message-ID: Subject: Re: [PATCH v2 01/14] mm: Batch-copy PTE ranges during fork() To: Ryan Roberts Cc: david@redhat.com, akpm@linux-foundation.org, andreyknvl@gmail.com, anshuman.khandual@arm.com, ardb@kernel.org, catalin.marinas@arm.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 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email 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 (pete.vger.email [0.0.0.0]); Mon, 27 Nov 2023 02:29:44 -0800 (PST) On Mon, Nov 27, 2023 at 11:11=E2=80=AFPM Ryan Roberts wrote: > > On 27/11/2023 09:59, Barry Song wrote: > > On Mon, Nov 27, 2023 at 10:35=E2=80=AFPM Ryan Roberts wrote: > >> > >> On 27/11/2023 08:42, Barry Song wrote: > >>>>> + for (i =3D 0; i < nr; i++, page++) { > >>>>> + if (anon) { > >>>>> + /* > >>>>> + * If this page may have been pinned by= the > >>>>> + * parent process, copy the page immedi= ately for > >>>>> + * the child so that we'll always guara= ntee the > >>>>> + * pinned page won't be randomly replac= ed in the > >>>>> + * future. > >>>>> + */ > >>>>> + if (unlikely(page_try_dup_anon_rmap( > >>>>> + page, false, src_vma)))= { > >>>>> + if (i !=3D 0) > >>>>> + break; > >>>>> + /* Page may be pinned, we have = to copy. */ > >>>>> + return copy_present_page( > >>>>> + dst_vma, src_vma, dst_p= te, > >>>>> + src_pte, addr, rss, pre= alloc, > >>>>> + page); > >>>>> + } > >>>>> + rss[MM_ANONPAGES]++; > >>>>> + VM_BUG_ON(PageAnonExclusive(page)); > >>>>> + } else { > >>>>> + page_dup_file_rmap(page, false); > >>>>> + rss[mm_counter_file(page)]++; > >>>>> + } > >>>>> } > >>>>> - rss[MM_ANONPAGES]++; > >>>>> - } else if (page) { > >>>>> - folio_get(folio); > >>>>> - page_dup_file_rmap(page, false); > >>>>> - rss[mm_counter_file(page)]++; > >>>>> + > >>>>> + nr =3D i; > >>>>> + folio_ref_add(folio, nr); > >>>> > >>>> You're changing the order of mapcount vs. refcount increment. Don't. > >>>> Make sure your refcount >=3D mapcount. > >>>> > >>>> You can do that easily by doing the folio_ref_add(folio, nr) first a= nd > >>>> then decrementing in case of error accordingly. Errors due to pinned > >>>> pages are the corner case. > >>>> > >>>> I'll note that it will make a lot of sense to have batch variants of > >>>> page_try_dup_anon_rmap() and page_dup_file_rmap(). > >>>> > >>> > >>> i still don't understand why it is not a entire map+1, but an increme= nt > >>> in each basepage. > >> > >> Because we are PTE-mapping the folio, we have to account each individu= al page. > >> If we accounted the entire folio, where would we unaccount it? Each pa= ge can be > >> unmapped individually (e.g. munmap() part of the folio) so need to acc= ount each > >> page. When PMD mapping, the whole thing is either mapped or unmapped, = and its > >> atomic, so we can account the entire thing. > > > > Hi Ryan, > > > > There is no problem. for example, a large folio is entirely mapped in > > process A with CONPTE, > > and only page2 is mapped in process B. > > then we will have > > > > entire_map =3D 0 > > page0.map =3D -1 > > page1.map =3D -1 > > page2.map =3D 0 > > page3.map =3D -1 > > .... > > > >> > >>> > >>> as long as it is a CONTPTE large folio, there is no much difference w= ith > >>> PMD-mapped large folio. it has all the chance to be DoubleMap and nee= d > >>> split. > >>> > >>> When A and B share a CONTPTE large folio, we do madvise(DONTNEED) or = any > >>> similar things on a part of the large folio in process A, > >>> > >>> this large folio will have partially mapped subpage in A (all CONTPE = bits > >>> in all subpages need to be removed though we only unmap a part of the > >>> large folioas HW requires consistent CONTPTEs); and it has entire map= in > >>> process B(all PTEs are still CONPTES in process B). > >>> > >>> isn't it more sensible for this large folios to have entire_map =3D 0= (for > >>> process B), and subpages which are still mapped in process A has map_= count > >>> =3D0? (start from -1). > >>> > >>>> Especially, the batch variant of page_try_dup_anon_rmap() would only > >>>> check once if the folio maybe pinned, and in that case, you can simp= ly > >>>> drop all references again. So you either have all or no ptes to proc= ess, > >>>> which makes that code easier. > >> > >> I'm afraid this doesn't make sense to me. Perhaps I've misunderstood. = But > >> fundamentally you can only use entire_mapcount if its only possible to= map and > >> unmap the whole folio atomically. > > > > > > > > My point is that CONTPEs should either all-set in all 16 PTEs or all ar= e dropped > > in 16 PTEs. if all PTEs have CONT, it is entirely mapped; otherwise, > > it is partially > > mapped. if a large folio is mapped in one processes with all CONTPTEs > > and meanwhile in another process with partial mapping(w/o CONTPTE), it = is > > DoubleMapped. > > There are 2 problems with your proposal, as I see it; > > 1) the core-mm is not enlightened for CONTPTE mappings. As far as it is > concerned, its just mapping a bunch of PTEs. So it has no hook to inc/dec > entire_mapcount. The arch code is opportunistically and *transparently* m= anaging > the CONT_PTE bit. > > 2) There is nothing to say a folio isn't *bigger* than the contpte block;= it may > be 128K and be mapped with 2 contpte blocks. Or even a PTE-mapped THP (2M= ) and > be mapped with 32 contpte blocks. So you can't say it is entirely mapped > unless/until ALL of those blocks are set up. And then of course each bloc= k could > be unmapped unatomically. > > For the PMD case there are actually 2 properties that allow using the > entire_mapcount optimization; It's atomically mapped/unmapped through the= PMD > and we know that the folio is exactly PMD sized (since it must be at leas= t PMD > sized to be able to map it with the PMD, and we don't allocate THPs any b= igger > than PMD size). So one PMD map or unmap operation corresponds to exactly = one > *entire* map or unmap. That is not true when we are PTE mapping. well. Thanks for clarification. based on the above description, i agree the current code might make more sense by always using mapcount in subpage. I gave my proposals as I thought we were always CONTPTE size for small-THP then we could drop the loop to iterate 16 times rmap. if we do it entirely, we only need to do dup rmap once for all 16 PTEs by increasing entire_map. BTW, I have concerns that a variable small-THP size will really work as userspace is probably friendly to only one fixed size. for example, userspace heap management might be optimized to a size for freeing memory to the kernel. it is very difficult for the heap to adapt to various sizes at the same time. frequent unmap/fre= e size not equal with, and particularly smaller than small-THP size will defeat all efforts to use small-THP. > > > > > Since we always hold ptl to set or drop CONTPTE bits, set/drop is > > still atomic in a > > spinlock area. > > > >> > >>>> > >>>> But that can be added on top, and I'll happily do that. > >>>> > >>>> -- > >>>> Cheers, > >>>> > >>>> David / dhildenb > >>> > > Thanks Barry