From patchwork Mon Nov 6 20:10:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: andrey.konovalov@linux.dev X-Patchwork-Id: 13447462 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 795DFC4167D for ; Mon, 6 Nov 2023 20:12:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F00BF6B02A2; Mon, 6 Nov 2023 15:12:48 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id DBB2F6B02A6; Mon, 6 Nov 2023 15:12:48 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BD9DF6B02A3; Mon, 6 Nov 2023 15:12:48 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id A470E6B02A1 for ; Mon, 6 Nov 2023 15:12:48 -0500 (EST) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id F40571208D6 for ; Mon, 6 Nov 2023 20:12:47 +0000 (UTC) X-FDA: 81428627574.24.3AF9FFB Received: from out-186.mta1.migadu.com (out-186.mta1.migadu.com [95.215.58.186]) by imf13.hostedemail.com (Postfix) with ESMTP id 3CECE20011 for ; Mon, 6 Nov 2023 20:12:46 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=t8CeOtDw; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf13.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.186 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1699301566; 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=lOIDRjZO1avx1i6mH90OIJOQ1dfwcpWT2tH0Qop8VTU=; b=q2EJ1FKk2uaEJ1lHi31724CXKdo7M6NPyvamkZUeE1uHtGkDtjbMnveJQeYJkDSbz1RZKm kBElaaC6ByLGhndPH0C9/PEaNLZ43cTSAHkexD0vetUI8FfKsCqTdAMg1nwmtU5QshjsVT aFwm4TkNF2pmcblkYp/rsQZzFn07ehs= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=t8CeOtDw; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf13.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.186 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1699301566; a=rsa-sha256; cv=none; b=BzxK6i8NXufh9XkDJOfYlIqdD29K2xicKj56+2gMc/QU/G0Z6leun5UVMBd36lr2qLOeW3 NTYDIxQgP+kpro/MBHoCjWstK7ijGiBZGzPc0wzG2s1mzZVJaATZ7zxhHfHjfR61JmH+0r BPhAMAXp6qYYKSdbULPQxZYJ79/j/iI= 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 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: 3CECE20011 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: d75gt8goyrf7s5tbxfktre4d958pd1gu X-HE-Tag: 1699301566-48206 X-HE-Meta: U2FsdGVkX1/A/ERN5DLUij94roigsRG/mFEHFZVclC0mbUhaGLTuFzTVHD93eDJQAe10ZaEQKBSxpmdT0ffKqXEPMUNecm/2SjNYWOm3o4uqnM1iEberWlSDS9dkH2GfVz44DYfOoY6ONBbz6bYDzfrqEKcrIZuv3Ci7B0Y1aHAGACowHm5cWOABi4E24aqiU3/9bomwGhmP8rZxY5JBk/HNKYW3UAAvfBbnx5uaSqstdzOtMHPLRlhKaFpGXPYj8Ltw76G6FLTZyeQiDD3EL0DHNdIJ/JB3A58/tlA/bv7ff4vQKom5+ZObBVtuBbEvAi+Lmrg7ms5GOrMoYTrAeaPR0nxmgOTBd4E3JL/S2MK24KYjtgtpdjwXO+vX3PzlGdDzkyAjUHZEYH5/90MoljJBsJ2PdNGo90Rm3pXlOc1fPzPEXxpkFUCP2ABpRqjC+nrIWNCn62oK/XhuLKV7ZYF36lsL262tZhHJv899/xg5icHamuuN1TV1wGQ/niiU1yL8zfBAb92z+viDJD5NiL58Xk0HsGd3NNx1kCz6tZBU1hAgyiXPX12DV+23ikJL3wih78+qljrO2tqHoMEw6MVIe4G/ZhQqt/K3HoeAc6KC1NWybQMm/BeHsOpmuDzWiNjAyD1yS3jndIYvY2jxf9ziGAR6Dv7sDart9H45PviaA/wfX/jV9dMsvsJ/yMLhmCqN+m67+Rmjkk851oMtjP42IMq3JmieJt+wicFqdChI15I4H2FOFMWsRKLTuQOc7B4OV7nYc+VoFqcNL431D9jI2CM13oG80yuGJX6HHSbEh8aSO5ruziT3ivMIcoaBBTlyLP/KhRX3AgpGtI4G/D/sj8IAuX9NmMcpDTlXHItGHcVYuyddPjwy/8OimjoxRomocGD797sPlychmLHLKWm33pRR9FLMsBCnar0Wi7FySCFlBrQ2GaNlByG3ulOD1UZvXHOrySG/FwURhpo eHiZ5Vqs CmRngzPtvYd7FTwmQIHeqX1SGlPfAYz+tPxIpwS/suDcUXPAVt1xJ7mvHRAhmHQYWIc+8lp56SUimx1HPBsfGql7x8eeHZbVPodkAKO0fHkq5+Fgip5E2d1H5hCU5vq4frB/dMxyt4AdlYGhwOrSBRH9m8MsbULZVDdHmV7qwb3P6Ug5zXGAsQG0wk9dPux5QBPTM0drzRuUWGRxzG02BpGHNVQ== 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: 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.