Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp5378593imm; Tue, 19 Jun 2018 09:27:30 -0700 (PDT) X-Google-Smtp-Source: ADUXVKJr4jppBUfQOZtqrv99xmFhGdovVHUVPMbHqi0i1BA8OL3WGHg2uBz+BxJC/mKrrgeYs+Jf X-Received: by 2002:a62:a104:: with SMTP id b4-v6mr18980382pff.159.1529425650608; Tue, 19 Jun 2018 09:27:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1529425650; cv=none; d=google.com; s=arc-20160816; b=AfI4etv+RHzIaDh8jPLlVQfw3/rxd/bymh9tkVcFIMdtIRqNE5vlIWtDIAMazVTSCC Zkynvr/otn0htBhoaVbP3m4+X9o9WmrlzSW8EDNPRxQkFEZNhiN1WlqLBxVv4laKJ0lZ oPJjNXBqqK7BBg26T2iVLBPqyYn6BATJb1zLXIg1QB5iiMm21rDcXgavbfWKZ1PXOMN+ nFgP+/v6x7unFRCHiYgABQsVTfFVW0C00I0NtUwRen4E2ZBRQBx+AcWkl76mI45d5FwP zbOAAh6tODEjQgn8mF0K+IeYnX/Md8w+O8LOad8qVTZLnwE1aNfeTOUZFeLe4wbVMiiW 0Sgg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature:arc-authentication-results; bh=IjzRyh3dGjs3iY1KTmpl8JPs/rIgpmEkG8uepgThUhg=; b=ijaCVim+1D//JizsXhjD1xcAhbRMfoB/H7nLgG5XLq0wtwqlYCFegc0vaHlbKMqRqM ulXTjYsx9m0Y/J6YNSpmmI106+584q8LnQLMXx39GyOw+r4GIRce4uWpmK3nYwvE83Ng ziqcepThYh+F2+E4Who2/oPYcZzJ+2GcB8B3Sp8tvZT9NFOoZlF/5HcpfcMnEBH6cL3n +RY9YtCQFeaSiEqQK3+HYN/cq3NZOB48Ujcgg951jlVtwFwRDLa3asoBk4jVLoSFE7T4 X8Vcdjo7a8udbKx2ESM6oPizWkzvrJhYGKO9urNGM6Tb1Co2tKinv1qynJ6fDjB574H2 5cag== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@cmpxchg.org header.s=x header.b=nd7imqtf; 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=fail (p=NONE sp=NONE dis=NONE) header.from=cmpxchg.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r85-v6si48614pfa.259.2018.06.19.09.27.15; Tue, 19 Jun 2018 09:27:30 -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=fail header.i=@cmpxchg.org header.s=x header.b=nd7imqtf; 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=fail (p=NONE sp=NONE dis=NONE) header.from=cmpxchg.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S966956AbeFSQZW (ORCPT + 99 others); Tue, 19 Jun 2018 12:25:22 -0400 Received: from gum.cmpxchg.org ([85.214.110.215]:48458 "EHLO gum.cmpxchg.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S966208AbeFSQZU (ORCPT ); Tue, 19 Jun 2018 12:25:20 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=cmpxchg.org ; s=x; h=In-Reply-To:Content-Type:MIME-Version:References:Message-ID:Subject: Cc:To:From:Date:Sender:Reply-To:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=IjzRyh3dGjs3iY1KTmpl8JPs/rIgpmEkG8uepgThUhg=; b=nd7imqtf/Bt7PIXzfoMP72TpvJ fcVl5OhKp5LuMWMtBUzrPXPexpbfBrKHpgX7cPhiUwFi700FVyXbJ15hbfZ8W/XH5wMX62k9qiK9M 1yqEscaFfNPDQwB1xjupB5TmDP0srDPuWD8XtBQfssqtlhrioexcIeIw2/B1SwcVsumM=; Date: Tue, 19 Jun 2018 12:27:41 -0400 From: Johannes Weiner To: Shakeel Butt Cc: Andrew Morton , Michal Hocko , Vladimir Davydov , Jan Kara , Greg Thelen , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, Jan Kara , Alexander Viro Subject: Re: [PATCH 3/3] fs, mm: account buffer_head to kmemcg Message-ID: <20180619162741.GC27423@cmpxchg.org> References: <20180619051327.149716-1-shakeelb@google.com> <20180619051327.149716-4-shakeelb@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20180619051327.149716-4-shakeelb@google.com> User-Agent: Mutt/1.10.0 (2018-05-17) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Jun 18, 2018 at 10:13:27PM -0700, Shakeel Butt wrote: > The buffer_head can consume a significant amount of system memory and > is directly related to the amount of page cache. In our production > environment we have observed that a lot of machines are spending a > significant amount of memory as buffer_head and can not be left as > system memory overhead. > > Charging buffer_head is not as simple as adding __GFP_ACCOUNT to the > allocation. The buffer_heads can be allocated in a memcg different from > the memcg of the page for which buffer_heads are being allocated. One > concrete example is memory reclaim. The reclaim can trigger I/O of pages > of any memcg on the system. So, the right way to charge buffer_head is > to extract the memcg from the page for which buffer_heads are being > allocated and then use targeted memcg charging API. > > Signed-off-by: Shakeel Butt > Cc: Jan Kara > Cc: Greg Thelen > Cc: Michal Hocko > Cc: Johannes Weiner > Cc: Vladimir Davydov > Cc: Alexander Viro > Cc: Andrew Morton > --- > fs/buffer.c | 14 +++++++++++++- > include/linux/memcontrol.h | 7 +++++++ > mm/memcontrol.c | 21 +++++++++++++++++++++ > 3 files changed, 41 insertions(+), 1 deletion(-) > > diff --git a/fs/buffer.c b/fs/buffer.c > index 8194e3049fc5..26389b7a3cab 100644 > --- a/fs/buffer.c > +++ b/fs/buffer.c > @@ -815,10 +815,17 @@ struct buffer_head *alloc_page_buffers(struct page *page, unsigned long size, > struct buffer_head *bh, *head; > gfp_t gfp = GFP_NOFS; > long offset; > + struct mem_cgroup *old_memcg; > + struct mem_cgroup *memcg = get_mem_cgroup_from_page(page); > > if (retry) > gfp |= __GFP_NOFAIL; > > + if (memcg) { > + gfp |= __GFP_ACCOUNT; > + old_memcg = memalloc_memcg_save(memcg); > + } Please move the get_mem_cgroup_from_page() call out of the declarations and down to right before the if (memcg) branch. > head = NULL; > offset = PAGE_SIZE; > while ((offset -= size) >= 0) { > @@ -835,6 +842,11 @@ struct buffer_head *alloc_page_buffers(struct page *page, unsigned long size, > /* Link the buffer to its page */ > set_bh_page(bh, page, offset); > } > +out: > + if (memcg) { > + memalloc_memcg_restore(old_memcg); > +#ifdef CONFIG_MEMCG > + css_put(&memcg->css); > +#endif Please add a put_mem_cgroup() ;) Otherwise this looks fine to me.