Received: by 2002:a25:e7d8:0:0:0:0:0 with SMTP id e207csp2690156ybh; Mon, 16 Mar 2020 07:57:07 -0700 (PDT) X-Google-Smtp-Source: ADFU+vuCcTERZWguhGLPA9DkQMVlRpkYpJUvaOA3vMxjrkbfHtYDzNaTpmstJjNymq/scvdEXdEU X-Received: by 2002:a9d:63d2:: with SMTP id e18mr22506088otl.277.1584370627106; Mon, 16 Mar 2020 07:57:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1584370627; cv=none; d=google.com; s=arc-20160816; b=EHumEULBNuBKRWTafYIPz2Vunnc0DqcGmRAcrX0NwLJfF8pfGAdlZXgSrixDI3NTPa LnspRJMJIso0ShmrXAq87rf/zEC/hyeJ++p+IEekjkPPOse91WCoLDGGtoME7CYX4Ff4 CkVSQf3TTDhJR7Tmp+lhvQAFP9l73IG2TW8sGIcZrZmTLkwjSI84z5t8qbaHyga/w3ov iQO3R8ab1ENxKVitebCbDppy6RTgiXvwfP6/1nMc+wUj/67xjWnQnzpLLo0a9r2C9sze yX4EjBEr6/M0PYq4Fhw3VavotcR8U9s2abubrDede+CCuamrjByb25KbRaUFZ+ScxdMh G1aw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date; bh=VrtE/xlfUCPSQK0yP3Q30OMAAICBZ67o1F4lAQpydlc=; b=xrsoiz6HaFA9ovRENoMkoQaUW0WTA9KuRV+vLO6bVlPeX7aZX4cNxCAhXkne1Ykkl7 yTfRgGp5A+KIN0NYiDNgPvB/DiPH5FeGFteInYalDEBtZAgBG3Mw88ea4oXc0domP55W cCS5Nlkbh5FA5q3cHTBEEIYW0ISLtfxIxKFEomVvY/Q04rff1O34XB9v9R37zbs5dCpQ SOp6NUXW74sAa2/JcSUpiLoZpbMkaU7B1CmcVwqIapr9Z61eJJsymu91Y6G1bRwRrlig JOP898r2bl3gd7NgAKNPuxY8JUvWT3JqWhXWXwc5y1kIB9j2WENgYlhuPOWYWhqAnGfn AFng== 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=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id k6si72680otn.172.2020.03.16.07.56.54; Mon, 16 Mar 2020 07:57:07 -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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731641AbgCPO4g (ORCPT + 99 others); Mon, 16 Mar 2020 10:56:36 -0400 Received: from mail-wm1-f65.google.com ([209.85.128.65]:55720 "EHLO mail-wm1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731539AbgCPO4g (ORCPT ); Mon, 16 Mar 2020 10:56:36 -0400 Received: by mail-wm1-f65.google.com with SMTP id 6so18010891wmi.5; Mon, 16 Mar 2020 07:56:35 -0700 (PDT) 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:in-reply-to; bh=VrtE/xlfUCPSQK0yP3Q30OMAAICBZ67o1F4lAQpydlc=; b=eLPvMiRXCRNnK1WYt2DxTDt73T3O1Qp4jIGlaMzCWQ12B20QhdXUJIkJvMD43KBkNV P0CGpi5/YbdfzmnPmLN/g8s2NVd2Hj08ua7RE0ovHu9aR/ozAk/mOnwHxWppeQR77NQ3 iVv29CyH7+3NzpEymrb8xkHRngrgohsWnXOJg39CYFp0MIFdj9lkgmGTDHm5LdTwCQGi KAs6O86h3MlNk5XvqqLZVvR9Ggs/kyZo/27aOqvdK+tTdAyhTOfkbPAov+PibwyeUjRR fLL4rnJBm1SILbG1oEPau+ZAJt86ihwzytrTJBxb/w0OVjGoORmeBDYdFL2uBhkWog3F oBIg== X-Gm-Message-State: ANhLgQ1PS+2DRM5bXG/qegFqWRQ5Jwbl3fKteNSH8vtCPXU+OazcR3Ro jlyi2gPZmXWBj4JDOBiTCCU= X-Received: by 2002:a1c:4c16:: with SMTP id z22mr29205085wmf.50.1584370594403; Mon, 16 Mar 2020 07:56:34 -0700 (PDT) Received: from localhost ([37.188.132.163]) by smtp.gmail.com with ESMTPSA id b202sm5587869wmd.15.2020.03.16.07.56.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Mar 2020 07:56:33 -0700 (PDT) Date: Mon, 16 Mar 2020 15:56:30 +0100 From: Michal Hocko To: Chris Down Cc: Andrew Morton , Johannes Weiner , Tejun Heo , Roman Gushchin , linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, kernel-team@fb.com Subject: Re: [PATCH 2/6] mm, memcg: Prevent memory.max load tearing Message-ID: <20200316145630.GN11482@dhcp22.suse.cz> References: <50a31e5f39f8ae6c8fb73966ba1455f0924e8f44.1584034301.git.chris@chrisdown.name> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <50a31e5f39f8ae6c8fb73966ba1455f0924e8f44.1584034301.git.chris@chrisdown.name> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu 12-03-20 17:32:56, Chris Down wrote: > This one is a bit more nuanced because we have memcg_max_mutex, which is > mostly just used for enforcing invariants, but we still need to > READ_ONCE since (despite its name) it doesn't really protect memory.max > access. > > On write (page_counter_set_max() and memory_max_write()) we use xchg(), > which uses smp_mb(), so that's already fine. > > Signed-off-by: Chris Down > Cc: Andrew Morton > Cc: Johannes Weiner > Cc: Roman Gushchin > Cc: Tejun Heo > Cc: linux-mm@kvack.org > Cc: cgroups@vger.kernel.org > Cc: linux-kernel@vger.kernel.org > Cc: kernel-team@fb.com Acked-by: Michal Hocko > --- > mm/memcontrol.c | 12 ++++++------ > 1 file changed, 6 insertions(+), 6 deletions(-) > > diff --git a/mm/memcontrol.c b/mm/memcontrol.c > index d32d3c0a16d4..aca2964ea494 100644 > --- a/mm/memcontrol.c > +++ b/mm/memcontrol.c > @@ -1507,7 +1507,7 @@ void mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg) > > pr_info("memory: usage %llukB, limit %llukB, failcnt %lu\n", > K((u64)page_counter_read(&memcg->memory)), > - K((u64)memcg->memory.max), memcg->memory.failcnt); > + K((u64)READ_ONCE(memcg->memory.max)), memcg->memory.failcnt); > if (cgroup_subsys_on_dfl(memory_cgrp_subsys)) > pr_info("swap: usage %llukB, limit %llukB, failcnt %lu\n", > K((u64)page_counter_read(&memcg->swap)), > @@ -1538,7 +1538,7 @@ unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg) > { > unsigned long max; > > - max = memcg->memory.max; > + max = READ_ONCE(memcg->memory.max); > if (mem_cgroup_swappiness(memcg)) { > unsigned long memsw_max; > unsigned long swap_max; > @@ -3006,7 +3006,7 @@ static int mem_cgroup_resize_max(struct mem_cgroup *memcg, > * Make sure that the new limit (memsw or memory limit) doesn't > * break our basic invariant rule memory.max <= memsw.max. > */ > - limits_invariant = memsw ? max >= memcg->memory.max : > + limits_invariant = memsw ? max >= READ_ONCE(memcg->memory.max) : > max <= memcg->memsw.max; > if (!limits_invariant) { > mutex_unlock(&memcg_max_mutex); > @@ -3753,8 +3753,8 @@ static int memcg_stat_show(struct seq_file *m, void *v) > /* Hierarchical information */ > memory = memsw = PAGE_COUNTER_MAX; > for (mi = memcg; mi; mi = parent_mem_cgroup(mi)) { > - memory = min(memory, mi->memory.max); > - memsw = min(memsw, mi->memsw.max); > + memory = min(memory, READ_ONCE(mi->memory.max)); > + memsw = min(memsw, READ_ONCE(mi->memsw.max)); > } > seq_printf(m, "hierarchical_memory_limit %llu\n", > (u64)memory * PAGE_SIZE); > @@ -4257,7 +4257,7 @@ void mem_cgroup_wb_stats(struct bdi_writeback *wb, unsigned long *pfilepages, > *pheadroom = PAGE_COUNTER_MAX; > > while ((parent = parent_mem_cgroup(memcg))) { > - unsigned long ceiling = min(memcg->memory.max, > + unsigned long ceiling = min(READ_ONCE(memcg->memory.max), > READ_ONCE(memcg->high)); > unsigned long used = page_counter_read(&memcg->memory); > > -- > 2.25.1 -- Michal Hocko SUSE Labs