From patchwork Fri Apr 19 17:56:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jianfeng Wang X-Patchwork-Id: 13636702 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 83427C04FF6 for ; Fri, 19 Apr 2024 17:56:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D41F76B0088; Fri, 19 Apr 2024 13:56:20 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CC8246B0089; Fri, 19 Apr 2024 13:56:20 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B42D46B008A; Fri, 19 Apr 2024 13:56:20 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 838C86B0088 for ; Fri, 19 Apr 2024 13:56:20 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 09417A196E for ; Fri, 19 Apr 2024 17:56:20 +0000 (UTC) X-FDA: 82027035720.26.4F3D0F7 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by imf02.hostedemail.com (Postfix) with ESMTP id 0587A8001A for ; Fri, 19 Apr 2024 17:56:17 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b=j4j7oaQr; spf=pass (imf02.hostedemail.com: domain of jianfeng.w.wang@oracle.com designates 205.220.165.32 as permitted sender) smtp.mailfrom=jianfeng.w.wang@oracle.com; dmarc=pass (policy=quarantine) header.from=oracle.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1713549378; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=bsbvgyQd3N2o4z+XaeSxWb9HErpReSjR0K5j0Z+qNLg=; b=iWBotTFWC2r6MYa6vhP9m2AaMPTN92BwMipbQ/jDXFR1dewi1OtEjYBYJkUYvkGADQlBmg 4ZaPW0DC20zHy7o3deKOAXpenoS0CZDxKv+XGn2mswYDoxXnChXAaADmyZqbqpN5vVJPM2 z+IADy5X61gf39YNSlB0fSPZSawLJow= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1713549378; a=rsa-sha256; cv=none; b=jBnbUWzvP2hnuIehZwdDEBxSlLG7lgRbaWpNhD2d5gKdve7xdfYaQpek4HVYfDk8K6UXK6 OVviZWNiXgQ5hy+lapv5UwOrjIOLhfnm3dtZ5LArczWTX+fhuulakJJurRwzynR1CHGWJU N7CsSs9dO4KYwol709C8vfH0fQ1LHbU= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b=j4j7oaQr; spf=pass (imf02.hostedemail.com: domain of jianfeng.w.wang@oracle.com designates 205.220.165.32 as permitted sender) smtp.mailfrom=jianfeng.w.wang@oracle.com; dmarc=pass (policy=quarantine) header.from=oracle.com Received: from pps.filterd (m0333521.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 43JGiiJF007533; Fri, 19 Apr 2024 17:56:14 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding; s=corp-2023-11-20; bh=bsbvgyQd3N2o4z+XaeSxWb9HErpReSjR0K5j0Z+qNLg=; b=j4j7oaQrnxmDcqnDNWQW1YUe0vnk2KwsvXDsIaBhwTGzycPx04GWqXaN/TyFLsD6MokI XWnAEaFW/CXcDX0XfTKZKrUZ2XnQ+LgMVS4c6K8ZrMqzSNUFAhUbtOnDUL45KkOvb/wC fHO8F9+ITZ1jo1xinSVbWcY9oBfclkGARDN3y1avJIANwqjIS4TSQKDcw7A/WEVZVEp+ WjqueqDZnjTvX7zr6xS40TjiWwLL9j9tUUEzgn0jN8lqUMzTlVF743Ujs7S8zfn7CVjD WH+cNvFDX0aMdHLgqaUoK5qKoNYqpKOAsmmIAw9xSkZjhaGyZeQbZGAsVaxGUOhREaMw mA== Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.appoci.oracle.com [138.1.114.2]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3xfgycwer1-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 19 Apr 2024 17:56:13 +0000 Received: from pps.filterd (phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (8.17.1.19/8.17.1.19) with ESMTP id 43JH48SI005568; Fri, 19 Apr 2024 17:56:12 GMT Received: from jfwang-mac.us.oracle.com (dhcp-10-159-230-131.vpn.oracle.com [10.159.230.131]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTP id 3xkc7xd76r-2; Fri, 19 Apr 2024 17:56:12 +0000 From: Jianfeng Wang To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: vbabka@suse.cz, cl@linux.com, akpm@linux-foundation.org, penberg@kernel.org, rientjes@google.com Subject: [PATCH v3 1/2] slub: introduce count_partial_free_approx() Date: Fri, 19 Apr 2024 10:56:10 -0700 Message-ID: <20240419175611.47413-2-jianfeng.w.wang@oracle.com> X-Mailer: git-send-email 2.42.1 In-Reply-To: <20240419175611.47413-1-jianfeng.w.wang@oracle.com> References: <20240419175611.47413-1-jianfeng.w.wang@oracle.com> MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1011,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2024-04-19_13,2024-04-19_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=999 bulkscore=0 malwarescore=0 suspectscore=0 spamscore=0 adultscore=0 phishscore=0 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2404010000 definitions=main-2404190137 X-Proofpoint-ORIG-GUID: GxkjSI6Zb372cMx3xe4mFXOWggEqqXOn X-Proofpoint-GUID: GxkjSI6Zb372cMx3xe4mFXOWggEqqXOn X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 0587A8001A X-Stat-Signature: i6s5ryw5egiafannxz8oxtfpxsmsq3uu X-Rspam-User: X-HE-Tag: 1713549377-969764 X-HE-Meta: U2FsdGVkX1+XByrYD7pDqHTCAri5ZStAOOec6wyZN9uZiglWV1RxYVbtWoBRR9W5AcVQbJrjtQhSxbGY74xhcQPPvNwOR2eBk8Ib3zC1+OeXmgW6OlBFm8JJaCNdGmEUSJ89gSGcGK3si4kJGCyvOgR0r8BpIbIUZRdHrwxyxCaopHF/AcvUIMie4nx8bJAYDtICdb0o+wSak1pvlQo2h5xPsMjFiHXRtWirC0xgXocPNFfoOZgeJy3x3ZQ11flJ6g01C20xHxpfoX8DcCBA5l9aOztuBiN9AG273DCB6klm1Q/xX6tVHQbNnUX7D19DUGpOXipsVrhkNsrf0hrn1bWSdFMfX9kmxXW2ZI/f8Za8ZvlU0mCOqsTEG+SX5WL9M1utTLf7wQWy5vRziyn3LHlCJzIju6s+IgJJEDReA88tsGLmReTzQ9pYVT39+4Te23SltUxRl+5D8+MhxSvxge5U61bah8whThWwrn/Us2khnEDstXg/EbMhZ3GXQtXPgmNNwOz/LTxz8RuWcRxt/gCNz2SoIlVBBJnZq2v0uI131Pxlm3RL1ezdOUWHnDQ7OyQqmg5oNyuxVGIKRbTW5PYZ2kD4+ZswrVS1Ew6hRK7y+DhA3wyG0oxaqCQdCAENpCU46iU9XVM3YCXvpRKm/lk4PxksuzsIg1oGdUN4R5jN7Oy4XdGdTEsT5e+CoTwtMlE6YwM/NCV3OpSX7ZkQQKDxTjvuoynrkgMGg4+yu13q4GNNbqyVEW4LXqmI0w/ox5WBVjlLIlyqo6/MY8W/AI9+75/+dvpDKAqX6WzWh2f97boTfCdNOw2Jepo/zKYwV/9/wE31h2fYLoBIRSr4rKEWRos1h7Obqors3tw+R/tBVJ9MdiP9EW4bWbiMcvYQBziSVygN3/eX/LC1ICq9G5NsaKn8FTkEHlgF8T4Pr+qsVKsdRSq+iuHcU5c7a604yyfsx+3kmEMuwNkyQ7v 06xN2h3l 4f36hPRzvBLFHpQAbNIENkkbpZuvoNEQVnskoHdbuoqObS6GqHklAFIPTP6EvrBAmHRGxT1Y1IyXzZ9ZeaNELiqE61HgHoZz/ovzQx6ytLde9K2QNU2F61PqWcQc4EmJ9v5HHV5rwjPKNmnrdx3qftbVf6XVqBSYELeJtqS8A3OW8svOjXBmJ3e1EEmHX9GdJyWvsacrii12LJh6LyYeniGRKezUEfu/C2rNiVEWoRL3RTp4+aPJBHn26+nF6I3mjvZeXMX0xeBWXY6GRB6Kt7DO9Ogxo55RxGSbmXQ3uK8q6sQG4SrPCW5owf/KNjwdTVm55qkraRUiVM/QyySiPFaL85TQ/u/1216Q6m1B/3VQrjw3SBjeK3R0xVNPidWxv18NmzsAhSghSiZjpNeDUKtqIJSmHaeGeKUL2mr/PUCPhZRql/XD/72Pr4w== 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: List-Subscribe: List-Unsubscribe: When reading "/proc/slabinfo", the kernel needs to report the number of free objects for each kmem_cache. The current implementation uses count_partial() to get it by scanning each kmem_cache_node's partial slab list and summing free objects from every partial slab. This process must hold per-kmem_cache_node spinlock and disable IRQ, and may take a long time. Consequently, it can block slab allocations on other CPUs and cause timeouts for network devices, when the partial list is long. In production, even NMI watchdog can be triggered due to this matter: e.g., for "buffer_head", the number of partial slabs was observed to be ~1M in one kmem_cache_node. This problem was also confirmed by others [1-3]. Iterating a partial list to get the exact count of objects can cause soft lockups for a long list with or without the lock (e.g., if preemption is disabled), and may not be very useful: the object count can change after the lock is released. The approach of maintaining free-object counters requires atomic operations on the fast path [3]. So, the fix is to introduce count_partial_free_approx(). This function can be used for getting the free object count in a kmem_cache_node's partial list. It limits the number of slabs to scan and avoids scanning the whole list by giving an approximation for a long list. Suppose the limit is N. If the list's length is not greater than N, output the exact count by traversing the list; if its length is greater than N, output an approximated count by traversing a subset of the list. The proposed method is to scan N/2 slabs from the list's head and N/2 slabs from the tail. For a partial list with ~280K slabs, benchmarks show that it performs better than just counting from the list's head, after slabs get sorted by kmem_cache_shrink(). Default the limit to 10000, as it produces an approximation within 1% of the exact count for both scenarios. Then, use count_partial_free_approx() in get_slabinfo(). Benchmarks: Diff = (exact - approximated) / exact * Normal case (w/o kmem_cache_shrink()): | MAX_TO_SCAN | Diff (count from head)| Diff (count head+tail)| | 1000 | 0.43 % | 1.09 % | | 5000 | 0.06 % | 0.37 % | | 10000 | 0.02 % | 0.16 % | | 20000 | 0.009 % | -0.003 % | * Skewed case (w/ kmem_cache_shrink()): | MAX_TO_SCAN | Diff (count from head)| Diff (count head+tail)| | 1000 | 12.46 % | 6.75 % | | 5000 | 5.38 % | 1.27 % | | 10000 | 4.99 % | 0.22 % | | 20000 | 4.86 % | -0.06 % | [1] https://lore.kernel.org/linux-mm/ alpine.DEB.2.21.2003031602460.1537@www.lameter.com/T/ [2] https://lore.kernel.org/lkml/ alpine.DEB.2.22.394.2008071258020.55871@www.lameter.com/T/ [3] https://lore.kernel.org/lkml/ 1e01092b-140d-2bab-aeba-321a74a194ee@linux.com/T/ Signed-off-by: Jianfeng Wang Acked-by: David Rientjes --- mm/slub.c | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/mm/slub.c b/mm/slub.c index 1bb2a93cf7b6..993cbbdd2b6c 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -3213,6 +3213,43 @@ static inline bool free_debug_processing(struct kmem_cache *s, #endif /* CONFIG_SLUB_DEBUG */ #if defined(CONFIG_SLUB_DEBUG) || defined(SLAB_SUPPORTS_SYSFS) +#define MAX_PARTIAL_TO_SCAN 10000 + +static unsigned long count_partial_free_approx(struct kmem_cache_node *n) +{ + unsigned long flags; + unsigned long x = 0; + struct slab *slab; + + spin_lock_irqsave(&n->list_lock, flags); + if (n->nr_partial <= MAX_PARTIAL_TO_SCAN) { + list_for_each_entry(slab, &n->partial, slab_list) + x += slab->objects - slab->inuse; + } else { + /* + * For a long list, approximate the total count of objects in + * it to meet the limit on the number of slabs to scan. + * Scan from both the list's head and tail for better accuracy. + */ + unsigned long scanned = 0; + + list_for_each_entry(slab, &n->partial, slab_list) { + x += slab->objects - slab->inuse; + if (++scanned == MAX_PARTIAL_TO_SCAN / 2) + break; + } + list_for_each_entry_reverse(slab, &n->partial, slab_list) { + x += slab->objects - slab->inuse; + if (++scanned == MAX_PARTIAL_TO_SCAN) + break; + } + x = mult_frac(x, n->nr_partial, scanned); + x = min(x, node_nr_objs(n)); + } + spin_unlock_irqrestore(&n->list_lock, flags); + return x; +} + static unsigned long count_partial(struct kmem_cache_node *n, int (*get_count)(struct slab *)) { @@ -7089,7 +7126,7 @@ void get_slabinfo(struct kmem_cache *s, struct slabinfo *sinfo) for_each_kmem_cache_node(s, node, n) { nr_slabs += node_nr_slabs(n); nr_objs += node_nr_objs(n); - nr_free += count_partial(n, count_free); + nr_free += count_partial_free_approx(n); } sinfo->active_objs = nr_objs - nr_free;