Received: by 2002:ab2:60d1:0:b0:1f7:5705:b850 with SMTP id i17csp725164lqm; Wed, 1 May 2024 13:48:24 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXwK4/ZOl/sBoFD3oUBCcbcnxaCp55/Rpa+ZLxJHn5qLqHr8wF1sv7/s3UcV13GHI+YlfygXW4Mae/EZncUntYKADDJMBmEPPslikSgsA== X-Google-Smtp-Source: AGHT+IHUO4WZHsU2yTq2udNyogce4C9DvGMr1ZF7nq4I5PwQvpXkPA2YCLl+DA1nYmq7B3NOPvEy X-Received: by 2002:a17:906:248b:b0:a58:eb0d:f2a6 with SMTP id e11-20020a170906248b00b00a58eb0df2a6mr491062ejb.31.1714596504241; Wed, 01 May 2024 13:48:24 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714596504; cv=pass; d=google.com; s=arc-20160816; b=VHfR9kJ3JYvr1VMFwnRzMfd923UHh+3J6LqNNlrL/bTZe/G7dkTJOJCcJlwGCBqUrZ H5OY04v/Mi6it0r3cI6GKszmMKloiZBXQmxrV9BV7Yf53SjtYd9egpxBAP2HZsj7Ovbm LpR+ypI2Kadry91SFEjuDTgkCwoioIRsk6UimA9yOlre/VmnjLIJHTq27PWTDrD/KPPa bCma38MngdAPSy72kxM+Z32RKRXQMN4mo57GSIJC81bToOjB0Cvz1ux1cJKejBIWMCRp m63lHPJVdGqHwKVsh3AQq/rS/861BugNPvxu+A2iymAo2KqE4RvkOWEe+n6L0uTfyXWw dXwQ== 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=XiLp0x8XfXFTs7cYbJkOcVg/j2s1qKapf0EMhMbr3E8=; fh=aX4+BAv9cyaGqayWJX7I/dHRhhPNKl6jZGtMNg9tNkM=; b=kt5g9jp1HAzI5pzvzc0ce+wQjEYawoLyIPVNqDbkauhuc1lqH7ZRYHQ4RMqCUce6DJ LfGm1sPqeFYd9oVK247wACM8ilP5pzRqWnSvj/U4Uv27vbjs4HrEEC2fXHScXnkAwtQy 7hLD6zB6ASXUw4ikonQr/ltLjHlwRwm1EwvIIuQXJpKpdwfh2FqIMOHxq1Kd7kF4o0eI CuQmc6QfAAwqH1Xxc15/17ND7S88s2DMo2t/KR1HCeG99lqu+XYcuZLoJKn6S6pejHS6 /XEvEBZROg/KKKMvrjmAdgjxsvjXAf3fi4c+zuAjYOHG8Ase5/ATpJH4SU43bYJLJZ0u CM7w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=ti7JLpz3; 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-165763-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-165763-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id f9-20020a170906560900b00a58e9778bb9si5246084ejq.386.2024.05.01.13.48.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 May 2024 13:48:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-165763-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; dkim=pass header.i=@linux.dev header.s=key1 header.b=ti7JLpz3; 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-165763-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-165763-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 am.mirrors.kernel.org (Postfix) with ESMTPS id F3D7C1F22C75 for ; Wed, 1 May 2024 20:48:23 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2DB06168AE7; Wed, 1 May 2024 20:47:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="ti7JLpz3" Received: from out-189.mta0.migadu.com (out-189.mta0.migadu.com [91.218.175.189]) (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 6831C1649A8 for ; Wed, 1 May 2024 20:47:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.189 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714596472; cv=none; b=RCwTbfxP/B7KiutCdNQH5UGoz1ThKnJnMKuVNDLbAMhbXdyV7RgCKxH7Qp6A7XKG3L+axrKjPlFFXiqs26EDPRCTwa+5YwMETcSDxQDyAPLXXsCsoqrqA7UWE3jUv2U/ufFesSmv5mvft+OD7qvSGnS7W3uaYNYUjwYDaHw6cN8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714596472; c=relaxed/simple; bh=is7OKVQ4oHTUSKCVgI8woUplkhLb0ChtKjfZ52WSoxI=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=kouGfvMxSlrK0MccBvYLJZ6KEaJ+eQsqMZ5Z9rnkzlh5ubZzwoo2tzrvI44q+ryiUpj0W/W5k60aO9V/E0BIePFQTfP/wisFAxyD82+7HqOzLLyG5Q0xGv3GK6cnZHq+JPMNrDLEnIRDgTgOmwZKdIEXmqK2r5lhpqq/4wcnzrY= 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=ti7JLpz3; arc=none smtp.client-ip=91.218.175.189 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 13:47:43 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1714596468; 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=XiLp0x8XfXFTs7cYbJkOcVg/j2s1qKapf0EMhMbr3E8=; b=ti7JLpz3C3Yb5ms1o6GyQawPVNzUf4kx27q/4+a0kn/c3JMW4r42eZJcujOqrrj4UIo5pq M0WChPcWDgdYd1OLBlfsmKqKC8970KRUNpxzQK1dTa50MrQCnhK8B68HnK8zrOmVfTmJ2J vcMTZH4mZqsM83qf39/5NWAxs22+7Hg= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Shakeel Butt To: Breno Leitao Cc: Johannes Weiner , Michal Hocko , Roman Gushchin , 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: <7hm5jb6sxg3hd2qtp3qpei6mskdva6kseik6jlna4dlpliangc@m4xs3i2cgg34> 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. > > 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 Acked-by: Shakeel Butt