Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp4285254imm; Tue, 11 Sep 2018 09:33:26 -0700 (PDT) X-Google-Smtp-Source: ANB0VdbQW5NCPLAxkEKZrtgLVYW/ruAw/zrix84aMgGqvaN9TdbLsNtUC2F4gSIXUdAx18UIVTUP X-Received: by 2002:a63:231c:: with SMTP id j28-v6mr29043333pgj.332.1536683606656; Tue, 11 Sep 2018 09:33:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536683606; cv=none; d=google.com; s=arc-20160816; b=MhVgK6oJMpMkOiYHu2l/HQxdwufStHcB2B7hzNxlxlAPz0XCeRSL5ki2oAQI8KIe1q amplEzZBP/VtT2U54SLNBzkbXuUBI49ZA7Uf5eyhkW6K4pREEeH8YIXhIwuFB8Dxas3y unQgyLdL3l5qeaaYW75kRsZqFdJVwZ/q0DNvMca4dWhXdeyAsBg/HtYz+xjo2NPn3SJv wbGIybzUk/rIIylYHX+BRkHQEYBmZ643g59KdS1+I73izvX1PRPK7OEdpIDfbiggmkCu rWa7XW748pDGdpfTakSv7gJ+qK67bo9xnCtiHI2/rFtCd877vINHzOw2dTXF2dTld3IY Avag== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date:from :references:cc:to:subject; bh=zNaFoDRZfFCJ4pu72EObw87OI3qV7QZ6Q7aYe5Gzn00=; b=DO+6U1t+Y0jQerxCyMeTTsA09Ny8tuA30GtIkJorZ63R/yITObQN0Y/f+F1/opfYr+ 1QCJ2VEyzEUMoHsLgVfDPOmOCoRAXEIJLMMQlGuDyj8j9O6l/5WUq8S1CcSeheiDTvr/ o/qtcGF6WoN+iiNsDyX6zj/DIbOScQ/IZGLTXZcVK9UcbhhhZkEomccDAkT8E48PIPrH V6x/DD/2hR7mKESB3akvtAeiK4qcKCJNdLNdWJ3rHExgzRiaDbe2hB6hqxjcSVQioAN8 YtZDqtLpXipaBYPXchrj/lvlFCP3NcAHnyVMnRfH7JOS/sh6Nt1tujKlm4CdCidKRciv 6lZg== 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=ibm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 4-v6si10957032pgw.559.2018.09.11.09.33.06; Tue, 11 Sep 2018 09:33:26 -0700 (PDT) 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=ibm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727994AbeIKVcd (ORCPT + 99 others); Tue, 11 Sep 2018 17:32:33 -0400 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:35004 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727044AbeIKVcc (ORCPT ); Tue, 11 Sep 2018 17:32:32 -0400 Received: from pps.filterd (m0098414.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.22/8.16.0.22) with SMTP id w8BGUS57036163 for ; Tue, 11 Sep 2018 12:32:27 -0400 Received: from e06smtp05.uk.ibm.com (e06smtp05.uk.ibm.com [195.75.94.101]) by mx0b-001b2d01.pphosted.com with ESMTP id 2megfatfwh-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Tue, 11 Sep 2018 12:32:26 -0400 Received: from localhost by e06smtp05.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Tue, 11 Sep 2018 17:32:24 +0100 Received: from b06cxnps3075.portsmouth.uk.ibm.com (9.149.109.195) by e06smtp05.uk.ibm.com (192.168.101.135) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; (version=TLSv1/SSLv3 cipher=AES256-GCM-SHA384 bits=256/256) Tue, 11 Sep 2018 17:32:19 +0100 Received: from d06av24.portsmouth.uk.ibm.com (d06av24.portsmouth.uk.ibm.com [9.149.105.60]) by b06cxnps3075.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id w8BGWInl54591736 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Tue, 11 Sep 2018 16:32:18 GMT Received: from d06av24.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 2C15C42042; Tue, 11 Sep 2018 19:32:10 +0100 (BST) Received: from d06av24.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 6BCBB42047; Tue, 11 Sep 2018 19:32:09 +0100 (BST) Received: from [9.101.4.33] (unknown [9.101.4.33]) by d06av24.portsmouth.uk.ibm.com (Postfix) with ESMTP; Tue, 11 Sep 2018 19:32:09 +0100 (BST) Subject: Re: [RFC PATCH v2 1/8] mm, memcontrol.c: make memcg lru stats thread-safe without lru_lock To: Daniel Jordan , linux-mm@kvack.org, linux-kernel@vger.kernel.org, cgroups@vger.kernel.org Cc: aaron.lu@intel.com, ak@linux.intel.com, akpm@linux-foundation.org, dave.dice@oracle.com, dave.hansen@linux.intel.com, hannes@cmpxchg.org, levyossi@icloud.com, mgorman@techsingularity.net, mhocko@kernel.org, Pavel.Tatashin@microsoft.com, steven.sistare@oracle.com, tim.c.chen@intel.com, vdavydov.dev@gmail.com, ying.huang@intel.com References: <20180911004240.4758-1-daniel.m.jordan@oracle.com> <20180911004240.4758-2-daniel.m.jordan@oracle.com> From: Laurent Dufour Date: Tue, 11 Sep 2018 18:32:17 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 MIME-Version: 1.0 In-Reply-To: <20180911004240.4758-2-daniel.m.jordan@oracle.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-TM-AS-GCONF: 00 x-cbid: 18091116-0020-0000-0000-000002C4BC82 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 18091116-0021-0000-0000-000021120E2D Message-Id: X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2018-09-11_08:,, signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=0 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1807170000 definitions=main-1809110164 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 11/09/2018 02:42, Daniel Jordan wrote: > lru_lock needs to be held to update memcg LRU statistics. This > requirement arises fairly naturally based on when the stats are updated > because callers are holding lru_lock already. > > In preparation for allowing concurrent adds and removes from the LRU, > however, make concurrent updates to these statistics safe without > lru_lock. The lock continues to be held until later in the series, when > it is replaced with a rwlock that also disables preemption, maintaining > the assumption of __mod_lru_zone_size, which is introduced here. > > Follow the existing pattern for statistics in memcontrol.h by using a > combination of per-cpu counters and atomics. > > Remove the negative statistics warning from ca707239e8a7 ("mm: > update_lru_size warn and reset bad lru_size"). Although an earlier > version of this patch updated the warning to account for the error > introduced by the per-cpu counters, Hugh says this warning has not been > seen in the wild and that for simplicity's sake it should probably just > be removed. > > Signed-off-by: Daniel Jordan > --- > include/linux/memcontrol.h | 43 +++++++++++++++++++++++++++++--------- > mm/memcontrol.c | 29 +++++++------------------ > 2 files changed, 40 insertions(+), 32 deletions(-) > > diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h > index d99b71bc2c66..6377dc76dc41 100644 > --- a/include/linux/memcontrol.h > +++ b/include/linux/memcontrol.h > @@ -99,7 +99,8 @@ struct mem_cgroup_reclaim_iter { > }; > > struct lruvec_stat { > - long count[NR_VM_NODE_STAT_ITEMS]; > + long node[NR_VM_NODE_STAT_ITEMS]; > + long lru_zone_size[MAX_NR_ZONES][NR_LRU_LISTS]; It might be better to use different name for the lru_zone_size field to distinguish it from the one in the mem_cgroup_per_node structure. > }; > > /* > @@ -109,9 +110,8 @@ struct mem_cgroup_per_node { > struct lruvec lruvec; > > struct lruvec_stat __percpu *lruvec_stat_cpu; > - atomic_long_t lruvec_stat[NR_VM_NODE_STAT_ITEMS]; > - > - unsigned long lru_zone_size[MAX_NR_ZONES][NR_LRU_LISTS]; > + atomic_long_t node_stat[NR_VM_NODE_STAT_ITEMS]; > + atomic_long_t lru_zone_size[MAX_NR_ZONES][NR_LRU_LISTS]; > > struct mem_cgroup_reclaim_iter iter[DEF_PRIORITY + 1]; > > @@ -446,7 +446,7 @@ unsigned long mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list lru) > > mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec); > for (zid = 0; zid < MAX_NR_ZONES; zid++) > - nr_pages += mz->lru_zone_size[zid][lru]; > + nr_pages += atomic64_read(&mz->lru_zone_size[zid][lru]); > return nr_pages; > } > > @@ -457,7 +457,7 @@ unsigned long mem_cgroup_get_zone_lru_size(struct lruvec *lruvec, > struct mem_cgroup_per_node *mz; > > mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec); > - return mz->lru_zone_size[zone_idx][lru]; > + return atomic64_read(&mz->lru_zone_size[zone_idx][lru]); > } > > void mem_cgroup_handle_over_high(void); > @@ -575,7 +575,7 @@ static inline unsigned long lruvec_page_state(struct lruvec *lruvec, > return node_page_state(lruvec_pgdat(lruvec), idx); > > pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec); > - x = atomic_long_read(&pn->lruvec_stat[idx]); > + x = atomic_long_read(&pn->node_stat[idx]); > #ifdef CONFIG_SMP > if (x < 0) > x = 0; > @@ -601,12 +601,12 @@ static inline void __mod_lruvec_state(struct lruvec *lruvec, > __mod_memcg_state(pn->memcg, idx, val); > > /* Update lruvec */ > - x = val + __this_cpu_read(pn->lruvec_stat_cpu->count[idx]); > + x = val + __this_cpu_read(pn->lruvec_stat_cpu->node[idx]); > if (unlikely(abs(x) > MEMCG_CHARGE_BATCH)) { > - atomic_long_add(x, &pn->lruvec_stat[idx]); > + atomic_long_add(x, &pn->node_stat[idx]); > x = 0; > } > - __this_cpu_write(pn->lruvec_stat_cpu->count[idx], x); > + __this_cpu_write(pn->lruvec_stat_cpu->node[idx], x); > } > > static inline void mod_lruvec_state(struct lruvec *lruvec, > @@ -619,6 +619,29 @@ static inline void mod_lruvec_state(struct lruvec *lruvec, > local_irq_restore(flags); > } > > +/** > + * __mod_lru_zone_size - update memcg lru statistics in batches > + * > + * Updates memcg lru statistics using per-cpu counters that spill into atomics > + * above a threshold. > + * > + * Assumes that the caller has disabled preemption. IRQs may be enabled > + * because this function is not called from irq context. > + */ > +static inline void __mod_lru_zone_size(struct mem_cgroup_per_node *pn, > + enum lru_list lru, int zid, int val) > +{ > + long x; > + struct lruvec_stat __percpu *lruvec_stat_cpu = pn->lruvec_stat_cpu; > + > + x = val + __this_cpu_read(lruvec_stat_cpu->lru_zone_size[zid][lru]); > + if (unlikely(abs(x) > MEMCG_CHARGE_BATCH)) { > + atomic_long_add(x, &pn->lru_zone_size[zid][lru]); > + x = 0; > + } > + __this_cpu_write(lruvec_stat_cpu->lru_zone_size[zid][lru], x); > +} > + > static inline void __mod_lruvec_page_state(struct page *page, > enum node_stat_item idx, int val) > { > diff --git a/mm/memcontrol.c b/mm/memcontrol.c > index 2bd3df3d101a..5463ad160e10 100644 > --- a/mm/memcontrol.c > +++ b/mm/memcontrol.c > @@ -962,36 +962,20 @@ struct lruvec *mem_cgroup_page_lruvec(struct page *page, struct pglist_data *pgd > * @zid: zone id of the accounted pages > * @nr_pages: positive when adding or negative when removing > * > - * This function must be called under lru_lock, just before a page is added > - * to or just after a page is removed from an lru list (that ordering being > - * so as to allow it to check that lru_size 0 is consistent with list_empty). > + * This function must be called just before a page is added to, or just after a > + * page is removed from, an lru list. Callers aren't required to hold lru_lock > + * because these statistics use per-cpu counters and atomics. > */ > void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru, > int zid, int nr_pages) > { > struct mem_cgroup_per_node *mz; > - unsigned long *lru_size; > - long size; > > if (mem_cgroup_disabled()) > return; > > mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec); > - lru_size = &mz->lru_zone_size[zid][lru]; > - > - if (nr_pages < 0) > - *lru_size += nr_pages; > - > - size = *lru_size; > - if (WARN_ONCE(size < 0, > - "%s(%p, %d, %d): lru_size %ld\n", > - __func__, lruvec, lru, nr_pages, size)) { > - VM_BUG_ON(1); > - *lru_size = 0; > - } > - > - if (nr_pages > 0) > - *lru_size += nr_pages; > + __mod_lru_zone_size(mz, lru, zid, nr_pages); > } > > bool task_in_mem_cgroup(struct task_struct *task, struct mem_cgroup *memcg) > @@ -1833,9 +1817,10 @@ static int memcg_hotplug_cpu_dead(unsigned int cpu) > struct mem_cgroup_per_node *pn; > > pn = mem_cgroup_nodeinfo(memcg, nid); > - x = this_cpu_xchg(pn->lruvec_stat_cpu->count[i], 0); > + x = this_cpu_xchg(pn->lruvec_stat_cpu->node[i], > + 0); > if (x) > - atomic_long_add(x, &pn->lruvec_stat[i]); > + atomic_long_add(x, &pn->node_stat[i]); > } > } >