Received: by 2002:a25:7ec1:0:0:0:0:0 with SMTP id z184csp1843270ybc; Sun, 24 Nov 2019 07:21:22 -0800 (PST) X-Google-Smtp-Source: APXvYqw03w5STzLfQDYq9QNKShusJWzP9tMDzejcg5sDmEl5oHX6HVwXpTudWOao78cjg9h3Rm6h X-Received: by 2002:a17:906:13d0:: with SMTP id g16mr9250567ejc.157.1574608881991; Sun, 24 Nov 2019 07:21:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1574608881; cv=none; d=google.com; s=arc-20160816; b=LHpQgAjn4eOBWbQ9KM/ZOILTv8hghG+NSWACAchxX5srIhzcTjQiJCwTG3turmnRre +fIggXHkro4MwT5pSmI1LSJ+qWmCwrMvoNvUMmC13S2hpUOHbT1JEq+slG/1uQVG4mgf 3B8nbLxgaKux1vN9WehtwDoR7C1NGe6cXnBoPbmn/bkOYfW/Rh3ivyD55HiXVifiJoZI /5t1fCeemG4M8EZWTZFPRKhmO3ZgrW8fGhGkcL9LYWGfWg2TlRp17fq604uRM/hbWsre gzXbTVtABG6UTn/1IAJte2Jvfe/z/2VBrlliQJdHAW5yhd+MPV4iugLynJzxswEfhPjl 88IQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:in-reply-to :mime-version:user-agent:date:message-id:from:references:cc:to :subject; bh=JYFWad50codNhCblY1QYc+Eyc3gEMXVqD8Oj1LUoq00=; b=tXa08Uqwm8TWluYVEvwquX8pM7JnLKNlaWqai2cpiDIPp5ApJ/1+YAUB3FTcI2bkrL iYaoqUR8JYfbdpSQFUC008Nv3o9ZkEN40CGqg9ZBfxJyx2McWG46BFb98TMejmMSXHTH mbRVsjFqkE07McsHWgqM3Uc9GjbtGXHmq4cJvbf08XV2liVUUBo63BKYjUcGuXeLzaAt yYnShvGlZ2/oYJyyci5bmg/mEjarPVlctYRF+OvFbk0DX5H5roG0lmVwTNH3UZ78sj1t Eon2Rd9FkfJ9z2TT38WOuTPywJHpzlbJ+EN3RARBp/wFMpOMOjHwncmYIqDBQPlNrJXm y8+A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id dd28si3442504edb.99.2019.11.24.07.20.58; Sun, 24 Nov 2019 07:21:21 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727091AbfKXPTX (ORCPT + 99 others); Sun, 24 Nov 2019 10:19:23 -0500 Received: from out4436.biz.mail.alibaba.com ([47.88.44.36]:29300 "EHLO out4436.biz.mail.alibaba.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726922AbfKXPTX (ORCPT ); Sun, 24 Nov 2019 10:19:23 -0500 X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R211e4;CH=green;DM=||false|;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01e04395;MF=alex.shi@linux.alibaba.com;NM=1;PH=DS;RN=38;SR=0;TI=SMTPD_---0TiwwSUG_1574608754; Received: from IT-FVFX43SYHV2H.local(mailfrom:alex.shi@linux.alibaba.com fp:SMTPD_---0TiwwSUG_1574608754) by smtp.aliyun-inc.com(127.0.0.1); Sun, 24 Nov 2019 23:19:15 +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> <20191122161652.GA489821@cmpxchg.org> From: Alex Shi Message-ID: Date: Sun, 24 Nov 2019 23:19:06 +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: <20191122161652.GA489821@cmpxchg.org> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 在 2019/11/23 上午12:16, Johannes Weiner 写道: > On Fri, Nov 22, 2019 at 10:36:32AM +0800, Alex Shi wrote: >> 在 2019/11/22 上午6:06, Johannes Weiner 写道: >>> 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. >>> >>> 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? > > Yes. > >> 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 = page->mem_cgroup; >> + memcg = lock_page_memcg(page); >> /* >> * Swapcache readahead pages are added to the LRU - and >> * possibly migrated - before they are charged. > > test_clear_page_writeback() calls this function with that lock already > held so that would deadlock. Let's keep locking in lock_page_lruvec(). > > lock_page_lruvec(): > > memcg = lock_page_memcg(page); > lruvec = mem_cgroup_lruvec(page_pgdat(page), memcg); > > spin_lock_irqsave(&lruvec->lru_lock, *flags); > return lruvec; > > unlock_lruvec(); > > spin_unlock_irqrestore(&lruvec->lru_lock); > __unlock_page_memcg(lruvec_memcg(lruvec)); > > The lock ordering should be fine as well. But it might be a good idea > to stick a might_lock(&memcg->move_lock) in lock_page_memcg() before > that atomic_read() and test with lockdep enabled. Hi Johannes, Thanks a lot for the suggestion. I will look into this and try. > > > But that leaves me with one more worry: compaction. We locked out > charge moving now, so between that and knowing that the page is alive, > we have page->mem_cgroup stable. But compaction doesn't know whether > the page is alive - it comes from a pfn and finds out using PageLRU. > > In the current code, pgdat->lru_lock remains the same before and after > the page is charged to a cgroup, so once compaction has that locked > and it observes PageLRU, it can go ahead and isolate the page. > > But lruvec->lru_lock changes during charging, and then compaction may > hold the wrong lock during isolation: > > compaction: generic_file_buffered_read: > > page_cache_alloc() > > !PageBuddy() > > lock_page_lruvec(page) > lruvec = mem_cgroup_page_lruvec() > spin_lock(&lruvec->lru_lock) > if lruvec != mem_cgroup_page_lruvec() > goto again > > add_to_page_cache_lru() > mem_cgroup_commit_charge() > page->mem_cgroup = foo > lru_cache_add() > __pagevec_lru_add() > SetPageLRU() > > if PageLRU(page): > __isolate_lru_page() > > I don't see what prevents the lruvec from changing under compaction, > neither in your patches nor in Hugh's. Maybe I'm missing something? Yes, it's a problem. Guess we could move the lruvec recheck after PageLRU() test in compaction. Then it could be safe, and add a bit more burden on compaction should be fine. at last we have no disturb to file read. Thanks Alex