Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp2135261iof; Tue, 7 Jun 2022 20:56:09 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzETHSreKh/9fcI9zH2zWKnxmTR4P3yoMlbve82X9cDuEqd2LuYRDvz1nENBHLajbwMyJJq X-Received: by 2002:a17:90b:3ece:b0:1e8:90be:4cda with SMTP id rm14-20020a17090b3ece00b001e890be4cdamr11892872pjb.67.1654660569078; Tue, 07 Jun 2022 20:56:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654660569; cv=none; d=google.com; s=arc-20160816; b=YMTcRHtnBqizEnj2QJGYv3W4xL6W7E09Ikhpls5r9AbRMl/W5O8E6BW3kHNVC//x8c HUEEpz8twid8vPAquGPcyRfmNKQ8ZItEUlbWtMzgqV++BP9fe7GFioWKm3lxG6PApS6o G6OTTFQVAxZBkjK+QcUOy4GXJBRKvDrdy11Qpi5sHPDQzdA7HYqlPBLlTWBXb/HDBhas a1Ycc+r5YHrdO1g1S5a94SRWLxMFLJwuNHwaC66nO3mtPulsBklK6USxfCOXtBx1lG7W C/rw2kxhfUtUSUSVUFr65pDhBf/yRSUorqJ24kXTl873pnfj9PztIG7iI3TKRHxbvmbj 9J6Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=WS/YdT6ua32na97CqWFvFSVkMxLLpKjcoq8R6JJDuMg=; b=dXke2eiO/fanReK/EI92by8FULkX3OoivaHY+cpKjBYUWj6b0/QeEzZ6D85V3sL9wr cStNis68MBSSqXRU2An4FgtoDaYUPF72QUUuIiJIxopYqLjv3apBlr30gjjXYtdiKvtf 8UOTM166FXBUiJ8Il7ThihTUkjduM+MQQSRlgdA2SK200SIYmEORbSaKmTl0JNnAh+st u08ulA4wFUCJIDFK+EW0xzK1bjHQT19ti4QZUQ+G64yYOWjuOtgr/HxFqbfqrwBd0m3m Zgzt0VRPJ8JfmAfC6O6EQPGwN48d2MkKd3Xv6rm2yAI6b/z7U/tKXsmGBaRc9OsUZnKe SJ6w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=TfSh23+p; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id oa12-20020a17090b1bcc00b001e851c352easi17716905pjb.135.2022.06.07.20.56.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jun 2022 20:56:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=TfSh23+p; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 1BDBB3D673F; Tue, 7 Jun 2022 20:20:44 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241220AbiFGKiF (ORCPT + 99 others); Tue, 7 Jun 2022 06:38:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45472 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239832AbiFGKiC (ORCPT ); Tue, 7 Jun 2022 06:38:02 -0400 Received: from mail-ej1-x633.google.com (mail-ej1-x633.google.com [IPv6:2a00:1450:4864:20::633]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 285A214D11; Tue, 7 Jun 2022 03:38:01 -0700 (PDT) Received: by mail-ej1-x633.google.com with SMTP id fu3so32811100ejc.7; Tue, 07 Jun 2022 03:38:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=WS/YdT6ua32na97CqWFvFSVkMxLLpKjcoq8R6JJDuMg=; b=TfSh23+p1T9WwCPk5m4pQlk64AAbGXFaKssSeb89lrBlH61XMvE7agjozS4le3KPgE eE1vOoqCF7AaTVQN3k826+EK49j/m3GPCkZUSxV7jYuh07XbgEEYYLT3EIzjsR02+2sg qhAdaoiyNuGLlzfGyyB+BBvcz+Kr/dfUbk64LEDzKjKejwj7TIZLmCZiI7mgISfYD6F3 DJUvAhyW0TYYxQ5uXcyFnNC1Mp3pJ2iq2Pm1qBaHk0DD+WPmwORRNVH0JNWq3LfRKHHJ 3c2b57CotnxLsd/CFm55hivT/1ehZlKY17NOY7kbeR4WyXue2Om4pmQMyb6udCpGif9G ksJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=WS/YdT6ua32na97CqWFvFSVkMxLLpKjcoq8R6JJDuMg=; b=aK8AYsPQUpq8L3XjhuGcKnZ0f7TzzTNxLPNau4pGnnXSsGva5IJMkhuKDCYOOWOs/3 mXDg2vJZUUpQyTr4cZCuj/7y2VYBEKplDDWxG3vfHC5J7HArHJv+ZD6IZMaox3EKfSUM 1IEiKSnAZ+temz0a6AarY/zRdF8QE8l9M6qEJyBQPRhMELv6omZlCS/E8g+KiWgy4nQR qr2Weo6VYN/RtNw2XPBfW7tofUxJ2lpt8f3kNEhroXYrKPRm/d1Zj4fglak669afKm0x c/L0yqOHvBm5EDqgK9u84DWsIYr2Qrd8RQpYk6+tpHBL5YCWcbfd/kp+T9+GTKMmzw4t mULQ== X-Gm-Message-State: AOAM533DnGbdXJDlc9EBDM6F0ZhNtk8Yj5Oma0CR92W/pwMHAQOAZCZz wC+YDtCVkpDp0Dy7Aga8Y+wjkGtWnx6mzu6NNqA= X-Received: by 2002:a17:907:9715:b0:711:ca06:ca50 with SMTP id jg21-20020a170907971500b00711ca06ca50mr10634070ejc.192.1654598279517; Tue, 07 Jun 2022 03:37:59 -0700 (PDT) MIME-Version: 1.0 References: <20220518014632.922072-1-yuzhao@google.com> <20220518014632.922072-8-yuzhao@google.com> <20220607102135.GA32448@willie-the-truck> In-Reply-To: <20220607102135.GA32448@willie-the-truck> From: Barry Song <21cnbao@gmail.com> Date: Tue, 7 Jun 2022 10:37:46 +1200 Message-ID: Subject: Re: [PATCH v11 07/14] mm: multi-gen LRU: exploit locality in rmap To: Will Deacon Cc: Yu Zhao , Andrew Morton , Linux-MM , Andi Kleen , Aneesh Kumar , Catalin Marinas , Dave Hansen , Hillf Danton , Jens Axboe , Johannes Weiner , Jonathan Corbet , Linus Torvalds , Matthew Wilcox , Mel Gorman , Michael Larabel , Michal Hocko , Mike Rapoport , Peter Zijlstra , Tejun Heo , Vlastimil Babka , LAK , Linux Doc Mailing List , LKML , x86 , Kernel Page Reclaim v2 , Brian Geffon , Jan Alexander Steffens , Oleksandr Natalenko , Steven Barrett , Suleiman Souhlal , Daniel Byrne , Donald Carr , =?UTF-8?Q?Holger_Hoffst=C3=A4tte?= , Konstantin Kharlamov , Shuang Zhai , Sofia Trinh , Vaibhav Jain , huzhanyuan@oppo.com Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-0.7 required=5.0 tests=BAYES_00,DATE_IN_PAST_12_24, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Jun 7, 2022 at 10:21 PM Will Deacon wrote: > > On Tue, Jun 07, 2022 at 07:37:10PM +1200, Barry Song wrote: > > On Mon, Jun 6, 2022 at 9:25 PM Barry Song <21cnbao@gmail.com> wrote: > > > On Wed, May 18, 2022 at 4:49 PM Yu Zhao wrote: > > > > diff --git a/mm/rmap.c b/mm/rmap.c > > > > index fedb82371efe..7cb7ef29088a 100644 > > > > --- a/mm/rmap.c > > > > +++ b/mm/rmap.c > > > > @@ -73,6 +73,7 @@ > > > > #include > > > > #include > > > > #include > > > > +#include > > > > > > > > #include > > > > > > > > @@ -821,6 +822,12 @@ static bool folio_referenced_one(struct folio *folio, > > > > } > > > > > > > > if (pvmw.pte) { > > > > + if (lru_gen_enabled() && pte_young(*pvmw.pte) && > > > > + !(vma->vm_flags & (VM_SEQ_READ | VM_RAND_READ))) { > > > > + lru_gen_look_around(&pvmw); > > > > + referenced++; > > > > + } > > > > + > > > > if (ptep_clear_flush_young_notify(vma, address, > > > > > > Hello, Yu. > > > look_around() is calling ptep_test_and_clear_young(pvmw->vma, addr, pte + i) > > > only without flush and notify. for flush, there is a tlb operation for arm64: > > > static inline int ptep_clear_flush_young(struct vm_area_struct *vma, > > > unsigned long address, pte_t *ptep) > > > { > > > int young = ptep_test_and_clear_young(vma, address, ptep); > > > > > > if (young) { > > > /* > > > * We can elide the trailing DSB here since the worst that can > > > * happen is that a CPU continues to use the young entry in its > > > * TLB and we mistakenly reclaim the associated page. The > > > * window for such an event is bounded by the next > > > * context-switch, which provides a DSB to complete the TLB > > > * invalidation. > > > */ > > > flush_tlb_page_nosync(vma, address); > > > } > > > > > > return young; > > > } > > > > > > Does it mean the current kernel is over cautious? is it > > > safe to call ptep_test_and_clear_young() only? > > > > I can't really explain why we are getting a random app/java vm crash in monkey > > test by using ptep_test_and_clear_young() only in lru_gen_look_around() on an > > armv8-a machine without hardware PTE young support. > > > > Moving to ptep_clear_flush_young() in look_around can make the random > > hang disappear according to zhanyuan(Cc-ed). > > > > On x86, ptep_clear_flush_young() is exactly ptep_test_and_clear_young() > > after > > 'commit b13b1d2d8692 ("x86/mm: In the PTE swapout page reclaim case clear > > the accessed bit instead of flushing the TLB")' > > > > But on arm64, they are different. according to Will's comments in this > > thread which > > tried to make arm64 same with x86, > > https://www.mail-archive.com/linux-kernel@vger.kernel.org/msg1793881.html > > > > " > > This is blindly copied from x86 and isn't true for us: we don't invalidate > > the TLB on context switch. That means our window for keeping the stale > > entries around is potentially much bigger and might not be a great idea. > > > > If we roll a TLB invalidation routine without the trailing DSB, what sort of > > performance does that get you? > > " > > We shouldn't think ptep_clear_flush_young() is safe enough in LRU to > > clear PTE young? Any comments from Will? > > Given that this issue is specific to the multi-gen LRU work, I think Yu is > the best person to comment. However, looking quickly at your analysis above, > I wonder if the code is relying on this sequence: > > > ptep_test_and_clear_young(vma, address, ptep); > ptep_clear_flush_young(vma, address, ptep); > > > to invalidate the TLB. On arm64, that won't be the case, as the invalidation > in ptep_clear_flush_young() is predicated on the pte being young (and this > patches the generic implementation in mm/pgtable-generic.c. In fact, that > second function call is always going to be a no-op unless the pte became > young again in the middle. Hi Will, thanks for your reply, sorry for failing to let you understand my question. my question is actually as below, right now lru_gen_look_around() is using ptep_test_and_clear_young() only without flush to clear pte for a couple of pages including the specific address: void lru_gen_look_around(struct page_vma_mapped_walk *pvmw) { ... for (i = 0, addr = start; addr != end; i++, addr += PAGE_SIZE) { ... if (!ptep_test_and_clear_young(pvmw->vma, addr, pte + i)) continue; ... } I wonder if it is safe to arm64. Do we need to move to ptep_clear_flush_young() in the loop? > > Will Thanks Barry