Received: by 2002:a05:7412:da14:b0:e2:908c:2ebd with SMTP id fe20csp2206356rdb; Mon, 9 Oct 2023 17:10:13 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFiNjVakfAdbVyKTdTdlqC1Pd/Yae96887U0FjEVtOKgtXzB/M+cYbgqnC98VQP5h8BfhyZ X-Received: by 2002:a17:902:b48c:b0:1c0:a5c9:e072 with SMTP id y12-20020a170902b48c00b001c0a5c9e072mr12746668plr.11.1696896612996; Mon, 09 Oct 2023 17:10:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696896612; cv=none; d=google.com; s=arc-20160816; b=GD+gAecDBmVOSEPjbYAymlKRVsbQM22diKxRyOO3UGRh5sRpYPtLSXZtpVSGxw6qF+ NCw9PCt9Is/laeqrM0JjfeIsyPFr+SbkuWOCfZDFaCD621UHfywjvYVI/QYpPAbzco5H Zd4m4sodTK/1hLPR3Q/4tuJdzshYEcUq/Fo8cEd9lmdk5N1GI0I04T/lIjldHLeBDri8 I7QAaAaxvHdmAzkmd7el0/xhlc5l2RGTseo/3qmJfvrBFLnGQhpBy8gnBL+LDx+XrriT EfyYVA4gDRs2aO8DyswT0hDSJfzd7zaqh30IjtDGwv6NSSGp/WlAJb9WYxJMg5ewJ+NF bLfw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=Zu2srZGRza0i12D/f9sOSWHgmsyy71BOzkS7lMMPjis=; fh=eIV/wktpjV+EeUDLeXhmhAmcuYEtVvgOWNik/7Ld4iQ=; b=g1mll8EtyFTHOfev8eN5ILNkR/t+KG4E3mGi42EuzBKheGZbDZpVsCZlNdpzh1tIx1 mbSZ63VQudyF4ZSWXFhOWlPC66buBoCaJ3BEqYT7aNBMoDy53kQ2IfQ0ijmYoka1WLlc wQXTQfBXB7gyImkWPWV7iTnctsDMkifT+x6w9SIthRKvSLgAEjIRQVW+gQ1Dv6VuvySt yEnRZxNFopUVVU8ha86ZBwK0sHf60Kj/l5QlYx5civHvwRg6TX2pmGSQ/s4jhvVu1jh2 PrpbhiOdBXa8MtbuVJpv2JL8j6dXtgFxrLs25Qd91UFipFFD5ScjfDGI2xJmL9I4dYwO 3iWA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=DnHfbVma; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id h12-20020a170902f7cc00b001c604f8de9fsi10137656plw.631.2023.10.09.17.10.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Oct 2023 17:10:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=DnHfbVma; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 5BEA081DAAC5; Mon, 9 Oct 2023 17:10:09 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1379141AbjJJAKC (ORCPT + 99 others); Mon, 9 Oct 2023 20:10:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42680 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1379138AbjJJAKB (ORCPT ); Mon, 9 Oct 2023 20:10:01 -0400 Received: from out-210.mta0.migadu.com (out-210.mta0.migadu.com [91.218.175.210]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 12F4EAC for ; Mon, 9 Oct 2023 17:09:59 -0700 (PDT) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1696896596; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=Zu2srZGRza0i12D/f9sOSWHgmsyy71BOzkS7lMMPjis=; b=DnHfbVmaqpEspZYM0pD00NCFUZGLSVOOtcdBPfHVbSOARxf2o1fJ/qt7mqIbtSIPUqM1Xs AlBwkqXu83Dk3Ms55eJaUyr5vrS7cGAm3R0CItbKKNrFTRdhXFx9Lg2iXrue1HccXLZJDr mkrBXCUahp/34s8KWx+jyweMYmAnjR8= From: Roman Gushchin To: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, Johannes Weiner , Michal Hocko , Shakeel Butt , Muchun Song , Dennis Zhou , Andrew Morton , David Rientjes , Vlastimil Babka , Naresh Kamboju , Roman Gushchin Subject: [PATCH v2 0/5] mm: improve performance of accounted kernel memory allocations Date: Mon, 9 Oct 2023 17:09:24 -0700 Message-ID: <20231010000929.450702-1-roman.gushchin@linux.dev> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT X-Spam-Status: No, score=2.7 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, RCVD_IN_SBL_CSS,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Mon, 09 Oct 2023 17:10:09 -0700 (PDT) X-Spam-Level: ** This patchset improves the performance of accounted kernel memory allocations by ~30% as measured by a micro-benchmark [1]. The benchmark is very straightforward: 1M of 64 bytes-large kmalloc() allocations. Below are results with the disabled kernel memory accounting, the original state and with this patchset applied. | | Kmem disabled | Original | Patched | Delta | |-------------+---------------+----------+---------+--------| | User cgroup | 29764 | 84548 | 59078 | -30.0% | | Root cgroup | 29742 | 48342 | 31501 | -34.8% | As we can see, the patchset removes the majority of the overhead when there is no actual accounting (a task belongs to the root memory cgroup) and almost halves the accounting overhead otherwise. The main idea is to get rid of unnecessary memcg to objcg conversions and switch to a scope-based protection of objcgs, which eliminates extra operations with objcg reference counters under a rcu read lock. More details are provided in individual commit descriptions. v2: - fixed a bug discovered by Naresh Kamboju - code changes asked by Johannes (added comments, open-coded bit ops) - merged in a couple of small fixes v1: - made the objcg update fully lockless - fixed !CONFIG_MMU build issues rfc: https://lwn.net/Articles/945722/ -- [1]: static int memory_alloc_test(struct seq_file *m, void *v) { unsigned long i, j; void **ptrs; ktime_t start, end; s64 delta, min_delta = LLONG_MAX; ptrs = kvmalloc(sizeof(void *) * 1000000, GFP_KERNEL); if (!ptrs) return -ENOMEM; for (j = 0; j < 100; j++) { start = ktime_get(); for (i = 0; i < 1000000; i++) ptrs[i] = kmalloc(64, GFP_KERNEL_ACCOUNT); end = ktime_get(); delta = ktime_us_delta(end, start); if (delta < min_delta) min_delta = delta; for (i = 0; i < 1000000; i++) kfree(ptrs[i]); } kvfree(ptrs); seq_printf(m, "%lld us\n", min_delta); return 0; } -- Signed-off-by: Roman Gushchin (Cruise) Roman Gushchin (5): mm: kmem: optimize get_obj_cgroup_from_current() mm: kmem: add direct objcg pointer to task_struct mm: kmem: make memcg keep a reference to the original objcg mm: kmem: scoped objcg protection percpu: scoped objcg protection include/linux/memcontrol.h | 14 ++- include/linux/sched.h | 4 + mm/memcontrol.c | 204 ++++++++++++++++++++++++++++++++----- mm/percpu.c | 8 +- mm/slab.h | 10 +- 5 files changed, 202 insertions(+), 38 deletions(-) -- 2.42.0