Received: by 2002:a05:7412:8521:b0:e2:908c:2ebd with SMTP id t33csp2375969rdf; Mon, 6 Nov 2023 12:13:20 -0800 (PST) X-Google-Smtp-Source: AGHT+IEdXKQynWHxRHcCUdiRLTHFSsCU1BckQQGW/qzVFJyTfIvmYAL5oIm53YMgwKcV/8NyD1um X-Received: by 2002:a05:6871:4695:b0:1e9:68b5:d418 with SMTP id ni21-20020a056871469500b001e968b5d418mr1107511oab.34.1699301600083; Mon, 06 Nov 2023 12:13:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699301600; cv=none; d=google.com; s=arc-20160816; b=Hm/DtGryuPrRWdfJ7qyz9rxJr4ZyegDctaFkV9vMNJtHqdPGQwIjvUfKEPDyclC31f nkGPhfUpg7/ik1QouWRpBEMs1Gu2RQwJDRdPzvwAZ7OzgYgj2db1PH6ZNAKY6YvAqO6m t519PrpWWc4nA9+08JNeaV5wTFZYCyCjF8aQV2nP4/WDLpJSMNtkKIngrUeaSpm6IINg lLh6LoHzx0IoA7+9ADb0YjM+MlqlwM/1QRKKvadapVb5awVq5hAGR0y8XWZLsDCyjhO+ 9Gf+26HnQx5Yzk3X6WIDmCus+F4GCpyzOReMwGMz+Op2EoWv744Zrb4FSAGei2BZuIkG TmXw== 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=lOIDRjZO1avx1i6mH90OIJOQ1dfwcpWT2tH0Qop8VTU=; fh=n5KN85tQTomq0Sa/nFFC9xbnc77mxkBzF8HoolO/QOw=; b=QDUowQhIJQb+OPzVo2RT/hOn2KOTNqq6fvv6/cNL1Kc+BvgYqZrumvL2zutCDJjekN oh0fyoBtst9oIAMfSnSgBjViPKQ13oCqQM+hmwej6UoA6yVs0MFCWlMaTvcJP8KHY9H9 /dRbVFUs986vOpZaDVveKgdfumCFNpnV/ppNq7LW/XDVfykxcjCsMMSl58giLDA9VA45 zgLvYUEWY1LL2IacGfbgcjSbuGJ/jDWdA9w4OFFvGjDxAbHik3L+GxA02MQNpckXF4a7 e2/R3anRHJUw/ELt8PoChGJgi86yLB9ERNKs3I9lJY46Jj1gofTObZQfXaTHqZ/kKhn1 pwAg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=t8CeOtDw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id bz16-20020a056a02061000b00553800fe4dcsi459683pgb.583.2023.11.06.12.13.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Nov 2023 12:13:20 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=t8CeOtDw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id D08C5801BA78; Mon, 6 Nov 2023 12:13:18 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233140AbjKFUNN (ORCPT + 99 others); Mon, 6 Nov 2023 15:13:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34126 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233033AbjKFUMt (ORCPT ); Mon, 6 Nov 2023 15:12:49 -0500 Received: from out-176.mta1.migadu.com (out-176.mta1.migadu.com [95.215.58.176]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6614810C8 for ; Mon, 6 Nov 2023 12:12:46 -0800 (PST) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1699301565; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=lOIDRjZO1avx1i6mH90OIJOQ1dfwcpWT2tH0Qop8VTU=; b=t8CeOtDwG84XGm08T/bHl0dU0SgTNjwxEbFpQe0150BlnXaBD0GRxcGWFE3gsolklSS+ox lTkeYhEYrQQ5j6iilU4kS0eMM5/B2HVjeNJmRJ19p/j/CS/27IgI29IHA++NsiwgXZYidM ve2wNSyQK1IC+l9HF2etSKPK0c0wFik= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Evgenii Stepanov , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH RFC 14/20] mempool: introduce mempool_use_prealloc_only Date: Mon, 6 Nov 2023 21:10:23 +0100 Message-Id: <9752c5fc4763e7533a44a7c9368f056c47b52f34.1699297309.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net 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 (snail.vger.email [0.0.0.0]); Mon, 06 Nov 2023 12:13:18 -0800 (PST) From: Andrey Konovalov Introduce a new mempool_use_prealloc_only API that tells the mempool to only use the elements preallocated during the mempool's creation and to not attempt allocating new ones. This API is required to test the KASAN poisoning/unpoisoning functinality in KASAN tests, but it might be also useful on its own. Signed-off-by: Andrey Konovalov --- include/linux/mempool.h | 2 ++ mm/mempool.c | 27 ++++++++++++++++++++++++--- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/include/linux/mempool.h b/include/linux/mempool.h index 4aae6c06c5f2..822adf1e7567 100644 --- a/include/linux/mempool.h +++ b/include/linux/mempool.h @@ -18,6 +18,7 @@ typedef struct mempool_s { int min_nr; /* nr of elements at *elements */ int curr_nr; /* Current nr of elements at *elements */ void **elements; + bool use_prealloc_only; /* Use only preallocated elements */ void *pool_data; mempool_alloc_t *alloc; @@ -48,6 +49,7 @@ extern mempool_t *mempool_create_node(int min_nr, mempool_alloc_t *alloc_fn, mempool_free_t *free_fn, void *pool_data, gfp_t gfp_mask, int nid); +extern void mempool_use_prealloc_only(mempool_t *pool); extern int mempool_resize(mempool_t *pool, int new_min_nr); extern void mempool_destroy(mempool_t *pool); extern void *mempool_alloc(mempool_t *pool, gfp_t gfp_mask) __malloc; diff --git a/mm/mempool.c b/mm/mempool.c index f67ca6753332..59f7fcd355b3 100644 --- a/mm/mempool.c +++ b/mm/mempool.c @@ -365,6 +365,20 @@ int mempool_resize(mempool_t *pool, int new_min_nr) } EXPORT_SYMBOL(mempool_resize); +/** + * mempool_use_prealloc_only - mark a pool to only use preallocated elements + * @pool: pointer to the memory pool that should be marked + * + * This function should only be called right after the pool creation via + * mempool_init() or mempool_create() and must not be called concurrently with + * mempool_alloc(). + */ +void mempool_use_prealloc_only(mempool_t *pool) +{ + pool->use_prealloc_only = true; +} +EXPORT_SYMBOL(mempool_use_prealloc_only); + /** * mempool_alloc - allocate an element from a specific memory pool * @pool: pointer to the memory pool which was allocated via @@ -397,9 +411,11 @@ void *mempool_alloc(mempool_t *pool, gfp_t gfp_mask) repeat_alloc: - element = pool->alloc(gfp_temp, pool->pool_data); - if (likely(element != NULL)) - return element; + if (!pool->use_prealloc_only) { + element = pool->alloc(gfp_temp, pool->pool_data); + if (likely(element != NULL)) + return element; + } spin_lock_irqsave(&pool->lock, flags); if (likely(pool->curr_nr)) { @@ -415,6 +431,11 @@ void *mempool_alloc(mempool_t *pool, gfp_t gfp_mask) return element; } + if (pool->use_prealloc_only) { + spin_unlock_irqrestore(&pool->lock, flags); + return NULL; + } + /* * We use gfp mask w/o direct reclaim or IO for the first round. If * alloc failed with that and @pool was empty, retry immediately. -- 2.25.1