Received: by 2002:a25:e7d8:0:0:0:0:0 with SMTP id e207csp758944ybh; Thu, 12 Mar 2020 10:34:26 -0700 (PDT) X-Google-Smtp-Source: ADFU+vvnC5BsX0B0HV/B+g96iIY2T3jzmYZpo3B7GSFsP3PisEiHKqkDgsdk0YsbuzOq6ok6nEFg X-Received: by 2002:aca:210c:: with SMTP id 12mr3361968oiz.0.1584034465902; Thu, 12 Mar 2020 10:34:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1584034465; cv=none; d=google.com; s=arc-20160816; b=D/7udAC6RyZNloYl7CrDoKSZpBiVCChHytGJqgE9SCFsbGCiHFL+4asEasaoiXWKUR FkFs/r/lSaiGXqdiV1XHrXRweykUlbWP62RC/siD82hjKKEqEIJBty2OYTSrRZt8BknL Ig9FZZcpHYteZH6uYC3EVShENvLzQNSyepsR5LqvO1t//kPLiTWm+Cwjnm9W//b63Uv0 stZPp2LBQwuG6wYoWF5bhWMLxzTl6WIxKruaCu/8CS6xNa9Byt1kem7hpXZ56N4feczR 5xJHR5YOTsZs7zxHxy8DophMS4YypcjM6+1845wXmrxcOVLNFPQ9r66ICwhDK8xfVspJ kzCw== 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 :dkim-signature; bh=Wi04zoB+1lwUS/JkrIWiJtz6qrlQUGWxV2d3AofBbt4=; b=RlSIkedtkdtsI++XSiqLWHt7dDmFuMR+GTGXRufZHcJujkHyNFRQAVG+I0Re+JONdM bOVPT785yp33xJ3CPJn0XWl/RUQmHnq+iwhd396/ceQHp81+EGEuEVaT35gjQ1peAkgn PFLK/RhA7sTOgZQFzalVL+2Dwm8v9S7bZ82EWhRo49COeLooamhkDQ0OK8XGDipuBNoD JeEXQ2NhBH8H0IQGAjijohoKDZ9IN6hWXms6GTmMbBaiy/XIvD4V+pD1s1kxIPFitpp6 VLm+jX7ejcoMQVkCBri9Ru+jMokejwQc1fF8+bEYTcHI7KtD2bdzXiwFmxbo3VU1epWW o1uw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chrisdown.name header.s=google header.b=qdHoaPhx; 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=pass (p=NONE sp=NONE dis=NONE) header.from=chrisdown.name Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o2si3058937otj.312.2020.03.12.10.34.11; Thu, 12 Mar 2020 10:34:25 -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; dkim=pass header.i=@chrisdown.name header.s=google header.b=qdHoaPhx; 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=pass (p=NONE sp=NONE dis=NONE) header.from=chrisdown.name Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726669AbgCLRdE (ORCPT + 99 others); Thu, 12 Mar 2020 13:33:04 -0400 Received: from mail-wr1-f65.google.com ([209.85.221.65]:33572 "EHLO mail-wr1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726390AbgCLRdE (ORCPT ); Thu, 12 Mar 2020 13:33:04 -0400 Received: by mail-wr1-f65.google.com with SMTP id a25so8595750wrd.0 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=V9jN21rccMRG8qhK3WXJq8A1PZIKq5h+KdbOjfwxQMy+eqAozfcaAFoykvPVDHz4x7 NgNmK+5ad/MTEe28f7WoYDBPS+fQxdade/+CptNV8Ro2a/reESSxT2pC7ytQlYvNPeq5 mELWa7ML5VPQ2MzjRFZz/KIul+nBj/wdau9wMu+Pw6AVgmUFyLXEbgcQyC7MQnw2CEnu PKlFWa8ctRa6ufd2IUJlwmaRs1ro82BheLX/lYUbIZha/1nH60GkjYZoo4p9/mnGsHOn ZRIBD1DkzGtHorWAjcTc7g1gM+f1Mp8gCHBm2BSI71fIFuC4rAIo4oOokQB7JhMopRdW iqkA== X-Gm-Message-State: ANhLgQ2lhw1q4jtpuTjbmLo/wrrxXChjibDrh9GbRtneL4J5yJWbgGbB ZPmyQBUP5TF+my/2JauHDar4cg== 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-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- 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)); -- 2.25.1