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 X-Spam-Level: X-Spam-Status: No, score=-9.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 24115C56201 for ; Thu, 12 Nov 2020 16:24:57 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 5CC4620936 for ; Thu, 12 Nov 2020 16:24:56 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=cmpxchg-org.20150623.gappssmtp.com header.i=@cmpxchg-org.20150623.gappssmtp.com header.b="MfnOv52S" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 5CC4620936 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=cmpxchg.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id B607E6B0074; Thu, 12 Nov 2020 11:24:55 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B11EA6B0075; Thu, 12 Nov 2020 11:24:55 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9B4E76B0078; Thu, 12 Nov 2020 11:24:55 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0245.hostedemail.com [216.40.44.245]) by kanga.kvack.org (Postfix) with ESMTP id 705B36B0074 for ; Thu, 12 Nov 2020 11:24:55 -0500 (EST) Received: from smtpin04.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 1CE12180AD802 for ; Thu, 12 Nov 2020 16:24:55 +0000 (UTC) X-FDA: 77476290150.04.plant87_5c00ef427307 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin04.hostedemail.com (Postfix) with ESMTP id ED3AE8005A84 for ; Thu, 12 Nov 2020 16:24:54 +0000 (UTC) X-HE-Tag: plant87_5c00ef427307 X-Filterd-Recvd-Size: 7819 Received: from mail-qk1-f194.google.com (mail-qk1-f194.google.com [209.85.222.194]) by imf45.hostedemail.com (Postfix) with ESMTP for ; Thu, 12 Nov 2020 16:24:54 +0000 (UTC) Received: by mail-qk1-f194.google.com with SMTP id 11so5778205qkd.5 for ; Thu, 12 Nov 2020 08:24:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cmpxchg-org.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=bIYmdruPemXDJ3Xp68i09oRAPpoygt4oEDIIpGtbWd4=; b=MfnOv52Sl2tr/+7U7bOXFlHJxRPRo2cVdp6cQyGR+2XWM1DldIwDbCEM1pG3CbXsIL is2vKvZcrHfsFwGoloEtwzdd6SyJPna5sxFpUKhifDVcVWPVZU1RxZ09Vs0B70qianHh MM7h7TwsDn6k2UkoFXoSCw+wI88f1D33RVdmycmTmvrIAlFmpsM0P5YFOC1cyidHgBk4 Sivhh16GAKQF+E8iornHQtqPtpT+Hpi9v4cFiAqA+D0ZlM0Ewenr2wK8IIhXjAJIdpE/ 6P5Sz7pJtctxGc4JAj+gmUNnN/Br0Ra4exOxH4xUdxx/XKVJCj9J5yOt7X/xOdpAU89T xrvQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=bIYmdruPemXDJ3Xp68i09oRAPpoygt4oEDIIpGtbWd4=; b=T39RWOlFcPKyul79UArw3C56hTiE2Ad+PYPJRxm8CjLl8AVlmGC75Jw3ALZC24nSGw B5OmK4goMCJKUkN9eJUJyV9fH17YFaCe7W9xl84in7f6Vnw9iFLna9oqQvQ5KXzOM82N EK2YMM0UGqY4Zr+ScinEuep++b7k8s9gyzR20sWC+ih0/jBad+yqItb3pjHw2xrEnfzS nHo9G9Ll7b/nC9qR0jIJPpIQx6NMGzaoB0pItp+csOqX2F3O8IO82zWz9aQ6RKQZE+PP DePP5m4lZHQz0SKIl2YP7oBr0MV1Zk+c9EW+TXQeyEOAqdxzYxnISi/maNrLqWu9CPT+ UeMA== X-Gm-Message-State: AOAM533iMqTdptrgDwv7tGeGgNWra30qkobDdnNCKAdBLjM/JQ1mQutj NsjE5hIYrYh8tY7XZ/eokU5nug== X-Google-Smtp-Source: ABdhPJz3bBHwHr7iBtOgV9aSEUhCSmoPjhng/xQ6gvFywTHE5XT7O9LRuZOOs/APZv07THoTT8w+zw== X-Received: by 2002:ae9:c211:: with SMTP id j17mr556311qkg.458.1605198293350; Thu, 12 Nov 2020 08:24:53 -0800 (PST) Received: from localhost ([2620:10d:c091:480::1:7257]) by smtp.gmail.com with ESMTPSA id c203sm893673qkg.60.2020.11.12.08.24.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Nov 2020 08:24:52 -0800 (PST) Date: Thu, 12 Nov 2020 11:23:03 -0500 From: Johannes Weiner To: Roman Gushchin Cc: Andrew Morton , linux-mm@kvack.org, Shakeel Butt , Michal Hocko , Christoph Lameter , linux-kernel@vger.kernel.org, kernel-team@fb.com Subject: Re: [PATCH 2/2] mm: memcg/slab: pre-allocate obj_cgroups for slab caches with SLAB_ACCOUNT Message-ID: <20201112162303.GB873621@cmpxchg.org> References: <20201110195753.530157-1-guro@fb.com> <20201110195753.530157-2-guro@fb.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20201110195753.530157-2-guro@fb.com> 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 Tue, Nov 10, 2020 at 11:57:53AM -0800, Roman Gushchin wrote: > In general it's unknown in advance if a slab page will contain > accounted objects or not. In order to avoid memory waste, an > obj_cgroup vector is allocated dynamically when a need to account > of a new object arises. Such approach is memory efficient, but > requires an expensive cmpxchg() to set up the memcg/objcgs pointer, > because an allocation can race with a different allocation on another > cpu. > > But in some common cases it's known for sure that a slab page will > contain accounted objects: if the page belongs to a slab cache with a > SLAB_ACCOUNT flag set. It includes such popular objects like > vm_area_struct, anon_vma, task_struct, etc. > > In such cases we can pre-allocate the objcgs vector and simple assign > it to the page without any atomic operations, because at this early > stage the page is not visible to anyone else. > > Signed-off-by: Roman Gushchin That's a nice optimization! Some comments inline: > @@ -485,14 +485,20 @@ static inline struct obj_cgroup **page_objcgs_check(struct page *page) > * set_page_objcgs - associate a page with a object cgroups vector > * @page: a pointer to the page struct > * @objcgs: a pointer to the object cgroups vector > + * @atomic: save the value atomically > * > * Atomically associates a page with a vector of object cgroups. > */ > static inline bool set_page_objcgs(struct page *page, > - struct obj_cgroup **objcgs) > + struct obj_cgroup **objcgs, bool atomic) bool parameters make callsites pretty hard to understand - unless the function interface obviously has two modes (read vs write etc.), which isn't the case here. > - return !cmpxchg(&page->memcg_data, 0, (unsigned long)objcgs | > - MEMCG_DATA_OBJCGS); > + unsigned long memcg_data = (unsigned long) objcgs | MEMCG_DATA_OBJCGS; > + > + if (atomic) > + return !cmpxchg(&page->memcg_data, 0, memcg_data); > + > + page->memcg_data = memcg_data; > + return true; > } > #else > static inline struct obj_cgroup **page_objcgs(struct page *page) > @@ -506,7 +512,7 @@ static inline struct obj_cgroup **page_objcgs_check(struct page *page) > } > > static inline bool set_page_objcgs(struct page *page, > - struct obj_cgroup **objcgs) > + struct obj_cgroup **objcgs, bool atomic) > { > return true; > } > diff --git a/mm/memcontrol.c b/mm/memcontrol.c > index 69a2893a6455..37bffd336235 100644 > --- a/mm/memcontrol.c > +++ b/mm/memcontrol.c > @@ -2874,7 +2874,7 @@ static void commit_charge(struct page *page, struct mem_cgroup *memcg) > > #ifdef CONFIG_MEMCG_KMEM > int memcg_alloc_page_obj_cgroups(struct page *page, struct kmem_cache *s, > - gfp_t gfp) > + gfp_t gfp, bool atomic) > { > unsigned int objects = objs_per_slab_page(s, page); > void *vec; > @@ -2884,7 +2884,7 @@ int memcg_alloc_page_obj_cgroups(struct page *page, struct kmem_cache *s, > if (!vec) > return -ENOMEM; > > - if (!set_page_objcgs(page, vec)) > + if (!set_page_objcgs(page, vec, atomic)) > kfree(vec); > else > kmemleak_not_leak(vec); The life of page->memcg_data and this optimization could use a central comment somewhere, because it's hard to understand what's going on from the code alone. This function here seems like a good place? I don't see a way to eliminate the bool on the allocation function, but maybe it could be more descriptive. Maybe bool slab_account? set_page_objcgs() can be inlined at this point. It made some sense to abstract away the atomics with setter and matching getter, but with a non-atomic mode, inlining makes things clearer and allows for better in-place documentation in the sole callsite. How about something like this? vec = kcalloc(...); memcg_data = (unsigned long)vec | MEMCG_DATA_OBJCGS; /* * Set up the objcg vector for the page. * * When only some objects in a slab are tracked (think GFP_ACCOUNT * kmalloc allocations), the objcg vector is set up when the first * tracked object in the slab page is allocated. Multiple concurrent * slab allocations can race to this, so synchronization is required. * * When SLAB_ACCOUNT is set on the cache, however, all objects in the * slab page will be tracked, and the vector is allocated along with * the page itself, while it's still exclusive; no atomics necessary. */ if (slab_account) { page->memcg_data = memcg_data; } else { if (cmpxchg(&page->memcg_data, 0, memcg_data)) { /* Somebody else beat us, use their vec */ kfree(vec); return 0; } } kmemleak_not_leak(vec); return 0;