Received: by 2002:a89:48b:0:b0:1f5:f2ab:c469 with SMTP id a11csp758421lqd; Wed, 24 Apr 2024 16:41:53 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVkCsQ6CIN0Ih2FK0h7yCz2azlIZsjYpaH7gfMyXkbjLqWuz95z/T1dblbbYn5pTr0XFm9zoNgkyaJoY9rEfk8NoBPFmEoIWAQQtyRokg== X-Google-Smtp-Source: AGHT+IFbzpN+CSE2aE5RZPNXQUh0hb6lzwfeJV3189HeHsfqq4VHdcIy6w5yEKrgFerEmNfpnMxc X-Received: by 2002:a05:6a20:d48f:b0:1ad:85c1:2d21 with SMTP id im15-20020a056a20d48f00b001ad85c12d21mr4036682pzb.8.1714002113044; Wed, 24 Apr 2024 16:41:53 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714002113; cv=pass; d=google.com; s=arc-20160816; b=EXKy5Gg9HM24hzye5JP8zf2sFaoAnYZXMp3gt+bsb8mgj2Z0mr01q1xG0EFQheQh9u RbIb2TC0jB80nFE53Ho9ZgYYs+JoKRzSk0b8GG+cN3lqwB4b+nV1stbYbCCB4a0p6C4p mfs9/boumzk8+GNz9n5qS79fx13rQL1NGDEFlmW1M1R4UkD2t2jcrMVweLeJPCNQjRBo +j72W1dyjMkIG30H8YP+MxmQkbxJDrWZctM2aDbIz+2gUJWCfWkBeGccvihztK9s80Dz ersFg2Ss+7IRhAxpUeAvKO0Sie7hl+UIXfw/i+Ushj1KHlVGYiRhdUSxbhUIVaiFrZq2 2K7A== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=R4Xhz5NWCXtt8nKXxeIFmhH8Oed5NRQI3JQnJYBJb4s=; fh=/FXLld8jrmsdu17FW1jDsmKNIUWA+ZZGmlL/sNc/t9U=; b=vEYfqqGqiiEPkfHdPTtirs5h7OOIouynbYvE/ay93rDAYXBXQksNHUiB++8XA6o/xK RNNLHl0KBvIWxT1jpyhRAZ00mqPstXnHT75nwUTPtV9i8sr+tDtXmajBpBK35SExQKLJ JWS4qWnYVREbD7h3Cjaiu3PuMpqsem0r4YOM6y+mPGnkrU4oY5jDHXQK5BE7XsNCRAeE ioeWUVR1oFV9iUQR0AX0X8+dPjS2J5jBXp2ignIXTGybhz0P7vTYNSI2aPEkEMvjqlAn Ne6Y4lUYOOqi0T7jM5kAlTfqLQernWCGWfIT0agZ3YyJrbd2TbDfxyT/8NjDE92AYANP CR8w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=ZDBmXtqK; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-157838-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-157838-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id m67-20020a633f46000000b005f7fd775d79si10537494pga.229.2024.04.24.16.41.52 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Apr 2024 16:41:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-157838-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=ZDBmXtqK; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-157838-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-157838-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com 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 sy.mirrors.kernel.org (Postfix) with ESMTPS id D81A9B277A2 for ; Wed, 24 Apr 2024 23:24:09 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CC43416F0EF; Wed, 24 Apr 2024 23:23:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ZDBmXtqK" Received: from mail-lj1-f178.google.com (mail-lj1-f178.google.com [209.85.208.178]) (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 593B51E86F for ; Wed, 24 Apr 2024 23:23:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.178 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714001035; cv=none; b=hLlBLwWvuatsq14yjpD6tcUrTxFgXaKR3N4SKEIU3bhi4PR7y10URN6ipeK0qU1E76XuUbVDhzDMcce1SHQRhgRrzQhi33aVJitzaTXlFGtPZqGQZ1CyyT3JM4Uv0rCkoCL/i6YZVaA1pEl5VpbqhX+a2vdt9E0mBxndso4GGss= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714001035; c=relaxed/simple; bh=kWYPE52TsD9tInankw4Nl3ZO8d6bCgXJgGeWHlBI7E8=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=N4PzVRcNR7KRptnJb52Pv3cwTDa5elQ7nTfKAcpi14RIByzI6eMnfmN3Uv1VVjAyX84dMlMtLOGyFoR4EAQrVTYJQKOhl5Gbm9UyAy5c0duboMVBJD/11yniWo7mdhaXUvI1ZU61uYQJH3dWSVTvzS2HqJMJyDHXZZOEDcT6KLE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=ZDBmXtqK; arc=none smtp.client-ip=209.85.208.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Received: by mail-lj1-f178.google.com with SMTP id 38308e7fff4ca-2dd041acff1so4208491fa.1 for ; Wed, 24 Apr 2024 16:23:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1714001031; x=1714605831; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=R4Xhz5NWCXtt8nKXxeIFmhH8Oed5NRQI3JQnJYBJb4s=; b=ZDBmXtqKUgzM5W6Y9v/FaSWCHcLxNWr12HbqWLBMO6yf6A8OTiahKd3JK1posYwxSG vuMm8PFjLRvrMt5lwZUyiweQsVDVF/F62rnhi0zCE97/mNJZDsIrMMajRsNQZSWmr4og 91mlctLys46RrYkEjgAGu3mrzHmHtO6IJiTk66weMjRoMXvW0gpylRits2TiFvcY1cMt BY5wd9sliMz2uIigVPYvjLwCd1a6JIZf3M2FhGr/gSilxQWrA4VkRFCwDVXgAf+hN4jo BJ3M7EGjRQdm3yamJoku/qFFDjGF1fVDfDEZ7VgXBFd+amcVrRPryrjanJWvvXizAaAS nCGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714001031; x=1714605831; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=R4Xhz5NWCXtt8nKXxeIFmhH8Oed5NRQI3JQnJYBJb4s=; b=ox5hxNHuRaZScxbF6/DCTJt5P0Hd05CrvBVnoF7Lv4rUf9+MMBWIfhpAXUsm0xjVU2 C9uUbHljQCmrNZIFUF3DD4Abtgvt+oefSXefibqEk+TaXDgguqxNp7XXbiIZQ0s+2a9F DCU9Q/wzKNGb+SQCXS5OGuuBdFwPp65NkSaUlBZ/JVvPbJrKxGGb6exOxDMMuxGegyRv ftn2sB/YOi4hzp0KoSJqp/qXlWA/I1apATz8Cj65qMoj6MIaJb2spB7oCiAs7G74rc9M mwe46WCIR5nLDQDAdhdikRKLqd5mBfWCU82JRQxq7zvDxCV1XDgH7uPfGMfuoIKw16Sp bIxA== X-Forwarded-Encrypted: i=1; AJvYcCUS0yW5GsBB+oG8D6ZjDetxc0UWZlgzm224dPwWvvEgC5MKntEPpLVaALz9geuSfZRYwkXT/jPOxR5wcI8/9Yqf28pFK9bajgLBR2k2 X-Gm-Message-State: AOJu0YzfkGETwkrYyBygXxyHg/8ttK4J6ZT49n9rwNJjyZ7bMVvJdcYn +JGeNdLJBUsl1LTj7v4xi3IY0GUm4xk03JOEgDf3+X1h3M6EMeEy1T7uyl43vQ4YrWAYZHWR/Iq dEiDbvHZsifkTTV63nxnZEQkGciPZ77QQMsAQ X-Received: by 2002:ac2:4254:0:b0:519:63c1:6f45 with SMTP id m20-20020ac24254000000b0051963c16f45mr2385942lfl.61.1714001031354; Wed, 24 Apr 2024 16:23:51 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240424125940.2410718-1-leitao@debian.org> In-Reply-To: <20240424125940.2410718-1-leitao@debian.org> From: Yosry Ahmed Date: Wed, 24 Apr 2024 16:23:12 -0700 Message-ID: Subject: Re: [PATCH] memcg: Fix data-race KCSAN bug in rstats To: Breno Leitao Cc: Johannes Weiner , Michal Hocko , Roman Gushchin , Shakeel Butt , Muchun Song , Andrew Morton , leit@meta.com, "open list:CONTROL GROUP - MEMORY RESOURCE CONTROLLER (MEMCG)" , "open list:CONTROL GROUP - MEMORY RESOURCE CONTROLLER (MEMCG)" , open list Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Wed, Apr 24, 2024 at 6:00=E2=80=AFAM Breno Leitao wr= ote: > > 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_rs= tat_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 (discriminat= or 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 Reviewed-by: Yosry Ahmed , and or posterity: Fixes: 9cee7e8ef3e3 ("mm: memcg: optimize parent iteration in memcg_rstat_updated()")