From patchwork Mon Nov 6 20:10:14 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: 13447420 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 A2E71C001B2 for ; Mon, 6 Nov 2023 20:10:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 612DC6B028A; Mon, 6 Nov 2023 15:10:40 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4FBD86B028E; Mon, 6 Nov 2023 15:10:40 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3266D6B028A; Mon, 6 Nov 2023 15:10:40 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 0C5B76B028C for ; Mon, 6 Nov 2023 15:10:40 -0500 (EST) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id E0A231CB46F for ; Mon, 6 Nov 2023 20:10:39 +0000 (UTC) X-FDA: 81428622198.09.8FF7E25 Received: from out-172.mta1.migadu.com (out-172.mta1.migadu.com [95.215.58.172]) by imf30.hostedemail.com (Postfix) with ESMTP id 2DE1A80029 for ; Mon, 6 Nov 2023 20:10:37 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=IRiR9duy; spf=pass (imf30.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.172 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=1699301438; 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=VrpDyLzZeYtu1H7+z0aR/HPyxv+dwo0N2+jHNRcNovg=; b=ilWz3pScsFtKTra7y6v3bpxQlUgDjIwk4P284EZmvQQku4OJhlzDF9PIfl/XOHpO1r+mNy 0NtaWrJ+waqt23iCXtGoheS3lRnPPN8Fj+i9ty/vQ8G5PMh4L0BrgkZFbO18GRZ2kYsQiu JDwFRXm0rQrGaax7QKBmJvlrs2JbfrU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1699301438; a=rsa-sha256; cv=none; b=Ez3gNNZX6I0pR5YqdSkLA3u8tqLWCFDo3QtfyNHqTQ0VruNqolYOHm0cvxmDjhtFKh5wVb +seqZQgdFvtQ35Ne18CECbywpCBYLQedrQSlehDbnPnTqxYfpz2DzrmhjBvCpUO2iiyRIP hjw4qt+u3qZneO3RS2w2K4W4u5cRfHg= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=IRiR9duy; spf=pass (imf30.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.172 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=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=1699301436; 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=VrpDyLzZeYtu1H7+z0aR/HPyxv+dwo0N2+jHNRcNovg=; b=IRiR9duyySKTYMlcSCduunnoFku2ROSf7vVls3D3d5l3t64Tp6PJw5Tzev1W2QXnSS/iWi scQl4qOULKDYkIq8kuBLwHryMfjiAPS/Y+ufLRnA9LEHL20nZi55MDGuAm5yVuXtUNIc/i InULrYEGV1Mdxdx3Gw/YrKrgNHW65dM= 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 05/20] kasan: introduce kasan_mempool_unpoison_object Date: Mon, 6 Nov 2023 21:10:14 +0100 Message-Id: <6b096bcf531f457b13959ea99b1e270b96d5ca34.1699297309.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: 2DE1A80029 X-Rspam-User: X-Stat-Signature: mxr9ddff7n3bhu5s4sk9cfwf4h33sbmm X-Rspamd-Server: rspam03 X-HE-Tag: 1699301437-479020 X-HE-Meta: U2FsdGVkX19aaGMpGOMbqfn+6iH9+LyRB8mtJCEWHCJhUW/8u52oHt8J7T/IsdwVhDi9R+99xHGaIGcT74hP21R5BF3k537ILKi9KvX4qS32EZ9G57esPsqwZxCC2USuGW8sGDgTUo9ObaCVGDemsQUVgz4oq5sNacpW8WblG0R3/+DVYuNjXH7x0Wnh7OLQw6887ffBJgYEEWbR2q6zXPnHb8FWFot9rBQo0yUt/fqUT/KBpv7OUJFCA3N+33wCyPlHJJMH+LnjCYiJjBLuGHfVqVizbNyokaXbhpBFo/JK1DO33R376bPbqcfgqzfl7hBVq24aaX1e6tkEV6qjquriGBslU1NW7A6MjRJvUmnWBRhdngctakYbKGxrYm63nlln9qz1QhkWMlEyftCDi3HrqSXAl+rZ95qRJa/1POWRIHWP+SXJ+rRNVC1uvCr/myzUPDW7PAZRFkUSaqsImMM5WYgZ5Yz6HpnoTM3jZYgzl+eFhAM7oWLx1G+MBZznXfevJl1iI1bunMbgL4y+Oe9JbTI+MZ2os+KAOwIGxfqXLbrPq8ie1IJWPbOtgwHhKKoZBEPSoN7LruEYXfrDktvh26LRu3GJS057hPtG+iWLMVRUacCh4aBIu+WrFqoeiFKuZlWFz3/y3Lzq/1CEHQ0C0PNp4eyx84o7fYhuLAiQs270xVRKSHEDkRiVXY2TqFnuAZF+xl1LYyOGzGoJk0Cxa2gYtnWyzzW5+5NLlPx8FYMcXFimDqD54FYKoVS18u5iVrCdcX4c9PPeOPYI1BH2Hh6uC1jMJWtjS8oKc9zYyxBd6tn/qtvwEyysJe61TywbjyMCdkOaAnlOW0Y24zqsNw3MgsSlzWDqcvJjatx7VDWu+qQHSVjcwPSXWBFxkQVg86KgIzUCT2Vezdr8EsfY53ctfW62ZXsG1+CqFNBWcU3ipWN8YeJKptGYkkJWkmU927Eq1mfxvEiWTtJ FdFk5clP hUA9snqT0EHAp08oYaQbTTPvagMH+12pL3Rezd747pA0AILz0DSmfDIkeiQcTO6hXWwUR2OFo0gs0xScVmD2kVEJIsdaRUtNh0n0doF+B6ZOpgwJInoVRL0AhgQW85E7bV/kSUFpZEtHW0ui74iVkAqEPwawwbDTQPgeu022KbzSSkAAT6ey2Mgt9Ky9wsQJKcvZdT/BTcex46nE= 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 087f93629132..033c860afe51 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -441,6 +441,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)) {