From patchwork Mon Oct 16 03:22:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peng Zhang X-Patchwork-Id: 13422447 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 CFDCDCDB465 for ; Mon, 16 Oct 2023 03:23:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 668AF8D0023; Sun, 15 Oct 2023 23:23:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 60E658D0001; Sun, 15 Oct 2023 23:23:11 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4AFCE8D0023; Sun, 15 Oct 2023 23:23:11 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 37A298D0001 for ; Sun, 15 Oct 2023 23:23:11 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 05207B5C0E for ; Mon, 16 Oct 2023 03:23:10 +0000 (UTC) X-FDA: 81349878582.12.2BF406C Received: from mail-pf1-f174.google.com (mail-pf1-f174.google.com [209.85.210.174]) by imf21.hostedemail.com (Postfix) with ESMTP id 354A41C0005 for ; Mon, 16 Oct 2023 03:23:08 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=hiI+Gy2T; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf21.hostedemail.com: domain of zhangpeng.00@bytedance.com designates 209.85.210.174 as permitted sender) smtp.mailfrom=zhangpeng.00@bytedance.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1697426589; 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=XXZWHgZHrQ6fRhi1dWH8Hwu+H4KBhNEo2ODkb4cUdHA=; b=CUOaeNw7ocUnCYfL3RvQeQmWa8ja7bpcuGpxBPJE4zIUegamdTmViyaKAkIDPS/k1Smr9g nXvTEwRpN7vFa5a7GkYl+JIOnwqwc71ie2uFcq6qj+WTxBKT9E2GOr2wSIWuZqT3J/XxiE L++zbqJAfVsxMM/2tcS0iBkk2+JTGlg= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=hiI+Gy2T; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf21.hostedemail.com: domain of zhangpeng.00@bytedance.com designates 209.85.210.174 as permitted sender) smtp.mailfrom=zhangpeng.00@bytedance.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1697426589; a=rsa-sha256; cv=none; b=QLlUMwAf7PCeLpHsBrufeL5LePWJk0ZJ1Yn21ssopmj4fSwwRUKhnfWix9DSlbX0POGcx/ 5gg5G1eux0d8t5nN9tZljqQzA2X0j2amLs+CvN+3fBpthFV5D+UKBCWIEnmebstTFum+xC dgB0/kjIcyK14Exthoup8XHodMpTurI= Received: by mail-pf1-f174.google.com with SMTP id d2e1a72fcca58-694ed847889so3078489b3a.2 for ; Sun, 15 Oct 2023 20:23:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1697426588; x=1698031388; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=XXZWHgZHrQ6fRhi1dWH8Hwu+H4KBhNEo2ODkb4cUdHA=; b=hiI+Gy2TOw1F3sUdmvq9OnzSKqIf4GXZuWhnBym7zQ5S0Z85T3X6HIf47kPFM2IR+c srjELovHd4G6iCuKUzMNotrn6VVUMN5UG9lGZZnLhsePoxh3p29DrRlKS3TT+ZZ7pRid j9Rbg5P5ru0PUe6DZk9rv8p5tEpdsIDLiGevoPMstNKDvLrwkd4wsTx5db6Qa8401srs uXvO3ArU/wzCb2tAGgtL3M6DEFpXLBjUxd6IokodaRX0fbdztR5mzNmsw+akI8CxLRf+ 9Wq6uAHjvQSQ57BYWHAKc1+E38RBPvF9qCBE7Ae4BuqsDqOCJDfQi3hWcgOoa2peGuga aFog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697426588; x=1698031388; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=XXZWHgZHrQ6fRhi1dWH8Hwu+H4KBhNEo2ODkb4cUdHA=; b=J/BQ+cotZzpRoU0XYs93RoELE1ffYEnJ5JqHl4zEWCzPsK3r6oaUL67U8YICzMw9DD KsSylgxg/UwZah4+g3gjrDnavzOCjCLoBfGK5Om09mkB2AqBmp3xFLlRxLQJ06sO5grw O00O6ousHwmYI+NfikREEiAh8DNp7OOyEDWwBnX4zXLhHip0SyE86XPnZvbnoh34gLHZ eLNn9QTHvPOZe3HcIVptthiGkJhgN0KNtsvFnf57anMTg/KG9tRhANaIGkFiZfgC325z 3bgG8QnEZH48kNOKse3xZe6im4NTTQn2I0NTJ23bAXznX7rhYDdtbsCazlw9K7lnO+T0 lfwQ== X-Gm-Message-State: AOJu0YznCgWdbtTKRutccMQgWHPZmcoFaSh5fjVWle1kSx/3l/BYPmGc WiuR0QgbMVbb0WjUXomiHBzrjQ== X-Google-Smtp-Source: AGHT+IHJ/d4Imcp6odYT1h7aWQHUYbM1ac42oVm99u/t154/4XdiI3P2y5GHneaRUB847sqBMYQ7vA== X-Received: by 2002:a05:6a21:7784:b0:173:318:b1ec with SMTP id bd4-20020a056a21778400b001730318b1ecmr12877225pzc.35.1697426588170; Sun, 15 Oct 2023 20:23:08 -0700 (PDT) Received: from GL4FX4PXWL.bytedance.net ([139.177.225.232]) by smtp.gmail.com with ESMTPSA id d8-20020a17090ae28800b0027758c7f585sm3452770pjz.52.2023.10.15.20.23.02 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Sun, 15 Oct 2023 20:23:08 -0700 (PDT) From: Peng Zhang To: Liam.Howlett@oracle.com, corbet@lwn.net, akpm@linux-foundation.org, willy@infradead.org, brauner@kernel.org, surenb@google.com, michael.christie@oracle.com, mjguzik@gmail.com, mathieu.desnoyers@efficios.com, npiggin@gmail.com, peterz@infradead.org, oliver.sang@intel.com, mst@redhat.com Cc: zhangpeng.00@bytedance.com, maple-tree@lists.infradead.org, linux-mm@kvack.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org Subject: [PATCH v5 04/10] radix tree test suite: Align kmem_cache_alloc_bulk() with kernel behavior. Date: Mon, 16 Oct 2023 11:22:20 +0800 Message-Id: <20231016032226.59199-5-zhangpeng.00@bytedance.com> X-Mailer: git-send-email 2.39.3 (Apple Git-145) In-Reply-To: <20231016032226.59199-1-zhangpeng.00@bytedance.com> References: <20231016032226.59199-1-zhangpeng.00@bytedance.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 354A41C0005 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: mz6i5zn6i9dncjpdfwgmycx9au6zrytk X-HE-Tag: 1697426588-239597 X-HE-Meta: U2FsdGVkX19AIeL2XWgBpqJmHMJrq3Mi/JALsNQvK2+u8FC+6JHoKnWQV9yqu1+0q317tOBdeeEhZyzmDZVPONDXFnQ6rV/2LqHJfmMi7Z/AFM8Nqe6zSuBCrPcZYs1b8Sh+u40yIZ1SWTVvCwx9HMjQAnuprecZaXMcFHxzB34wJz4O1NRPo5j1fD1/G3dvIX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hChZ1lsan4++ID1rdYCCpFbMIiK6jd6J0JGlPnp/w7jG44d27qxHALopVH446X0luPwLmv7ouHhblUVAeYPUL3jpONRPWsaKLc/+a9gsD+WXg6CIS9I4/U4fVgsUmUdj9EwTyDnNgD2o5TSU8iHs2wYG5T1PwOrZu14WDpIaZMLXG6rMjk+mw== 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: When kmem_cache_alloc_bulk() fails to allocate, leave the freed pointers in the array. This enables a more accurate simulation of the kernel's behavior and allows for testing potential double-free scenarios. Signed-off-by: Peng Zhang --- tools/testing/radix-tree/linux.c | 45 +++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/tools/testing/radix-tree/linux.c b/tools/testing/radix-tree/linux.c index 61fe2601cb3a..4eb442206d01 100644 --- a/tools/testing/radix-tree/linux.c +++ b/tools/testing/radix-tree/linux.c @@ -93,13 +93,9 @@ void *kmem_cache_alloc_lru(struct kmem_cache *cachep, struct list_lru *lru, return p; } -void kmem_cache_free_locked(struct kmem_cache *cachep, void *objp) +void __kmem_cache_free_locked(struct kmem_cache *cachep, void *objp) { assert(objp); - uatomic_dec(&nr_allocated); - uatomic_dec(&cachep->nr_allocated); - if (kmalloc_verbose) - printf("Freeing %p to slab\n", objp); if (cachep->nr_objs > 10 || cachep->align) { memset(objp, POISON_FREE, cachep->size); free(objp); @@ -111,6 +107,15 @@ void kmem_cache_free_locked(struct kmem_cache *cachep, void *objp) } } +void kmem_cache_free_locked(struct kmem_cache *cachep, void *objp) +{ + uatomic_dec(&nr_allocated); + uatomic_dec(&cachep->nr_allocated); + if (kmalloc_verbose) + printf("Freeing %p to slab\n", objp); + __kmem_cache_free_locked(cachep, objp); +} + void kmem_cache_free(struct kmem_cache *cachep, void *objp) { pthread_mutex_lock(&cachep->lock); @@ -141,18 +146,17 @@ int kmem_cache_alloc_bulk(struct kmem_cache *cachep, gfp_t gfp, size_t size, if (kmalloc_verbose) pr_debug("Bulk alloc %lu\n", size); - if (!(gfp & __GFP_DIRECT_RECLAIM)) { - if (cachep->non_kernel < size) - return 0; - - cachep->non_kernel -= size; - } - pthread_mutex_lock(&cachep->lock); if (cachep->nr_objs >= size) { struct radix_tree_node *node; for (i = 0; i < size; i++) { + if (!(gfp & __GFP_DIRECT_RECLAIM)) { + if (!cachep->non_kernel) + break; + cachep->non_kernel--; + } + node = cachep->objs; cachep->nr_objs--; cachep->objs = node->parent; @@ -163,11 +167,19 @@ int kmem_cache_alloc_bulk(struct kmem_cache *cachep, gfp_t gfp, size_t size, } else { pthread_mutex_unlock(&cachep->lock); for (i = 0; i < size; i++) { + if (!(gfp & __GFP_DIRECT_RECLAIM)) { + if (!cachep->non_kernel) + break; + cachep->non_kernel--; + } + if (cachep->align) { posix_memalign(&p[i], cachep->align, cachep->size); } else { p[i] = malloc(cachep->size); + if (!p[i]) + break; } if (cachep->ctor) cachep->ctor(p[i]); @@ -176,6 +188,15 @@ int kmem_cache_alloc_bulk(struct kmem_cache *cachep, gfp_t gfp, size_t size, } } + if (i < size) { + size = i; + pthread_mutex_lock(&cachep->lock); + for (i = 0; i < size; i++) + __kmem_cache_free_locked(cachep, p[i]); + pthread_mutex_unlock(&cachep->lock); + return 0; + } + for (i = 0; i < size; i++) { uatomic_inc(&nr_allocated); uatomic_inc(&cachep->nr_allocated);