Received: by 2002:ac0:a581:0:0:0:0:0 with SMTP id m1-v6csp1738411imm; Sat, 30 Jun 2018 03:11:58 -0700 (PDT) X-Google-Smtp-Source: AAOMgpdfcbudClveYBhAAch2akcUh4W+6KmPIMhmtXkCwZ/Y/xTeoiU1L42W1xCCCujXqIrgfFzC X-Received: by 2002:a62:4b48:: with SMTP id y69-v6mr6608589pfa.93.1530353517939; Sat, 30 Jun 2018 03:11:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1530353517; cv=none; d=google.com; s=arc-20160816; b=pFA9v3xW5yrPCPalj6AXu21gJSsnBQy39xjt0Gx8jxGcCN4+qM+lKuugu6mYLbi1hZ wJwM7tUgl78UMTP/hXRES6VIBVC5s1FxTGiBBW9JxK7OTCzUR0MsKR7Mq1vx2FnpXfM8 Kpjica99O/pFkQtWiiDn+okEbZeN8e0ksj8jQDUrN08NGg61SOGyFIwxxghhkzww83Y7 CTL1R3S7iVkzzqjdTHOxsR8U8nHPdqW7sBdfjKV74dBwmZD59/MaWMINVxlHD6auhmN2 fVCbmZWs/M3/HeoqHzKPbblXNS30Dex/YNPcXOaEtcvkwJwIlHeExbGEyI0n30nmHQU0 LOsA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=0fBxVKiB6P7zFYgm5gVeN4Vt+kQXtyxhsPRM1/V59iQ=; b=lahmshox9DoYLTdxBbVE2hjNNiNVE+J8qAdeQTNbgLAIBlzBCpRq1opBBxMIO5KMbU cmD8dPGG8OMjLqINOhNm/2DA2aRXEnLQVPRTmJUigp3UfLOMtmZr/pwOZOkefS0t2XGi e8WiG2C9amVUsiO2VRDi5Zk5O5YqD2Hr8oRJY9fh/HnHL0zWhfkp4OEjXi46V16w81Vr 9NSGWuo9KEIp1OaLMYJWJqxR2au1KHmw/iwu/nYuzpIU3OViQ0hA0DMdizB2HE2M1Q0u vDbIuZDwZwj8kkgh5LhhdSO9prOwZvtvHPKws5R/KRylc3/Ct0liqfLidYhaZpUAdNIo Nbxw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=R8OvG1zQ; 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 f13-v6si9694626pgv.374.2018.06.30.03.11.43; Sat, 30 Jun 2018 03:11:57 -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=R8OvG1zQ; 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 S936662AbeF3KLC (ORCPT + 99 others); Sat, 30 Jun 2018 06:11:02 -0400 Received: from mail-pf0-f195.google.com ([209.85.192.195]:46630 "EHLO mail-pf0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S934675AbeF3KK7 (ORCPT ); Sat, 30 Jun 2018 06:10:59 -0400 Received: by mail-pf0-f195.google.com with SMTP id l123-v6so2265382pfl.13; Sat, 30 Jun 2018 03:10:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=0fBxVKiB6P7zFYgm5gVeN4Vt+kQXtyxhsPRM1/V59iQ=; b=R8OvG1zQXGpBJ5lB5gdhIdL00tjoUfwHWN4VbIOzdLtwXjlsZznNSZzXZID+Nl+Z+D XlWjYW1C7ZWfjpRSWccxMCt4jjp2Dcv7mOCDrmIgsaQ+3yiUOHQvg+8xwRL/eJ6OB+05 MK2oKTe29PqwBHdiFzW061Q4ym76zrTmfnLcgIpGL9q5XVEelKB/f6sunZ0IWW8l58HF xvxBo4Q+aApxYJ74lMZxmCDwB84eLq5HQPpJzEEhERiu4CUy93guwBvqhCmkxwLhhAsy rupLaJQPkS5ANiXMNQjjysnpSvPI4t1H5704VZsnp589b+MNMSd8qiZFMK9BGXmCkCUr eL+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=0fBxVKiB6P7zFYgm5gVeN4Vt+kQXtyxhsPRM1/V59iQ=; b=r1deIYrRq9Et2nUg7Mex0Yje7xtERndqV2nBLNNpzYc4N5A+UNUX0z1fM2uB5T86TZ CibbirO62RVUW+MIMcRzGTPNQsfEKPDmYW/5G0jwh06qHVz/wbgTsHylDUb0hhlg+5BP Kyd1xHl+hLB2iGe5GnA5oqu0xowlFkNKv3q2DvFvvwLyo8syErPTtu4TWMos/lwyF8JV ROauup+QaxA/wR1EkgX73ejDOW97VYF2Fht4HpJrxacey05RBA7ODeokEA0JjgnEe0+S I6M5uXvc7g0BQOeY0qKKu6miIcoh+E5PMCoyXqltu62j7nnruXRXRjywH3eksioXlWpn ZvdQ== X-Gm-Message-State: APt69E1xMrRsRMw64weO+RgpxjK8JqYLQy39zBbAlutLf3BwQOHXfB0M VUxQjm0UlX3EzdwJU7Nfh6D50Phu X-Received: by 2002:a62:ea14:: with SMTP id t20-v6mr17929011pfh.117.1530353459302; Sat, 30 Jun 2018 03:10:59 -0700 (PDT) Received: from li1588-6.members.linode.com (li1588-6.members.linode.com. [139.162.104.6]) by smtp.gmail.com with ESMTPSA id g136-v6sm5589194pfb.23.2018.06.30.03.10.57 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 30 Jun 2018 03:10:58 -0700 (PDT) From: Yafang Shao To: davem@davemloft.net Cc: hannes@cmpxchg.org, mhocko@kernel.org, cgroups@vger.kernel.org, linux-mm@kvack.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Yafang Shao Subject: [PATCH net-next] net, mm: avoid unnecessary memcg charge skmem Date: Sat, 30 Jun 2018 18:09:57 +0800 Message-Id: <1530353397-12948-1-git-send-email-laoar.shao@gmail.com> X-Mailer: git-send-email 1.8.3.1 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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