From patchwork Fri Oct 27 03:38:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peng Zhang X-Patchwork-Id: 13438097 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 DD805C25B47 for ; Fri, 27 Oct 2023 03:39:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6708C6B038C; Thu, 26 Oct 2023 23:39:34 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6210D6B038D; Thu, 26 Oct 2023 23:39:34 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4C1686B038E; Thu, 26 Oct 2023 23:39:34 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 39FC76B038C for ; Thu, 26 Oct 2023 23:39:34 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 168881A0FFC for ; Fri, 27 Oct 2023 03:39:34 +0000 (UTC) X-FDA: 81389836668.03.C5E18A3 Received: from mail-pl1-f178.google.com (mail-pl1-f178.google.com [209.85.214.178]) by imf14.hostedemail.com (Postfix) with ESMTP id 3BE8410000B for ; Fri, 27 Oct 2023 03:39:31 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b="jULV/eMJ"; spf=pass (imf14.hostedemail.com: domain of zhangpeng.00@bytedance.com designates 209.85.214.178 as permitted sender) smtp.mailfrom=zhangpeng.00@bytedance.com; dmarc=pass (policy=quarantine) header.from=bytedance.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1698377971; 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=foGetFZ9t8EgLLDV0Dnb7jyONvdftSjfOOfLaWzQbAY=; b=Phgbd1htyxfF/eNGhSXIhxXl5moouSB87PM4ihEoqrj4xPPXLMmcym5w7yF80PQ/0IPRRY 7gwDU88VrrvTKoISUjfBnIC87u4tkGXmUHl2zaRBaqCI8zMvTeoykx0L6y677YUpHyBQal YEAbEn2fevA9MhrQlEYS1bpbk/iTohY= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698377971; a=rsa-sha256; cv=none; b=yRQfe8+BZuOcJyIGOYNH2/0go9OjLz+3pWNCGdBVHcIhVLUID50IuzP9VQCouPnTHICeIt Y1DSw0cRHrIdMGeo9Ow/xN8khGgiT3NZEX15cNbrhtKqyCh/N4pkNfonXR9iB0g0AJu2hu BeyxDNckzaXngAHqpw6ZhbjySiOfm2k= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b="jULV/eMJ"; spf=pass (imf14.hostedemail.com: domain of zhangpeng.00@bytedance.com designates 209.85.214.178 as permitted sender) smtp.mailfrom=zhangpeng.00@bytedance.com; dmarc=pass (policy=quarantine) header.from=bytedance.com Received: by mail-pl1-f178.google.com with SMTP id d9443c01a7336-1caa371dcd8so14331525ad.0 for ; Thu, 26 Oct 2023 20:39:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1698377970; x=1698982770; 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=foGetFZ9t8EgLLDV0Dnb7jyONvdftSjfOOfLaWzQbAY=; b=jULV/eMJetP/m1/CX+iGXRcPz61o2s+dPLtQNvlxGiPrkoZceoa8rC3zT5af7Gxv4p ClZZO/rqnq7dtSTRZrokfFIIohJbUE326ngHIUMg7AsigA4xAofiCHju+Gj7YqbueGAS LWDq0CuczHKhc9XyFLzx7K228Qsy4i5k46W4epXdjjiZM5bFszy2T0gqnprssu6OcAbE 288zWRu3SBC8I28ZVsVm5fnFu9ZpepAJjPQizJxTFiBc1ckQsXdAXdDT3QxatkhltEz+ ErQFafeHLOdqXh1lAGl+s8nqrYIM6FnccfIqFSteUmkEMDXIARDWwVBdeSbb7VVXnjG5 FhNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698377970; x=1698982770; 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=foGetFZ9t8EgLLDV0Dnb7jyONvdftSjfOOfLaWzQbAY=; b=DFuifZjvxgjK6JzdQsEgTBiFOQcg8QOG+48ndEEBLzFuvndFkGauLEIRgEfzHZjzi3 mV+VBxsGxUHMFELoSbycJ1wJrbv8gt/pTOji9VG98U661KIERnLnxfNDBTNPxn5+WrPp xHMDKX5aAAjsu18nKQb9PaiRGIP8nbRc36RlWHqEwx+TNT6P87USTqvUAF4DbKTZm1rm xH9LE2WziKj9ACS1sZ4WtlCASNMDr9fzLiKqB5y/6abIZjGApX4Lnw/mjPxEcWHW5Epk ZwG8aFJ+/K+JHUre21aFL1IgUKueF3dBKDLuL5uH3Vcfw8m1O3bczFWP1YlPwSINsQzp zfgg== X-Gm-Message-State: AOJu0YyMWl9GLMwz3mrDMv1i2CUw+Vg4ygQNVGCG11oEA1a6A+gLxYJm ZB9bMmhO+ekcaPw8ueq4TUmO3Q== X-Google-Smtp-Source: AGHT+IEXV//tpE9s5GCs40kvntDCcan4ON4xoFdITglGv9jOXpY7Lz29M0cQjMI93TcPesAbndJDog== X-Received: by 2002:a17:90a:1a03:b0:280:f4a:86b4 with SMTP id 3-20020a17090a1a0300b002800f4a86b4mr240851pjk.17.1698377970124; Thu, 26 Oct 2023 20:39:30 -0700 (PDT) Received: from GL4FX4PXWL.bytedance.net ([203.208.189.7]) by smtp.gmail.com with ESMTPSA id ms19-20020a17090b235300b00267d9f4d340sm2345676pjb.44.2023.10.26.20.39.22 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 26 Oct 2023 20:39:29 -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 v7 04/10] radix tree test suite: Align kmem_cache_alloc_bulk() with kernel behavior. Date: Fri, 27 Oct 2023 11:38:39 +0800 Message-Id: <20231027033845.90608-5-zhangpeng.00@bytedance.com> X-Mailer: git-send-email 2.39.3 (Apple Git-145) In-Reply-To: <20231027033845.90608-1-zhangpeng.00@bytedance.com> References: <20231027033845.90608-1-zhangpeng.00@bytedance.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 3BE8410000B X-Rspam-User: X-Stat-Signature: sxtcjx5r69iei8yijjy6hi37g31chet3 X-Rspamd-Server: rspam03 X-HE-Tag: 1698377971-737099 X-HE-Meta: U2FsdGVkX18lPqxE4UbqaCB5XlzBq1+EYm0SQk8vkI/TPOq5z2vOGvDpO+gTWvhnWkZcOyVRx0hZJJnJuiZB/w4aO6VvD7rhAkJuEtIHxG8GgCEPORX4JXaCN3uoyDJF2HxwyR8fdqjV0gUw8PovxcolPsCyMNk53tFqXpzTnnTzmu9Vf83GgICDz/NXkVo1I4ZzPWglVeArzYSCyMLUp55P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9swKBgEWs88ykH38R1NeIeM5mq/27kefoODBuHD6K/v26r1UJ/3R5aW8hKz913cWsBnrLFpjvi51hvaBfm4peR+2vJnnMcE99ITqp73YIC8ejRm/PTx3hDGPrJsv14NxM3YXxPNnf7ST+0V8/ckkfqXFfXrcWDPq+qEk0jnU= 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 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 Reviewed-by: Liam R. Howlett --- 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);