From patchwork Thu Mar 12 17:33:01 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chris Down X-Patchwork-Id: 11435133 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 943686CA for ; Thu, 12 Mar 2020 17:33:05 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 605A6206B1 for ; Thu, 12 Mar 2020 17:33:05 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=chrisdown.name header.i=@chrisdown.name header.b="qdHoaPhx" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 605A6206B1 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=chrisdown.name Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 556A16B0072; Thu, 12 Mar 2020 13:33:04 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 506C16B0073; Thu, 12 Mar 2020 13:33:04 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 41D306B0074; Thu, 12 Mar 2020 13:33:04 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0107.hostedemail.com [216.40.44.107]) by kanga.kvack.org (Postfix) with ESMTP id 28CA56B0072 for ; Thu, 12 Mar 2020 13:33:04 -0400 (EDT) Received: from smtpin16.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id DAAD98E7F for ; Thu, 12 Mar 2020 17:33:03 +0000 (UTC) X-FDA: 76587405846.16.root74_37d08df31a544 X-Spam-Summary: 2,0,0,9e24ad115754caf5,d41d8cd98f00b204,chris@chrisdown.name,,RULES_HIT:41:355:379:800:960:973:988:989:1260:1277:1312:1313:1314:1345:1359:1431:1437:1516:1518:1519:1535:1542:1593:1594:1595:1596:1711:1730:1747:1777:1792:2393:2553:2559:2562:3138:3139:3140:3141:3142:3353:3865:3867:4321:4605:5007:6261:6653:7904:10004:10400:11026:11473:11658:11914:12043:12296:12297:12438:12517:12519:12555:12895:12986:13439:13846:13895:14096:14097:14181:14394:14721:21080:21444:21451:21627:21990:30054:30064:30090,0,RBL:209.85.221.68:@chrisdown.name:.lbl8.mailshell.net-62.2.0.100 66.100.201.201,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:none,Custom_rules:0:0:0,LFtime:24,LUA_SUMMARY:none X-HE-Tag: root74_37d08df31a544 X-Filterd-Recvd-Size: 5255 Received: from mail-wr1-f68.google.com (mail-wr1-f68.google.com [209.85.221.68]) by imf14.hostedemail.com (Postfix) with ESMTP for ; Thu, 12 Mar 2020 17:33:03 +0000 (UTC) Received: by mail-wr1-f68.google.com with SMTP id 6so8568929wre.4 for ; Thu, 12 Mar 2020 10:33:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chrisdown.name; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=Wi04zoB+1lwUS/JkrIWiJtz6qrlQUGWxV2d3AofBbt4=; b=qdHoaPhxQicwt6H3R0TCGLXYf5k3L9ooFg374fONxjv8C3rvNxgZwp+11xl3vl5riH 0+LwXAumxpp4RyUzxzdWqtEjG5BG9CDRPl4N6+xAtMsl5UUdZ8hz2enRswBOwYzfdka9 XGBadlITdSJeRvdGcod95gw7PRehSEFfoQdJU= 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=Wi04zoB+1lwUS/JkrIWiJtz6qrlQUGWxV2d3AofBbt4=; b=ucltX9TPHMCXJOZWmtXrtY+LUxo6A1YWvtEnxYxUe9JhGj6w5VlduABdgWevTUqIjj f6HJJU1XIyHjUkpKpkxaIlAXW7TuxoIX3dXjTs6Ae6QoDfldHw+PajcaSaVXXrGK1zjf z+dWROL8KHDWPMqRupG9LQPO2Uv2GDsaBdNipm9q3i7D88R7up5EJ4DK3Ubx/RTwNLZw AU6KTvF/FsvUo/+jqnllmZJgk+BXr5N+kI2YhoqR10a4TozYGNxjf/KhClHuu8NWPFwZ 7IuAqagT1nHyf+LxVHTRMfDN8SUJzfKS6Ivye4rg5UpoPu7yiMI/dvqQei36Po6Y9iCK ZEnQ== X-Gm-Message-State: ANhLgQ2X+XL/LE7d5JXZhXBVjNNbVJ4d1B+ecy5hv+0TWcU3Qt+K0XWu yCMJgqfXzTnokIqMakn8/sfy3w== X-Google-Smtp-Source: ADFU+vs6lh4JtrMC8cO3JiGvHgD7AQUqQmn80t3o/bMOWdGzTkGWTWbLaTayMV0C4e7AmUIfCKkQxw== X-Received: by 2002:adf:e4ca:: with SMTP id v10mr11831736wrm.305.1584034382407; Thu, 12 Mar 2020 10:33:02 -0700 (PDT) Received: from localhost ([89.32.122.5]) by smtp.gmail.com with ESMTPSA id s7sm9500905wro.10.2020.03.12.10.33.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Mar 2020 10:33:01 -0700 (PDT) Date: Thu, 12 Mar 2020 17:33:01 +0000 From: Chris Down To: Andrew Morton Cc: Johannes Weiner , Tejun Heo , Roman Gushchin , linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, kernel-team@fb.com Subject: [PATCH 3/6] mm, memcg: Prevent memory.low load/store tearing Message-ID: <448206f44b0fa7be9dad2ca2601d2bcb2c0b7844.1584034301.git.chris@chrisdown.name> References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-Bogosity: Ham, tests=bogofilter, spamicity=0.000001, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: This can be set concurrently with reads, which may cause the wrong value to be propagated. 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 | 4 ++-- mm/page_counter.c | 9 ++++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index aca2964ea494..c85a304fa4a1 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -6262,7 +6262,7 @@ enum mem_cgroup_protection mem_cgroup_protected(struct mem_cgroup *root, return MEMCG_PROT_NONE; emin = memcg->memory.min; - elow = memcg->memory.low; + elow = READ_ONCE(memcg->memory.low); parent = parent_mem_cgroup(memcg); /* No parent means a non-hierarchical mode on v1 memcg */ @@ -6291,7 +6291,7 @@ enum mem_cgroup_protection mem_cgroup_protected(struct mem_cgroup *root, if (elow && parent_elow) { unsigned long low_usage, siblings_low_usage; - low_usage = min(usage, memcg->memory.low); + low_usage = min(usage, READ_ONCE(memcg->memory.low)); siblings_low_usage = atomic_long_read( &parent->memory.children_low_usage); diff --git a/mm/page_counter.c b/mm/page_counter.c index 50184929b61f..18b7f779f2e2 100644 --- a/mm/page_counter.c +++ b/mm/page_counter.c @@ -17,6 +17,7 @@ static void propagate_protected_usage(struct page_counter *c, unsigned long usage) { unsigned long protected, old_protected; + unsigned long low; long delta; if (!c->parent) @@ -34,8 +35,10 @@ static void propagate_protected_usage(struct page_counter *c, atomic_long_add(delta, &c->parent->children_min_usage); } - if (c->low || atomic_long_read(&c->low_usage)) { - if (usage <= c->low) + low = READ_ONCE(c->low); + + if (low || atomic_long_read(&c->low_usage)) { + if (usage <= low) protected = usage; else protected = 0; @@ -231,7 +234,7 @@ void page_counter_set_low(struct page_counter *counter, unsigned long nr_pages) { struct page_counter *c; - counter->low = nr_pages; + WRITE_ONCE(counter->low, nr_pages); for (c = counter; c; c = c->parent) propagate_protected_usage(c, atomic_long_read(&c->usage));