From mboxrd@z Thu Jan 1 00:00:00 1970 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 781BAC3ABC9 for ; Tue, 13 May 2025 18:09:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C3DCB6B0093; Tue, 13 May 2025 14:09:42 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BEF0B6B00BB; Tue, 13 May 2025 14:09:42 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AB6696B00BC; Tue, 13 May 2025 14:09:42 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 89BA96B0093 for ; Tue, 13 May 2025 14:09:42 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 42C131A139C for ; Tue, 13 May 2025 18:09:44 +0000 (UTC) X-FDA: 83438672688.09.A59385E Received: from out-186.mta1.migadu.com (out-186.mta1.migadu.com [95.215.58.186]) by imf14.hostedemail.com (Postfix) with ESMTP id 48F5410000E for ; Tue, 13 May 2025 18:09:41 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=NK5qxc+N; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf14.hostedemail.com: domain of shakeel.butt@linux.dev designates 95.215.58.186 as permitted sender) smtp.mailfrom=shakeel.butt@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1747159782; a=rsa-sha256; cv=none; b=lu2jPsY2ynBqVEScAeNHhgtBURaOoArKAzr9VYIJddGxOkG00TkGJVzH45wS1+uP/VC6xo WOyW9Mx+WIdadkeeaoeqAwU1adLQhJ3GTMypblZBxtG1U8GtuC/7mZCr7J7F7fmdDOcsSN C9gecHsiM9gWnH0n/aaNw257ZaxG4/c= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=NK5qxc+N; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf14.hostedemail.com: domain of shakeel.butt@linux.dev designates 95.215.58.186 as permitted sender) smtp.mailfrom=shakeel.butt@linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1747159782; 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-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Vv+FYo0F71tS4hoW14bBVNzWUW6bVJMvqDmpJmWU8uw=; b=Y/J/4qF29ZuGRhZEYKbNXpR0XlETAdbkgaESEPCQ/1lx1Fel/sAlrWWFjkJqxog5Zpbw0f S4UfSHWAsMUDmPdvVPn+5Baguur/PXH+XFS4XZDjqULXIPHCNeucCSYhnVzK+laQVFJP2s gIbvkr7BnzR/h+zdfzUb9PCLNyVGWfE= Date: Tue, 13 May 2025 11:09:30 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1747159777; 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=Vv+FYo0F71tS4hoW14bBVNzWUW6bVJMvqDmpJmWU8uw=; b=NK5qxc+N9KbpV7A9sskYymD/Y/qFYeyIOYkIqp8eVhxVMCF9CP++QsCUmcFROxS9kCjx4y peu0FilNvu3/3rvrw2CaQ/hX/vq5L3hC191u+jSXPgAFJiYaz0WCeS/mzfI6yHxRTd59mS idEKvwVvwgQuqhmEw5NNq35UhpGTmYk= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Shakeel Butt To: Vlastimil Babka Cc: Andrew Morton , Johannes Weiner , Michal Hocko , Roman Gushchin , Muchun Song , Alexei Starovoitov , Sebastian Andrzej Siewior , Harry Yoo , Yosry Ahmed , bpf@vger.kernel.org, linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, Meta kernel team , Mathieu Desnoyers Subject: Re: [RFC PATCH 1/7] memcg: memcg_rstat_updated re-entrant safe against irqs Message-ID: References: <20250513031316.2147548-1-shakeel.butt@linux.dev> <20250513031316.2147548-2-shakeel.butt@linux.dev> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Migadu-Flow: FLOW_OUT X-Stat-Signature: qidaj953yhxoksackuwbedbsbk5nw9ph X-Rspam-User: X-Rspamd-Queue-Id: 48F5410000E X-Rspamd-Server: rspam06 X-HE-Tag: 1747159781-583420 X-HE-Meta: U2FsdGVkX192mLiKDUrzuDRQuhdpY3qqXfPwKCqd2zYMNpZc6r5Bmv+612rb5gVlK3gEL5Q2sysHrH47G1jFM3RG9GeDdZoo5Wi9VPRNcoU1NZTqBP0/HStKKMr40wFS85x3LKN7UUjaFDyhkSi6Zvm5pBuYfqpKGWX9tAvorKHJcSi0GZQwA5knxCEd80rUfh3xfc4CATYuUgPuIdBXkk8dph3Jhm5kddKC5snFh3/3Poimfrkx3N54+XP36Z3frl5TLs733kTe7uinlfUlX9DyeADzUdg51QmWZO586Ce6GyY5HL4bLhUcwQnRKsaOLNpVCVo/cTh8XMxWehHvzHmobPUGFhxdMqs3XFG8jwVVobAKz3lpleSuVEbJRACERIjdg2+dYrQSmBxvLF+8m+b4D+SW1v7clFtPJnIjFSAPNap1KexhGuqYaMbkK+byUMz7ShVfWeO+ME7Ol5ihcmBTIC54x8rL/7T1WGzkKmHJtO4RDwlpI0W4AyVDrhGtn/m1oOuNdUnvnKFlLuWt6MsiWZQJSXuTWeETSaAvTFKAZjPLEP2J2E4YKfpura/3/+DEbzVc97Afjc1O283EdD6yvofIFa79StWeUtio8lOM+be5Ua/MhifWAIGTDISwyL+JSl5/LQ+8oKjOjjF7E44ern7dTFUtTgMCBLm94n2XoBGyCRVBZ5OP8eVHBwuY+UIqUzQJsJu94ZzsvIcUBBA/xwcZk+Xe0At5OF9PflzzjX6vmWJuTIk7n5MkUHireCnpGGbZjTWeV5KX5oXv+d1PjRUnPQz/vFqBi3V2ntjIYd1NSUCZCmPYFCpzQopSO3CsoIHfWdcIakzdNvzvbrrLGQo0YjSWx29zbsn9WN88NSnzKI1bT8S+XlwfFgLKqWMPQSwmU/J4IwZu8zPR1NF6mpzYsD7h5GxV3VlGjvVqH/5kGNZtVcDXeanZpp/OghYgFGuZEzaJTvse+Qc kOOPcLI4 423Htf2UKntwaHfIVCcLLlQg5MVDEhEtpSstwkiyeb67TSL8LQWD8zgtkmgCWJmfhceOzldRrpzZyMVmKBy6B5tGefRkfBOqkXLIjbuwaccrfjY+Akhw6J4g867Gq6yAnr/lyPn/4UX4oxK19qZhV9rM1KmJvOV3925O7zadHD8Nns2Fuigb4oCQMbg9KX6yVPXtAgKdJgmY3XHIx16cA8Ubw89gvHwedBWgnaGsrqZrDyxUn4YA+FI5Hbz+uEUQmFh1Wz9mlrTphwKQvuGhAiQ0dMA== 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: On Tue, May 13, 2025 at 12:22:28PM +0200, Vlastimil Babka wrote: > On 5/13/25 05:13, Shakeel Butt wrote: > > The function memcg_rstat_updated() is used to track the memcg stats > > updates for optimizing the flushes. At the moment, it is not re-entrant > > safe and the callers disabled irqs before calling. However to achieve > > the goal of updating memcg stats without irqs, memcg_rstat_updated() > > needs to be re-entrant safe against irqs. > > > > This patch makes memcg_rstat_updated() re-entrant safe against irqs. > > However it is using atomic_* ops which on x86, adds lock prefix to the > > instructions. Since this is per-cpu data, the this_cpu_* ops are > > preferred. However the percpu pointer is stored in struct mem_cgroup and > > doing the upward traversal through struct mem_cgroup may cause two cache > > misses as compared to traversing through struct memcg_vmstats_percpu > > pointer. > > > > NOTE: explore if there is atomic_* ops alternative without lock prefix. > > local_t might be what you want here > https://docs.kernel.org/core-api/local_ops.html > > Or maybe just add __percpu to parent like this? > > struct memcg_vmstats_percpu { > ... > struct memcg_vmstats_percpu __percpu *parent; > ... > } > > Yes, it means on each cpu's struct memcg_vmstats_percpu instance there will > be actually the same value stored (the percpu offset) instead of the > cpu-specific parent pointer, which might seem wasteful. But AFAIK this_cpu_* > is optimized enough thanks to the segment register usage, that it doesn't > matter? It shouldn't cause any extra cache miss you worry about, IIUC? > > With that I think you could refactor that code to use e.g. > this_cpu_add_return() and this_cpu_xchg() on the stats_updates and obtain > the parent "pointer" in a way that's also compatible with these operations. > Thanks, I will try both of these and see which one looks better. > That is unless we want also nmi safety, then we're back to the issue of the > previous series... Nah just irq safety for now and thanks a lot of quick feedback and review.