Received: by 2002:a05:7412:da14:b0:e2:908c:2ebd with SMTP id fe20csp1766405rdb; Mon, 9 Oct 2023 02:05:07 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFFR9T7iGs885xFedqongNhuHdkDsY+K7dgcliBgk9QjZkr8CZRvFOZ55wWJrG0/oicsXVR X-Received: by 2002:a05:6a20:dd9e:b0:169:7d6f:9f22 with SMTP id kw30-20020a056a20dd9e00b001697d6f9f22mr11936158pzb.54.1696842307137; Mon, 09 Oct 2023 02:05:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696842307; cv=none; d=google.com; s=arc-20160816; b=EaGjA/XcvImdHEndc5biXBRzgvROkSYVHQVSrKId7dhaImcnyAMXWosxD7cDo4rq6o aQSI37KqCOqF4J9N3TusFG4K7K1teT/VTPRIB3tv0KX7JO+7g/T2iICyAdc6HS06ibr6 AlW6xxkc0NYM6sMzHQIFSc5TvadgqL9yWriwvgaOu7JTVc63j06NTU0jWUrHaZ9F7Nxb Cd5NzL6L+QLtvD+byk1aOxvn8bONJvs13dMhzThIw2BvBDkiTsm6DM5HS9ELrin1EbF/ INm9JVRRBXzB63yROtD66NioE9GvhygVUvUAl0aaZUp9cTsxvnrDQNU3fMIxU/1HK/aE ODGw== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=XXZWHgZHrQ6fRhi1dWH8Hwu+H4KBhNEo2ODkb4cUdHA=; fh=W1ujNbDikRM6mL+MA7cZ93eipzimk+g+Mb7QYnlff5s=; b=BOqT4io68jClpLRpkoy4TaDUuI3zzVL3iYv+AyPCwJYRd6Slg/pNKDXSOhbkHL9C3i tIFr8lsLHGlqAjeI6pJAn3a536EmWrMbBIdY18sy8RvhBYN3mWSpo27af07bd2FiYnOI +gForNEFLyTjqmYaCpVuFz6IMX1hFxYh21KMfuKjyiA0QGIhhjPKPFBgXvh55O00HtPi SGFE3nM20DNCHp4yxfEEPaR3leRxQDB9n1lzXHg7WjoEdDuDK8gc+vCYbTGZ6p8BVc8y 6L5CTWb6JuJlbeG4YeI6IoPZh/r39l6lte+99aJVMC0H+Jyq7tZxk2VRTM0VuoDQE0av nucw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=GZ99i+5l; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=bytedance.com Return-Path: Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id z26-20020a056a001d9a00b0069342cee02esi6782555pfw.298.2023.10.09.02.05.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Oct 2023 02:05:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=GZ99i+5l; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=bytedance.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 0EBE3809E21A; Mon, 9 Oct 2023 02:05:04 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345811AbjJIJEv (ORCPT + 99 others); Mon, 9 Oct 2023 05:04:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53302 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345732AbjJIJEd (ORCPT ); Mon, 9 Oct 2023 05:04:33 -0400 Received: from mail-oa1-x29.google.com (mail-oa1-x29.google.com [IPv6:2001:4860:4864:20::29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 24331D8 for ; Mon, 9 Oct 2023 02:04:10 -0700 (PDT) Received: by mail-oa1-x29.google.com with SMTP id 586e51a60fabf-1e562706d29so2459953fac.2 for ; Mon, 09 Oct 2023 02:04:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1696842249; x=1697447049; darn=vger.kernel.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=GZ99i+5lGt2piHXNvsRZ9ejcz11OMKmMIf0lxBL8NTHU9pwvLULkkVPiCH2prE+tQC uJXpkPLS3QzCiH7Yxt3Gbf4VoolSkC4a5I2zyaf3L92fXUGJnawZP8AJ8zLBM6bMkvVt 2f4mWILlkfXoADzcCbVNVCcWY5pO4hLEF+KwKgFXn/JDQDsEpM+DxFGJbjIB14xl0M7m gFX/H0crq4AEitLs+yIE0URNX9k+xEAXcL49EAI5yPsTT9TkLUpDMNkMNX1EVogKP/s2 nAnE4XCS65vYN5wVKla3zI+pAw6X6b3v3mqe6Z4auRph2qbUF/4yqS6JUVXLs0Gz3AAD hr2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696842249; x=1697447049; 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=r+qT0n0vmir5dZHl+14qDo8AL4HwP+KrcjAArFtMzyQTAjpstTy5z0GbFjWJ5E3bGY SRLbIEvUPto7n3cMeQBS75D4FyYeVLCUGNEz2rDzFFvBidlNF4LAdmJNOUIqQvNBpYqe c9/NyxMb/6B1vAmrr36CyUgNYmt4dXxizNaBKFcO0uClgE9joEMtyx51Q1Sn0jvz5UxD IPxwrWrYvx9lResNxFS2ZqjWfr0ajwGmXI4VXw3C/FOnQCARjm+FXwJJLJ+tuFdyn+gj 8p+fdz2ZMUgp+G1k9DNCZJ5yfYovctUXo/iQp1xbEc77dGxJ2waDJm0v8N7Uxp06RWSV LtVg== X-Gm-Message-State: AOJu0YzrAF8S+VkJ9klxKKbfyVYdvO7HOoXp6MgKX7T+ldBXPGjPsW9j tKlhigD9ZbKhXKJ5DmZIaTT+hQ== X-Received: by 2002:a05:6870:b618:b0:1c8:d72a:d6ba with SMTP id cm24-20020a056870b61800b001c8d72ad6bamr17273488oab.45.1696842248950; Mon, 09 Oct 2023 02:04:08 -0700 (PDT) Received: from GL4FX4PXWL.bytedance.net ([139.177.225.243]) by smtp.gmail.com with ESMTPSA id fk3-20020a056a003a8300b00690ca4356f1sm5884847pfb.198.2023.10.09.02.04.02 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 09 Oct 2023 02:04: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 v4 04/10] radix tree test suite: Align kmem_cache_alloc_bulk() with kernel behavior. Date: Mon, 9 Oct 2023 17:03:14 +0800 Message-Id: <20231009090320.64565-5-zhangpeng.00@bytedance.com> X-Mailer: git-send-email 2.37.0 (Apple Git-136) In-Reply-To: <20231009090320.64565-1-zhangpeng.00@bytedance.com> References: <20231009090320.64565-1-zhangpeng.00@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 lipwig.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 (lipwig.vger.email [0.0.0.0]); Mon, 09 Oct 2023 02:05:04 -0700 (PDT) X-Spam-Level: ** 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); -- 2.20.1