Received: by 2002:ac0:a581:0:0:0:0:0 with SMTP id m1-v6csp1769916imm; Sat, 30 Jun 2018 04:01:01 -0700 (PDT) X-Google-Smtp-Source: ADUXVKJllSS/UZQC+bsJ21lZTjU6rGkVUdaB1d33ic3FPkBwI67vnCCSvF4eoRHOxblF70llAnS1 X-Received: by 2002:a63:bd01:: with SMTP id a1-v6mr15428066pgf.319.1530356461289; Sat, 30 Jun 2018 04:01:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1530356461; cv=none; d=google.com; s=arc-20160816; b=oz87C18Q/wh6V9XyUqbL0jm/Lx6VdOsWACxgL1exJ/aXdUAUt2uip286hnoHXtbXAr jT8Oni7q1eKcNZFR3DKgFiUMAAe2OizOngjhHmgnCPwKorCcLOe6ogFT9jPy1796vy2C tgCYsx1B/5TSJE3ppiFtWavrhm7ZrRVZ9BEDqoE+3rb4sbODbhU2WAMtLp01pWeT6MJc noYoOPFUWyy+5m6sJU7Go20NcDOIUW5ALoCJ/YN8vLmj0c5zslEOTekzDmKLNBu036AB p5A8339ihE/SCTMU2QHYO872B2WNmwAvb9uuzxrEjvJYCRsU/KI6kXGhT9lsJVcjxM1a o4eg== 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 :references:in-reply-to:mime-version:dkim-signature :arc-authentication-results; bh=B11nRb1M9ylxyDLHy7hHr/o2CQ0FpLQ5xZPddyL+xJI=; b=eRGFN4y7+vRZ2e9yCbemAvN/jOrUbu1Ftlp+84bm3IApPT1t9ohrVgQroauSXLK7Q5 KAGzNe0itju0qx7HSSH7bswjydDKoKnNDNIxm7bWBpfcnhZrq1ThdJeYphGcaVkreu7c aLehSkMHNWt43l0uZlh3ppiqMSvoCtu6bEebsOIlpxF/iYz/61oQ9TSrnolj/Wq1Yyoh dJj4KsWdmsc4npIovSX3nzhweYU3orJBiluR3r1zb7dt/nqe4iaxhzOCDe2JRZ8Ixol8 W5YeITL729WS7LG1qUXP/11bP709VPTj6nnC8u3Stq3uzf5t5Dn459NSJGAJ7T4FPvwZ Y8xw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=PzEBfRWg; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f20-v6si5394360pgb.82.2018.06.30.04.00.46; Sat, 30 Jun 2018 04:01:01 -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=@gmail.com header.s=20161025 header.b=PzEBfRWg; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S936952AbeF3KyV (ORCPT + 99 others); Sat, 30 Jun 2018 06:54:21 -0400 Received: from mail-io0-f196.google.com ([209.85.223.196]:40943 "EHLO mail-io0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S934688AbeF3KyR (ORCPT ); Sat, 30 Jun 2018 06:54:17 -0400 Received: by mail-io0-f196.google.com with SMTP id t135-v6so10697450iof.7; Sat, 30 Jun 2018 03:54:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=B11nRb1M9ylxyDLHy7hHr/o2CQ0FpLQ5xZPddyL+xJI=; b=PzEBfRWgRvUXpvE702NWEr7P+KLLvxN5lXTZYnq+K4GKy6TvkfemVU8aOHzS3COhB5 BBSNef59Cp1r31G1MpJP943sAK4dA8ymWfhjdhf/O3lX27aMdv3/cUn6i211wdNn6LC/ 8viaE58eyv22CUIowXwn4XHJnIi9r9+Y3uGA6+AjJkU0YRLjEiPSv2+d8SbsKSMNqn4P 4F+1ef6S/vEhrGAkC1N6JpBtXVAgXfo+wNly0TXo+sz6w/NQD4j1HmMWDypR3MQtlNR9 KhT63RbABfJhMYXWCD/tHD7EzdQ2cdi5avl2cFLSqMwlzaPXo8COEyydzWHEPeZ63+VV 5kmw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=B11nRb1M9ylxyDLHy7hHr/o2CQ0FpLQ5xZPddyL+xJI=; b=FCigsaInoydVHYjqtbTT0z1BV6Br9ZpT1FOI5Bv89gm2mJZDbJ2N79K+VjsWdO+Cse 1BxK1U9x/ywOl65/iK5rTEJwkdvl57m7S7Nq7x7aLANGpxF0skz99AtYP1h9VRzDAzoq QarUfHX77IoXyuc6wUrncaLVXzuJanfs+RCehZzeBzFaKgtKFN6KgEHugCRkgJl+cS8W DfIAF06RSSjEMeafNA0OzQ57V7FaUsmbv/wszWuOfOO14N16UZPf++n8WF8H9cG2/g+X vBxLaSA6H2t2wk9PoemDQujVwItmZ7Me1G2R+wOQvnsz6t31VfncXSAHu5CvcHnNeCpB EQ4w== X-Gm-Message-State: APt69E2nCfscDROwvY8ePlqcBRFYoV5yRpkQqt0bqk+waQMoVAfYy0i2 6YsxLmFLFcxdzEo7ee25TRStRvNks1/EwBzYD+c= X-Received: by 2002:a6b:b845:: with SMTP id i66-v6mr5527134iof.142.1530356056932; Sat, 30 Jun 2018 03:54:16 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a6b:ef03:0:0:0:0:0 with HTTP; Sat, 30 Jun 2018 03:53:36 -0700 (PDT) In-Reply-To: <1530353397-12948-1-git-send-email-laoar.shao@gmail.com> References: <1530353397-12948-1-git-send-email-laoar.shao@gmail.com> From: Yafang Shao Date: Sat, 30 Jun 2018 18:53:36 +0800 Message-ID: Subject: Re: [PATCH net-next] net, mm: avoid unnecessary memcg charge skmem To: David Miller Cc: Johannes Weiner , Michal Hocko , Cgroups , Linux MM , netdev , LKML , Yafang Shao 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 Sat, Jun 30, 2018 at 6:09 PM, Yafang Shao wrote: > In __sk_mem_raise_allocated(), if mem_cgroup_charge_skmem() return > false, mem_cgroup_uncharge_skmem will be executed. > > The logic is as bellow, > __sk_mem_raise_allocated > ret = mem_cgroup_uncharge_skmem > try_charge(memcg, gfp_mask|__GFP_NOFAIL, nr_pages); > return false > if (!ret) > mem_cgroup_uncharge_skmem(sk->sk_memcg, amt); > > So it is unnecessary to charge if it is not forced. > > Signed-off-by: Yafang Shao > --- > include/linux/memcontrol.h | 3 ++- > mm/memcontrol.c | 12 +++++++++--- > net/core/sock.c | 5 +++-- > net/ipv4/tcp_output.c | 2 +- > 4 files changed, 15 insertions(+), 7 deletions(-) > > diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h > index 6c6fb11..56c07c9 100644 > --- a/include/linux/memcontrol.h > +++ b/include/linux/memcontrol.h > @@ -1160,7 +1160,8 @@ static inline void mem_cgroup_wb_stats(struct bdi_writeback *wb, > #endif /* CONFIG_CGROUP_WRITEBACK */ > > struct sock; > -bool mem_cgroup_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages); > +bool mem_cgroup_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages, > + bool force); > void mem_cgroup_uncharge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages); > #ifdef CONFIG_MEMCG > extern struct static_key_false memcg_sockets_enabled_key; > diff --git a/mm/memcontrol.c b/mm/memcontrol.c > index e6f0d5e..1122be2 100644 > --- a/mm/memcontrol.c > +++ b/mm/memcontrol.c > @@ -5929,7 +5929,8 @@ void mem_cgroup_sk_free(struct sock *sk) > * Charges @nr_pages to @memcg. Returns %true if the charge fit within > * @memcg's configured limit, %false if the charge had to be forced. > */ > -bool mem_cgroup_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages) > +bool mem_cgroup_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages, > + bool force) > { > gfp_t gfp_mask = GFP_KERNEL; > > @@ -5940,7 +5941,10 @@ bool mem_cgroup_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages) > memcg->tcpmem_pressure = 0; > return true; > } > - page_counter_charge(&memcg->tcpmem, nr_pages); > + > + if (force) > + page_counter_charge(&memcg->tcpmem, nr_pages); > + > memcg->tcpmem_pressure = 1; > return false; > } > @@ -5954,7 +5958,9 @@ bool mem_cgroup_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages) > if (try_charge(memcg, gfp_mask, nr_pages) == 0) > return true; > > - try_charge(memcg, gfp_mask|__GFP_NOFAIL, nr_pages); > + if (force) > + try_charge(memcg, gfp_mask|__GFP_NOFAIL, nr_pages); > + > return false; > } > > diff --git a/net/core/sock.c b/net/core/sock.c > index bcc4182..148a840 100644 > --- a/net/core/sock.c > +++ b/net/core/sock.c > @@ -2401,9 +2401,10 @@ int __sk_mem_raise_allocated(struct sock *sk, int size, int amt, int kind) > { > struct proto *prot = sk->sk_prot; > long allocated = sk_memory_allocated_add(sk, amt); > + bool charged = false; > > if (mem_cgroup_sockets_enabled && sk->sk_memcg && > - !mem_cgroup_charge_skmem(sk->sk_memcg, amt)) > + !(charged = mem_cgroup_charge_skmem(sk->sk_memcg, amt, false))) > goto suppress_allocation; > > /* Under limit. */ > @@ -2465,7 +2466,7 @@ int __sk_mem_raise_allocated(struct sock *sk, int size, int amt, int kind) > > sk_memory_allocated_sub(sk, amt); > > - if (mem_cgroup_sockets_enabled && sk->sk_memcg) > + if (mem_cgroup_sockets_enabled && sk->sk_memcg && charged) > mem_cgroup_uncharge_skmem(sk->sk_memcg, amt); > > return 0; > diff --git a/net/ipv4/tcp_output.c b/net/ipv4/tcp_output.c > index f8f6129..9b741d4 100644 > --- a/net/ipv4/tcp_output.c > +++ b/net/ipv4/tcp_output.c > @@ -3014,7 +3014,7 @@ void sk_forced_mem_schedule(struct sock *sk, int size) > sk_memory_allocated_add(sk, amt); > > if (mem_cgroup_sockets_enabled && sk->sk_memcg) > - mem_cgroup_charge_skmem(sk->sk_memcg, amt); > + mem_cgroup_charge_skmem(sk->sk_memcg, amt, true); > } > > /* Send a FIN. The caller locks the socket for us. > -- > 1.8.3.1 > Pls. ignore this patch. Sorry about the noise. Thanks Yafang