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 E2DF1E776CE for ; Mon, 2 Oct 2023 22:04:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5AB808D0018; Mon, 2 Oct 2023 18:04:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 55B998D0003; Mon, 2 Oct 2023 18:04:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4234A8D0018; Mon, 2 Oct 2023 18:04:02 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 349128D0003 for ; Mon, 2 Oct 2023 18:04:02 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 031F212011B for ; Mon, 2 Oct 2023 22:04:01 +0000 (UTC) X-FDA: 81301899924.01.7CA74E4 Received: from out-200.mta1.migadu.com (out-200.mta1.migadu.com [95.215.58.200]) by imf12.hostedemail.com (Postfix) with ESMTP id 2250840024 for ; Mon, 2 Oct 2023 22:03:57 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=aMU8C6+g; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf12.hostedemail.com: domain of roman.gushchin@linux.dev designates 95.215.58.200 as permitted sender) smtp.mailfrom=roman.gushchin@linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1696284238; 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=o3rGOO1zzAI6sGQiZBiVHTxVUTx8U4hPLeaBLW4Dbqo=; b=b20de5hYyQKLbYSjdCxrjM3w2ilUGWF2H9wB3N9Uzr2caXZ1ojR6fnH7r2tUXhblUmx1e4 BYPv9WBFUAUSa8OkoK6jCm0QNz3ja4N6X1KvmuhaFuZOQDe/4b/4siR/NBOhPRwuYNaHlL MQ7YfHKCsxWrsfVN5ZXze0WfcfchWFs= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=aMU8C6+g; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf12.hostedemail.com: domain of roman.gushchin@linux.dev designates 95.215.58.200 as permitted sender) smtp.mailfrom=roman.gushchin@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1696284238; a=rsa-sha256; cv=none; b=F3cE1RXMIqlcOUP5tMrCB1/xeoonRkSe2ziu3h33wFrUYh4enu3u5uwqKZAHLBB0y37cX3 DazGsmy21BpwHAukeVnyPC35Tme0GdLWs3UHxrMLe5N06QHWWtYLF9h2KxQyhWjqrCahTf 45zEfLDo+RhmYdSzYmtkTTfrdn1Fymg= Date: Mon, 2 Oct 2023 15:03:48 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1696284234; 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=o3rGOO1zzAI6sGQiZBiVHTxVUTx8U4hPLeaBLW4Dbqo=; b=aMU8C6+gDYX9Rh2K28PLdF/LkSHtF/2ivJP7BkQWxGfsHbFh8sB+WnyZIJH8mjr5lSASnq 2C83mJ6EBVkBQ3aJZeCXqktu2U86y9f3ij7rS5q8KUT5HSqgeObIkkB9411jnq6KVMeunM qQMKuH2QKt/oJGXu3jYdhpz+uDzDQpo= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Roman Gushchin To: Johannes Weiner Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, Michal Hocko , Shakeel Butt , Muchun Song , Dennis Zhou , Andrew Morton Subject: Re: [PATCH rfc 2/5] mm: kmem: add direct objcg pointer to task_struct Message-ID: References: <20230927150832.335132-1-roman.gushchin@linux.dev> <20230927150832.335132-3-roman.gushchin@linux.dev> <20231002201254.GA8435@cmpxchg.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20231002201254.GA8435@cmpxchg.org> X-Migadu-Flow: FLOW_OUT X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 2250840024 X-Stat-Signature: csb398tj5gu1ftxy8rn8gtdd9rjxyxd4 X-HE-Tag: 1696284237-94387 X-HE-Meta: U2FsdGVkX1/xGPNDWUj6Ytk9A/j+FXBYSFA1EkBNK/fUH7iZabGiIOKIiiKJpAIT4ptJ5dyGz8YAsW4GGwJJQYezwuNnKoBXShw1gWq5epClcG9IVnVhOCxmZ1BRDHmtHp+gG1CWFt8psD+ZuyL+9q2oLBw3YTntleU1230xy4VZPyAi3ar/74B40qM5yZjlyPCdDc9hPIBaWpEB6bzGV9BZc9mdJoGDKHshWmoLAsc1WNwY3mYO3K0kq9kdDPdyVZdXAjnwOxob901VFSeD0YAZL/E4rvm/OZ+NH2StA6mTjle3VN6bg+jxi50c6lgX+dGCylCjgljqh+rxvj0Pum+S1vVB/N5u6IeXLTFEnuGbhqbDcLZxi1tZ/gBwDE/PQ2Ek3H9pRo8cn46N8/Vu7Pe00l8n8y8WkXzBqGMp5iSO2l8uSjwkdl/T+5k+TKSQl/Uco2EuJVUviso/LyVxPH27OXWdoYDkLhbibeBbcKyFiOmDmsLXSgBe6eF5SZ0tevgEVunSiE3bA2Oiwwd1jdHcGQngzpm3FeRMRRPV/Olqit0dccuDHjcwJv5U21iZQoWEgsOmOBGBLk3QGCQXYR29gbianDcrkpBy7s9Y0Q0HftKxImnStpcfK278IcgzsVjUASbYXamT0By2e/Zty/OyDR6wP1UKioaRuaG9lX76MehUHzksKC65OXLDvXuh3YPj7VC7PqLxy6YNUAKx7uvRSGpf8FZMDgifDEQJi3Gv1jb/Bx9Yb9UO0BJm0mthBGowMGtlxT3ckI7Kf9LRLBB74vhKrvkOICGaYbZstpQDIx6TPMndxEc2rDpytNx6OH7h+TxhEhrYsotHzdOnXM+pT5noOfjio8CKzP5su+sRAdlJ7y8IeHr3qs8wxaVRGPf+nUzh0hV+BFxig6s8VmAR6KysCOb4tLM6U4JgQybOQ08fcoykfY/4CZUjq1RRj6z1f5Cf9a8GlWDwihQ PmyqEtJi xhI9DhGFAFzLUJhfqjo3wHwPu5L1JFC66RiU05enP7unKlwCmsSWfRLeK1I+9byAfXAHgr0oL3QtArMhO3OaHJiEzBgmGeTa2NpcVhqvo2j5CvMggWTkPfAQocg== 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: On Mon, Oct 02, 2023 at 04:12:54PM -0400, Johannes Weiner wrote: > On Wed, Sep 27, 2023 at 08:08:29AM -0700, Roman Gushchin wrote: > > @@ -3001,6 +3001,47 @@ static struct obj_cgroup *__get_obj_cgroup_from_memcg(struct mem_cgroup *memcg) > > return objcg; > > } > > > > +static DEFINE_SPINLOCK(current_objcg_lock); > > + > > +static struct obj_cgroup *current_objcg_update(struct obj_cgroup *old) > > +{ > > + struct mem_cgroup *memcg; > > + struct obj_cgroup *objcg; > > + unsigned long flags; > > + > > + old = current_objcg_clear_update_flag(old); > > + if (old) > > + obj_cgroup_put(old); > > + > > + spin_lock_irqsave(¤t_objcg_lock, flags); > > + rcu_read_lock(); > > + memcg = mem_cgroup_from_task(current); > > + for (; memcg != root_mem_cgroup; memcg = parent_mem_cgroup(memcg)) { > > + objcg = rcu_dereference(memcg->objcg); > > + if (objcg && obj_cgroup_tryget(objcg)) > > + break; > > + objcg = NULL; > > + } > > + rcu_read_unlock(); > > Can this tryget() actually fail when this is called on the current > task during fork() and attach()? A cgroup cannot be offlined while > there is a task in it. Highly theoretically it can if it races against a migration of the current task to another memcg and the previous memcg is getting offlined. I actually might make sense to apply the same approach for memcgs as well (saving a lazily-updating memcg pointer on task_struct). Then it will be possible to ditch this "for" loop. But I need some time to master the code and run benchmarks. Idk if it will make enough difference to justify the change. Btw, this is the rfc version, while there is a newer v1 version, which Andrew already picked for mm-unstable. Both of your comments still apply, just fyi. > > > @@ -6345,6 +6393,22 @@ static void mem_cgroup_move_task(void) > > mem_cgroup_clear_mc(); > > } > > } > > + > > +#ifdef CONFIG_MEMCG_KMEM > > +static void mem_cgroup_fork(struct task_struct *task) > > +{ > > + task->objcg = (struct obj_cgroup *)0x1; > > dup_task_struct() will copy this pointer from the old task. Would it > be possible to bump the refcount here instead? That would save quite a > bit of work during fork(). Yeah, it should be possible. It won't save a lot, but I agree it makes sense. I'll take a look and will prepare a separate patch for this. Thank you!