Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp253256pxb; Mon, 2 Nov 2020 21:05:16 -0800 (PST) X-Google-Smtp-Source: ABdhPJyTP685WIZgBM/nii0VlnKetPwXQvIWkw74rl82LOMKWE4kARPMBlWTPmm70qgWVa7Bxi7y X-Received: by 2002:a17:906:4748:: with SMTP id j8mr1543252ejs.22.1604379916147; Mon, 02 Nov 2020 21:05:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1604379916; cv=none; d=google.com; s=arc-20160816; b=ADU57uNJ7rxfl21C3b7ihUimgYYrt8s54ezhKWSWluEe4TYPkAHIpatoSw8+BY13GX fyTwOvbhZeBWqxLexQf83I8lKGWrEc3HTg4FlgieLGKFQmB0D4E9r+BIA5ry6BKzBgHE IblUWtg5biP3bbsLuGVAgImD+1JQq5cuPrmM+qGX5AmT2hEqe4VroQHx6QtA1kRP5Cv4 S7LTUl1LHKtqaUbxLlRD+zonqb68zor5Q54/UOQ5C/s1fVLeNX8jBI1X46oNnp6dNz5m WxwYO3god3ZX8JNcjkP5ehV4AnNmtB7N5Rh1m1G7pn35j+rS6CXo0x2DAq9ev6hQuGfp tZhg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to :mime-version:user-agent:date:message-id:from:references:cc:to :subject; bh=tY0QsdBhqA9xVGQps7qHEQ9LYVpLpmInkzo3OSSQb0E=; b=lVY28EhjkCFOKxjKk5pJvDCo3eqDcPCg4ieY5M4wxREhAKdDr2XPNWUqSS3M0p8Hrf yTxpU6LdjKUVZVtgu4yb5D4lBKypRiFXwygPIR02C/LmctY3OFCZlULERszPqBH+dmik bC0rnH1R6GVrt+OKsMIvYx95F9Wn7BnD/Ni5eTuyKq52ALa/dyqQpw7HokukrWKAGEu/ mvaQPYqxhMFpSsIBhHsxwORtBFewbxHAWdNtrWyDa+r8oO53yQUWju6/bPI6LFzNUozb Gl/O4DK8Z81YiuFpgCzJHHb/capFr6/2V0qW6gOjBL7c/xdkyZwtcaiQxMxRU3U57BFs mqOg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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. [23.128.96.18]) by mx.google.com with ESMTP id v21si5480886ejx.104.2020.11.02.21.04.53; Mon, 02 Nov 2020 21:05:16 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S1726264AbgKCFBM (ORCPT + 99 others); Tue, 3 Nov 2020 00:01:12 -0500 Received: from out30-132.freemail.mail.aliyun.com ([115.124.30.132]:42997 "EHLO out30-132.freemail.mail.aliyun.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725958AbgKCFBM (ORCPT ); Tue, 3 Nov 2020 00:01:12 -0500 X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R121e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=alimailimapcm10staff010182156082;MF=alex.shi@linux.alibaba.com;NM=1;PH=DS;RN=23;SR=0;TI=SMTPD_---0UE3fJvY_1604379663; Received: from IT-FVFX43SYHV2H.local(mailfrom:alex.shi@linux.alibaba.com fp:SMTPD_---0UE3fJvY_1604379663) by smtp.aliyun-inc.com(127.0.0.1); Tue, 03 Nov 2020 13:01:04 +0800 Subject: Re: [PATCH v20 18/20] mm/lru: replace pgdat lru_lock with lruvec lock To: Johannes Weiner Cc: akpm@linux-foundation.org, mgorman@techsingularity.net, tj@kernel.org, hughd@google.com, khlebnikov@yandex-team.ru, daniel.m.jordan@oracle.com, willy@infradead.org, lkp@intel.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, shakeelb@google.com, iamjoonsoo.kim@lge.com, richard.weiyang@gmail.com, kirill@shutemov.name, alexander.duyck@gmail.com, rong.a.chen@intel.com, mhocko@suse.com, vdavydov.dev@gmail.com, shy828301@gmail.com, Michal Hocko , Yang Shi References: <1603968305-8026-1-git-send-email-alex.shi@linux.alibaba.com> <1603968305-8026-19-git-send-email-alex.shi@linux.alibaba.com> <20201102204136.GB740958@cmpxchg.org> From: Alex Shi Message-ID: Date: Tue, 3 Nov 2020 12:58:29 +0800 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:68.0) Gecko/20100101 Thunderbird/68.12.0 MIME-Version: 1.0 In-Reply-To: <20201102204136.GB740958@cmpxchg.org> Content-Type: text/plain; charset=gbk Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org ?? 2020/11/3 ????4:41, Johannes Weiner ะด??: > On Fri, Oct 30, 2020 at 10:49:41AM +0800, Alex Shi wrote: >> >> >> @@ -1367,6 +1380,51 @@ struct lruvec *mem_cgroup_page_lruvec(struct page *page, struct pglist_data *pgd >> return lruvec; >> } >> >> +struct lruvec *lock_page_lruvec(struct page *page) >> +{ >> + struct lruvec *lruvec; >> + struct pglist_data *pgdat = page_pgdat(page); >> + >> + rcu_read_lock(); >> + lruvec = mem_cgroup_page_lruvec(page, pgdat); >> + spin_lock(&lruvec->lru_lock); >> + rcu_read_unlock(); >> + >> + lruvec_memcg_debug(lruvec, page); >> + >> + return lruvec; >> +} >> + >> +struct lruvec *lock_page_lruvec_irq(struct page *page) >> +{ >> + struct lruvec *lruvec; >> + struct pglist_data *pgdat = page_pgdat(page); >> + >> + rcu_read_lock(); >> + lruvec = mem_cgroup_page_lruvec(page, pgdat); >> + spin_lock_irq(&lruvec->lru_lock); >> + rcu_read_unlock(); >> + >> + lruvec_memcg_debug(lruvec, page); >> + >> + return lruvec; >> +} >> + >> +struct lruvec *lock_page_lruvec_irqsave(struct page *page, unsigned long *flags) >> +{ >> + struct lruvec *lruvec; >> + struct pglist_data *pgdat = page_pgdat(page); >> + >> + rcu_read_lock(); >> + lruvec = mem_cgroup_page_lruvec(page, pgdat); >> + spin_lock_irqsave(&lruvec->lru_lock, *flags); >> + rcu_read_unlock(); >> + >> + lruvec_memcg_debug(lruvec, page); >> + >> + return lruvec; >> +} > > As these are used quite widely throughout the VM code now, it would be > good to give them kerneldoc comments that explain the interface. > > In particular, I think it's necessary to explain the contexts from > which this is safe to use (in particular wrt pages moving between > memcgs - see the comment in commit_charge()). > > I'm going to go through the callsites that follow and try to identify > what makes them safe. It's mostly an exercise to double check our > thinking here. > > Most of them are straight-forward, and I don't think they warrant > individual comments. But some do, IMO. And it appears at least one > actually isn't safe yet: Thanks a lot reminder. is the following comments fine? /** * lock_page_lruvec - return lruvec for the locked page. * @page: the page * * This series functions should be used in either conditions: * PageLRU is cleared or unset * or, page->_refcount is zero */ struct lruvec *lock_page_lruvec(struct page *page) { .... >> @@ -274,9 +270,8 @@ void lru_note_cost(struct lruvec *lruvec, bool file, unsigned int nr_pages) >> { >> do { >> unsigned long lrusize; >> - struct pglist_data *pgdat = lruvec_pgdat(lruvec); >> >> - spin_lock_irq(&pgdat->lru_lock); >> + spin_lock_irq(&lruvec->lru_lock); >> /* Record cost event */ >> if (file) >> lruvec->file_cost += nr_pages; >> @@ -300,7 +295,7 @@ void lru_note_cost(struct lruvec *lruvec, bool file, unsigned int nr_pages) >> lruvec->file_cost /= 2; >> lruvec->anon_cost /= 2; >> } >> - spin_unlock_irq(&pgdat->lru_lock); >> + spin_unlock_irq(&lruvec->lru_lock); >> } while ((lruvec = parent_lruvec(lruvec))); >> } > > This is safe because it either comes from > > 1) the pinned lruvec in reclaim, or > > 2) from a pre-LRU page during refault (which also holds the > rcu lock, so would be safe even if the page was on the LRU > and could move simultaneously to a new lruvec). > > The second one seems a bit tricky. It could be good to add a comment > to lru_note_cost_page() that explains why its mem_cgroup_page_lruvec() > is safe. Thanks for pointed, is the following comments fine? diff --git a/mm/swap.c b/mm/swap.c index 9fe5ff9a8111..55ccc93ffb49 100644 --- a/mm/swap.c +++ b/mm/swap.c @@ -264,6 +264,13 @@ void lru_note_cost(struct lruvec *lruvec, bool file, unsigned int nr_pages) do { unsigned long lrusize; + /* + * Hold lruvec->lru_lock is safe here, since + * 1) The pinned lruvec in reclaim, or + * 2) From a pre-LRU page during refault (which also holds the + * rcu lock, so would be safe even if the page was on the LRU + * and could move simultaneously to a new lruvec). + */ spin_lock_irq(&lruvec->lru_lock); /* Record cost event */ > >> @@ -364,13 +359,13 @@ static inline void activate_page_drain(int cpu) >> >> static void activate_page(struct page *page) >> { >> - pg_data_t *pgdat = page_pgdat(page); >> + struct lruvec *lruvec; >> >> page = compound_head(page); >> - spin_lock_irq(&pgdat->lru_lock); >> + lruvec = lock_page_lruvec_irq(page); > > I don't see what makes this safe. There is nothing that appears to > lock out a concurrent page move between memcgs/lruvecs, which means > the following could manipulate an unlocked lru list: > This funtion is for !CONFIG_SMP, could the cpu be preempt with RT kernel? >> if (PageLRU(page)) >> - __activate_page(page, mem_cgroup_page_lruvec(page, pgdat)); >> - spin_unlock_irq(&pgdat->lru_lock); >> + __activate_page(page, lruvec); >> + unlock_page_lruvec_irq(lruvec); >> } >> #endif > > Shouldn't this be something like this? > > if (TestClearPageLRU()) { > lruvec = lock_page_lruvec_irq(page); > __activate_page(page, lruvec); > unlock_page_lruvec_irq(lruvec); > SetPageLRU(page); > } But anyway, your new changes are more beautiful and logcially. I will change to this. Thanks a lot! Alex