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 X-Spam-Level: X-Spam-Status: No, score=-5.2 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,UNPARSEABLE_RELAY, USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 08FECC432C0 for ; Fri, 22 Nov 2019 02:36:48 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 9D9202067D for ; Fri, 22 Nov 2019 02:36:47 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 9D9202067D Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id D71696B049C; Thu, 21 Nov 2019 21:36:46 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D48096B049D; Thu, 21 Nov 2019 21:36:46 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C88306B049E; Thu, 21 Nov 2019 21:36:46 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0150.hostedemail.com [216.40.44.150]) by kanga.kvack.org (Postfix) with ESMTP id B20936B049C for ; Thu, 21 Nov 2019 21:36:46 -0500 (EST) Received: from smtpin19.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with SMTP id 67BE75853 for ; Fri, 22 Nov 2019 02:36:46 +0000 (UTC) X-FDA: 76182350412.19.sack58_1de3017e95913 X-HE-Tag: sack58_1de3017e95913 X-Filterd-Recvd-Size: 5299 Received: from out30-42.freemail.mail.aliyun.com (out30-42.freemail.mail.aliyun.com [115.124.30.42]) by imf46.hostedemail.com (Postfix) with ESMTP for ; Fri, 22 Nov 2019 02:36:45 +0000 (UTC) X-Alimail-AntiSpam:AC=PASS;BC=-1|-1;BR=01201311R201e4;CH=green;DM=||false|;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01f04427;MF=alex.shi@linux.alibaba.com;NM=1;PH=DS;RN=38;SR=0;TI=SMTPD_---0TilD44h_1574390198; Received: from IT-FVFX43SYHV2H.local(mailfrom:alex.shi@linux.alibaba.com fp:SMTPD_---0TilD44h_1574390198) by smtp.aliyun-inc.com(127.0.0.1); Fri, 22 Nov 2019 10:36:39 +0800 Subject: Re: [PATCH v4 3/9] mm/lru: replace pgdat lru_lock with lruvec lock To: Johannes Weiner Cc: cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, akpm@linux-foundation.org, mgorman@techsingularity.net, tj@kernel.org, hughd@google.com, khlebnikov@yandex-team.ru, daniel.m.jordan@oracle.com, yang.shi@linux.alibaba.com, willy@infradead.org, shakeelb@google.com, Michal Hocko , Vladimir Davydov , Roman Gushchin , Chris Down , Thomas Gleixner , Vlastimil Babka , Qian Cai , Andrey Ryabinin , "Kirill A. Shutemov" , =?UTF-8?B?SsOpcsO0bWUgR2xpc3Nl?= , Andrea Arcangeli , David Rientjes , "Aneesh Kumar K.V" , swkhack , "Potyra, Stefan" , Mike Rapoport , Stephen Rothwell , Colin Ian King , Jason Gunthorpe , Mauro Carvalho Chehab , Peng Fan , Nikolay Borisov , Ira Weiny , Kirill Tkhai , Yafang Shao References: <1574166203-151975-1-git-send-email-alex.shi@linux.alibaba.com> <1574166203-151975-4-git-send-email-alex.shi@linux.alibaba.com> <20191119160456.GD382712@cmpxchg.org> <20191121220613.GB487872@cmpxchg.org> From: Alex Shi Message-ID: Date: Fri, 22 Nov 2019 10:36:32 +0800 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:60.0) Gecko/20100101 Thunderbird/60.9.1 MIME-Version: 1.0 In-Reply-To: <20191121220613.GB487872@cmpxchg.org> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable 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: =E5=9C=A8 2019/11/22 =E4=B8=8A=E5=8D=886:06, Johannes Weiner =E5=86=99=E9= =81=93: >> >> Forgive my idiot, I still don't know the details of unsafe lruvec here= . >> From my shortsight, the spin_lock_irq(embedded a preempt_disable) coul= d block all rcu syncing thus, keep all memcg alive until the preempt_enab= led in unspinlock, is this right? >> If so even the page->mem_cgroup is migrated to others cgroups, the new= and old cgroup should still be alive here. > You are right about the freeing part, I missed this. And I should have > read this email here before sending out my "fix" to the current code; > thankfully Hugh re-iterated my mistake on that thread. My apologies. >=20 That's all right. You and Hugh do give me a lot of help! :) > But I still don't understand how the moving part is safe. You look up > the lruvec optimistically, lock it, then verify the lookup. What keeps > page->mem_cgroup from changing after you verified it? >=20 > lock_page_lruvec(): mem_cgroup_move_account(): > again: > rcu_read_lock() > lruvec =3D page->mem_cgroup->lruvec > isolate_lru_page() > spin_lock_irq(&lruvec->lru_lock) > rcu_read_unlock() > if page->mem_cgroup->lruvec !=3D lruvec: > spin_unlock_irq(&lruvec->lru_lock) > goto again; > page->mem_cgroup =3D new cgroup > putback_lru_page() // new lruvec > SetPageLRU() > return lruvec; // old lruvec >=20 > The caller assumes page belongs to the returned lruvec and will then > change the page's lru state with a mismatched page and lruvec. >=20 Yes, that's the problem we have to deal. > If we could restrict lock_page_lruvec() to working only on PageLRU > pages, we could fix the problem with memory barriers. But this won't > work for split_huge_page(), which is AFAICT the only user that needs > to freeze the lru state of a page that could be isolated elsewhere. >=20 > So AFAICS the only option is to lock out mem_cgroup_move_account() > entirely when the lru_lock is held. Which I guess should be fine. I guess we can try from lock_page_memcg, is that a good start? diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 7e6387ad01f0..f4bbbf72c5b8 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -1224,7 +1224,7 @@ struct lruvec *mem_cgroup_page_lruvec(struct page *= page, struct pglist_data *pgd goto out; } - memcg =3D page->mem_cgroup; + memcg =3D lock_page_memcg(page); /* * Swapcache readahead pages are added to the LRU - and * possibly migrated - before they are charged. Thanks a lot! Alex