Received: by 2002:a89:48b:0:b0:1f5:f2ab:c469 with SMTP id a11csp424243lqd; Wed, 24 Apr 2024 06:40:57 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUrZ7Vj2xebYSvPRBLH19zPPOh35yBD2YCgsA3E9DGJYR46sSyACpkchv6VIVQxH6BR/vt9paWps/4XPcKOY7oKY6d8IRizmMcF+OBt9A== X-Google-Smtp-Source: AGHT+IE4zm82JlR5oolzi5riPJ/JP4RG5AsxMD0R3MSi0ZbpE8U15CiFLaI7mrqYEQvp/0emvT50 X-Received: by 2002:a05:6e02:1a0b:b0:36b:80d:b930 with SMTP id s11-20020a056e021a0b00b0036b080db930mr3048791ild.26.1713966056807; Wed, 24 Apr 2024 06:40:56 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713966056; cv=pass; d=google.com; s=arc-20160816; b=Nt+Z0anR9+tLkZ99A3fOML5Hioaf5FRO3SjtjEA+gHoe4MCA7/bHz9Hi2a+F5tV6QN tJg4+hFoEhygOv0FDMMtStK2k9cVTe0ho88KeCIgllLt5dXreEzW5UyBsyPVwgclZSUb EO1ZsqAy0udxBxg7ZpPVtym0Hgl7e2Yrj7jamnKDNLFhF3BlDvPhXCB7K953L5XKK4ho WRiYZfR3tfSj9qgqxeLZ/50Fr/102UXCs8z5dPJmBPQCkz7HSSAIyT/lcEhc7/bGYHo0 HCVLp2kUUV6ZT1CAgArz4EWEuR/qUsSd7wTsFEKsotULmYh3wojzFoF6jah9C9MrepC+ VdvA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=xzkq09b2+VRg5r2hzH+VShVRDqHUAWf8E5efl7fvk7w=; fh=FMpt/2qTqh7wX6v7km557ruehD2XpjjP+kSgd3X+kk0=; b=LyEnDflGolQUZLlqMF5fRFqX/zEaNprAnBgYKIHz0d2V10typ1nDLkO5lQwpb/28Cg 1dyd8RUiRtQi0KwIhM/Sb9qRmzNtIRthxVC4ks25wQPZR/XaHBkyW3+i8LCV/inyY3uX L7XPG1u9RkMmRHhE2hZ6SiNTlmHsZ78FkSLnechqC+VlPz8kM3dIz5W6q/VyanybomCy FgGwaaAICycTIK5cqyFiJJVguCSKVf9GJYebSzI8KgaQaURTKBe7+Oo73P0ePw1CAUj+ pKSzhJFNsBxnRsyOZNw3ee5naXuRwmeTNKW9eJJ7qbsDtGBCGs0omGHociEL2Ix4fYnR wbgQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@cmpxchg-org.20230601.gappssmtp.com header.s=20230601 header.b=EULqXNl7; arc=pass (i=1 spf=pass spfdomain=cmpxchg.org dkim=pass dkdomain=cmpxchg-org.20230601.gappssmtp.com dmarc=pass fromdomain=cmpxchg.org); spf=pass (google.com: domain of linux-kernel+bounces-156944-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-156944-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=cmpxchg.org Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id v14-20020a63b64e000000b005f07f350e33si11147922pgt.139.2024.04.24.06.40.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Apr 2024 06:40:56 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-156944-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@cmpxchg-org.20230601.gappssmtp.com header.s=20230601 header.b=EULqXNl7; arc=pass (i=1 spf=pass spfdomain=cmpxchg.org dkim=pass dkdomain=cmpxchg-org.20230601.gappssmtp.com dmarc=pass fromdomain=cmpxchg.org); spf=pass (google.com: domain of linux-kernel+bounces-156944-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-156944-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=cmpxchg.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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 6E0B0B22F05 for ; Wed, 24 Apr 2024 13:19:33 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id EFA8415B546; Wed, 24 Apr 2024 13:19:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=cmpxchg-org.20230601.gappssmtp.com header.i=@cmpxchg-org.20230601.gappssmtp.com header.b="EULqXNl7" Received: from mail-qk1-f175.google.com (mail-qk1-f175.google.com [209.85.222.175]) (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 B1A4F1422AF for ; Wed, 24 Apr 2024 13:19:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.175 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713964763; cv=none; b=t6ScPXlII5CXVqOkHjXs5cqezxSuG9Tc1B/vQd7/Yq+rrlMoHzSjvgpXKoBBVXwW+DLzLb9uBJ8LFuUMbpHbodcITB912BAtsx2ZNfFond3eH4qOOy6nP8uqMRD8SqQ5ok0eRjdLhVZmUKZgEUH0SgXqQ+0IxzhVm5dU2cdqrBY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713964763; c=relaxed/simple; bh=eWDgcUPoqiHy04toIRDjF+zURyREPsGTrP5YsYJgcwg=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=tdRSw1+ZxwtGhM+kwnaC/BwHQoybFHaixd2gomiVbvScfVvkrHY9+Mi1mdz4HMZAbFUEuMhP185g0w8jAuLVU1xtntSiJ4iwqCMz6k7uRxlsjjyaBchJ9re+PzBjgzjnUPuJZwg072N2diC3woibeiVtKJb4OSUbLTmLd0KbWbY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=cmpxchg.org; spf=pass smtp.mailfrom=cmpxchg.org; dkim=pass (2048-bit key) header.d=cmpxchg-org.20230601.gappssmtp.com header.i=@cmpxchg-org.20230601.gappssmtp.com header.b=EULqXNl7; arc=none smtp.client-ip=209.85.222.175 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=cmpxchg.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=cmpxchg.org Received: by mail-qk1-f175.google.com with SMTP id af79cd13be357-78edc3e80e6so467041085a.2 for ; Wed, 24 Apr 2024 06:19:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cmpxchg-org.20230601.gappssmtp.com; s=20230601; t=1713964759; x=1714569559; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=xzkq09b2+VRg5r2hzH+VShVRDqHUAWf8E5efl7fvk7w=; b=EULqXNl7siYpv3gW8S3krA4vhPOfm6iSjsVn3vOU+BIWVDq2YIIHdQcdAijDBs48rV Oj1HY5ouU80qtG7hYn6CXBYcBce8TvjnFPTFHHCGi7FrsoUzbhVmtZURkz5ezDlL2W1G frROq6TEdTZlLmqQdww5EozJTzqe7tnOhK4m+pgSLNmCtisdpwFg5Ty0IWFO8/paqy1N I9vMPdyTdXvlpqXpyEq3QvZd1i47A4WsbSQ36mw6nkjNWl/AQW47I+dLlUuBc/jYBgnx U42sRvUdm1/+otk52Od89WUsInKo+bYuigMiSLIkEw1dU09l5oVWWQdo95VE24Z38Z/S xX2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713964759; x=1714569559; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=xzkq09b2+VRg5r2hzH+VShVRDqHUAWf8E5efl7fvk7w=; b=mBny6+QjiqdzC28yRdyOEJu46Je2x3R0ACiwcIZw+zFTgq/ODS5CU0APuJHyAkW7sH XjTk6pZfUFLSDhKc2FVeHGrPSsDl3DOrOZvvhywIKKEFKjni0lCv3p3VQ2lo38VeEuRY 48c+4DQQ+/Xs+4Ga2fVA2qMwkHxqXG9EQaQFXLDfKyNqYcH5VhdIOzGDW+rjbEoHtjIk Kx8tnaUnyGBSsF6LQN+HhjBHJVX0BpIu7V+DPkGE66FFfnVuUf1005LJyvQknMuaNKGo zg4U0UNVP6pJB6a4cCmdudM+eH4COiTOxUXNIJk9TVkw2SB6E0J+CwWmapI1RH28FtZu mqig== X-Forwarded-Encrypted: i=1; AJvYcCVqgvE6dDcVUOkrYwnC1saXpbZ0WNg+iAMSsZx7YlLh1xaq25VlcQ1f5KyaW724j8WXcH3yC5LFi515xfplaLFsEOeP9JxJGSi11LCW X-Gm-Message-State: AOJu0Ywr6yb774ImASpfNba/LjE3hzS8NvRlAdLSNbfEtiqIXTmFF9Hv i46DQbuRFbgAcyKHXM+cCEk65fbefemWNx3GyHjm8EL0cmsstGvqR8suh8bItgk= X-Received: by 2002:a05:620a:8222:b0:790:652a:f419 with SMTP id ow34-20020a05620a822200b00790652af419mr2528675qkn.41.1713964759520; Wed, 24 Apr 2024 06:19:19 -0700 (PDT) Received: from localhost ([2620:10d:c091:400::5:9cfb]) by smtp.gmail.com with ESMTPSA id u24-20020a05620a023800b0078f12e42f0csm5747909qkm.20.2024.04.24.06.19.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Apr 2024 06:19:19 -0700 (PDT) Date: Wed, 24 Apr 2024 09:19:18 -0400 From: Johannes Weiner To: Breno Leitao Cc: 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 Subject: Re: [PATCH] memcg: Fix data-race KCSAN bug in rstats Message-ID: <20240424131918.GC318022@cmpxchg.org> References: <20240424125940.2410718-1-leitao@debian.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240424125940.2410718-1-leitao@debian.org> On Wed, Apr 24, 2024 at 05:59:39AM -0700, Breno Leitao wrote: > 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