Received: by 2002:a89:48b:0:b0:1f5:f2ab:c469 with SMTP id a11csp395391lqd; Wed, 24 Apr 2024 06:00:34 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWXNZFqJokqg/UpShOVEYh/Dd3UKBpNPyjjYDMXFig9QZXXwc1wGMBnqShTPW/Ro6kxS9hqAsP/luVd/lzbwSqCFR652hPrK/dag5F+yA== X-Google-Smtp-Source: AGHT+IHw8mAtKrpV+b8CaROw+85HZoAhev7gg6aVd4IEDsKFnaLV7oeo7ax8kVBxeUtL9Wr15+Qv X-Received: by 2002:a17:906:3387:b0:a4e:3fad:b973 with SMTP id v7-20020a170906338700b00a4e3fadb973mr1585405eja.65.1713963634287; Wed, 24 Apr 2024 06:00:34 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713963634; cv=pass; d=google.com; s=arc-20160816; b=i9BYI2r00oE2rBOYrnzmSKM0goYGNOf7WIcy33/rokGqoywNsavuJZU/+g48kqUBoB pQ7XjD7Qp7TAEn+/GQasSSG0zr1yCLvWegEhT92GvZ8AgWtUZMwuQ/vji1tH3ALcIlaQ 7SHO31h3CrcrYeunemQAAPZWT9WthF87HcNdgUixJ3WeCVsd4mCJdgOjFqQL/GFHDhiZ W5nzSjA74+dPuSvmQ22/5CrHc6zlSr/Q5gZ+iygU6fd+nqakBuTTYOcp6lR8wAwmK2OI PQdvAAl4G1QXmzng0qJ6r8SP+QFcFBvKCUhB6HbkkzYrx9znr8ut8JpHYkCrbyaj5Oy3 3LBw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from; bh=bZpXDLWdbY5cxtVKR5gQdGHFoKg4O1yoZrRZTfWHXEU=; fh=0Z3sbgQPKCW/joR4ny7SgCEEe2wHq4H9fcaN6PtCT+k=; b=hfruP3z6S7IRTPTmKLw0W47D5DFjLipvVFgA0AUktwKkYHems+QJIgEmKTsNcWa7Yf FqsJ7rPB+6BEdIpeb29iKaih/ClEx/ZXxywa8zU8cgOMXRRu260h/NeAwR/uL4GFe26G MVWIhrhwomh/AIIE0RUJ40wQb1ecO/ZEtSjEOKUPFpEkM4KN+aLgybB480DIQC9Q8DSL 2Z9qEJAkNVPBlGrE5Vh4KWMQJcAyyxSMcZ1/sO0KC5PJpVhCL5Gn9gRRmi/ZrxaQ0KTz KWUxQF2TL/8WIOM8vcFAgEcZaK0ydKyp+2TpKfkm2Eqj87Bfrj48BdpQQy0gC6voPfli QpRA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-156916-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-156916-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id hd10-20020a170907968a00b00a559f83ead1si6326562ejc.880.2024.04.24.06.00.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Apr 2024 06:00:34 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-156916-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-156916-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-156916-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 0A6F21F25DEE for ; Wed, 24 Apr 2024 13:00:34 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 985B615B979; Wed, 24 Apr 2024 13:00:01 +0000 (UTC) Received: from mail-ej1-f51.google.com (mail-ej1-f51.google.com [209.85.218.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4624815ADB8; Wed, 24 Apr 2024 12:59:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.51 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713963601; cv=none; b=UYUh7PgNB4pUOAib2NBOHx7OtAd9Yg2Ldf6kgGvYLZqfsoeV5GkKif7KHb5VaHzS5p+kg9uNaM8mKaGLeDuS4LND1qiXQ/PDaYfLjT9cLnDp+XZ4OWPd6aiEw8n9MWv3f0QY/1A12a+7dHR+RlcJXsJhniiXR60ngho5fP7FOUw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713963601; c=relaxed/simple; bh=6Yy7m/6CzXiExG1qEJxxc9fYsBWXedfhGZTx/aOQ58k=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=EML/tXcrsYmNg8JPEmU8u5LGs0KJgqhKN2gI4PT2aySGpK6o8kmTxh5fegmJsBar+ogYY+9JBKFzs9Yxk3tvqSD7NHAf1Q4c7N6ds/fTi0NsmxjxPBthgtZAuLSvndWiF99mo8fgiWkUA+VFRHfp4gODa48d8Gb9aya2ruQ4JMI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=debian.org; spf=pass smtp.mailfrom=gmail.com; arc=none smtp.client-ip=209.85.218.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=debian.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-ej1-f51.google.com with SMTP id a640c23a62f3a-a524ecaf215so701199766b.2; Wed, 24 Apr 2024 05:59:59 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713963597; x=1714568397; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=bZpXDLWdbY5cxtVKR5gQdGHFoKg4O1yoZrRZTfWHXEU=; b=EGzCv4dOQFnCIUPYodSHgrTVT6q/HuntRurdy1lPQZvVic2k2+VYfyEC5coIiW2ahZ lGMKjHPH1sDd5VLbtaSd+zt5TQ0KmcQz5bnhnvl1ccI0azwk1N0437OB+YbLK7K89PI/ FaOqrlEbuUeXh3jKkBqPQ69YtLhNC9PRlsBME2MvI+iwlX9aGNBHHJBKWyZDAO6/Wm2x fmOZsLlzksx5HrPBhz1A/WQOReBka5S9YSW1OkylROjVd1gzZ6TsvigWvlwIGetJnpQ4 OKZOnn0NKBJj7K4Tdx7fIPbOu4oklmww+fJG2jdyKvKC6Jf8nonqHhBUdJwz/gigJg06 c8/w== X-Forwarded-Encrypted: i=1; AJvYcCVIV59K3FBaDeYEJMeoPzLgOljA6wYEOFa3lZ9TlJ8XigYLh8hMj40y3mR2Xze2gvn+gRrEUkuN01k+ac7OqytmZnMSKCZCP7CBTmBZ0PZVYvj42koxhUdzv5yVarC5TwTADp/a+A== X-Gm-Message-State: AOJu0YyDLK+85DyNzt+7hR9HCSdcSKOhXOTDARrPqM2+CuPpiDZSvF7P d3RxK3MPwQAKR0SmPiUPiwnfxSkAiVVEoYirVpDeRmqxYTHNZyi6 X-Received: by 2002:a17:906:d142:b0:a58:828b:554e with SMTP id br2-20020a170906d14200b00a58828b554emr1607782ejb.71.1713963597341; Wed, 24 Apr 2024 05:59:57 -0700 (PDT) Received: from localhost (fwdproxy-lla-003.fbsv.net. [2a03:2880:30ff:3::face:b00c]) by smtp.gmail.com with ESMTPSA id lu10-20020a170906faca00b00a52299d8eecsm8214771ejb.135.2024.04.24.05.59.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Apr 2024 05:59:56 -0700 (PDT) From: Breno Leitao To: Johannes Weiner , Michal Hocko , Roman Gushchin , Shakeel Butt , Muchun Song , Andrew Morton Cc: leit@meta.com, cgroups@vger.kernel.org (open list:CONTROL GROUP - MEMORY RESOURCE CONTROLLER (MEMCG)), linux-mm@kvack.org (open list:CONTROL GROUP - MEMORY RESOURCE CONTROLLER (MEMCG)), linux-kernel@vger.kernel.org (open list) Subject: [PATCH] memcg: Fix data-race KCSAN bug in rstats Date: Wed, 24 Apr 2024 05:59:39 -0700 Message-ID: <20240424125940.2410718-1-leitao@debian.org> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit A data-race issue in memcg rstat occurs when two distinct code paths access the same 4-byte region concurrently. KCSAN detection triggers the following BUG as a result. BUG: KCSAN: data-race in __count_memcg_events / mem_cgroup_css_rstat_flush write to 0xffffe8ffff98e300 of 4 bytes by task 5274 on cpu 17: mem_cgroup_css_rstat_flush (mm/memcontrol.c:5850) cgroup_rstat_flush_locked (kernel/cgroup/rstat.c:243 (discriminator 7)) cgroup_rstat_flush (./include/linux/spinlock.h:401 kernel/cgroup/rstat.c:278) mem_cgroup_flush_stats.part.0 (mm/memcontrol.c:767) memory_numa_stat_show (mm/memcontrol.c:6911) read to 0xffffe8ffff98e300 of 4 bytes by task 410848 on cpu 27: __count_memcg_events (mm/memcontrol.c:725 mm/memcontrol.c:962) count_memcg_event_mm.part.0 (./include/linux/memcontrol.h:1097 ./include/linux/memcontrol.h:1120) handle_mm_fault (mm/memory.c:5483 mm/memory.c:5622) value changed: 0x00000029 -> 0x00000000 The race occurs because two code paths access the same "stats_updates" location. Although "stats_updates" is a per-CPU variable, it is remotely accessed by another CPU at cgroup_rstat_flush_locked()->mem_cgroup_css_rstat_flush(), leading to the data race mentioned. Considering that memcg_rstat_updated() is in the hot code path, adding a lock to protect it may not be desirable, especially since this variable pertains solely to statistics. Therefore, annotating accesses to stats_updates with READ/WRITE_ONCE() can prevent KCSAN splats and potential partial reads/writes. Suggested-by: Shakeel Butt Signed-off-by: Breno Leitao --- mm/memcontrol.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index fabce2b50c69..3c99457b36a1 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -715,6 +715,7 @@ static inline void memcg_rstat_updated(struct mem_cgroup *memcg, int val) { struct memcg_vmstats_percpu *statc; int cpu = smp_processor_id(); + unsigned int stats_updates; if (!val) return; @@ -722,8 +723,9 @@ static inline void memcg_rstat_updated(struct mem_cgroup *memcg, int val) cgroup_rstat_updated(memcg->css.cgroup, cpu); statc = this_cpu_ptr(memcg->vmstats_percpu); for (; statc; statc = statc->parent) { - statc->stats_updates += abs(val); - if (statc->stats_updates < MEMCG_CHARGE_BATCH) + stats_updates = READ_ONCE(statc->stats_updates) + abs(val); + WRITE_ONCE(statc->stats_updates, stats_updates); + if (stats_updates < MEMCG_CHARGE_BATCH) continue; /* @@ -731,9 +733,9 @@ static inline void memcg_rstat_updated(struct mem_cgroup *memcg, int val) * redundant. Avoid the overhead of the atomic update. */ if (!memcg_vmstats_needs_flush(statc->vmstats)) - atomic64_add(statc->stats_updates, + atomic64_add(stats_updates, &statc->vmstats->stats_updates); - statc->stats_updates = 0; + WRITE_ONCE(statc->stats_updates, 0); } } @@ -5845,7 +5847,7 @@ static void mem_cgroup_css_rstat_flush(struct cgroup_subsys_state *css, int cpu) } } } - statc->stats_updates = 0; + WRITE_ONCE(statc->stats_updates, 0); /* We are in a per-cpu loop here, only do the atomic write once */ if (atomic64_read(&memcg->vmstats->stats_updates)) atomic64_set(&memcg->vmstats->stats_updates, 0); -- 2.43.0