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 D9721C47258 for ; Mon, 29 Jan 2024 02:15:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 541476B0078; Sun, 28 Jan 2024 21:15:20 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4F1736B007B; Sun, 28 Jan 2024 21:15:20 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3B9016B007D; Sun, 28 Jan 2024 21:15:20 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 2D57C6B0078 for ; Sun, 28 Jan 2024 21:15:20 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id BC86412099D for ; Mon, 29 Jan 2024 02:15:19 +0000 (UTC) X-FDA: 81730731558.27.D00A041 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf09.hostedemail.com (Postfix) with ESMTP id C98FB140035 for ; Mon, 29 Jan 2024 02:15:17 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Z7fuT1X6; spf=pass (imf09.hostedemail.com: domain of chrisl@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=chrisl@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706494517; 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=+el0gUjdX5Zst6EnOvZv4FnvnlgP4KVBuktL0MXjeTs=; b=ktuTRo+otQuIIL2bVwdFmgrZhVOIf5xEOD4YwjjnjrV/yXjQ/iPL1muKd3gEctcDGdlX8c rEN/r5zPU/wCzPLXTCYfV2DyIO8qbMaoj9G3bmvo4fL6kRHBMuYm+p3L46ZVSfDahzeOkm DY5luDDdCUvZsqzTkhJBZ3iK54Ze0i8= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706494517; a=rsa-sha256; cv=none; b=aOjrG6oTbRFpxiKO6Kwt8QCk3Pb5MVaIJWywS5kjI3uHjVp6BqfNwT9tFH3rd4dROg6c0q sEtRMFuJCkB5k4GU4HWYekg81D7BSnuKSJiXmJ9ByELKOYA9skEhYmobJk2DAl6v44Az/x G/qeRi/fcrwE6HIaKMoKUgnO9vb78eo= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Z7fuT1X6; spf=pass (imf09.hostedemail.com: domain of chrisl@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=chrisl@kernel.org; dmarc=pass (policy=none) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id AE39961D66 for ; Mon, 29 Jan 2024 02:15:16 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 03926C43330 for ; Mon, 29 Jan 2024 02:15:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1706494516; bh=7055U/9Nxv5bXUdnlD++5r6zZXVPj28DxkcLh6h6JgA=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=Z7fuT1X6IyKphgcImv6Ykm47ABZiD2GM5GnUNxemHnSy1Awg8vRetFR5HijZO4EZP aJDXHqJmKhv1IMBNETgFSjKPhJ6F1o9qEoihV5F4eN8KovEir4q7Ub0//AE3M7TUeK a/rZ7CIQ0HM7+Pw7gMx6IJ9PMnm3Tlahk4HkgFegw52pKsLCNXtTFUowlbQEcy6bkM qx44r2J6q4RNlD7nPs8WJELo3+yvcdbcsZOLj58YVYBHyZFurZg4jbVrtodzrDm/1W mA3Fd4vycib3VPieknw6Sv9sok4GL4vBNgzy9be9PQi0Fl/yVMQY2fNfO0D0LHtBzO zSCWKoE6rom4Q== Received: by mail-io1-f53.google.com with SMTP id ca18e2360f4ac-7baa8da5692so136631139f.0 for ; Sun, 28 Jan 2024 18:15:15 -0800 (PST) X-Gm-Message-State: AOJu0Yxj9EmUQVrrjVkuyepi3ofVfWQ4TimCiBGfl1kkc1TjPiNJCrFW vS/yxZ1pCEuOFpA+z7zdxl7Pyfpwm4Vcp+Um0JKp+lvBoJ3YzEdLiyENoVwPKXaetYG6fgpherB lsPNH3Ht/XbxHtC+/2xBg5wCOG5DQfjhj0S+D X-Google-Smtp-Source: AGHT+IEvOpo+ECoXVFn7hv2AVOp8va3Wt0QdiIVOOWoN1MX9U9R8xdEZCYi3P/pz3z/fNn14gfjoa8NV2HIbgL0orU0= X-Received: by 2002:a05:6e02:1a4a:b0:361:a7a2:9daf with SMTP id u10-20020a056e021a4a00b00361a7a29dafmr7366338ilv.27.1706494515194; Sun, 28 Jan 2024 18:15:15 -0800 (PST) MIME-Version: 1.0 References: <20231025144546.577640-1-ryan.roberts@arm.com> <20240118111036.72641-1-21cnbao@gmail.com> <20240118111036.72641-7-21cnbao@gmail.com> In-Reply-To: <20240118111036.72641-7-21cnbao@gmail.com> From: Chris Li Date: Sun, 28 Jan 2024 18:15:03 -0800 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH RFC 6/6] mm: madvise: don't split mTHP for MADV_PAGEOUT To: Barry Song <21cnbao@gmail.com> Cc: ryan.roberts@arm.com, akpm@linux-foundation.org, david@redhat.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, mhocko@suse.com, shy828301@gmail.com, wangkefeng.wang@huawei.com, willy@infradead.org, xiang@kernel.org, ying.huang@intel.com, yuzhao@google.com, surenb@google.com, steven.price@arm.com, Chuanhua Han , Barry Song Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspamd-Queue-Id: C98FB140035 X-Rspam-User: X-Stat-Signature: shegtna7eiyx3keib486a3cn8zjxchrq X-Rspamd-Server: rspam03 X-HE-Tag: 1706494517-800435 X-HE-Meta: U2FsdGVkX18Qv396va6BtPjyTzeP6TgiqTk0MV7npOtqEjapBjxYZ4ZlaUYk+3VVXhMFx2VD3F3eyOB0tSnncPhjWoQgCy5ljvh6IRpKrWiqY4C0FaukgbkbqKS5BjBQeTbQeO0vsqbKCzL1Lju+vTBf4nNImFz6lnw9juSHnFOO0TNMmIZM8OPn2pe4jFG+jLQXNDKHx3ItjQvL9apT2P4QGy2Uw27qkYWx//U6wue5sbwwyDFMmT7OKV7Ap+LDwvqNTEQLfDxuKv0EwYRNCoe6B4AGSKDirfpnXgCZJAt2ciocbefsqZBhyWkOiWwFQHiH09zaVeZ6PlIg2VSAxoFi4ynFNKx4t53fDt95/ErWIWdncDIAam2/KMSU3GCGdSH6MggARO0dPyJvhlyM+0MHPDeG50OMtUZW8w2+kJYfdlWGmWlGJv22Ml+bqNfaChv7dA90xeoXD9JXLnNS00/0WnA4fdTYov6fveruZxzfdNNUMsPkFYp1hRa4ildXpFbh6plFh2f9HZ1O8ufIGTAzjN0ml7Ed6IwCV9VWxjm10sLVhYejrG1OMHfEtBFKNYXQTIjc6edyerC/d3sBFLckVbvC67L0fFbsTrWoWvP1bsLgpln9gmJz5iNqzZL5nPe4v9B2WCrz6UrrCRcxz4GfrEQcDPGLnGoZolDe52IiwCeDIGKmzSyDZ6QBLKeLPh5GZT7Rn5B4OiQMUwnUJBKXE4sp1lJ0or3iJSiaYDLChLfkY2JGXm8OcnIxNuyJfgTwQlAo878o0zJXV3hRooVHaKPSe7jR+P86TIW3SzubzEpdecY9WaC3qCbe2LL/2MuTbKX19NyYIXqpofucGeITMyibVtpDcVLLGyQKddzYEX3mhU5iXEn4wBSnRREohLuvhJp1i1OEmXhGv0HgW43NgoN7YxC+5crbDXEpzoIxgm8IIyape43APmASX1T907RkvLCYSEpmMKM7v7Q Zb+LmaD+ ojurWNCFRSBVpESTRYzveeCvEgICyEbsxAvTu9s3IX3L0jd8DFn/FSiUNn4p6ZwGS3FDjmL0yFq+vU+bU/ZcALS6sJXpaWyNVb2BZOTbXtm1J+H2wAaJAIVGZKOQzbN5lMCKIEMUEnc1opgZPbXdPknc3UpYw5LYvQG9X5tOEVTVP5iYjqFCDV4D0Yhi1Zs5Jpjyxo5nAzVrIPnLkKPmSYbi2JEs7p2bq2z39affbK+vIX3wqVS6SqTSCea517KI7Wc53twv07/hGW919hhOi9UnCU/Kwz8cQHElPPusmlbC29S6g3JJhmLrl6uhDvjcv8hDASKABTTgPLMGB0p/DcBEqxu6Pc/Ofx6LC 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 Thu, Jan 18, 2024 at 3:12=E2=80=AFAM Barry Song <21cnbao@gmail.com> wrot= e: > > From: Chuanhua Han > > MADV_PAGEOUT and MADV_FREE are common cases in Android. Ryan's patchset h= as > supported swapping large folios out as a whole for vmscan case. This patc= h > extends the feature to madvise. > > If madvised range covers the whole large folio, we don't split it. Otherw= ise, > we still need to split it. > > This patch doesn't depend on ARM64's CONT-PTE, alternatively, it defines = one > helper named pte_range_cont_mapped() to check if all PTEs are contiguousl= y > mapped to a large folio. > > Signed-off-by: Chuanhua Han > Co-developed-by: Barry Song > Signed-off-by: Barry Song > --- > include/asm-generic/tlb.h | 10 +++++++ > include/linux/pgtable.h | 60 +++++++++++++++++++++++++++++++++++++++ > mm/madvise.c | 48 +++++++++++++++++++++++++++++++ > 3 files changed, 118 insertions(+) > > diff --git a/include/asm-generic/tlb.h b/include/asm-generic/tlb.h > index 129a3a759976..f894e22da5d6 100644 > --- a/include/asm-generic/tlb.h > +++ b/include/asm-generic/tlb.h > @@ -608,6 +608,16 @@ static inline void tlb_flush_p4d_range(struct mmu_ga= ther *tlb, > __tlb_remove_tlb_entry(tlb, ptep, address); \ > } while (0) > > +#define tlb_remove_nr_tlb_entry(tlb, ptep, address, nr) = \ > + do { \ > + int i; \ > + tlb_flush_pte_range(tlb, address, \ > + PAGE_SIZE * nr); \ > + for (i =3D 0; i < nr; i++) = \ > + __tlb_remove_tlb_entry(tlb, ptep + i, \ > + address + i * PAGE_SIZE); \ > + } while (0) > + > #define tlb_remove_huge_tlb_entry(h, tlb, ptep, address) \ > do { \ > unsigned long _sz =3D huge_page_size(h); \ > diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h > index 37fe83b0c358..da0c1cf447e3 100644 > --- a/include/linux/pgtable.h > +++ b/include/linux/pgtable.h > @@ -320,6 +320,42 @@ static inline pgd_t pgdp_get(pgd_t *pgdp) > } > #endif > > +#ifndef pte_range_cont_mapped > +static inline bool pte_range_cont_mapped(unsigned long start_pfn, > + pte_t *start_pte, > + unsigned long start_addr, > + int nr) > +{ > + int i; > + pte_t pte_val; > + > + for (i =3D 0; i < nr; i++) { > + pte_val =3D ptep_get(start_pte + i); > + > + if (pte_none(pte_val)) > + return false; Hmm, the following check pte_pfn =3D=3D start_pfn + i should have covered the pte none case? I think the pte_none means it can't have a valid pfn. So this check can be skipped? > + > + if (pte_pfn(pte_val) !=3D (start_pfn + i)) > + return false; > + } > + > + return true; > +} > +#endif > + > +#ifndef pte_range_young > +static inline bool pte_range_young(pte_t *start_pte, int nr) > +{ > + int i; > + > + for (i =3D 0; i < nr; i++) > + if (pte_young(ptep_get(start_pte + i))) > + return true; > + > + return false; > +} > +#endif > + > #ifndef __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG > static inline int ptep_test_and_clear_young(struct vm_area_struct *vma, > unsigned long address, > @@ -580,6 +616,23 @@ static inline pte_t ptep_get_and_clear_full(struct m= m_struct *mm, > } > #endif > > +#define __HAVE_ARCH_PTEP_GET_AND_CLEAR_RANGE_FULL > +static inline pte_t ptep_get_and_clear_range_full(struct mm_struct *mm, > + unsigned long start_add= r, > + pte_t *start_pte, > + int nr, int full) > +{ > + int i; > + pte_t pte; > + > + pte =3D ptep_get_and_clear_full(mm, start_addr, start_pte, full); > + > + for (i =3D 1; i < nr; i++) > + ptep_get_and_clear_full(mm, start_addr + i * PAGE_SIZE, > + start_pte + i, full); > + > + return pte; > +} > > /* > * If two threads concurrently fault at the same page, the thread that > @@ -995,6 +1048,13 @@ static inline void arch_swap_restore(swp_entry_t en= try, struct folio *folio) > }) > #endif > > +#ifndef pte_nr_addr_end > +#define pte_nr_addr_end(addr, size, end) \ > +({ unsigned long __boundary =3D ((addr) + size) & (~(size - 1)); = \ > + (__boundary - 1 < (end) - 1)? __boundary: (end); \ > +}) > +#endif > + > /* > * When walking page tables, we usually want to skip any p?d_none entrie= s; > * and any p?d_bad entries - reporting the error before resetting to non= e. > diff --git a/mm/madvise.c b/mm/madvise.c > index 912155a94ed5..262460ac4b2e 100644 > --- a/mm/madvise.c > +++ b/mm/madvise.c > @@ -452,6 +452,54 @@ static int madvise_cold_or_pageout_pte_range(pmd_t *= pmd, > if (folio_test_large(folio)) { > int err; > > + if (!folio_test_pmd_mappable(folio)) { This session of code indent into the right too much. You can do: if (folio_test_pmd_mappable(folio)) goto split; to make the code flatter. > + int nr_pages =3D folio_nr_pages(folio); > + unsigned long folio_size =3D PAGE_SIZE * = nr_pages; > + unsigned long start_addr =3D ALIGN_DOWN(a= ddr, nr_pages * PAGE_SIZE);; > + unsigned long start_pfn =3D page_to_pfn(f= olio_page(folio, 0)); > + pte_t *start_pte =3D pte - (addr - start_= addr) / PAGE_SIZE; > + unsigned long next =3D pte_nr_addr_end(ad= dr, folio_size, end); > + > + if (!pte_range_cont_mapped(start_pfn, sta= rt_pte, start_addr, nr_pages)) > + goto split; > + > + if (next - addr !=3D folio_size) { Nitpick: One line statement does not need { > + goto split; > + } else { When the previous if statement already "goto split", there is no need for the else. You can save one level of indentation. > + /* Do not interfere with other ma= ppings of this page */ > + if (folio_estimated_sharers(folio= ) !=3D 1) > + goto skip; > + > + VM_BUG_ON(addr !=3D start_addr ||= pte !=3D start_pte); > + > + if (pte_range_young(start_pte, nr= _pages)) { > + ptent =3D ptep_get_and_cl= ear_range_full(mm, start_addr, start_pte, > + = nr_pages, tlb->fullmm); > + ptent =3D pte_mkold(ptent= ); > + > + set_ptes(mm, start_addr, = start_pte, ptent, nr_pages); > + tlb_remove_nr_tlb_entry(t= lb, start_pte, start_addr, nr_pages); > + } > + > + folio_clear_referenced(folio); > + folio_test_clear_young(folio); > + if (pageout) { > + if (folio_isolate_lru(fol= io)) { > + if (folio_test_un= evictable(folio)) > + folio_put= back_lru(folio); > + else > + list_add(= &folio->lru, &folio_list); > + } > + } else > + folio_deactivate(folio); I notice this section is very similar to the earlier statements inside the same function. "if (pmd_trans_huge(*pmd)) {" Wondering if there is some way to unify the two a bit somehow. Also notice if you test the else condition first, If (!pageout) { folio_deactivate(folio); goto skip; } You can save one level of indentation. Not your fault, I notice the section inside (pmd_trans_huge(*pmd)) does exactly the same thing. Chris > + } > +skip: > + pte +=3D (next - PAGE_SIZE - (addr & PAGE= _MASK))/PAGE_SIZE; > + addr =3D next - PAGE_SIZE; > + continue; > + > + } > +split: > if (folio_estimated_sharers(folio) !=3D 1) > break; > if (pageout_anon_only_filter && !folio_test_anon(= folio)) > -- > 2.34.1 > >