Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751863AbdH2KCp (ORCPT ); Tue, 29 Aug 2017 06:02:45 -0400 Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:52490 "EHLO mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751828AbdH2KCk (ORCPT ); Tue, 29 Aug 2017 06:02:40 -0400 From: Roman Gushchin To: CC: Roman Gushchin , Johannes Weiner , Michal Hocko , Vladimir Davydov , , , Subject: [PATCH] mm: memcontrol: use per-cpu stocks for socket memory uncharging Date: Tue, 29 Aug 2017 11:01:50 +0100 Message-ID: <20170829100150.4580-1-guro@fb.com> X-Mailer: git-send-email 2.13.5 MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [2620:10d:c092:200::1:64c] X-ClientProxiedBy: VI1P194CA0009.EURP194.PROD.OUTLOOK.COM (2603:10a6:800:be::19) To CO1PR15MB1078.namprd15.prod.outlook.com (2a01:111:e400:7b66::8) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: fd2e16cf-0c55-48a2-b1a6-08d4eec50dcf X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(300000500095)(300135000095)(300000501095)(300135300095)(22001)(300000502095)(300135100095)(2017030254152)(300000503095)(300135400095)(201703131423075)(201703031133081)(201702281549075)(300000504095)(300135200095)(300000505095)(300135600095)(300000506095)(300135500095);SRVR:CO1PR15MB1078; X-Microsoft-Exchange-Diagnostics: 1;CO1PR15MB1078;3:QWWKF9Ljph+8+SrkawZ2/7C8TDBck+SJ+F09Ku2UlXCg52zcMDweI7/WAQJ/mi1LpNnblMjBiUa/20p61T7ac2EGBFsIXlxD7VaSQ2l9d4sQTncflbYwqYPImG1PO/4FEHYfVNO3vg8G97hoHqwRRzc8Mv/BvzP65+X1fMfKPsZg2bnUyMafKV2aDJc45eR8q3vyu2rfk3wECcG3X0093vEY9iaWu10kGIeMDSJVPoA46+ecBX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D9Ob9tTYXMLVhFWhaFlWSk/TW1xu3Jzl7kopyPLqX/bUxvvrZJLR2gRi5iBfWAtUPhEs+V3iNx0oHFCCuE7GWub2LDZiWkR8218hj;4:BaoNpq8CfVnMVAoYNKkJDT4jT0jd9bGYhRAzn/9c0xXCsyXlf16rddMm1GMSEU0mnu00p2BEhBGTpCRaZQUCY1SRFdPwfgRRzsZ2O0ztUW/AAsiyxg+34eyDCaOY4sFW0b0rcvWXAjwXIPtboyeX18XzoBhENyja5ojgTs+6YpF1WezAtqoZmz/I+wA/UJO4Jg5QXKn6tOfeNfQURmqJXbS4NFLPY9pfwnm9CcHwz8cJLNWQ79qLqOzVwGVW2yWlrokl+lPK9xgZ9HjKEDikKrq0mTBiyGWKL2fT07bsii/NToCyuNlGAdxGS3L2t1f9sDs8LnIK0zbDVVZoos8rTA== X-Exchange-Antispam-Report-Test: UriScan:(9452136761055)(67672495146484); X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(100000700101)(100105000095)(100000701101)(100105300095)(100000702101)(100105100095)(6040450)(601004)(2401047)(8121501046)(5005006)(93006095)(93001095)(10201501046)(100000703101)(100105400095)(3002001)(6041248)(20161123562025)(20161123564025)(201703131423075)(201702281528075)(201703061421075)(201703061406153)(20161123560025)(20161123555025)(20161123558100)(6072148)(201708071742011)(100000704101)(100105200095)(100000705101)(100105500095);SRVR:CO1PR15MB1078;BCL:0;PCL:0;RULEID:(100000800101)(100110000095)(100000801101)(100110300095)(100000802101)(100110100095)(100000803101)(100110400095)(100000804101)(100110200095)(100000805101)(100110500095);SRVR:CO1PR15MB1078; X-Forefront-PRVS: 0414DF926F X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10019020)(4630300001)(7370300001)(6009001)(199003)(189002)(81166006)(8676002)(81156014)(106356001)(50226002)(33646002)(7736002)(305945005)(105586002)(189998001)(47776003)(101416001)(5660300001)(97736004)(478600001)(86362001)(68736007)(50466002)(36756003)(48376002)(42186005)(69596002)(53936002)(110136004)(50986999)(4326008)(25786009)(53416004)(54906002)(6512007)(5003940100001)(6116002)(2361001)(2351001)(6916009)(7350300001)(2906002)(6666003)(6506006)(1076002)(6486002)(42262002);DIR:OUT;SFP:1102;SCL:1;SRVR:CO1PR15MB1078;H:castle.thefacebook.com;FPR:;SPF:None;PTR:InfoNoRecords;A:1;MX:1;LANG:en; X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1;CO1PR15MB1078;23:kQWcLjRtimhSNs34yTyNRRThOVYdbvBWNuRe69q+z?= =?us-ascii?Q?27fjmgFpAUa5GD9xDfc18QHOp4LggALJ/ySZDeVw26fBpZj5jtzNvCjlpnCL?= =?us-ascii?Q?0AKFK4nlM/tgX/eX7Q1W6pDhhQRoFmEOfEQ217PXuznd4FkSZFGUjo2gOGam?= =?us-ascii?Q?zjoBb/EcqzIiZmvBd1NBhFLdIYmozYOUoCT4XLgRkW0OGJvQDwBGDjJHvJ8f?= =?us-ascii?Q?g/s0Wgrvsg25zCQouLfFdiiHcB4ENqig4S5xdvATNWXFMl7Na0WCw544hn8B?= =?us-ascii?Q?SAQzqcLRZL2zp8EURR3F7iAbwgqr6OgAoT+7XVZ9NX/YaH3mknpmn+7PB5wD?= =?us-ascii?Q?l6k+zGjG/TaxW4UrC0aVZsNFkWjxBAzRNmhvohkxWLws6UXXUXJ39Ep7BMiP?= =?us-ascii?Q?XXp+D5WRGLM45iHEUFhEIX+T8rdT/3Ejtl2owDH0a1YAsOEAXx/YyKsDKbAo?= =?us-ascii?Q?rhJjeQuPc1uMuKMn9YthFCFbBpp9POyEL8CQSMI/yytsc1bihxfpPYYcTyj6?= =?us-ascii?Q?ycpz6+NR+Hp0qwrDv4e8Zg0cztDw9Ov3bSJgumKSE0XXI3DJxTigxMlCGD0b?= =?us-ascii?Q?Siyyrbmr4IcyLb466Jbg1wJNwVpt+tPs8Sq+T1ZT8TdlGI/IrSIh2wxdD3ms?= =?us-ascii?Q?4pHLb/c+8JpLarp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MUzpbnsKjDOM0rsCEso9qQalRgvNrCYHZrUPqH0BcpFrnOUi51vQiN9Rg==;24:mS9ToFd7p+f9wOOHKXzuZe5mZAUff+8mTt9XFHARsyNOF/GSHM9nS4yfWvK6CvXH559mwVE5AWF48miUzg/4P/ZkBbZbQRCWePuEbLHrJFo=;7:TBjh8A+0DSlbNUkhoQKoTOvzPrAGOlsHVB/EGmJTclO9W+16mQB/Zls+jtPtJeGk2X6YAagHabJj+m+QilcQjxKy0vQcTEyID4yAs/X9vBzlw4novd6fVdxmFW0Adu8/+QjlcnpdGcGCXdWzKZ625UXmBuPYOQe5gUbMDkB3vU1mXFKWxsf7xaa2RXYdCCs6U0VOHVYCJuRlCLhgpZo/A3baIG8or4QNIIuu4qVtNGw= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1;CO1PR15MB1078;20:gqmApf16KNV8jfWRDAJY8T8V/ld8FZM0ZwbUb5LC6RmM+5bVWVWu06YDgMw1enTwYFGS6I01NDYCy3yFD8fXZtedWOtfItRNyt38gvejnyua4YnmzrgOent292ijgVRg28bmJEz0CHpJNM44lD+DQ5+rNWvyS9Tji3Gn5Re3tTY= X-MS-Exchange-CrossTenant-OriginalArrivalTime: 29 Aug 2017 10:02:25.3764 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: CO1PR15MB1078 X-OriginatorOrg: fb.com X-Proofpoint-Spam-Reason: safe X-FB-Internal: Safe X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:,, definitions=2017-08-29_03:,, signatures=0 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 3157 Lines: 87 We've noticed a quite sensible performance overhead on some hosts with significant network traffic when socket memory accounting is enabled. Perf top shows that socket memory uncharging path is hot: 2.13% [kernel] [k] page_counter_cancel 1.14% [kernel] [k] __sk_mem_reduce_allocated 1.14% [kernel] [k] _raw_spin_lock 0.87% [kernel] [k] _raw_spin_lock_irqsave 0.84% [kernel] [k] tcp_ack 0.84% [kernel] [k] ixgbe_poll 0.83% < workload > 0.82% [kernel] [k] enqueue_entity 0.68% [kernel] [k] __fget 0.68% [kernel] [k] tcp_delack_timer_handler 0.67% [kernel] [k] __schedule 0.60% < workload > 0.59% [kernel] [k] __inet6_lookup_established 0.55% [kernel] [k] __switch_to 0.55% [kernel] [k] menu_select 0.54% libc-2.20.so [.] __memcpy_avx_unaligned To address this issue, the existing per-cpu stock infrastructure can be used. refill_stock() can be called from mem_cgroup_uncharge_skmem() to move charge to a per-cpu stock instead of calling atomic page_counter_uncharge(). To prevent the uncontrolled growth of per-cpu stocks, refill_stock() will explicitly drain the cached charge, if the cached value exceeds CHARGE_BATCH. This allows significantly optimize the load: 1.21% [kernel] [k] _raw_spin_lock 1.01% [kernel] [k] ixgbe_poll 0.92% [kernel] [k] _raw_spin_lock_irqsave 0.90% [kernel] [k] enqueue_entity 0.86% [kernel] [k] tcp_ack 0.85% < workload > 0.74% perf-11120.map [.] 0x000000000061bf24 0.73% [kernel] [k] __schedule 0.67% [kernel] [k] __fget 0.63% [kernel] [k] __inet6_lookup_established 0.62% [kernel] [k] menu_select 0.59% < workload > 0.59% [kernel] [k] __switch_to 0.57% libc-2.20.so [.] __memcpy_avx_unaligned Signed-off-by: Roman Gushchin Cc: Johannes Weiner Cc: Michal Hocko Cc: Vladimir Davydov Cc: cgroups@vger.kernel.org Cc: kernel-team@fb.com Cc: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org --- mm/memcontrol.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index b9cf3cf4a3d0..a69d23082abf 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -1792,6 +1792,9 @@ static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages) } stock->nr_pages += nr_pages; + if (stock->nr_pages > CHARGE_BATCH) + drain_stock(stock); + local_irq_restore(flags); } @@ -5886,8 +5889,7 @@ void mem_cgroup_uncharge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages) this_cpu_sub(memcg->stat->count[MEMCG_SOCK], nr_pages); - page_counter_uncharge(&memcg->memory, nr_pages); - css_put_many(&memcg->css, nr_pages); + refill_stock(memcg, nr_pages); } static int __init cgroup_memory(char *s) -- 2.13.5