Received: by 2002:ab2:710b:0:b0:1ef:a325:1205 with SMTP id z11csp1267630lql; Tue, 12 Mar 2024 11:53:09 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXFxUQ8BXE+AJIYHBRa+79jF4IKD3m+ZQj9w5dUS4uzJNFSVVh1JfQzTwL2z1i6dqm5JOp5Vb+vcEq+gt80nZEBbJ23FtdWoLsFicKQ2g== X-Google-Smtp-Source: AGHT+IGsQzBqIwUss3GaCBs/4rxcZEWeo/q7y0s0BhkoYCOWgJAtPBwohLSY/UXE2upG7cv6j8SL X-Received: by 2002:a25:b227:0:b0:dc7:45d3:ffd0 with SMTP id i39-20020a25b227000000b00dc745d3ffd0mr369347ybj.1.1710269589453; Tue, 12 Mar 2024 11:53:09 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710269589; cv=pass; d=google.com; s=arc-20160816; b=KKnF0KnNAnA0KP+/tDjXq1uM/tQ1p70Fg2XWbjRMz7wGsOmfhv/weXuPZXXhZJCLvc aYV5aIvAMQ2Xni24PpenjJML8KndirzyekBEy6GSvccfOoxfY7yLfnqQCLw1tDkBbPgJ ADzB5w7hKJrGquQtK6kUkZ+ca+v2iXGmx4FvXy4xMTqLy33GziuMEG5rsK6agZjCZjAs CckhnK6UR7aN8riEDtbS0YvMix/fe/Cuc99aCHJ3cHYs2jeep6FwSdaU+8AP1Z0ayKiO 89L9RcwcjOTaZUWDtPtjbb7x/TjkA/Y5oSsAxAhIkpVBTaTGndyqRhB8cYYty3T3ulKh oY8g== 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=cB9p4AvFVqG7BzTvwz43ZlsmGe2aR9Z38/ULirCZ1Xs=; fh=t/Fx4bd8LLi3HWU2sF5dpgPrJabsC3sJzE47VeTuJy0=; b=U/AdE0o504Bx19yL8a+hx+t+UfazcNx9fOUPYk6xqUG2JTrXmxTneLdwaP1nScloWJ ZlgSyA124CDoNzXqGMub2mn9PZu+BI0YPj83TNEDbsZU7f+CoXthjn7fr0FOtMOQjfVN h8xuXyJPhny/OU0REX6a71qE8vyuSGyxPH2mcVzKXOtcgMW8g10wlcX1I7zXYm+wQhzt EcSSp4tY5K98QRsaRzSLXQ5qGLMCL2Bif7zIXpx5dxvxb9PknIelZLRiP4hvUqKCgi4L t1ZYtizTp/qwdL/8juX/o3lYdInjXPGzkSLjYw6iewVuWYSLEg98FnGK9nlyndGC9cqh 2H8A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=aMHEXlN8; 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-100692-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-100692-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. [147.75.199.223]) by mx.google.com with ESMTPS id z20-20020a05622a061400b0042ef4674ba3si8245591qta.623.2024.03.12.11.53.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Mar 2024 11:53:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-100692-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=aMHEXlN8; 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-100692-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-100692-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 30F961C23284 for ; Tue, 12 Mar 2024 18:53:09 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D2CA614262B; Tue, 12 Mar 2024 18:52:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="aMHEXlN8" Received: from out-181.mta0.migadu.com (out-181.mta0.migadu.com [91.218.175.181]) (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 BEA4D433A3 for ; Tue, 12 Mar 2024 18:52:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.181 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710269578; cv=none; b=YARapa9ENjCgOBeUNgNYEzf2cPe4nc2Fd8smY1C2FdnycrpjLX3PIUgkZLuEF5S0RSa53/iycnaIhPviUcJ4TD0gCima/fRg8GB0Pyqkkg2B6Og/KrCXUUyM+sNRFD+ePxAH9urHXB+f2j4Y6PphchgMySkvj7GT8bUuRuZcS8o= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710269578; c=relaxed/simple; bh=NKg4Q71TmG2wl3jNVwI2w6kog2D0YPNFn53K+82hu64=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=eMeY/v/hvBJ8agMIelduTywirBcsuLQVyF5s9b6hyjNZSdJ02VlUDMSxqjMg/atk7UHm2p51aT5jntErcVIkroDW7lvrAWWnXW8bJw4gWoh0/FBAmrdC5EIXddpP2rya+KIQ8855mWYwDZMlRwkGvVZmXkrMbUF0emYF+sf8aLs= 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=aMHEXlN8; arc=none smtp.client-ip=91.218.175.181 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: Tue, 12 Mar 2024 11:52:46 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1710269574; 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=cB9p4AvFVqG7BzTvwz43ZlsmGe2aR9Z38/ULirCZ1Xs=; b=aMHEXlN8b45FtQM0pRTQ8DM07syqKxAPvaKz0ABqgotLq+deoNr8afEOlc6CKRZ2v0k9E6 a3P5y8gz/TOouO5c09EhY0Rs9i2czqGoJGzyZDMPGbY0GJc2E1dpz830jhg8Df+eUWarjv ks8+DS5cwsk8Ga5QHoEc6NIZLTd/L7k= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Roman Gushchin To: Vlastimil Babka Cc: Linus Torvalds , Josh Poimboeuf , Jeff Layton , Chuck Lever , Kees Cook , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , Hyeonggon Yoo <42.hyeyoo@gmail.com>, Johannes Weiner , Michal Hocko , Shakeel Butt , Muchun Song , Alexander Viro , Christian Brauner , Jan Kara , linux-mm@kvack.org, linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-fsdevel@vger.kernel.org Subject: Re: [PATCH RFC 1/4] mm, slab: move memcg charging to post-alloc hook Message-ID: References: <20240301-slab-memcg-v1-0-359328a46596@suse.cz> <20240301-slab-memcg-v1-1-359328a46596@suse.cz> 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: <20240301-slab-memcg-v1-1-359328a46596@suse.cz> X-Migadu-Flow: FLOW_OUT On Fri, Mar 01, 2024 at 06:07:08PM +0100, Vlastimil Babka wrote: > The MEMCG_KMEM integration with slab currently relies on two hooks > during allocation. memcg_slab_pre_alloc_hook() determines the objcg and > charges it, and memcg_slab_post_alloc_hook() assigns the objcg pointer > to the allocated object(s). > > As Linus pointed out, this is unnecessarily complex. Failing to charge > due to memcg limits should be rare, so we can optimistically allocate > the object(s) and do the charging together with assigning the objcg > pointer in a single post_alloc hook. In the rare case the charging > fails, we can free the object(s) back. > > This simplifies the code (no need to pass around the objcg pointer) and > potentially allows to separate charging from allocation in cases where > it's common that the allocation would be immediately freed, and the > memcg handling overhead could be saved. > > Suggested-by: Linus Torvalds > Link: https://lore.kernel.org/all/CAHk-=whYOOdM7jWy5jdrAm8LxcgCMFyk2bt8fYYvZzM4U-zAQA@mail.gmail.com/ > Signed-off-by: Vlastimil Babka Nice cleanup, Vlastimil! Couple of small nits below, but otherwise, please, add my Reviewed-by: Roman Gushchin Thanks! --- > mm/slub.c | 180 +++++++++++++++++++++++++++----------------------------------- > 1 file changed, 77 insertions(+), 103 deletions(-) > > diff --git a/mm/slub.c b/mm/slub.c > index 2ef88bbf56a3..7022a1246bab 100644 > --- a/mm/slub.c > +++ b/mm/slub.c > @@ -1897,23 +1897,36 @@ static inline size_t obj_full_size(struct kmem_cache *s) > return s->size + sizeof(struct obj_cgroup *); > } > > -/* > - * Returns false if the allocation should fail. > - */ > -static bool __memcg_slab_pre_alloc_hook(struct kmem_cache *s, > - struct list_lru *lru, > - struct obj_cgroup **objcgp, > - size_t objects, gfp_t flags) > +static bool __memcg_slab_post_alloc_hook(struct kmem_cache *s, > + struct list_lru *lru, > + gfp_t flags, size_t size, > + void **p) > { > + struct obj_cgroup *objcg; > + struct slab *slab; > + unsigned long off; > + size_t i; > + > /* > * The obtained objcg pointer is safe to use within the current scope, > * defined by current task or set_active_memcg() pair. > * obj_cgroup_get() is used to get a permanent reference. > */ > - struct obj_cgroup *objcg = current_obj_cgroup(); > + objcg = current_obj_cgroup(); > if (!objcg) > return true; > > + /* > + * slab_alloc_node() avoids the NULL check, so we might be called with a > + * single NULL object. kmem_cache_alloc_bulk() aborts if it can't fill > + * the whole requested size. > + * return success as there's nothing to free back > + */ > + if (unlikely(*p == NULL)) > + return true; Probably better to move this check up? current_obj_cgroup() != NULL check is more expensive. > + > + flags &= gfp_allowed_mask; > + > if (lru) { > int ret; > struct mem_cgroup *memcg; > @@ -1926,71 +1939,51 @@ static bool __memcg_slab_pre_alloc_hook(struct kmem_cache *s, > return false; > } > > - if (obj_cgroup_charge(objcg, flags, objects * obj_full_size(s))) > + if (obj_cgroup_charge(objcg, flags, size * obj_full_size(s))) > return false; > > - *objcgp = objcg; > + for (i = 0; i < size; i++) { > + slab = virt_to_slab(p[i]); Not specific to this change, but I wonder if it makes sense to introduce virt_to_slab() variant without any extra checks for this and similar cases, where we know for sure that p resides on a slab page. What do you think?