From patchwork Tue Dec 19 22:28:49 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: 13499298 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 6C697C46CCD for ; Tue, 19 Dec 2023 22:29:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C3D5D6B0098; Tue, 19 Dec 2023 17:29:18 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id BEC136B0099; Tue, 19 Dec 2023 17:29:18 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A66468D0009; Tue, 19 Dec 2023 17:29:18 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 909FD6B0098 for ; Tue, 19 Dec 2023 17:29:18 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 552191C121F for ; Tue, 19 Dec 2023 22:29:18 +0000 (UTC) X-FDA: 81585009996.08.EA7483F Received: from out-178.mta1.migadu.com (out-178.mta1.migadu.com [95.215.58.178]) by imf22.hostedemail.com (Postfix) with ESMTP id 58C36C003A for ; Tue, 19 Dec 2023 22:29:16 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="F86/EPhH"; spf=pass (imf22.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.178 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1703024956; 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=DO8tjmS9YVd+JQvWum2ppGekXJiIG6c57s1H7fN9b9c=; b=G4zL+v/QUoxBYAcMdbbqjSd1IntvmOlkjZ6zumQPnmBnclbjDuehdwuUvlPcQdxFG6Gbxu c5hv3GLJe+IhVswxilLcv/lxvGnJiefrTB4LldJdGlyRj6hu5Rnf977m3PsHH0eFET/Qc4 NpPHw++yJ7ZAwwtxyVeNFEqE4pfgt6c= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="F86/EPhH"; spf=pass (imf22.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.178 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1703024956; a=rsa-sha256; cv=none; b=1N3lGkC3jmFskGDrD0vJa40PpFoLRzsz/yTfLtP7cJkBM5ijPWLciA/pUC1acPVoWrrUTJ bmnIFKRo8wlDlEhzkccgNshPqEMQdlNAZcyEGOGVHT0URKIYl8bA725N8Rowc2K+yzYHUJ jqosBoHSnSj1w4b8Un8OhY/RCd/C6/g= 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=1703024954; 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=DO8tjmS9YVd+JQvWum2ppGekXJiIG6c57s1H7fN9b9c=; b=F86/EPhH4w6/WbWrDENKsFnnUYgyTxWsWcPGWpBY23pYkD6fHfvr174y/cag/gCnwWPUSr 5v6xLH4KEmbEYh1Enf/9T6dx3MnCR1H0deL92bZhphAwsZ5re8vi40q1doi4AozQsFx4SP 8eXPaY9dqKTzvnqybENGUX0YdLOqPQA= 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 05/21] kasan: introduce kasan_mempool_unpoison_object Date: Tue, 19 Dec 2023 23:28:49 +0100 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: 58C36C003A X-Rspam-User: X-Stat-Signature: cdnpo56wh1sqw34bpqstatyzs1rpsa9h X-Rspamd-Server: rspam01 X-HE-Tag: 1703024956-946804 X-HE-Meta: U2FsdGVkX1+RO4ecfJ5Ju/FluegD93NPgE570InWi0NTBoi0nntTG+LA/JGBQFqENTkA1YSl0m3chsYGBeHGVIgnAIqZ6RPeRMXOPnfwC4iLavQam+NSK0HEgFwLrVfsJSHMmSsdAVEyJKoaPAlUcLb0U/GgZrsXdk9BRYQhBU7LhBSQTlqPovCfQCyYYEf4ZdVtH7o8loByB8AkL2F6eHbv5QMoksPgPP1/OJUmqPtBHDuPGHBHL3Jtu94Sm17jdRworBQL7rQ466X29XFWGB2+QWA487EoUtKOD547houpHIgOXv5x4JwDQMSAKmny3xd0WOdFXWVyasDqWhFHDUIYZWkc4qbY7OXvzq1srNeZrVPwz7kYsUAR7EopuVdl1Xo4JkugcAXaln4o6fE1DWCNRYhjf3IUwYsVEGcw+h9w7cvOjvekNcU541/TjgK7wcY/UfAoig92/V79Bs2GQSlH6Os3oHbzPVY2FLMJkyBAcPzDLa7QZGflhLCFngOD9O2tDPeWhH8xUs1tihoE4qAhyepYUAs0ZXhoMhEvXZTtLwUqhX6MQ6sCVx9KV5KrT7emr1WOwF+GS3SHYwirN35xm9mhtBQnFqpZzRcwz8JC5dIiyC1XlYZxshJBuoPnw78pePfGJYSj8wfOiQPFqa3hfvnOOpT4k9cfs9Vsp6TgGxMmKn5O4R5jKY35CYj3SkcpBlwbYytdUlbeBd95tcS8P5f4ArBBT2PQzGcaJDAqCr4YmdIKmlE+x6iJgkOn4jM682nScY3qjrdq4kK6GC7FHKeiZltOSzF6quMjHWNISYCBNYviGh6caq1suxTmdt3iW3mAS/z+QdlQwCdxonjzrn/YI7qLB35nvguKm+Jihd1KyGMWW7WuX3oda9gJwg2GYG8nqk3l1qLB+jIHzktz2d3/yzL7PU+Ux+Ye5NjV3WZo/Z06D/tkLzLHkAj+15jPW5Ko1xAgC2Z+Jdj iuC5e2Ac 5+4hBqgIJMQGfbEWpuCC9il0NZLRe8sdl89/cn2EyQsRSscuRoUUqLCWRHQV9DYW8P3Yw3Hwg3r5Fd9IgAgDnmEiNBGIZSb9KnCERB3eRWbvGGU8/+ufrOGIkRV7pTTrG2T2N2Cz6ygrHH2RX8uzrHjfZynnHInpgApHBOSPUNOBn9uJJPVZJM0Zpzjy9OgbXql8kFDFZD8HUG/Dm/0QuZGZN/Q== 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 and document a kasan_mempool_unpoison_object hook. This hook serves as a replacement for the generic kasan_unpoison_range that the mempool code relies on right now. mempool will be updated to use the new hook in one of the following patches. For now, define the new hook to be identical to kasan_unpoison_range. One of the following patches will update it to add stack trace collection. Signed-off-by: Andrey Konovalov --- include/linux/kasan.h | 31 +++++++++++++++++++++++++++++++ mm/kasan/common.c | 5 +++++ 2 files changed, 36 insertions(+) diff --git a/include/linux/kasan.h b/include/linux/kasan.h index 33387e254caa..c5fe303bc1c2 100644 --- a/include/linux/kasan.h +++ b/include/linux/kasan.h @@ -228,6 +228,9 @@ bool __kasan_mempool_poison_object(void *ptr, unsigned long ip); * bugs and reports them. The caller can use the return value of this function * to find out if the allocation is buggy. * + * Before the poisoned allocation can be reused, it must be unpoisoned via + * kasan_mempool_unpoison_object(). + * * This function operates on all slab allocations including large kmalloc * allocations (the ones returned by kmalloc_large() or by kmalloc() with the * size > KMALLOC_MAX_SIZE). @@ -241,6 +244,32 @@ static __always_inline bool kasan_mempool_poison_object(void *ptr) return true; } +void __kasan_mempool_unpoison_object(void *ptr, size_t size, unsigned long ip); +/** + * kasan_mempool_unpoison_object - Unpoison a mempool slab allocation. + * @ptr: Pointer to the slab allocation. + * @size: Size to be unpoisoned. + * + * This function is intended for kernel subsystems that cache slab allocations + * to reuse them instead of freeing them back to the slab allocator (e.g. + * mempool). + * + * This function unpoisons a slab allocation that was previously poisoned via + * kasan_mempool_poison_object() without initializing its memory. For the + * tag-based modes, this function does not assign a new tag to the allocation + * and instead restores the original tags based on the pointer value. + * + * This function operates on all slab allocations including large kmalloc + * allocations (the ones returned by kmalloc_large() or by kmalloc() with the + * size > KMALLOC_MAX_SIZE). + */ +static __always_inline void kasan_mempool_unpoison_object(void *ptr, + size_t size) +{ + if (kasan_enabled()) + __kasan_mempool_unpoison_object(ptr, size, _RET_IP_); +} + /* * Unlike kasan_check_read/write(), kasan_check_byte() is performed even for * the hardware tag-based mode that doesn't rely on compiler instrumentation. @@ -301,6 +330,8 @@ static inline bool kasan_mempool_poison_object(void *ptr) { return true; } +static inline void kasan_mempool_unpoison_object(void *ptr, size_t size) {} + static inline bool kasan_check_byte(const void *address) { return true; diff --git a/mm/kasan/common.c b/mm/kasan/common.c index 2b4869de4985..4b85d35bb8ab 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -451,6 +451,11 @@ bool __kasan_mempool_poison_object(void *ptr, unsigned long ip) } } +void __kasan_mempool_unpoison_object(void *ptr, size_t size, unsigned long ip) +{ + kasan_unpoison(ptr, size, false); +} + bool __kasan_check_byte(const void *address, unsigned long ip) { if (!kasan_byte_accessible(address)) {