From patchwork Wed Apr 24 12:59:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Breno Leitao X-Patchwork-Id: 13641833 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7FF27C4345F for ; Wed, 24 Apr 2024 13:00:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AE4848D000B; Wed, 24 Apr 2024 09:00:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A94748D0001; Wed, 24 Apr 2024 09:00:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 95C118D000B; Wed, 24 Apr 2024 09:00:02 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 739648D0001 for ; Wed, 24 Apr 2024 09:00:02 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 18069C0FFD for ; Wed, 24 Apr 2024 13:00:02 +0000 (UTC) X-FDA: 82044433044.03.1FA41FC Received: from mail-ej1-f47.google.com (mail-ej1-f47.google.com [209.85.218.47]) by imf15.hostedemail.com (Postfix) with ESMTP id 1017DA0011 for ; Wed, 24 Apr 2024 12:59:58 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=none; spf=pass (imf15.hostedemail.com: domain of breno.debian@gmail.com designates 209.85.218.47 as permitted sender) smtp.mailfrom=breno.debian@gmail.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1713963599; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references; bh=bZpXDLWdbY5cxtVKR5gQdGHFoKg4O1yoZrRZTfWHXEU=; b=UBCxkgGUNjc1oIk134XXQYHl3CEjRMMv3BFGLRO9QSjzhYIlln393IufxTZ6roka7laisA K2+cIRAiWiYjKrM3MUmAZMs3bdlEOt6uX+GmUaP3rypRhSW76SEzPPPjSwviQriGElf7qS XuVbXWYmFKll69Vb15ncCQlDOJN75z0= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=none; spf=pass (imf15.hostedemail.com: domain of breno.debian@gmail.com designates 209.85.218.47 as permitted sender) smtp.mailfrom=breno.debian@gmail.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1713963599; a=rsa-sha256; cv=none; b=4dN6fGe5FcNVh8UkoYRinx35uyvTIdQH3CjPeccIRXgxjZjIURppFU002cMwM/yUtE/CWQ RZejGoE/D9osimd5/ucufqHtXhxe7Y3YwIs+9BsOpf7WBreUiKBOs0rZAmZ8ccRtmvpE3e ZDe6YLavUFHYDcrwWtJo6nYvixaXzuc= Received: by mail-ej1-f47.google.com with SMTP id a640c23a62f3a-a4702457ccbso773460966b.3 for ; Wed, 24 Apr 2024 05:59:58 -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=irfX8BPHxxgN3k3JrlTAq4NixHgmHToTERD7o4Dco6ACoyepqokZ/XqyH0zRf8CjzV Tq4kAcRlKIke72eUf5T1EhCMo0tiKydNv4JAhfhH81OSUggy5aLLbHocZjR/GmtWE5ky j/u1qKHUKixy/t9wBbAhi+KI570oT6rjkJeRK0No9CDnBlrJHeajD3E2+VDWu2f56EY9 sHx9M7HOCIgFcYnZPP70Rggi+7+0wWhJhKbDB39vDbOzEWw3YC7Fiy4dXj3g0PawTxg4 4r96dOhoZW+hvua8XlU5SH8hHXCQUxfLa2wP9Yb78kwD6eZ+39JuR66qHpmuQ1E1Smyt vKzQ== X-Forwarded-Encrypted: i=1; AJvYcCXGE9aEMae/0vJ3yUDAAwGyDnpJUgkDUnLYXM2/Xxxsx7T4IoyCMRK66RRc2eFzumqVlZlivwIA96isN6MLoijqwq0= X-Gm-Message-State: AOJu0YxrEyok3IkhLcHYmkNge/RvxSnpDBkblx/wyQ5ZRDdTkiz0Y1it eKt7Sg7Ly108uf+0zDX7CXwR8T2kTZ+28/T3iZ2lgrIunvDuuBTH X-Google-Smtp-Source: AGHT+IF/RV0/1XswegILNQyeX/GUzV1RYykInrtqHpdfC1ZxMCeLSWwtzZwxtUJZYZPRpwl7qXgTJQ== 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 MIME-Version: 1.0 X-Rspamd-Queue-Id: 1017DA0011 X-Stat-Signature: p7szrgshs7hg67m53w8r49z61sca85mt X-Rspam-User: X-Rspamd-Server: rspam07 X-HE-Tag: 1713963598-311804 X-HE-Meta: U2FsdGVkX1/6YAzArkth6iR185H6jXAP9v1QdEyXmQFnsJlF6WFkwN2whPZDjiCLsqRYW/H1bhD3EmCYetjO75ymG++B/AHxox3ll9cVdRHXzQSckhJNoaDMzt3Ti6qFwr/hddXEqLxerWSUJ3d7A8CnTL3qc5xJJDS5eg+aCYys1x5E/eZMV5Pfv8+zz/YGY+/6mSCRMi1PuDrVytTOGWFrQbTi9FydvK/irbKsRwsFd4ixWgS3QxRmWigoVlGAwGPl6kjYNyB09C0J0Egr9eW0MYleJQtl4x2v+gQr4996fn2S/yh5UhU03fOL/QzXdaEteIK2oRZSNDzbln0N6I8T2Rn831Vv0VLZXiBJ4ZJ2xY+z7+VBq9X9+GyCbdO6gakk3LrclqS705YPJ3SmxsJ99shluSDUu5eV40qOWX/bpMmJF30KwJAZZozkxJGeDHaJQWKghPQiLZ2ixoI5SxcGQecuTwC2R5wYyped07aIisjSbNRSN+HSHnsgS4hzcK66U8bHdTENGXQZOpMXzt0mq4GcdAOlAzGNk7i1fT82OK4aczQlgC7mtIlWQOBsi/dFy7xFAPnurg/+RryKOTA6K/7GLx+jAJVMx4kRXmowEBIhTscp1/gW9jYqX06f4MF7rYv/vxCXvN6wVL/2/e6oGV2RV5DQnRNlW2gcqfXfrJqehQCvceD+LwgeX+e/vLe4ZF6AjdrmWJQ0bGLX+mtD9cE73kUeiH9/TZxRZtbtexDEz6jadMd9VVWOAULt6xQsF9x4QXWy+kprrKDv6PWGndgEnY1so5INXXmK06/XzsFOoS/sR1jjSUpGpEsF5nLDAn6ZjGOiDi8QNkq+cT16Zwz4rOQoWZNohXvgF2M/l7d+5GovzsNllY0K2zdu1TuIcCxDlFWoxp3sF1CODMVCyHpUtZP0b1VWSEvvxrZzPYa7/d3hY4bQV28kOhtmXM/nLbg0aDhpSSi4XkX 69UjJzds GMj8PNKP4V46cfcE5y9pwIDSUreVpn+eSxGF5CCBMRqrp6q4LxlRA5W+jOwV/IMn9P3UY+E2I5EIV4NzefB+MYZ5FkTVJbiWUMNekIfCObbd0BkYYWn5Oj8Jw7LgTwYcT6TQzVAExd/S4Zov5gHnis16lOFFmDG4DWFUrtETd6v5P0gplGZ60FbxQ3Zs1XW1sJ4kxaBkPIk+iPvkFVUvWgpmpml+mk2ZLivf8PPnEUcAYVJwEqWcbjeWvgGjONipz/z6JQk7YMloa6EN44vL8fJfVnTnanXfrYDP0uxhwB5s9HeHyR91zRXiP6HNL3aXAG1YsfeG6Y8pKPbsAhlO3FzGwr4CE+TI3h1r04IetyBdBgIAdJ0GSNE7eZXFDg13SsMnH8yHUfR4a+YZFlRYeWBXQ0JNC1CsGwXPHVUC4zqq+Mnqd8bx6nJcKq32H4eDzUMqF+VW+5wJFzYai47FOX68S8mMuDhw2zqf6CE6dvfalgWsWHFWJxiL9ixW+j/05w4Iez9XulkVVTwKT23BV2XUjJx6kpDYqfiERXqgrWcq2P+6AuMz2pr3khQxFDR4tw8CmrsfqEHfnqHl3Ie36oLRY19fSfwzEuXb9Kyl9KbtZ3BgtyieuJCEo8R8sFmJpdhAzuFo9JM/4cvBIBDrdRywCKJHHMDhqldyj06RZUSDnO1Y= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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 Acked-by: Johannes Weiner Acked-by: Shakeel Butt Reviewed-by: Yosry Ahmed Acked-by: Michal Hocko --- 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);