Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp1750538imu; Thu, 24 Jan 2019 00:59:19 -0800 (PST) X-Google-Smtp-Source: ALg8bN4sfItdr997F0m0yabXNhgrzuiA2yKTDIEVhBbkv76kIguOLl1KTUYVoJlzblX+d5rhyUaa X-Received: by 2002:a63:184a:: with SMTP id 10mr5098611pgy.81.1548320359374; Thu, 24 Jan 2019 00:59:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548320359; cv=none; d=google.com; s=arc-20160816; b=DwOXyX1xLg8OZL7Tc6vNc5kL4RT51D2FkFMaO0prheN9Fxe9A+N/FaTev6FKmiToU0 I8I7E9leTK1wXNGuktZEUIed0yKpHraZmf8Zvc0epq2r8JpY3qpAAGLIaL/4maMqvxJs N+jfGcTFWZbQVVHil8XuhDxcPNp5V63snHwcTm6gLs/cpD4jH+X91oU/BnuPZgIop6Vd C+yIpsBvVKxGg4V8DHY19wdW+tvo4fq8WcVJUyUgx6ktCzZCKMGKfilw7lQ3uK62I824 cwgGnOvkR/ZcLUEtASg5+JTy63t7XjZCSF3Fap9wqvrSfoalELzJd5ZS1hh1Q2DTzGuQ HF1g== 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; bh=EOiF3q2mL92pzMhnDwUStb/7cUrmPVsgOMD/ak5ttTk=; b=ILncYIFUIOV3p5zj+qhvP++8GKdlv7u5kNWwTGNNL0THIMaD6s20hTnLkEIBMTPbdK rOKhIDV1NjOOA4FLKWGaz6mm/y18SmbXxNKq+ysBzW0fo7v/sVHS9E564WWq4hv3+a8O TXyqItezpb1d99GIzOuwyTr9O7JSSb4pnnW0x8MdLDmW1KA3gy3vhlSpYk7v7k+kVUbP eyF+Uh1vKnS4b9yHHG+m0BB26Z0hvrni8pBzwYlCdbsl9WMax+xOyhxKjpvvMJFOE9UM nN2uDhHIAuOXpCBVw6ZzfRE9NB7IryFUbjqPoCxhnVE53wYU36TpG+Rn/VbGWaBWaKua 8xqg== ARC-Authentication-Results: i=1; mx.google.com; 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=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h127si21875358pfe.204.2019.01.24.00.59.04; Thu, 24 Jan 2019 00:59:19 -0800 (PST) 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; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727573AbfAXI6l (ORCPT + 99 others); Thu, 24 Jan 2019 03:58:41 -0500 Received: from mx2.suse.de ([195.135.220.15]:36876 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726880AbfAXI6h (ORCPT ); Thu, 24 Jan 2019 03:58:37 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 2A79BACC3; Thu, 24 Jan 2019 08:58:35 +0000 (UTC) Date: Thu, 24 Jan 2019 09:58:34 +0100 From: Michal Hocko To: Chris Down Cc: Andrew Morton , Johannes Weiner , Tejun Heo , Roman Gushchin , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-mm@kvack.org, kernel-team@fb.com Subject: Re: [PATCH] mm: Move maxable seq_file logic into a single place Message-ID: <20190124085834.GF4087@dhcp22.suse.cz> References: <20190124061718.GA15486@chrisdown.name> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190124061718.GA15486@chrisdown.name> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu 24-01-19 01:17:18, Chris Down wrote: > memcg has a significant number of files exposed to kernfs where their > value is either exposed directly or is "max" in the case of > PAGE_COUNTER_MAX. > > There's a fair amount of duplicated code here, since each file involves > turning a seq_file to a css, getting the memcg from the css, safely > reading the counter value, and then doing the right thing depending on > whether the value is PAGE_COUNTER_MAX or not. > > This patch adds the macro DEFINE_MEMCG_MAX_OR_VAL, which defines and > implements a generic way to do this work, avoiding fragmenting logic. I am not a huge fan of macro defined functions but it is true this will save more LOC than a simple helper used by each $foo_show function. > Signed-off-by: Chris Down > Cc: Andrew Morton > Cc: Johannes Weiner > Cc: Tejun Heo > Cc: Roman Gushchin > Cc: linux-kernel@vger.kernel.org > Cc: cgroups@vger.kernel.org > Cc: linux-mm@kvack.org > Cc: kernel-team@fb.com Acked-by: Michal Hocko Thanks! > --- > mm/memcontrol.c | 78 ++++++++++++------------------------------------- > 1 file changed, 18 insertions(+), 60 deletions(-) > > diff --git a/mm/memcontrol.c b/mm/memcontrol.c > index 18f4aefbe0bf..90e2e0ff5ed9 100644 > --- a/mm/memcontrol.c > +++ b/mm/memcontrol.c > @@ -261,6 +261,19 @@ struct cgroup_subsys_state *vmpressure_to_css(struct vmpressure *vmpr) > return &container_of(vmpr, struct mem_cgroup, vmpressure)->css; > } > > +/* Convenience macro to define seq_file mutators that can return "max" */ > +#define DEFINE_MEMCG_MAX_OR_VAL(name, key) \ > + static int name(struct seq_file *m, void *v) \ > + { \ > + struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m)); \ > + unsigned long val = READ_ONCE(memcg->key); \ > + if (val == PAGE_COUNTER_MAX) \ > + seq_puts(m, "max\n"); \ > + else \ > + seq_printf(m, "%llu\n", (u64)val * PAGE_SIZE); \ > + return 0; \ > + } > + > #ifdef CONFIG_MEMCG_KMEM > /* > * This will be the memcg's index in each cache's ->memcg_params.memcg_caches. > @@ -5383,18 +5396,7 @@ static u64 memory_current_read(struct cgroup_subsys_state *css, > return (u64)page_counter_read(&memcg->memory) * PAGE_SIZE; > } > > -static int memory_min_show(struct seq_file *m, void *v) > -{ > - struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m)); > - unsigned long min = READ_ONCE(memcg->memory.min); > - > - if (min == PAGE_COUNTER_MAX) > - seq_puts(m, "max\n"); > - else > - seq_printf(m, "%llu\n", (u64)min * PAGE_SIZE); > - > - return 0; > -} > +DEFINE_MEMCG_MAX_OR_VAL(memory_min_show, memory.min) > > static ssize_t memory_min_write(struct kernfs_open_file *of, > char *buf, size_t nbytes, loff_t off) > @@ -5413,18 +5415,7 @@ static ssize_t memory_min_write(struct kernfs_open_file *of, > return nbytes; > } > > -static int memory_low_show(struct seq_file *m, void *v) > -{ > - struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m)); > - unsigned long low = READ_ONCE(memcg->memory.low); > - > - if (low == PAGE_COUNTER_MAX) > - seq_puts(m, "max\n"); > - else > - seq_printf(m, "%llu\n", (u64)low * PAGE_SIZE); > - > - return 0; > -} > +DEFINE_MEMCG_MAX_OR_VAL(memory_low_show, memory.low) > > static ssize_t memory_low_write(struct kernfs_open_file *of, > char *buf, size_t nbytes, loff_t off) > @@ -5443,18 +5434,7 @@ static ssize_t memory_low_write(struct kernfs_open_file *of, > return nbytes; > } > > -static int memory_high_show(struct seq_file *m, void *v) > -{ > - struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m)); > - unsigned long high = READ_ONCE(memcg->high); > - > - if (high == PAGE_COUNTER_MAX) > - seq_puts(m, "max\n"); > - else > - seq_printf(m, "%llu\n", (u64)high * PAGE_SIZE); > - > - return 0; > -} > +DEFINE_MEMCG_MAX_OR_VAL(memory_high_show, high) > > static ssize_t memory_high_write(struct kernfs_open_file *of, > char *buf, size_t nbytes, loff_t off) > @@ -5480,18 +5460,7 @@ static ssize_t memory_high_write(struct kernfs_open_file *of, > return nbytes; > } > > -static int memory_max_show(struct seq_file *m, void *v) > -{ > - struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m)); > - unsigned long max = READ_ONCE(memcg->memory.max); > - > - if (max == PAGE_COUNTER_MAX) > - seq_puts(m, "max\n"); > - else > - seq_printf(m, "%llu\n", (u64)max * PAGE_SIZE); > - > - return 0; > -} > +DEFINE_MEMCG_MAX_OR_VAL(memory_max_show, memory.max) > > static ssize_t memory_max_write(struct kernfs_open_file *of, > char *buf, size_t nbytes, loff_t off) > @@ -6620,18 +6589,7 @@ static u64 swap_current_read(struct cgroup_subsys_state *css, > return (u64)page_counter_read(&memcg->swap) * PAGE_SIZE; > } > > -static int swap_max_show(struct seq_file *m, void *v) > -{ > - struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m)); > - unsigned long max = READ_ONCE(memcg->swap.max); > - > - if (max == PAGE_COUNTER_MAX) > - seq_puts(m, "max\n"); > - else > - seq_printf(m, "%llu\n", (u64)max * PAGE_SIZE); > - > - return 0; > -} > +DEFINE_MEMCG_MAX_OR_VAL(swap_max_show, swap.max) > > static ssize_t swap_max_write(struct kernfs_open_file *of, > char *buf, size_t nbytes, loff_t off) > -- > 2.20.1 -- Michal Hocko SUSE Labs