Received: by 2002:ab2:60d1:0:b0:1f7:5705:b850 with SMTP id i17csp787892lqm; Wed, 1 May 2024 16:19:25 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWaHgzS9yP/fW2vjyuxrvZhoNfj775MBDGB8VzvgkMSu5HBOdIyC1YV8BoLS2Yqbr2qlYQujbHGrRW+60lV45HcTaPKp+ZbdG/b/nMXug== X-Google-Smtp-Source: AGHT+IGYOvNneHItC9jzmSr3hm3+xAgdnThq7bJPRorQHoxO0w9t/8L3+iJFe0QjwRteTNYFf1nI X-Received: by 2002:a05:6214:d6c:b0:6a0:caba:7e2f with SMTP id 12-20020a0562140d6c00b006a0caba7e2fmr4621409qvs.20.1714605565070; Wed, 01 May 2024 16:19:25 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714605565; cv=pass; d=google.com; s=arc-20160816; b=Q7OjWaZcBDTjmq/C2x/1NHSd4gE/1Pm8tXmsIjTxMmpPQrx/uIN14mxQvvEsFmhB90 Jb559/kpEl+5+WhQ5cXy8vpT6cFU0D5cFKAtqhquYtWSkulPah8XiyqXzbObEpXtg2nP ZCBf6n8EA1PO7rOisQPd0J/c4O3GtbBGeNBa9bsAw2uc35SwFwC9bVvJqR7Ucoq28XNr yElWNAvNalYyC0mUxxj7RntTFKuXsoo4m7EychWDxilLD60RB1wT810wL65AvS9HzTw5 eETDH3q2oBCaQX4jRzsqdDXqv6L8txlgLPiVoC/TOMUP9C24OOs/W/rYDQfVochY1i+6 Uwog== 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:dkim-signature:date; bh=RHAt9ryWcPi6r2+Mi2K4KwgaN6ilqj66ygb1ZhC0eWM=; fh=kYPMmEuwsLIBuw26cgHEBqCWwBnQTvp6VysJ9LBJmbQ=; b=sSgZxxDAJQPvM/r+dJ3J2E8yVzV6glRr6a2ZS22f4m7TF1SJr06LvLf/62MYvD+4eA ftgLoBwPT31BybBHKHQfdmXTuS4YB953SMLnc/fpxArohd+zpWMr3r5sb6gsfNxtrMl9 E6/K21qKFluCyOYiHzZmfHpIAZig1Otp/lelulASrnnKX9YtVwEeQGdxYPKdvbjtQYC+ rttqomlelY08Z31qqKTKesUHSO/fSVHOnzYtz0H7hTWw1x8KDYVUPBLRB5C8KUyZN4XG wGNoq9rY22ClPdxG74RTTsnVB3PBsD/Fb/eoTIgMFYrlR7ut/2aGAE68v/2VG6uANr+k VavQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=sT0Eez54; arc=pass (i=1 spf=pass spfdomain=linux.dev dkim=pass dkdomain=linux.dev dmarc=pass fromdomain=linux.dev); spf=pass (google.com: domain of linux-kernel+bounces-165832-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-165832-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id fw6-20020a056214238600b006a09a08c89dsi16694495qvb.450.2024.05.01.16.19.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 May 2024 16:19:25 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-165832-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=sT0Eez54; arc=pass (i=1 spf=pass spfdomain=linux.dev dkim=pass dkdomain=linux.dev dmarc=pass fromdomain=linux.dev); spf=pass (google.com: domain of linux-kernel+bounces-165832-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-165832-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev 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 ny.mirrors.kernel.org (Postfix) with ESMTPS id C709C1C20F63 for ; Wed, 1 May 2024 23:19:24 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 56EFE17C6B; Wed, 1 May 2024 23:19:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="sT0Eez54" Received: from out-178.mta1.migadu.com (out-178.mta1.migadu.com [95.215.58.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 42356165FAA for ; Wed, 1 May 2024 23:19:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.178 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714605546; cv=none; b=dL1J/VPCJwU1R7KXQwCqfKxDna0RETVio59kFUZK82U5n2KGF/njJ+5mr3wD8RuQ1rP14/TUVLcXMPrk/hv/qP6hkse2b9pm8PjKttoUygNMpeui1hrq2RQmknTRtmyQ7FLMF+cJi9NVEyn9t7I5L1ba2f8kZ6CJeo4oGothoSo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714605546; c=relaxed/simple; bh=RVX6CWee9Et4secAL4eq9G6Zu+mOoI+QY67WAZDE8E0=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=N+WwJVdD+kWUdY4xeUBXYFwU9fX96L1AHO/2aOkcNKZ1nSzBHju8QCN9ZbwRgRRn09DiEfNwWxYTEgqizd1LOTz7bZ+B4m/LB7k/cZtM9ILNs5lg0PLkFOBv8jtUOrhQXlKV+HjlrdXcV53lqom14pNzDpm6E0Q/UTxURsEw1zY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=sT0Eez54; arc=none smtp.client-ip=95.215.58.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Date: Wed, 1 May 2024 16:18:57 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1714605542; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=RHAt9ryWcPi6r2+Mi2K4KwgaN6ilqj66ygb1ZhC0eWM=; b=sT0Eez54kn5LXckYJgvnME+XvNbRTMK9yBJj9F8KcSEPZEt8zfhE/VofpK8jFd0lHvGqUk alZaUuT1i0JMSjV7KhPdjAwZ+m5HI8mZ3x1oxjNe15zrz/psBqoekqvPdbZccKxgsOvtus u/pG3K25JeP1Xw5D55xtgd6NAzYHsng= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Roman Gushchin To: Breno Leitao Cc: Johannes Weiner , Michal Hocko , Shakeel Butt , Muchun Song , Andrew Morton , paulmck@kernel.org, "open list:CONTROL GROUP - MEMORY RESOURCE CONTROLLER (MEMCG)" , "open list:CONTROL GROUP - MEMORY RESOURCE CONTROLLER (MEMCG)" , open list Subject: Re: [PATCH] mm: memcg: use READ_ONCE()/WRITE_ONCE() to access stock->nr_pages Message-ID: References: <20240501095420.679208-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: <20240501095420.679208-1-leitao@debian.org> X-Migadu-Flow: FLOW_OUT On Wed, May 01, 2024 at 02:54:20AM -0700, Breno Leitao wrote: > A memcg pointer in the per-cpu stock can be accessed by drain_all_stock() > and consume_stock() in parallel, causing a potential race. > > KCSAN shows this data-race clearly in the splat below: > > BUG: KCSAN: data-race in drain_all_stock.part.0 / try_charge_memcg > > write to 0xffff88903f8b0788 of 4 bytes by task 35901 on cpu 2: > try_charge_memcg (mm/memcontrol.c:2323 mm/memcontrol.c:2746) > __mem_cgroup_charge (mm/memcontrol.c:7287 mm/memcontrol.c:7301) > do_anonymous_page (mm/memory.c:1054 mm/memory.c:4375 mm/memory.c:4433) > __handle_mm_fault (mm/memory.c:3878 mm/memory.c:5300 mm/memory.c:5441) > handle_mm_fault (mm/memory.c:5606) > do_user_addr_fault (arch/x86/mm/fault.c:1363) > exc_page_fault (./arch/x86/include/asm/irqflags.h:37 > ./arch/x86/include/asm/irqflags.h:72 > arch/x86/mm/fault.c:1513 > arch/x86/mm/fault.c:1563) > asm_exc_page_fault (./arch/x86/include/asm/idtentry.h:623) > > read to 0xffff88903f8b0788 of 4 bytes by task 287 on cpu 27: > drain_all_stock.part.0 (mm/memcontrol.c:2433) > mem_cgroup_css_offline (mm/memcontrol.c:5398 mm/memcontrol.c:5687) > css_killed_work_fn (kernel/cgroup/cgroup.c:5521 kernel/cgroup/cgroup.c:5794) > process_one_work (kernel/workqueue.c:3254) > worker_thread (kernel/workqueue.c:3329 kernel/workqueue.c:3416) > kthread (kernel/kthread.c:388) > ret_from_fork (arch/x86/kernel/process.c:147) > ret_from_fork_asm (arch/x86/entry/entry_64.S:257) > > value changed: 0x00000014 -> 0x00000013 > > This happens because drain_all_stock() is reading stock->nr_pages, while > consume_stock() might be updating the same address, causing a potential > data-race. Btw there is an actual data race, which is periodically causing a needless draining of a per-cpu stock. > > Make the shared addresses bulletproof regarding to reads and writes, > similarly to what stock->cached_objcg and stock->cached. > Annotate all accesses to stock->nr_pages with READ_ONCE()/WRITE_ONCE(). > > Signed-off-by: Breno Leitao Reviewed-by: Roman Gushchin