Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp3578164rdh; Mon, 27 Nov 2023 19:14:21 -0800 (PST) X-Google-Smtp-Source: AGHT+IEZDGtJfApc/G30HBJLBeE/e6B7UtVUCcrS4MxZH87JOPlvWr++cEgTYnKJROQeCK+negR8 X-Received: by 2002:a17:902:d303:b0:1cc:5691:5113 with SMTP id b3-20020a170902d30300b001cc56915113mr15575325plc.26.1701141261629; Mon, 27 Nov 2023 19:14:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701141261; cv=none; d=google.com; s=arc-20160816; b=pC4roIvmLHeONI/eoRelu8ktbcsLzOQwLUkWFFNlnE/4GCMNmVkcPdu18XLp3nROhc LvkhSeuYOX5ocgGgocXjvn5WywPXpUEI1AnZR3xASqJrfE4KMuBdxO2F5CZt1E5rFhRr xr60aS0xZm++kCNOPtVCZQyfzPuVWqkVQCoPHbaE2IuTchfu7mStAON5L7uELICLlBiF 72cmTCsH9ki6BANWI7FeEEvxK9BzaLgbZfQhRZd+NZgDElDYbRFRdCNKXolVpVPOmVd8 PnRxau9yAXSm8M9VzLwotbnAmP6qZwzfrmjEezR/x1IE4fW8O0/ThBlnn4S1wRkofhwS f3fg== 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=kfP5mMSfLJ6WwJw6W2yDqYJjxBNmPJwPrm2m/+Ywi/Q=; fh=fwQ63E6BrTD8OutgKisz6F9K+TCNPGHO0jvxNcUhLsk=; b=n3okuEKh0BCOOpJPD0/w6U5HIZtE0BYJ/Zefs7qiWIW9g1LDizSYEgasex1XailUa9 3+o6AGFiQ0ZFOLhDdjBAvHDR1zZZD/GxEKbResuXg91QcqzsRLtMkOI/w7yN/cz1EgCK hiwcOfjOoo+XOeXFFTY3A0bv14vW61LBd2RZtz6Yfp0KnnzqYTrbSXxVlmdPbHr5rFs0 pDj6F2BiU75VuHVo9ChHnllJZNATaEIvDa4DmjW23VCG6eXVzhC0cLDlUtQrRLqnv9hH GgR2UeONU2AT1nUVWZol6CSV/SkGBMKX6/lVQ7jygQ7Cd8W6DV9srIFY8MXAFe51NxMp HSWQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=F85aA4Aj; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 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 lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id i9-20020a170902c94900b001cfb52101fesi7251119pla.214.2023.11.27.19.14.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Nov 2023 19:14:21 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=F85aA4Aj; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 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 lipwig.vger.email (Postfix) with ESMTP id 13D8E80A4F64; Mon, 27 Nov 2023 19:14:19 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234543AbjK1DNw (ORCPT + 99 others); Mon, 27 Nov 2023 22:13:52 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36512 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234545AbjK1DNv (ORCPT ); Mon, 27 Nov 2023 22:13:51 -0500 Received: from mail-pj1-x102f.google.com (mail-pj1-x102f.google.com [IPv6:2607:f8b0:4864:20::102f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 34C021A5 for ; Mon, 27 Nov 2023 19:13:54 -0800 (PST) Received: by mail-pj1-x102f.google.com with SMTP id 98e67ed59e1d1-2859551886cso2750496a91.2 for ; Mon, 27 Nov 2023 19:13:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701141233; x=1701746033; 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=kfP5mMSfLJ6WwJw6W2yDqYJjxBNmPJwPrm2m/+Ywi/Q=; b=F85aA4Ajl5IvZkiDOHK08YIkNzS547acVnF3q+T9K0iViDmbnmjFG35Pn4c+86W41m cTAnDDA6OCCDbQKHDYJtCk2+1AEa5an3uZTqAHdzaiXIKUfu2PyCn0drGu+ZFbxBodkh VlwCWjakmtvMEeLQOZK/YC7vkpXnO+I+2ppt3oALYTtc1V7W8k7JYlvDTUxvhJFAn3kL 2oQpZqIpCPwFtPPt+I66HLIwcczdtIsVHYg1nhMPYYMOHY6YHhPesMW7+hitLsgXAPP2 gSqi1WjF+3Tug8B6A6XXD3N/WMFuFN0/k3IW64Y7lLBzl2HgPhzmYQmczFt97MbI0wob w32Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701141233; x=1701746033; 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=kfP5mMSfLJ6WwJw6W2yDqYJjxBNmPJwPrm2m/+Ywi/Q=; b=hs6E5+c1UQgKg1bJdCkvQrs1oaX1pPnwZ+Fz7lfRFaZB1l5B/6YQZ42mSNbJ3Jlbo/ OIQYHNB9mAunrapHeRpmyK3apSXJfzGMgOevys+quJRI4d8a9xvoCgvLASqlSlJ+txql XoMySOtb0fcZHzsCIuB9hBgKqTE/Bxk1ukXppuuMD2WA6szCsh60Cb9vwhve4Pwn5bzA s+SgJoj93OLTvvf3Y8sWNq/D2QWn1sshDj62MFiOrpWnTTPplhJWxPfl6vy4SqwulTmy Q0Wo/4VeGUVWXxU77l8OycxKdQSaeGHe0Hiby38buIBDrYyFIiBIAbmKMulbtLSpVYwv Pm8Q== X-Gm-Message-State: AOJu0Yy/YxdNMZ3lO+MZYYNWZUHIc+oYFFcKYfmTcUiJ/B+vWTq0a8Nb JXnapjrFgUfhJVMIwcpytkSOl4RHDLRf95qlNQ8= X-Received: by 2002:a17:90b:1809:b0:27d:b244:cd28 with SMTP id lw9-20020a17090b180900b0027db244cd28mr15031170pjb.42.1701141233551; Mon, 27 Nov 2023 19:13:53 -0800 (PST) MIME-Version: 1.0 References: <20231115163018.1303287-1-ryan.roberts@arm.com> <20231127031813.5576-1-v-songbaohua@oppo.com> <234021ba-73c2-474a-82f9-91e1604d5bb5@arm.com> In-Reply-To: <234021ba-73c2-474a-82f9-91e1604d5bb5@arm.com> From: Yang Shi Date: Mon, 27 Nov 2023 19:13:41 -0800 Message-ID: Subject: Re: [PATCH v2 00/14] Transparent Contiguous PTEs for User Mappings To: Ryan Roberts Cc: Barry Song <21cnbao@gmail.com>, 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 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 lipwig.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 (lipwig.vger.email [0.0.0.0]); Mon, 27 Nov 2023 19:14:19 -0800 (PST) On Mon, Nov 27, 2023 at 1:15=E2=80=AFAM Ryan Roberts = wrote: > > On 27/11/2023 03:18, Barry Song wrote: > >> Ryan Roberts (14): > >> mm: Batch-copy PTE ranges during fork() > >> arm64/mm: set_pte(): New layer to manage contig bit > >> arm64/mm: set_ptes()/set_pte_at(): New layer to manage contig bit > >> arm64/mm: pte_clear(): New layer to manage contig bit > >> arm64/mm: ptep_get_and_clear(): New layer to manage contig bit > >> arm64/mm: ptep_test_and_clear_young(): New layer to manage contig bi= t > >> arm64/mm: ptep_clear_flush_young(): New layer to manage contig bit > >> arm64/mm: ptep_set_wrprotect(): New layer to manage contig bit > >> arm64/mm: ptep_set_access_flags(): New layer to manage contig bit > >> arm64/mm: ptep_get(): New layer to manage contig bit > >> arm64/mm: Split __flush_tlb_range() to elide trailing DSB > >> arm64/mm: Wire up PTE_CONT for user mappings > >> arm64/mm: Implement ptep_set_wrprotects() to optimize fork() > >> arm64/mm: Add ptep_get_and_clear_full() to optimize process teardown > > > > Hi Ryan, > > Not quite sure if I missed something, are we splitting/unfolding CONTPT= ES > > in the below cases > > The general idea is that the core-mm sets the individual ptes (one at a t= ime if > it likes with set_pte_at(), or in a block with set_ptes()), modifies its > permissions (ptep_set_wrprotect(), ptep_set_access_flags()) and clears th= em > (ptep_clear(), etc); This is exactly the same interface as previously. > > BUT, the arm64 implementation of those interfaces will now detect when a = set of > adjacent PTEs (a contpte block - so 16 naturally aligned entries when usi= ng 4K > base pages) are all appropriate for having the CONT_PTE bit set; in this = case > the block is "folded". And it will detect when the first PTE in the block > changes such that the CONT_PTE bit must now be unset ("unfolded"). One of= the > requirements for folding a contpte block is that all the pages must belon= g to > the *same* folio (that means its safe to only track access/dirty for thec= ontpte > block as a whole rather than for each individual pte). > > (there are a couple of optimizations that make the reality slightly more > complicated than what I've just explained, but you get the idea). > > On that basis, I believe all the specific cases you describe below are al= l > covered and safe - please let me know if you think there is a hole here! > > > > > 1. madvise(MADV_DONTNEED) on a part of basepages on a CONTPTE large fol= io > > The page will first be unmapped (e.g. ptep_clear() or ptep_get_and_clear(= ), or > whatever). The implementation of that will cause an unfold and the CONT_P= TE bit > is removed from the whole contpte block. If there is then a subsequent > set_pte_at() to set a swap entry, the implementation will see that its no= t > appropriate to re-fold, so the range will remain unfolded. > > > > > 2. vma split in a large folio due to various reasons such as mprotect, > > munmap, mlock etc. > > I'm not sure if PTEs are explicitly unmapped/remapped when splitting a VM= A? I > suspect not, so if the VMA is split in the middle of a currently folded c= ontpte > block, it will remain folded. But this is safe and continues to work corr= ectly. > The VMA arrangement is not important; it is just important that a single = folio > is mapped contiguously across the whole block. Even with different permissions, for example, read-only vs read-write? The mprotect() may change the permission. It should be misprogramming per ARM ARM. > > > > > 3. try_to_unmap_one() to reclaim a folio, ptes are scanned one by one > > rather than being as a whole. > > Yes, as per 1; the arm64 implementation will notice when the first entry = is > cleared and unfold the contpte block. > > > > > In hardware, we need to make sure CONTPTE follow the rule - always 16 > > contiguous physical address with CONTPTE set. if one of them run away > > from the 16 ptes group and PTEs become unconsistent, some terrible > > errors/faults can happen in HW. for example > > Yes, the implementation obeys all these rules; see contpte_try_fold() and > contpte_try_unfold(). the fold/unfold operation is only done when all > requirements are met, and we perform it in a manner that is conformant to= the > architecture requirements (see contpte_fold() - being renamed to > contpte_convert() in the next version). > > Thanks for the review! > > Thanks, > Ryan > > > > > case0: > > addr0 PTE - has no CONTPE > > addr0+4kb PTE - has CONTPTE > > .... > > addr0+60kb PTE - has CONTPTE > > > > case 1: > > addr0 PTE - has no CONTPE > > addr0+4kb PTE - has CONTPTE > > .... > > addr0+60kb PTE - has swap > > > > Unconsistent 16 PTEs will lead to crash even in the firmware based on > > our observation. > > > > Thanks > > Barry > > > > > >