Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp2261639rdg; Sun, 15 Oct 2023 20:23:43 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH+N0D5uM2v/Bq12qCjLOqhHhn8/Nz0UE8hFmk85+LoVvdaTgsizKFwYLfwDidWiZZWHbzW X-Received: by 2002:a25:8f8d:0:b0:d48:1936:14d0 with SMTP id u13-20020a258f8d000000b00d48193614d0mr33664798ybl.53.1697426622921; Sun, 15 Oct 2023 20:23:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697426622; cv=none; d=google.com; s=arc-20160816; b=v4Y85Rrk5MB1++AgKEImzKP3mcOPCT+pRsuxpDfw+OdKhaJfwlmRUKPG9jK4dPvFN9 5a/9T/+e5W6CZxiL8AJwblw1PcxHmv9w0d7jJVxQU7f4yHmnCOjeVw4v65ep2nTzqDI2 nVhXlWLHBadpkWj4KxAAfIJI8hk7LzOKWvfdZV02WI9rl2hcpZi2mhJoU8+0B59l+fwx Md8MSbGN28C7H+YzPrhUo6Jxhqv/z9UY7sYbwTJ1YTo/6spi31RrojVF3AXnsyokCpIf L9poCmPBoGZcdEsBCIGc//6+b81ZStGzinfNEemczFha3qE2DGZA3fZtCNzg0BYBL9sM eRGg== 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=E7RL16wY3TnIMbWwQk38oO3F+wulX0gZY/uQ7sfbTretiQRRri6JkFl7crG4j9xvwT jv5wx2tOeMDxNjXs3Xasa3sQLZpQs4Jhf6QGqi0pWndilrlEmAsBrwrxqvbGqThwtnFL YNMVFbhmsHAnAmoHi0tiAmHkfFznBs+U3HrnCseIUr3X/JT36uazVvjyQAwx5RLErq0h bijBFHOPrA/nPUYvBl+x8JPIKJnfiyTCc2Aym7VGv7m57/jZwbCrbY+uWmqz0dGkPloq q/OQV2FOshMWrWhVESakIZjgyWIULSKuaXbGXEb8gtTVKmUBURotn4FLcLHX6TcZbbZc 09uw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=i7jsGdw0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 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 agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id r26-20020a63441a000000b00578e98dec9dsi9584064pga.160.2023.10.15.20.23.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 15 Oct 2023 20:23:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=i7jsGdw0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 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 agentk.vger.email (Postfix) with ESMTP id C4CB08058B71; Sun, 15 Oct 2023 20:23:39 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231461AbjJPDXP (ORCPT + 99 others); Sun, 15 Oct 2023 23:23:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55628 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229600AbjJPDXK (ORCPT ); Sun, 15 Oct 2023 23:23:10 -0400 Received: from mail-pf1-x436.google.com (mail-pf1-x436.google.com [IPv6:2607:f8b0:4864:20::436]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BBC27E6 for ; Sun, 15 Oct 2023 20:23:08 -0700 (PDT) Received: by mail-pf1-x436.google.com with SMTP id d2e1a72fcca58-68fb85afef4so3086318b3a.1 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=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=i7jsGdw0ITgbB+sXok0kbvMVrmxqxWdLOsPKKGXRS7ZV+Ib32o4zDlQNIYOlzoCgF1 yE2Ajf9i0s+IDxGRVfs4J/vlG95B3IvV0ZCBvsUOIGdG+YKLo3SVkcMrESaxUZE/FbeT 4T8s04fybllvgRqqMnojQbfs/gEM0PY6ku0LwMpCrMXt2StGilC1J+qJxapzyt5/99kl EVmVLty5j4Bz/BH8HIwj60ApI0i6bJrsIy2ysfh7mOgMO7Lb1HmbBjcn+PyrYhTfoo+d DG/8RbemO3WNH+ectz5QHapeEy8QplLnpv331f0XaHl7CvQvAbgBsOoJwiabiFdNwA7q A0LQ== 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=VqTmEW8s4YEq8B21R240oNeIxcTKoDqNONVCpnu9BF2Iv82+NGUX2MtSbUXbWdxunD z3ZtcqxDghBX9Uwk+hP6DX9+FCBjK91kZSo1/i9yjWtQ+EpebW7UH20OVzYS8nzKvTpa eFeQ+uilDsubID70PciJyNyfGYTBSX5sWC2Oh/N3Uw1DbvADT2gPH1vb40RmnEZuzIWa gAHGZbwF28Nj8w927tscSOA+wgUnNnkS/YuGH5VA2LF39x5deJGYbSzoaVIZekUm1UD+ 2Uk/iNqvc8KxVH+PXFsvTYlCHwjUQ2/17ELtb9RhZ7ZZ7JFF34HtQSIzdkigMTpySyIE ZUUg== X-Gm-Message-State: AOJu0YwBA5dVGNvcAsb+71B0PBqZKLCTXL4JTtqIx/i3kFEwn6w4RgpM d8lIkbmNS60kmLppSkfkwffwww== 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 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.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 (agentk.vger.email [0.0.0.0]); Sun, 15 Oct 2023 20:23:40 -0700 (PDT) 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