Received: by 2002:ac0:a581:0:0:0:0:0 with SMTP id m1-v6csp88257imm; Tue, 19 Jun 2018 16:32:32 -0700 (PDT) X-Google-Smtp-Source: ADUXVKKPKeLhKPRwFB+2WmmaUBSjJUyBz5bbe6pE4yZRTRiCIFz1R/4Z+Xswp2r0YQSm/jXKe/22 X-Received: by 2002:a63:6a89:: with SMTP id f131-v6mr16755029pgc.311.1529451152160; Tue, 19 Jun 2018 16:32:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1529451152; cv=none; d=google.com; s=arc-20160816; b=L/QCQSAOjnFdpSBO6/Ax/5YhnDwPt8k5bOSM2OytPzVPu4oEgHqFE2jq4KnYfB6iMT IdDKuJrQ28ZMxjt7h2XgTv5ZXqMF9H5G+ynSm63mJ1cV8If+vU7bU9ZiopBgvESdkF6T 7OQ8tFEWhOYmfnZCUJolRlU1vzQvWl6Zt93Rg2pTsBxiwbM4ZrYwyVtlljlvf9fN23H2 mIgGklZHz6Dvpjkz1My3SP/ifWLHJ6r/UeFtrOVP1lMFb/y8hCHs35IewQajRsgOtqIt 7SnccWFVUkd37fjizPS4zdT2kfhH17hcu1yeGb0h3MFAE5NkKexag6aiTwF3vU7uBxPX k9xg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature :arc-authentication-results; bh=KLUGix+u8YxaEilfclW80OJnWpbxSGKRxeUu0OHvIRY=; b=ZqxWqsFo/7LIeOegL+OczXCEOmZUtYHa/bpUKgsUXRhuiaPiZP7mVLXtV/QvDsxjAC 2x5pJCdBkie96y7kwKUYmV6oFHRQn9PAlzBbWX5uUI+Rscg388xlotQdxU7VMul3Brm1 VswDVQf4Qzhc7fpxmSV85QFooiFUd5wchEUX0Ws4RYWgobBKBneQLQzq6LpyhK7q5YPa jFENDXiYDlBevKhlYMA9riKV78LD39ou6aPZ9sphjHzn7Wlk1iVwF6+cZgHSNzjimHEc A9KmwPku7BKduGrRp87agA+NUBrtNg4Z0AjAY+Zj6kBn3NeyaRheBj8CFBXq82TRXVVX DL4g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=LPEt3Wm+; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o3-v6si687330pgr.521.2018.06.19.16.32.17; Tue, 19 Jun 2018 16:32:32 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=LPEt3Wm+; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1750917AbeFSXbg (ORCPT + 99 others); Tue, 19 Jun 2018 19:31:36 -0400 Received: from mail-wm0-f68.google.com ([74.125.82.68]:54677 "EHLO mail-wm0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750726AbeFSXbd (ORCPT ); Tue, 19 Jun 2018 19:31:33 -0400 Received: by mail-wm0-f68.google.com with SMTP id o13-v6so2938925wmf.4 for ; Tue, 19 Jun 2018 16:31:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=KLUGix+u8YxaEilfclW80OJnWpbxSGKRxeUu0OHvIRY=; b=LPEt3Wm+H78tMEjCrA9SK1n5bsWI6kVikPAR+srvTd/psEn8EvN4jjIcY8vrz8RJfe 7SncILq6mg4QUAX0Fk/SvT7YYFZQGZA1RbeoIqZgKJPGofiBSfb4dWNLsOPTxqej7y1h rKCIyGiSs/OkbrFSGPz7+Xo1N5NCQKeX/OnDlCpJQFdSUDG5ANNxZ1EcYezz2bOwq1qB tohdgyd9JY9I+Fxu8XVwRDXrnCzj72xQ3rm9h81eLbp9HrJ3sTlO0Rz7Kwgh0C1wU7Xo SusUoVCoNjPCA1n80zhKeJyiDLYEhkECWPj2R5xl3swbT6WyDe6azjJzKM8xxijWWxNv NoaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=KLUGix+u8YxaEilfclW80OJnWpbxSGKRxeUu0OHvIRY=; b=cBqGM2QFxZGyep3HAiAX39RnTpmsCMzxL1WWlVH4mqEAS4Q8lsCJk3FxZ9g5m+Mbzh 1VZRbAqtuQriEb/9HPo9eHTVGW8GY8cPwAmhro5NH3l9UabDDb1F74Rhq0UP8ynSyKDW de1FTrrC98tRLSm5JLFcYpQU6PVnT4TO4xIFaFnbFgqnTg/pjjJdhr4c4+syshaAn4Fb BMEXm3JsAUAe5o1moGguuPzzj5jxwV6A1IWLIgxfXGGtzUqiS5/2ySwv2A2goNyv/nPY VGsBYNspWUcBrVupj5UaGXgg4/zBTvu08W221LrlgPv/jW0gm4VUJPc1xM0Gdjjt6CjJ s+ng== X-Gm-Message-State: APt69E212vg136sv2N/zMbHq6WhWNGpktdqc4Q0oRzdt6JYnFoIDAVDi aJX9MMtvLqFrQipcn0QhygcxsPJ+hqS9tZ9gh7xwvw== X-Received: by 2002:a1c:443:: with SMTP id 64-v6mr14195690wme.140.1529451091287; Tue, 19 Jun 2018 16:31:31 -0700 (PDT) MIME-Version: 1.0 References: <20180619051327.149716-1-shakeelb@google.com> <20180619051327.149716-2-shakeelb@google.com> <20180619162429.GB27423@cmpxchg.org> In-Reply-To: <20180619162429.GB27423@cmpxchg.org> From: Shakeel Butt Date: Tue, 19 Jun 2018 16:31:18 -0700 Message-ID: Subject: Re: [PATCH 1/3] mm: memcg: remote memcg charging for kmem allocations To: Johannes Weiner Cc: Andrew Morton , Michal Hocko , Vladimir Davydov , Jan Kara , Greg Thelen , LKML , Cgroups , linux-fsdevel , Linux MM , Jan Kara , Amir Goldstein , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Mel Gorman , Vlastimil Babka , Alexander Viro Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Jun 19, 2018 at 9:22 AM Johannes Weiner wrote: > > On Mon, Jun 18, 2018 at 10:13:25PM -0700, Shakeel Butt wrote: > > @@ -248,6 +248,30 @@ static inline void memalloc_noreclaim_restore(unsigned int flags) > > current->flags = (current->flags & ~PF_MEMALLOC) | flags; > > } > > > > +#ifdef CONFIG_MEMCG > > +static inline struct mem_cgroup *memalloc_memcg_save(struct mem_cgroup *memcg) > > +{ > > + struct mem_cgroup *old_memcg = current->target_memcg; > > + > > + current->target_memcg = memcg; > > + return old_memcg; > > +} > > + > > +static inline void memalloc_memcg_restore(struct mem_cgroup *memcg) > > +{ > > + current->target_memcg = memcg; > > +} > > The use_mm() and friends naming scheme would be better here: > memalloc_use_memcg(), memalloc_unuse_memcg(), current->active_memcg > Ack. Though do you still think is the right place for these functions? > > @@ -375,6 +376,27 @@ static __always_inline void kfree_bulk(size_t size, void **p) > > kmem_cache_free_bulk(NULL, size, p); > > } > > > > +/* > > + * Calling kmem_cache_alloc_memcg implicitly assumes that the caller wants > > + * a __GFP_ACCOUNT allocation. However if memcg is NULL then > > + * kmem_cache_alloc_memcg is same as kmem_cache_alloc. > > + */ > > +static __always_inline void *kmem_cache_alloc_memcg(struct kmem_cache *cachep, > > + gfp_t flags, > > + struct mem_cgroup *memcg) > > +{ > > + struct mem_cgroup *old_memcg; > > + void *ptr; > > + > > + if (!memcg) > > + return kmem_cache_alloc(cachep, flags); > > + > > + old_memcg = memalloc_memcg_save(memcg); > > + ptr = kmem_cache_alloc(cachep, flags | __GFP_ACCOUNT); > > + memalloc_memcg_restore(old_memcg); > > + return ptr; > > I'm not a big fan of these functions as an interface because it > implies that kmem_cache_alloc() et al wouldn't charge a memcg - but > they do, just using current's memcg. > > It's also a lot of churn to duplicate all the various slab functions. > > Can you please inline the save/restore (or use/unuse) functions into > the callsites? If you make them handle NULL as parameters, it merely > adds two bracketing lines around the allocation call in the callsites, > which I think would be better to understand - in particular with a > comment on why we are charging *that* group instead of current's. > Ack. > > +static __always_inline struct mem_cgroup *get_mem_cgroup( > > + struct mem_cgroup *memcg, struct mm_struct *mm) > > +{ > > + if (unlikely(memcg)) { > > + rcu_read_lock(); > > + if (css_tryget_online(&memcg->css)) { > > + rcu_read_unlock(); > > + return memcg; > > + } > > + rcu_read_unlock(); > > + } > > + return get_mem_cgroup_from_mm(mm); > > +} > > + > > /** > > * mem_cgroup_iter - iterate over memory cgroup hierarchy > > * @root: hierarchy root > > @@ -2260,7 +2274,7 @@ struct kmem_cache *memcg_kmem_get_cache(struct kmem_cache *cachep) > > if (current->memcg_kmem_skip_account) > > return cachep; > > > > - memcg = get_mem_cgroup_from_mm(current->mm); > > + memcg = get_mem_cgroup(current->target_memcg, current->mm); > > get_mem_cgroup_from_current(), which uses current->active_memcg if set > and current->mm->memcg otherwise, would be a nicer abstraction IMO. Ack. thanks, Shakeel