From patchwork Tue Dec 19 22:28:59 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: 181325 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:24d3:b0:fb:cd0c:d3e with SMTP id r19csp2274343dyi; Tue, 19 Dec 2023 14:34:08 -0800 (PST) X-Google-Smtp-Source: AGHT+IEdCaL4yOIUwo7JruD+04mTdVQnqtmwh3X4CAm/5zLDP7wOv+KneWHqBB7J8S9D2JMbfgh5 X-Received: by 2002:a05:6214:27c9:b0:67f:53a6:3e3d with SMTP id ge9-20020a05621427c900b0067f53a63e3dmr3182409qvb.66.1703025248052; Tue, 19 Dec 2023 14:34:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703025248; cv=none; d=google.com; s=arc-20160816; b=GDrkvG4uzvuyw8FONe8mu4GWsFrDAjSgV7HuA9aKVrJiutbJrm3d2RB2sNbNvcGW/9 43EhpzBxLsUVBMWlajx/b4lH7kYesmmsKHuXZwuSdphlTaRmUQCOq3hZBC3GwUOilPaZ oOwnlfT/xghwWIejDOf6n3mF0Wx2svjQ4aV4Gqlj9HakeV6ePIXCJ4d1O1sZmEgfbiIx kAyN7kLAxMP2dxO2kFyFLxy5VjKv2wV68IpfuX/RnHYFZE5aNfO9Y4yom7thbTLkZfZ4 tSUuf2a0ZnYPOB5t7JZmnQQtamKXGHr16Pl/TTfcBs7tgFD6vK533QFFB6m8J+t2AR6p /OOw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=Vw9Ju+XsuH7jTh0rV53QjnTfccLPGDaCnQHzua8vht8=; fh=AIzsiqkKUtrB7QnM+XRgAblgvDINIC5pDvD0mg8EzrU=; b=M3I/bz1fef1wL4ecM0p9WWC1zN+q0z40jRQvLC65vNaGdybzVlzzZRpNs/uKsNAZ1q zE7qVEF8kjkLmNiOcu7Dhq5r3ZYMby2ZREPlXtXeqUHa+tpssQtX6CvZKZ1ZtDVkhfdr ygl5gHHs2D48IJF6j6ASrzH5RuYKggA50eH8U7Y2QsHducrjgif/aRy4DeN3ybvMHFie yAMRQuvxbebC8Xjg7Mcnb469tUsgHJP4jQUInQA1qy6e4hQYDBZlVxBd5K/K3WWsWWB4 Meu/ReXLIFnggkw0SZkE5lJ405lx7Cz+5PLqUtNoehp91QFCQKy0kqILR+0m+ivaQSq3 VaSw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=CZpopGBZ; spf=pass (google.com: domain of linux-kernel+bounces-6055-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-6055-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id x18-20020a0ce0d2000000b0067f2956fe05si8620434qvk.504.2023.12.19.14.34.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Dec 2023 14:34:08 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-6055-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=CZpopGBZ; spf=pass (google.com: domain of linux-kernel+bounces-6055-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-6055-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id D3D8B1C25492 for ; Tue, 19 Dec 2023 22:34:07 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7218846439; Tue, 19 Dec 2023 22:31:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="CZpopGBZ" X-Original-To: linux-kernel@vger.kernel.org Received: from out-177.mta0.migadu.com (out-177.mta0.migadu.com [91.218.175.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D64AE3D0C4 for ; Tue, 19 Dec 2023 22:31:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev 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=1703025085; 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=Vw9Ju+XsuH7jTh0rV53QjnTfccLPGDaCnQHzua8vht8=; b=CZpopGBZsYX+uocBYMIAfDP7PDv7NGAgHL88CMPaHeNmC1Jd3UkHAbZqq1SXvnp8qR3PRk ko3vkaTQDOJV582w6cY6Jpq7gGMvDm6ekS2hgFsxwAQMhisoeEHQlLiOfoxsL6ydfk5Smi uW9JPOVYPoMNbIRTQbjw3x7wa9H6dN4= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Evgenii Stepanov , Breno Leitao , Alexander Lobakin , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH mm 15/21] mempool: introduce mempool_use_prealloc_only Date: Tue, 19 Dec 2023 23:28:59 +0100 Message-Id: In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785751402378843353 X-GMAIL-MSGID: 1785751402378843353 From: Andrey Konovalov Introduce a new mempool_alloc_preallocated API that asks the mempool to only use the elements preallocated during the mempool's creation when allocating and to not attempt allocating new ones from the underlying allocator. This API is required to test the KASAN poisoning/unpoisoning functionality in KASAN tests, but it might be also useful on its own. Signed-off-by: Andrey Konovalov --- Changes RFC->v1: - Introduce a new mempool_alloc_preallocated API instead of adding a flag into mempool_t. --- include/linux/mempool.h | 1 + mm/mempool.c | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/include/linux/mempool.h b/include/linux/mempool.h index 4aae6c06c5f2..7be1e32e6d42 100644 --- a/include/linux/mempool.h +++ b/include/linux/mempool.h @@ -51,6 +51,7 @@ extern mempool_t *mempool_create_node(int min_nr, mempool_alloc_t *alloc_fn, 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; +extern void *mempool_alloc_preallocated(mempool_t *pool) __malloc; extern void mempool_free(void *element, mempool_t *pool); /* diff --git a/mm/mempool.c b/mm/mempool.c index 103dc4770cfb..cb7b4b56cec1 100644 --- a/mm/mempool.c +++ b/mm/mempool.c @@ -456,6 +456,43 @@ void *mempool_alloc(mempool_t *pool, gfp_t gfp_mask) } EXPORT_SYMBOL(mempool_alloc); +/** + * mempool_alloc_preallocated - allocate an element from preallocated elements + * belonging to a specific memory pool + * @pool: pointer to the memory pool which was allocated via + * mempool_create(). + * + * This function is similar to mempool_alloc, but it only attempts allocating + * an element from the preallocated elements. It does not sleep and immediately + * returns if no preallocated elements are available. + * + * Return: pointer to the allocated element or %NULL if no elements are + * available. + */ +void *mempool_alloc_preallocated(mempool_t *pool) +{ + void *element; + unsigned long flags; + + spin_lock_irqsave(&pool->lock, flags); + if (likely(pool->curr_nr)) { + element = remove_element(pool); + spin_unlock_irqrestore(&pool->lock, flags); + /* paired with rmb in mempool_free(), read comment there */ + smp_wmb(); + /* + * Update the allocation stack trace as this is more useful + * for debugging. + */ + kmemleak_update_trace(element); + return element; + } + spin_unlock_irqrestore(&pool->lock, flags); + + return NULL; +} +EXPORT_SYMBOL(mempool_alloc_preallocated); + /** * mempool_free - return an element to the pool. * @element: pool element pointer.