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: 162172 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:8f47:0:b0:403:3b70:6f57 with SMTP id j7csp2903271vqu; Mon, 6 Nov 2023 12:14:25 -0800 (PST) X-Google-Smtp-Source: AGHT+IH7FLFCN8VNk8Q2C9jgaVY35zUFgYyOriCHKXoC+1gl8ySrjggxhJ6dZxq0CWB3SvlkFqTV X-Received: by 2002:a05:6870:961c:b0:1eb:1fa3:7b05 with SMTP id d28-20020a056870961c00b001eb1fa37b05mr931691oaq.27.1699301664836; Mon, 06 Nov 2023 12:14:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699301664; cv=none; d=google.com; s=arc-20160816; b=sZmwI6DlLSd/Tah3HAePZZlCe3+Uz0q/pj6YSnS0ly0dcsnXjG9bcEsR+OUb/ouQRp zPXs3CYzDlwabuJyhVMbBCUuae+5vF2EeFjFrnOmWc3qrPzTUknO82bIfqKKjcKcI3V8 Zyq7CNMR6Dm8uMowHuZ2l6U07wVaGEUirnBRd3G0plmJNQ8jStjzMXdb/Lb6aIsgo8DA hlclg3bEQ9ypAin/MiDWi6nAtWQyIAOf8rTmLGsG7FrLxktYTyDvCg0qKACpcYMwDxz1 HR4mzwBCEqbzdcF9s8xbKuqp1WHNt4jhJWCva0gdCoLteu6uetPqBj4q0mjR+w6DSXqx VChw== 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 23.128.96.35 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 groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id bv63-20020a632e42000000b00565e865d381si391153pgb.447.2023.11.06.12.14.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Nov 2023 12:14:24 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; 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 23.128.96.35 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 groat.vger.email (Postfix) with ESMTP id 703B2804A62D; Mon, 6 Nov 2023 12:14:09 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231483AbjKFUNO (ORCPT + 34 others); Mon, 6 Nov 2023 15:13:14 -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 X-Migadu-Flow: FLOW_OUT X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.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 (groat.vger.email [0.0.0.0]); Mon, 06 Nov 2023 12:14:09 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781846942825022408 X-GMAIL-MSGID: 1781846942825022408 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.