From mboxrd@z Thu Jan 1 00:00:00 1970 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 064B4C4345F for ; Fri, 3 May 2024 23:40:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 62D196B008A; Fri, 3 May 2024 19:40:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5DBB96B008C; Fri, 3 May 2024 19:40:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4CA606B0092; Fri, 3 May 2024 19:40:30 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 2E9DF6B008A for ; Fri, 3 May 2024 19:40:30 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 897DE1A11DF for ; Fri, 3 May 2024 23:40:29 +0000 (UTC) X-FDA: 82078706178.19.CF1363C Received: from mail-vs1-f48.google.com (mail-vs1-f48.google.com [209.85.217.48]) by imf18.hostedemail.com (Postfix) with ESMTP id D50841C0004 for ; Fri, 3 May 2024 23:40:27 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=erib6gpR; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf18.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.217.48 as permitted sender) smtp.mailfrom=21cnbao@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1714779627; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=8TYUHC6HPzfKj5/YyTSFNqCrAmae5EIR8KoLZK/tZ24=; b=Yr127KOqoI5dEzVQP0yvCpiI3Ta+oHqRRkJz5SX0HJqrgOkGLVSn0N9ONcc/Ts5bqEMuwv b1mf0XoVqHd2GS6KbHpB1cpb5dviiM9JCT2twB/XhstL1jQ7uqQmRoliU3hKdo2KACnkcn qhZyH7E5S+yE1JRr0U9POKVWhG7PZ04= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1714779627; a=rsa-sha256; cv=none; b=k6v9m+vEYtucTsMhQYSL0ypxLJ0I0tZsPGZCRFY5oa5iNUlYDL6Cvvv9YjkESF1HEwD9oK ioGgYoiZ/TvAhsyj29tvntzHwtdsMR2uv9YfbnFA+X2KP1Kf0Y/D9E0mAkaNY31zIfhLPW FODR3NBca0sZKpDD92OHsHpsHUKh0Ns= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=erib6gpR; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf18.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.217.48 as permitted sender) smtp.mailfrom=21cnbao@gmail.com Received: by mail-vs1-f48.google.com with SMTP id ada2fe7eead31-47eff9b3c23so33073137.0 for ; Fri, 03 May 2024 16:40:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714779627; x=1715384427; darn=kvack.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=8TYUHC6HPzfKj5/YyTSFNqCrAmae5EIR8KoLZK/tZ24=; b=erib6gpR15L3O7l/yEX4Y9eno9PE+l4Y4JhKNme0Jm91fdfw+aaZ5jGA9a8CpkBhsK z14CyKEvPA4F1MftMrr8i56EWDFIavFKWXZmzy5kskHV8AKx5vndLD3wSqjLvdGKnEh2 H6MG1HR1Ab2efipiDj2uWXfArdTZvqHjoa0TFJSDdhNAkZRQZw/VqNlSX/cMDOX7wSMN L4MsOKkj91OLIG0FWSMh42OluIK3IXBR4yO5YhYX0fkt/CW1XP11Sv0jvkBow4rj8Hp0 faJO+1lu87jwMJO04An7KBfPJE0KdPGxF+Ybt8B2HtZu5ZcRpz4r1SaSm5LcOgS0sWJ6 jhdg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714779627; x=1715384427; 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=8TYUHC6HPzfKj5/YyTSFNqCrAmae5EIR8KoLZK/tZ24=; b=JNUC/g1vyYYvSC8P2JFTSEPiKuHDT1kvjQWS/Mj7+soqrGJtC9FSrUOiNTc8Lp+gZp dXdziZ6WZSzDK1TczPlv3Ja9okCPr0RPgcIP36tWPiLUYyL0EA7lyzdxIRXiHcErzQev T0GMnzJ6frxHgxdfgcNcVifFcTl6SxzRh1iBayu9k+GQ4LiJnGHReO2euPvtiQ6HXw4P Tn7mbJLw9iqeNn/ll7gHm3SMnC/Zxvq0nDfehsc74c+PUwqgE7hT3mIkwAcgvCMojj/L 3BS1IkWCoB8UpdsClZNOJdM7dI2iBZ3qciGN4Me565WNv5wDlYjztxuNXczPlDeeIzcn D2dw== X-Forwarded-Encrypted: i=1; AJvYcCWvj4qz4QJcIj69vlMP5HSvBHNTDftja2KJ/I7ht1WqJV4+45ASLJS7wN4YAQfjynhr4DqryxaYQQAw1V14gE9B2zA= X-Gm-Message-State: AOJu0YxBcreHEVhKbfYi6braZEBFGLOrR1BpR6ccn6B79TUAhnv7+Bt+ wALnE102Istm9uvAMWlfLVvlZkomp3jklFEfUIqA8/i+1AfWaZRfaqZwwwYkCs9zZvN8OfWAOV8 0KKyB/os1tZp/AQQW5L3PO0aujkY= X-Google-Smtp-Source: AGHT+IGD/mAI021NpH5gkdvFb7aB6rdsm9wOJM0UduAXo8B49idF9sHO+qdSiU4GCUTJbaRdleo4EpOZ/oU1upg4jbM= X-Received: by 2002:a05:6122:1826:b0:4da:e977:1fb7 with SMTP id ay38-20020a056122182600b004dae9771fb7mr4342098vkb.15.1714779626868; Fri, 03 May 2024 16:40:26 -0700 (PDT) MIME-Version: 1.0 References: <20240503005023.174597-1-21cnbao@gmail.com> <20240503005023.174597-4-21cnbao@gmail.com> <7548e30c-d56a-4a57-ab87-86c9c8e523b1@arm.com> In-Reply-To: <7548e30c-d56a-4a57-ab87-86c9c8e523b1@arm.com> From: Barry Song <21cnbao@gmail.com> Date: Sat, 4 May 2024 07:40:16 +0800 Message-ID: Subject: Re: [PATCH v3 3/6] mm: introduce pte_move_swp_offset() helper which can move offset bidirectionally To: Ryan Roberts Cc: akpm@linux-foundation.org, linux-mm@kvack.org, baolin.wang@linux.alibaba.com, chrisl@kernel.org, david@redhat.com, hanchuanhua@oppo.com, hannes@cmpxchg.org, hughd@google.com, kasong@tencent.com, linux-kernel@vger.kernel.org, surenb@google.com, v-songbaohua@oppo.com, willy@infradead.org, xiang@kernel.org, ying.huang@intel.com, yosryahmed@google.com, yuzhao@google.com, ziy@nvidia.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspamd-Queue-Id: D50841C0004 X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: eg3197juir9o4537yf4faeh1aptyued7 X-HE-Tag: 1714779627-75524 X-HE-Meta: U2FsdGVkX1+GBmUrKd3FheTf+qFiyjkxYxeYfD9J7K2kG35YEDuT7D//vaSI6GGKE8E9KIOfpunieAGSliJfnNxIY0oYBOp3ALiYqR/Yeaf3BCc6cW4mmI4Vh4V5bRHUjY64dD1jQevKke7aBWfsBkZ+teK3u8NBNVPtKkoBu5kclugdvy4R8fepS3e6KZhiIfybpTBCRgQPTuXNUicqR1wx4oYk6Hi3znaKy93EmZ+l5QbAw8DruxwMjtfhJ+LT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NmN8BrQn8TIhb6ECQ5Vc1PpRF9aeGUxe9OhryoiKKJiv2un/HJVZSPcTP5/J+z3ZtPuge9pq2H3p5bOmmmnouAzH4YbeKYq9QKgdEjERG250HOpgcT6hMZNo/SEWc1v2yhe0E0fWbOVMwuvn3fm2P0EnOvYO2YOw5LTbGJ832iJZPsoRGx0djNtizcJjt5xvNuZ5m/qS25NqsSKiFlXI 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: On Fri, May 3, 2024 at 5:41=E2=80=AFPM Ryan Roberts = wrote: > > On 03/05/2024 01:50, Barry Song wrote: > > From: Barry Song > > > > There could arise a necessity to obtain the first pte_t from a swap > > pte_t located in the middle. For instance, this may occur within the > > context of do_swap_page(), where a page fault can potentially occur in > > any PTE of a large folio. To address this, the following patch introduc= es > > pte_move_swp_offset(), a function capable of bidirectional movement by > > a specified delta argument. Consequently, pte_increment_swp_offset() > > You mean pte_next_swp_offset()? yes. > > > will directly invoke it with delta =3D 1. > > > > Suggested-by: "Huang, Ying" > > Signed-off-by: Barry Song > > --- > > mm/internal.h | 25 +++++++++++++++++++++---- > > 1 file changed, 21 insertions(+), 4 deletions(-) > > > > diff --git a/mm/internal.h b/mm/internal.h > > index c5552d35d995..cfe4aed66a5c 100644 > > --- a/mm/internal.h > > +++ b/mm/internal.h > > @@ -211,18 +211,21 @@ static inline int folio_pte_batch(struct folio *f= olio, unsigned long addr, > > } > > > > /** > > - * pte_next_swp_offset - Increment the swap entry offset field of a sw= ap pte. > > + * pte_move_swp_offset - Move the swap entry offset field of a swap pt= e > > + * forward or backward by delta > > * @pte: The initial pte state; is_swap_pte(pte) must be true and > > * non_swap_entry() must be false. > > + * @delta: The direction and the offset we are moving; forward if delt= a > > + * is positive; backward if delta is negative > > * > > - * Increments the swap offset, while maintaining all other fields, inc= luding > > + * Moves the swap offset, while maintaining all other fields, includin= g > > * swap type, and any swp pte bits. The resulting pte is returned. > > */ > > -static inline pte_t pte_next_swp_offset(pte_t pte) > > +static inline pte_t pte_move_swp_offset(pte_t pte, long delta) > > We have equivalent functions for pfn: > > pte_next_pfn() > pte_advance_pfn() > > Although the latter takes an unsigned long and only moves forward current= ly. I > wonder if it makes sense to have their naming and semantics match? i.e. c= hange > pte_advance_pfn() to pte_move_pfn() and let it move backwards too. > > I guess we don't have a need for that and it adds more churn. we might have a need in the below case. A forks B, then A and B share large folios. B unmap/exit, then large folios of process A become single-mapped. Right now, while writing A's folios, we are CoWing A's large folios into many small folios. I believe we can reuse the entire large folios instead of doing nr_= pages CoW and page faults. In this case, we might want to get the first PTE from vmf->pte. Another case, might be A forks B, and we write either A or B, we might CoW an entire large folios instead CoWing nr_pages small folios. case 1 seems more useful, I might have a go after some days. then we might see pte_move_pfn(). > > Anyway: > > Reviewed-by: Ryan Roberts thanks! > > > > { > > swp_entry_t entry =3D pte_to_swp_entry(pte); > > pte_t new =3D __swp_entry_to_pte(__swp_entry(swp_type(entry), > > - (swp_offset(entry) + 1= ))); > > + (swp_offset(entry) + d= elta))); > > > > if (pte_swp_soft_dirty(pte)) > > new =3D pte_swp_mksoft_dirty(new); > > @@ -234,6 +237,20 @@ static inline pte_t pte_next_swp_offset(pte_t pte) > > return new; > > } > > > > + > > +/** > > + * pte_next_swp_offset - Increment the swap entry offset field of a sw= ap pte. > > + * @pte: The initial pte state; is_swap_pte(pte) must be true and > > + * non_swap_entry() must be false. > > + * > > + * Increments the swap offset, while maintaining all other fields, inc= luding > > + * swap type, and any swp pte bits. The resulting pte is returned. > > + */ > > +static inline pte_t pte_next_swp_offset(pte_t pte) > > +{ > > + return pte_move_swp_offset(pte, 1); > > +} > > + > > /** > > * swap_pte_batch - detect a PTE batch for a set of contiguous swap en= tries > > * @start_ptep: Page table pointer for the first entry. > Barry