From patchwork Sat Jun 30 10:09:57 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 10498095 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 75638603B4 for ; Sat, 30 Jun 2018 10:11:04 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4CC9A291F7 for ; Sat, 30 Jun 2018 10:11:04 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3E17B2920D; Sat, 30 Jun 2018 10:11:04 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AAB99291F7 for ; Sat, 30 Jun 2018 10:11:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1FB0A6B0003; Sat, 30 Jun 2018 06:11:02 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 1AADE6B0005; Sat, 30 Jun 2018 06:11:02 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 09E4E6B0006; Sat, 30 Jun 2018 06:11:02 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pf0-f197.google.com (mail-pf0-f197.google.com [209.85.192.197]) by kanga.kvack.org (Postfix) with ESMTP id B9E166B0003 for ; Sat, 30 Jun 2018 06:11:01 -0400 (EDT) Received: by mail-pf0-f197.google.com with SMTP id l2-v6so5785537pff.3 for ; Sat, 30 Jun 2018 03:11:01 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:from:to:cc:subject:date :message-id; bh=0fBxVKiB6P7zFYgm5gVeN4Vt+kQXtyxhsPRM1/V59iQ=; b=oerbpdGQ7vCmL3FRqfcmTWbqIIKhzMNgmQ1NkEc1kFV+2yxEz6bbvuzz+Q4jSc4u67 w64THUE8o495oZriCk0LZp8QQ7XmSLWOtadC3lEzQXJWOiiIY4LXJdC+51glgCiMhEah ezYgLQ30un0zwbH1zIakvqySZtYDpiYZngMT10EmiVUP1hr7jWdWohfN6KqkD7B+pTie hBuii8G1Ajdqg8hUfqMMF2HRv81nzzgau1iMVObs1QSMdLFlIBpz5lnhjdXYrE+Mh+qO oSHboNjKTmw0hgRZe0YPhuBnRQt8nnh3zy8wYwounQ7JD/hP9qUA61+7i5Hjc1iUYk1l 9FVQ== X-Gm-Message-State: APt69E2DCFPU5GFcIHmMNGd4D6m6ODuq7UkrY6W19Ugv65+zm5XmqvzS 9BlWCKKj/4q/yvv7e4BPJCUCxJ0YjCHSg37uuS8ZkeX8bVJz014xQr+ifn0YqG5AZSamAJWkKih YINAZpKal94nI9aMs03A5w+fqUdKprlF5ldgrEt53KcLA6xKoR7RPME/UUznnKGhkxZs7MUNkWG asBN8+H7t6/Y7v/aaBRY9JI34j7EZzaz5dTHfh8NBlUmd84y1kRceguJzQ/2FEveTUBo5IQaItJ GVtiGdgfmNee3y8Exae5bJ3TovmmuTbILlPDqcu3vT79I2NCWWkdhVi6jY4HfPM6aQKKlxuERSj 1A30F4dUgZjsjhNYG877SJsCRYvJ65lCo2ti56yPH85QkfeyWy+KdFtGV1Hi2fSc99mfY3kQiAY q X-Received: by 2002:a17:902:b946:: with SMTP id h6-v6mr17846347pls.1.1530353461403; Sat, 30 Jun 2018 03:11:01 -0700 (PDT) X-Received: by 2002:a17:902:b946:: with SMTP id h6-v6mr17846281pls.1.1530353459865; Sat, 30 Jun 2018 03:10:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1530353459; cv=none; d=google.com; s=arc-20160816; b=b1yLk+vwm0jMQvQ7chFfLVNi+FDk20PwgBS5FA7MPhio4I0jZi1Klg9vEMGAgHM+RM zCrEEPHaPmtWHd0Jy/NbOrTftu4Lq6eu4f7YiHq58lVNl6tzwjTYrKPdXxokDv+Hs6lU DyW262zsb+k1Elnbql/qi2PCVOAZs02jmB4uMrZcmCGBZaEs1LPkXQpHuwrqpI1ss6cg 3PS6IN2+GTBzniO22AMqVD7ixHi8y18IkhDL88WN5r2sOlaP+W6xAT08p1Ut9S8qdwqE qwJWunbR4hILFIG1AjsURX8QuyyasMuNl5Z/bBK7Qhlmo0hMnxAMqh7bClS2/Febzesz OBuw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=message-id:date:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=0fBxVKiB6P7zFYgm5gVeN4Vt+kQXtyxhsPRM1/V59iQ=; b=vliJUXdEVekjC4LfloO23F16D2QOIGXJnzfJedEMlMo+NePg1yT2P7bleq9oDrr+Ze LFR9DIbVSG2i1asGDT20Ep40LoMdjGSS9je7fpjuywZbwZgzb1K49lFawpY+tW48RakP GExcJhVPw/LMp32FrJU2p7uwJJjhWndoMoxIIaZC/9dFVUT/c3454QJhdXv3ksTTsudl qhEWeb1gfpNmGwecC2njYsBGDc9YMNS06cBSKUv+90cqBQQvrDkCnu3hleNkAhrcSIx5 RC1cQ4iHBUagRpinX4CmZOmOm97iBfg86rMzdaTVq/GYSPvVOL0/jjScYDp5+0T6VY7U mAaQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=R8OvG1zQ; spf=pass (google.com: domain of laoar.shao@gmail.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id m21-v6sor1594093pfg.107.2018.06.30.03.10.59 for (Google Transport Security); Sat, 30 Jun 2018 03:10:59 -0700 (PDT) Received-SPF: pass (google.com: domain of laoar.shao@gmail.com designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=R8OvG1zQ; spf=pass (google.com: domain of laoar.shao@gmail.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com 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-Smtp-Source: AAOMgpchzgFFiW4iyp9t8Gf6uv2ln2LBFTK0ejWGYTiCvc0z8SjV7sdC5086ZWNcY43Jmk4CMC5afg== 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 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP 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.