Received: by 2002:ab2:69cc:0:b0:1f4:be93:e15a with SMTP id n12csp1810175lqp; Mon, 15 Apr 2024 19:52:01 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVEuVUHvIc9rt0tI6QFDxgLckyQvwjFUKQfox0s39ah5tobqumUc1yf5yqz/RxJ76juqIPwu8ofLdut15OmDBdafKINCh/n8fbLbHAbjw== X-Google-Smtp-Source: AGHT+IEVjMTS/uJTUVO0zunwH9OolPxCD7flzvcjEFCwwTbyeGGbMs3/XCLFbHIqu7DXVhtYIxcb X-Received: by 2002:a05:651c:54f:b0:2da:a3ff:524e with SMTP id q15-20020a05651c054f00b002daa3ff524emr2252127ljp.9.1713235920904; Mon, 15 Apr 2024 19:52:00 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713235920; cv=pass; d=google.com; s=arc-20160816; b=bztoUMzSqhXR0fpfku1twztRilvEol3E7Xgn8hU/0hu4I6JeuDI55l923HV5M4hIob 1hcYyMFABFXQud7cCvEVVwoV4+g7KhCmZMa1sbLknLV0ADATAxhmIXqpgOWQa51jqbpp zMAFwAXB6LU9hlb1OqHvVFjwi9/g5BmIkRMSeHutSfvl3ME6Pr+6nQnsFmehTPAJDsOJ R06mH+ZpU2+YcX2jftYFQgH9ABxKcHEqbP1Pe5bWWBQYD+K4uCVO6okAr0CSA/CA0z2v Ubxpj2LoKWYEjovP4S53Cqy+8vMW1i66jUKjpYe5fSJbxeIWTSwAhgUMcU8Cj/j3tTjY G16g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:user-agent:message-id:date :references:in-reply-to:subject:cc:to:from:dkim-signature; bh=CYoLkK6laoWOVXo4OCNRWDC6ai9i2F5OJ4F8X22c6lk=; fh=WLN0ZzeB6fF5gTtr8w02Q/WYvLXsU15EBximDUADQrA=; b=Jbg5VpCUoZeLwKTtDPcVWwHCo4qtIozYipKdUdWJtQCW4zCxEVLm29Bm7YPdVT+l4c ZIzbzBt7OgOPZtIFbXyv2BwRZ9TZe8zkGOODCPh/VFMTOsrciPQ6MH1Nj8YXzI664UDp 6KyOKVxEv2yXhNoHyLlYfXVbkHr+AD0NFHYRsSJfMHjSzZ7td+1OrqYRNumkyEXLu1DO SA+D0DuM3+yNo99XelP0jOuaW4WxSVgHlU+mC5MVGpKW/pu5QRjR0oT/Ph1lOjIOvH1o qtUIrvhd64mMLte44t4Goiur7e4TTUFzi06TSKzgxQhEvWiIDK+2Ed1AF8G1GLqwIGzO xLug==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=O6RvZjS+; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-146128-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-146128-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id w15-20020a05640234cf00b0056e3153c8b5si5282195edc.32.2024.04.15.19.52.00 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 Apr 2024 19:52:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-146128-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=O6RvZjS+; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-146128-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-146128-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.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 am.mirrors.kernel.org (Postfix) with ESMTPS id 232951F21EE5 for ; Tue, 16 Apr 2024 02:41:43 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1543611181; Tue, 16 Apr 2024 02:41:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="O6RvZjS+" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.16]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9677E8485 for ; Tue, 16 Apr 2024 02:41:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.16 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713235295; cv=none; b=XrOfrv8J+L2g6cZ+IqINe0bB6/Ny+WH14Heu8axVmz3aaWL9f3otsStvI5iVY64k4msbh720/cUR3+GpoqvmFXA/F2bGnG/dwX7yhuX1BNv3QCx5BNlu4w2NlUSVjToyV/L0kgmu5YLENrwt5XDt/Q6dBcJ3WOe0KQczUXkghTc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713235295; c=relaxed/simple; bh=AF47lIYi7jUHNQdyQADHspwYl4p62TwwbeI5R4cu2sY=; h=From:To:Cc:Subject:In-Reply-To:References:Date:Message-ID: MIME-Version:Content-Type; b=G0gk6FutZnBr4evNkKGad2tkIJgQZIr4kQmCPY1m3ivrbBJhQDkFfaWuJsXAnV0b2LwoA+To41q7lS50qw9Fe0FKO8RuSzjESBpjLiCGwcFYNpe+AjUBk2f/PHjTCcjb++933yfU9oSFGoof4D3tgzNx1d8sNCuAGD1aum1Expc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=O6RvZjS+; arc=none smtp.client-ip=192.198.163.16 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1713235294; x=1744771294; h=from:to:cc:subject:in-reply-to:references:date: message-id:mime-version:content-transfer-encoding; bh=AF47lIYi7jUHNQdyQADHspwYl4p62TwwbeI5R4cu2sY=; b=O6RvZjS+NDqzAYyeU93SrwDu7u0T9+ScRcuVU7RxGEq82Z//G5w0tWJR D76BiTod0IiZJq//pVwws1lQTKnNSP+ADATYcx/yICXGgsHbN5lbEWvTw n40y7gZtl8hq9H7yXInkjm2HIF+tToERVI8ftdN5PJ2KUZCzi72Mn3wmI eYu3e/1nUE0tEFFMMC47eBBW4B5nVj859IzXpsAGbflWrgKFm25cClynP Y+WJBeW2AkxnimSJkKD0o8cS8AlME4Cx8+6wmUOCYyQYk5v65m5CpdLIc 7Izm0mBwO92fNL4uA6OlnqUPF9CQ2MF9x4qIjRUOC70asjSL1/BqWHo/j A==; X-CSE-ConnectionGUID: 3+EM1bE2Ru66xKJpLm1QqA== X-CSE-MsgGUID: S60cezNuSVumHAscCt9smg== X-IronPort-AV: E=McAfee;i="6600,9927,11045"; a="9205602" X-IronPort-AV: E=Sophos;i="6.07,204,1708416000"; d="scan'208";a="9205602" Received: from orviesa001.jf.intel.com ([10.64.159.141]) by fmvoesa110.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Apr 2024 19:41:33 -0700 X-CSE-ConnectionGUID: we6sQxMVTBua8dKd6KhhLQ== X-CSE-MsgGUID: G5IGqF7NRJiZww5sVUapIQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,204,1708416000"; d="scan'208";a="59553320" Received: from unknown (HELO yhuang6-desk2.ccr.corp.intel.com) ([10.238.208.55]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Apr 2024 19:41:28 -0700 From: "Huang, Ying" To: Barry Song <21cnbao@gmail.com> Cc: Khalid Aziz , 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, ryan.roberts@arm.com, surenb@google.com, v-songbaohua@oppo.com, willy@infradead.org, xiang@kernel.org, yosryahmed@google.com, yuzhao@google.com, ziy@nvidia.com, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2 4/5] mm: swap: entirely map large folios found in swapcache In-Reply-To: (Barry Song's message of "Tue, 16 Apr 2024 14:36:08 +1200") References: <20240409082631.187483-1-21cnbao@gmail.com> <20240409082631.187483-5-21cnbao@gmail.com> <87frvn2f89.fsf@yhuang6-desk2.ccr.corp.intel.com> <8734rm2gdj.fsf@yhuang6-desk2.ccr.corp.intel.com> Date: Tue, 16 Apr 2024 10:39:34 +0800 Message-ID: <87y19e115l.fsf@yhuang6-desk2.ccr.corp.intel.com> User-Agent: Gnus/5.13 (Gnus v5.13) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Barry Song <21cnbao@gmail.com> writes: > On Tue, Apr 16, 2024 at 2:27=E2=80=AFPM Huang, Ying wrote: >> >> >> Added Khalid for arch_do_swap_page(). >> >> Barry Song <21cnbao@gmail.com> writes: >> >> > On Mon, Apr 15, 2024 at 8:39=E2=80=AFPM Huang, Ying wrote: >> >> >> >> Barry Song <21cnbao@gmail.com> writes: >> >> [snip] >> >> >> >> >> > + bool any_swap_shared =3D false; >> >> > >> >> > if (!pte_unmap_same(vmf)) >> >> > goto out; >> >> > @@ -4137,6 +4141,35 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) >> >> > */ >> >> > vmf->pte =3D pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->a= ddress, >> >> > &vmf->ptl); >> >> >> >> We should move pte check here. That is, >> >> >> >> if (unlikely(!vmf->pte || !pte_same(ptep_get(vmf->pte), vmf->= orig_pte))) >> >> goto out_nomap; >> >> >> >> This will simplify the situation for large folio. >> > >> > the plan is moving the whole code block >> > >> > if (start_pte && folio_test_large(folio) && folio_test_swapcache(folio= )) >> > >> > after >> > if (unlikely(!folio_test_uptodate(folio))) { >> > ret =3D VM_FAULT_SIGBUS; >> > goto out_nomap; >> > } >> > >> > though we couldn't be !folio_test_uptodate(folio)) for hitting >> > swapcache but it seems >> > logically better for future use. >> >> LGTM, Thanks! >> >> >> >> >> > + >> >> > + /* We hit large folios in swapcache */ >> >> >> >> The comments seems unnecessary because the code tells that already. >> >> >> >> > + if (start_pte && folio_test_large(folio) && folio_test_swapca= che(folio)) { >> >> > + int nr =3D folio_nr_pages(folio); >> >> > + int idx =3D folio_page_idx(folio, page); >> >> > + unsigned long folio_start =3D vmf->address - idx * PA= GE_SIZE; >> >> > + unsigned long folio_end =3D folio_start + nr * PAGE_S= IZE; >> >> > + pte_t *folio_ptep; >> >> > + pte_t folio_pte; >> >> > + >> >> > + if (unlikely(folio_start < max(vmf->address & PMD_MAS= K, vma->vm_start))) >> >> > + goto check_pte; >> >> > + if (unlikely(folio_end > pmd_addr_end(vmf->address, v= ma->vm_end))) >> >> > + goto check_pte; >> >> > + >> >> > + folio_ptep =3D vmf->pte - idx; >> >> > + folio_pte =3D ptep_get(folio_ptep); >> >> >> >> It's better to construct pte based on fault PTE via generalizing >> >> pte_next_swp_offset() (may be pte_move_swp_offset()). Then we can fi= nd >> >> inconsistent PTEs quicker. >> > >> > it seems your point is getting the pte of page0 by pte_next_swp_offset= () >> > unfortunately pte_next_swp_offset can't go back. on the other hand, >> > we have to check the real pte value of the 0nd entry right now because >> > swap_pte_batch() only really reads pte from the 1st entry. it assumes >> > pte argument is the real value for the 0nd pte entry. >> > >> > static inline int swap_pte_batch(pte_t *start_ptep, int max_nr, pte_t = pte) >> > { >> > pte_t expected_pte =3D pte_next_swp_offset(pte); >> > const pte_t *end_ptep =3D start_ptep + max_nr; >> > pte_t *ptep =3D start_ptep + 1; >> > >> > VM_WARN_ON(max_nr < 1); >> > VM_WARN_ON(!is_swap_pte(pte)); >> > VM_WARN_ON(non_swap_entry(pte_to_swp_entry(pte))); >> > >> > while (ptep < end_ptep) { >> > pte =3D ptep_get(ptep); >> > >> > if (!pte_same(pte, expected_pte)) >> > break; >> > >> > expected_pte =3D pte_next_swp_offset(expected_pte); >> > ptep++; >> > } >> > >> > return ptep - start_ptep; >> > } >> >> Yes. You are right. >> >> But we may check whether the pte of page0 is same as "vmf->orig_pte - >> folio_page_idx()" (fake code). > > right, that is why we are reading and checking PTE0 before calling > swap_pte_batch() > right now. > > folio_ptep =3D vmf->pte - idx; > folio_pte =3D ptep_get(folio_ptep); > if (!is_swap_pte(folio_pte) || non_swap_entry(pte_to_swp_entry(folio_pt= e)) || > swap_pte_batch(folio_ptep, nr, folio_pte, &any_swap_shared) !=3D nr) > goto check_pte; > > So, if I understand correctly, you're proposing that we should directly c= heck > PTE0 in swap_pte_batch(). Personally, I don't have any objections to this= idea. > However, I'd also like to hear the feedback from Ryan and David :-) I mean that we can replace=20 !is_swap_pte(folio_pte) || non_swap_entry(pte_to_swp_entry(folio_pt= e)) in above code with pte_same() with constructed expected first pte. >> >> You need to check the pte of page 0 anyway. >> >> >> >> >> > + if (!is_swap_pte(folio_pte) || non_swap_entry(pte_to_= swp_entry(folio_pte)) || >> >> > + swap_pte_batch(folio_ptep, nr, folio_pte, &any_sw= ap_shared) !=3D nr) >> >> > + goto check_pte; >> >> > + >> >> > + start_address =3D folio_start; >> >> > + start_pte =3D folio_ptep; >> >> > + nr_pages =3D nr; >> >> > + entry =3D folio->swap; >> >> > + page =3D &folio->page; >> >> > + } >> >> > + >> >> > +check_pte: >> >> > if (unlikely(!vmf->pte || !pte_same(ptep_get(vmf->pte), vmf->= orig_pte))) >> >> > goto out_nomap; >> >> > >> >> > @@ -4190,6 +4223,10 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) >> >> > */ >> >> > exclusive =3D false; >> >> > } >> >> > + >> >> > + /* Reuse the whole large folio iff all entries are ex= clusive */ >> >> > + if (nr_pages > 1 && any_swap_shared) >> >> > + exclusive =3D false; >> >> > } >> >> > [snip] -- Best Regards, Huang, Ying