Received: by 2002:a25:7ec1:0:0:0:0:0 with SMTP id z184csp491629ybc; Fri, 22 Nov 2019 08:21:23 -0800 (PST) X-Google-Smtp-Source: APXvYqwXYS0kyoXsl/f5NEIF4wDK8bVMQxCFJP3se4+n9WkPsb00iu0v9w+lQIohP3rQL02Rhwy8 X-Received: by 2002:a17:906:1f98:: with SMTP id t24mr22153054ejr.143.1574439683764; Fri, 22 Nov 2019 08:21:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1574439683; cv=none; d=google.com; s=arc-20160816; b=KRVuNMDmJ2edXKsxwksw5dS6LljnzHpOVMjiqA+cpEWMbIz4QhiIFS1nkrrH9gn8qq OHPy3Mb66ylMw9iLM39aL23BJyn7MJ2kOt5resDZTYmE4fdpMLAZE2bK/w4w7En9pLio O/SOpNzQgOmGY4b04UwHzwjDZxph+4WxLPFuQPmdJkBrh9ioWFzI3nBmehtQvZ16rnUJ dGeju8P8S/1AWW23Y7sBPpUNNpNrtotAz9uxtHHJlQOpavW60DipejRy8CUfTZ/fsRQJ V8tYsOXN11sKSTxHFValZPWAHrQ7OPhOtCbY2H7h9NnXWZdNzTQdiXMalJoeTkurKres 3pQw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-transfer-encoding:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=wfrcrjX2H8D8RSvVc5LOIPsNb3WQnwmD64TJNBPNLUA=; b=eUdTPKTS4dfaw8u5bPe0/zNc3f0pljUc92OZVH0N9VGfJRKQftEwAT9y61Z+BfxJ9v CtkZ5cH5B+HpkTE1TXbX4tO56uI1jLLsY9ZLl3JJH+mKzsxxcj+UQUiCUb0khKBDM7mJ Ux7gPWOGJx72iR5Xf9lt4ZsadLP20INYOcVtdyppNW+siz+FqPoIpKubjvghWz9NQTGY WtpDPHbuSYzXQdNufdIgC1aho8ZrybnGjWlWZ3XDNWNjpdE6eXMcFQqvCJDOZEEvxIsB cbyXKN779Z+lH9HiplMZka7/P06Vo4jvanS/TL1lFMU25XCfP29FOPlUNLSilTuVGouy /QXQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cmpxchg-org.20150623.gappssmtp.com header.s=20150623 header.b=2CxFAF9v; 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=cmpxchg.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g26si4903893edv.109.2019.11.22.08.20.58; Fri, 22 Nov 2019 08:21:23 -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; dkim=pass header.i=@cmpxchg-org.20150623.gappssmtp.com header.s=20150623 header.b=2CxFAF9v; 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=cmpxchg.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727433AbfKVQQ5 (ORCPT + 99 others); Fri, 22 Nov 2019 11:16:57 -0500 Received: from mail-pl1-f194.google.com ([209.85.214.194]:43991 "EHLO mail-pl1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726546AbfKVQQ5 (ORCPT ); Fri, 22 Nov 2019 11:16:57 -0500 Received: by mail-pl1-f194.google.com with SMTP id a18so3275739plm.10 for ; Fri, 22 Nov 2019 08:16:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cmpxchg-org.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:content-transfer-encoding:in-reply-to :user-agent; bh=wfrcrjX2H8D8RSvVc5LOIPsNb3WQnwmD64TJNBPNLUA=; b=2CxFAF9vQ8BbOhqPOkf+HBDvDUxib+UWq9hNW0Tg4l/6YvGcIgULwb7zDfm9mbuP/9 3/EKpUXrSdv8B03hy14QpNLhWat83HNQyn1GgIfaBTMQMmmanUe+btbssVaH8haWbOpr QqRPK10O9tUUfndkbGrdfQe1z6ITlCczduG7UJhZ6JNU7ZoQRSrIbazx85Z8h4rz26Xb 6EJVTX6A/tEz9L4CMMWPjveLs+QHDUyDxvybrIHm+D9wpJ2bnJSnESNPWmTfEoXJTO6d RHERX6f1D2fDaO/gYzRlAdEHF6PJxDPr/jbgCMQMh3Z2Fj6YG7Q5lbEYKwW7Ol6/bl7e sgEw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:content-transfer-encoding :in-reply-to:user-agent; bh=wfrcrjX2H8D8RSvVc5LOIPsNb3WQnwmD64TJNBPNLUA=; b=ubMS6vFLrsFOnFcV3LGsbqJzA83Ozft+hYimwhYc4iT0/x7BS0MzTuqEUNMuAJ1NtI 5ziUN4lbmyG5d8Eoy16XWBi5VC89tYzH/i5PJfuFH8Cd2xnlFW4cbGzFx30r62DCYcNp VJkRggRyipQ9H77qMNJQJmpNCf3W3b3Z79aP6y9t+dWo52VEN5MuVmSKGazLkxuoVVQD mZEyZvQyZK7qm4BFXmbYfWTrC3afUlcF85ryVCJzEceDFZHM/eZ1MmqeU/yihJSMuzdW Crl0Z+ranTVJsgJlKXjkPkAm8OhqAhDL/CdTPynrzL1DcZ2GNfaZyfPDomflWbXiJtBX otTg== X-Gm-Message-State: APjAAAVoxQ7Pxmvwp37VLiNXu0uGx1/XV4rHz0H1jpPaLkBkQ97K2sME /I44E9ndmkYQ+EGWB32zyHffqA== X-Received: by 2002:a17:90a:b28c:: with SMTP id c12mr19445564pjr.22.1574439415868; Fri, 22 Nov 2019 08:16:55 -0800 (PST) Received: from localhost ([2620:10d:c090:180::9f2b]) by smtp.gmail.com with ESMTPSA id q3sm7349049pgl.15.2019.11.22.08.16.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Nov 2019 08:16:54 -0800 (PST) Date: Fri, 22 Nov 2019 11:16:52 -0500 From: Johannes Weiner To: Alex Shi 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" , =?iso-8859-1?B?Suly9G1l?= Glisse , 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 Subject: Re: [PATCH v4 3/9] mm/lru: replace pgdat lru_lock with lruvec lock Message-ID: <20191122161652.GA489821@cmpxchg.org> 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> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.12.2 (2019-09-21) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. 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?