Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp2445947ybl; Thu, 9 Jan 2020 12:56:20 -0800 (PST) X-Google-Smtp-Source: APXvYqxNDQRyS14ArAEny21Q/gSHceb23xrVJ7eN/0ILicvHBO6c8Iy2x842Yf7PTgnDJ/OVguD4 X-Received: by 2002:a9d:6b91:: with SMTP id b17mr10261214otq.321.1578603380741; Thu, 09 Jan 2020 12:56:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1578603380; cv=none; d=google.com; s=arc-20160816; b=mkddsxMIKOkw8+SDLN3bDYTIREXWvjLYz+jo+SgydQMT6xsBfOTyZlbXYUnnh05D5N BsaiU9NPxTljys84sqO5KfnR00R44l3Lo4zO7o4OM/XBblR8RX5nQoA2iU85LmbmB1dk Bh07z90nu4bvCIkK6Yd7JWeUIzhvMYMebQXlD3MHBCyLr0Ta2453RMGreWJyYSO3jTwB 8dnrE/mcrfpcDKBQtGj9lBHR+jftoYYcVwmvd3U4hmXyJeuc99CFs37Yi6PJdBOA18gX awrbWO/zgHMk8XQnN3oR5se20+OE+xCxT1kzeZ3TR4vOBZ0u0a5WAqjc7yRDPE+m4Qe/ jpJA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:smtp-origin-cluster:cc:to :smtp-origin-hostname:from:smtp-origin-hostprefix:dkim-signature; bh=Sky71T6ocPO8hx0rEsER0dZwwaqEusEZM0Nrks58OYc=; b=Yr9zQR/33MPXmTF7xePTHOVSR1/d7CRzqxnFkLuFf2PzgpjxJcuKivz3Dck/RXDOq6 qQ+3ZgXwscdzmA5V9mtQ2PMOFRad9Aag47qDqAX6UXs5Q32fJVzpRary3+BX0LVPFMWA vg8Ml1teGBRTcN67OyjaAoQTPIVjGLfgZFO4BnGdMQmhbfhWLnlb/0KZ2twBEl+PXTZ+ FydsHm5xzvkNhfkzxtTFl57IVW7GwJYuw0rJGHQpfkDbhDMLBPC3aibflUbRNuOFnN0B XNtox+Uuz9B8Q0voJgh1IJF6nGioFc3OijGLUTCWCvRTchRta5xHixelOHK0oWZU1mB5 hGdQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@fb.com header.s=facebook header.b=BG9AZcXT; 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=NONE sp=NONE dis=NONE) header.from=fb.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z25si4842105oto.211.2020.01.09.12.56.09; Thu, 09 Jan 2020 12:56:20 -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; dkim=pass header.i=@fb.com header.s=facebook header.b=BG9AZcXT; 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=NONE sp=NONE dis=NONE) header.from=fb.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730409AbgAIU1X (ORCPT + 99 others); Thu, 9 Jan 2020 15:27:23 -0500 Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:31350 "EHLO mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1730295AbgAIU1U (ORCPT ); Thu, 9 Jan 2020 15:27:20 -0500 Received: from pps.filterd (m0089730.ppops.net [127.0.0.1]) by m0089730.ppops.net (8.16.0.42/8.16.0.42) with SMTP id 009KPpCm025962 for ; Thu, 9 Jan 2020 12:27:19 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-type; s=facebook; bh=Sky71T6ocPO8hx0rEsER0dZwwaqEusEZM0Nrks58OYc=; b=BG9AZcXTJarBqJYln7SZ+gesITXWesDqUrUXm8Jolcrzp4wI3BuGqFUvkCWNFxSQzwHh vOkmEpPh2l80gqevFig9bpwajKFAJmUjHqQfEC5FHuIzCZ7MTmT2JIRPo6ZUJ8CoTHT1 vTpBJdfNrn8GIFdJwwzg418KILtcOgX4BYM= Received: from mail.thefacebook.com (mailout.thefacebook.com [199.201.64.23]) by m0089730.ppops.net with ESMTP id 2xdh777wsp-5 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Thu, 09 Jan 2020 12:27:19 -0800 Received: from intmgw002.06.prn3.facebook.com (2620:10d:c081:10::13) by mail.thefacebook.com (2620:10d:c081:35::126) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA) id 15.1.1713.5; Thu, 9 Jan 2020 12:27:16 -0800 Received: by devvm2643.prn2.facebook.com (Postfix, from userid 111017) id 35DD41D2F0DCC; Thu, 9 Jan 2020 12:27:07 -0800 (PST) Smtp-Origin-Hostprefix: devvm From: Roman Gushchin Smtp-Origin-Hostname: devvm2643.prn2.facebook.com To: , Andrew Morton CC: Michal Hocko , Johannes Weiner , Shakeel Butt , Vladimir Davydov , , , Roman Gushchin Smtp-Origin-Cluster: prn2c23 Subject: [PATCH v2 5/6] mm: memcg/slab: cache page number in memcg_(un)charge_slab() Date: Thu, 9 Jan 2020 12:26:58 -0800 Message-ID: <20200109202659.752357-6-guro@fb.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200109202659.752357-1-guro@fb.com> References: <20200109202659.752357-1-guro@fb.com> X-FB-Internal: Safe MIME-Version: 1.0 Content-Type: text/plain X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.138,18.0.572 definitions=2020-01-09_04:2020-01-09,2020-01-09 signatures=0 X-Proofpoint-Spam-Details: rule=fb_default_notspam policy=fb_default score=0 malwarescore=0 suspectscore=0 bulkscore=0 lowpriorityscore=0 mlxscore=0 priorityscore=1501 impostorscore=0 adultscore=0 spamscore=0 clxscore=1015 mlxlogscore=999 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-1910280000 definitions=main-2001090168 X-FB-Internal: deliver Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org There are many places in memcg_charge_slab() and memcg_uncharge_slab() which are calculating the number of pages to charge, css references to grab etc depending on the order of the slab page. Let's simplify the code by calculating it once and caching in the local variable. Signed-off-by: Roman Gushchin --- mm/slab.h | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/mm/slab.h b/mm/slab.h index d96c87a30a9b..43f8ce4aa325 100644 --- a/mm/slab.h +++ b/mm/slab.h @@ -348,6 +348,7 @@ static __always_inline int memcg_charge_slab(struct page *page, gfp_t gfp, int order, struct kmem_cache *s) { + unsigned int nr_pages = 1 << order; struct mem_cgroup *memcg; struct lruvec *lruvec; int ret; @@ -360,21 +361,21 @@ static __always_inline int memcg_charge_slab(struct page *page, if (unlikely(!memcg || mem_cgroup_is_root(memcg))) { mod_node_page_state(page_pgdat(page), cache_vmstat_idx(s), - (1 << order)); - percpu_ref_get_many(&s->memcg_params.refcnt, 1 << order); + nr_pages); + percpu_ref_get_many(&s->memcg_params.refcnt, nr_pages); return 0; } - ret = memcg_kmem_charge_memcg(memcg, gfp, 1 << order); + ret = memcg_kmem_charge_memcg(memcg, gfp, nr_pages); if (ret) goto out; lruvec = mem_cgroup_lruvec(memcg, page_pgdat(page)); - mod_lruvec_state(lruvec, cache_vmstat_idx(s), 1 << order); + mod_lruvec_state(lruvec, cache_vmstat_idx(s), nr_pages); /* transer try_charge() page references to kmem_cache */ - percpu_ref_get_many(&s->memcg_params.refcnt, 1 << order); - css_put_many(&memcg->css, 1 << order); + percpu_ref_get_many(&s->memcg_params.refcnt, nr_pages); + css_put_many(&memcg->css, nr_pages); out: css_put(&memcg->css); return ret; @@ -387,6 +388,7 @@ static __always_inline int memcg_charge_slab(struct page *page, static __always_inline void memcg_uncharge_slab(struct page *page, int order, struct kmem_cache *s) { + unsigned int nr_pages = 1 << order; struct mem_cgroup *memcg; struct lruvec *lruvec; @@ -394,15 +396,15 @@ static __always_inline void memcg_uncharge_slab(struct page *page, int order, memcg = READ_ONCE(s->memcg_params.memcg); if (likely(!mem_cgroup_is_root(memcg))) { lruvec = mem_cgroup_lruvec(memcg, page_pgdat(page)); - mod_lruvec_state(lruvec, cache_vmstat_idx(s), -(1 << order)); - memcg_kmem_uncharge_memcg(memcg, order); + mod_lruvec_state(lruvec, cache_vmstat_idx(s), -nr_pages); + memcg_kmem_uncharge_memcg(memcg, nr_pages); } else { mod_node_page_state(page_pgdat(page), cache_vmstat_idx(s), - -(1 << order)); + -nr_pages); } rcu_read_unlock(); - percpu_ref_put_many(&s->memcg_params.refcnt, 1 << order); + percpu_ref_put_many(&s->memcg_params.refcnt, nr_pages); } extern void slab_init_memcg_params(struct kmem_cache *); -- 2.21.1