Received: by 2002:a05:7412:3784:b0:e2:908c:2ebd with SMTP id jk4csp1601268rdb; Mon, 2 Oct 2023 15:04:44 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH1Jc9+Toh6wG0MWYu7Xjr7y000U6GD//WB+PB1Cbi+S5ghRGLv6dJasLbJjfp9Q6kLD1e4 X-Received: by 2002:a05:6a00:2315:b0:692:6d3f:483f with SMTP id h21-20020a056a00231500b006926d3f483fmr11356574pfh.27.1696284284456; Mon, 02 Oct 2023 15:04:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696284284; cv=none; d=google.com; s=arc-20160816; b=C3Oq5wi9WQAeBkWfARCH2oUsxwsph2VQmxjx6Sm2ZgkYet7LFnqyIYiAxBuvbGRYrA VXQRBQSFlvLNW7RfnZ/P2PCXYHaCSwqTDNsjzqkG2C9YRC5gomYRZYVkvYWSIdUVDS/f SqnVPf3jfY1xF3zMOWibGwRSd8cHZDPRquXMt6S6CdZ9APboEOaQNiL6quATour0GxXr vpop5Ycwc4n5Av6xDLglAJTpustoVDGXBRFROi70++6/4AQ+fUPV0rZaO/r4Mj0H+rvL iyZb7Aj0w4Woo5mRu770MyeH3lu3UvXQH/MdxI79gSPMFBx4WocE+5CTze/Tdkc3fumK 57mQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:dkim-signature:date; bh=o3rGOO1zzAI6sGQiZBiVHTxVUTx8U4hPLeaBLW4Dbqo=; fh=svmBatzJnAYhC5L/tJm8GrBnKyUfGrdZQ94p9HELi0M=; b=cV7vLCoLtV1VFr2ktwxZuq7bTkjankifY+8EvDIvMrS7tPHz++X5WMw5B2zchyAaxh R5OQGnBtqp/LNr9jsASTLsmGC6X//Mx5Snuh/YI7JD6ZLRRXaF1Mokhf1TlL5ule3k0H 1/TS0g3BdZ9GIRNC64VpEv5e+3BAkBmAH5DvaAQI9tDxXpGrtbZtEkDVckzyPILQ3ioN lakzK4DcYMxW9VmYater+TPU87eI81soz918vS7Y3ux7gEA1ZNBzlM8pTUKM1LHOKD5M Z53B2O814aA5IqsuNFggZqil5k9LmYiLltoCwtKm9WeC5HbcsZbv2SD9LnPL1bUr+BpU 9O6Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=aMU8C6+g; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id bv124-20020a632e82000000b0056da0ae25cfsi26973872pgb.196.2023.10.02.15.04.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Oct 2023 15:04:44 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=aMU8C6+g; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 4815D80331AF; Mon, 2 Oct 2023 15:04:42 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234330AbjJBWEB (ORCPT + 99 others); Mon, 2 Oct 2023 18:04:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33020 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229538AbjJBWEB (ORCPT ); Mon, 2 Oct 2023 18:04:01 -0400 Received: from out-209.mta1.migadu.com (out-209.mta1.migadu.com [IPv6:2001:41d0:203:375::d1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1F62AD3 for ; Mon, 2 Oct 2023 15:03:57 -0700 (PDT) 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-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Mon, 02 Oct 2023 15:04:42 -0700 (PDT) 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!