From patchwork Mon Nov 6 20:10:19 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: 13447425 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 95045C4332F for ; Mon, 6 Nov 2023 20:11:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C52816B0297; Mon, 6 Nov 2023 15:11:44 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B8C306B0299; Mon, 6 Nov 2023 15:11:44 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9904C6B029B; Mon, 6 Nov 2023 15:11:44 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 84A536B0299 for ; Mon, 6 Nov 2023 15:11:44 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 59B6A808E6 for ; Mon, 6 Nov 2023 20:11:44 +0000 (UTC) X-FDA: 81428624928.23.4827860 Received: from out-173.mta1.migadu.com (out-173.mta1.migadu.com [95.215.58.173]) by imf23.hostedemail.com (Postfix) with ESMTP id 728D9140022 for ; Mon, 6 Nov 2023 20:11:42 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=d537NTwR; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf23.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.173 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=1699301502; 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=mCp2zVxGwC1RwRlWPaWBJT09DIYULTSyRat3kYinLuo=; b=TAEFGKELJxqtOjOKpJT5gLBdoEljNLBMdpSjek4qp4FOmVTquGTyxDabZTk2DfGki/nau9 aHRcA0JGKHx2OwJlXS4hiJo+j8VwnhMMLOEOTXTgwHkvVoBKv5nk8Hxu0660yP9H9zueld vOw4jrWKjuWKM0GebpJhSxdwR65r57Y= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=d537NTwR; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf23.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.173 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1699301502; a=rsa-sha256; cv=none; b=IZEdWzCvg2PKHttlCfJnHkFBMTbvZ4RxLVgjrhp/ND/OxdffpX4j3vFI3OkrV3Avh+4Af2 M3I29FZBi5osfJlzcX02lVqy4u05MMNwXWxwkVpSF6mk8MAMCExzEU2uqPMuwxfjLNwK3H OLhiG96yuR3RrgX5LkjbFwaiBs5liHk= 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=1699301501; 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=mCp2zVxGwC1RwRlWPaWBJT09DIYULTSyRat3kYinLuo=; b=d537NTwRPy7XHKrbuKDfSOCLE08WU2ewkqbATysLDcBjkJxGbnCtGoml9BLyk6/HLbiYso RIWijpSbPsp80ot8o8jmcjVSegnslv45cg4Nvd81jyQ1AX00FXznhsA0/PT1hlWfYlVBc3 NcK/vgYL8Yq2KxPrM8UJdzKMgl1NbA8= 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 10/20] kasan: clean up and rename ____kasan_kmalloc Date: Mon, 6 Nov 2023 21:10:19 +0100 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspam-User: X-Stat-Signature: ajeut5e3iq8zywfh1zef7f9bcwe5tfbp X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 728D9140022 X-HE-Tag: 1699301502-794605 X-HE-Meta: U2FsdGVkX1+Lbn01zQQ4KC8UX3BcFaJTxMuZ1tCWUfY8lb7Tr9nf9eScBk8Pmcux7zBluOUhKQV3/wMYG9K6Sp3m02ySULpQYNO75PAmHx1GWbiVz9iRM/thdEnV/OB6CPz1Dw9jPwupQyQRLl4DgCe9W5KPubzH2HLNbxxIOcucVxuCiApyFiQ7WpOdJhBaLG8cXClM5l3CPuXsPsYHr47ikMsAbjAIY6mdXJ+iyzQ2F/TSOw/VJPDAEzcKtrXcDssabPSMNJkMFNA5j0XVHz1sEv594xXeV0ExIgvqN9gnCz1AMgap7c+eEtRbBGo9Fm32KMCV+mCNaHOEPLYRbHVpNFSjFFwl89OmII/jZ0w0IlYuaq7Jcx7LFSFuua4LmTJUaJuSyBkGw7rqui51AV6y6xLsTwEDEps/v9GqIlS4d/RZwFtfYE5Z53Ns8LaWnuKLEt9z9DhprlTtK6I2P1xGzanpHh4yn/TQEr+eYh25S33yIGu3WjF7BLyaScq3dj6CN6mvflSLFCVHMRwnkyeY1mAqSgXlUGGjPGTSNGC3DlnqZLVHENAyZ4hk7ywjf4fZLWFPEDfmjTGOZAqbmY1Y53uN0p2KdBdqoaQ8aI8SlCgK2TtIXOeIvawzOHBQcCVIX6u2O1nmDnRQRI/LEbqAfKXIpn9yFx6gZQ6JIvH6QM5/v26z4UpaSq4StQNjqw/s/GnHIEiYVtnXXRCdPAtTQudQAr0ZdTKoXDlocz39c7wosjfm6N2pvSfn1LMLjcTSG1q1OMMuMfdbC6PJ1vGXRsDOJbc9HjZz+kzQChXeGG5f1F2x752hpggK6nHrynXRScV4peAilzXrvOuzpigje2TYbDZwAUiLXJDYPd/tfgBjWmWWbE7oLJDAFvLXz8ERm2GKSELezWJmvgfFG467awmCJLs6ZYdxKe45jzNEMzP4k+5HoDivYIOmGcJnEPX6uBYrGYFI4KlLAid XzbVslUw Bz7Wb7d6/mR8GNu06xVWLT3E+PEnWwb39ZX3DVOXsoFCu1njzqp9b7DtNJUgnrZAyO+tfTffQPbIN5i5VoS7n0/cnEbrVxJpvl8wE8TAFtvcwNraANGnI2x74bdrrnKxOOLN5YrYtLDsvBdu3odC9tfjxAVdDWhGaEarmIsAKjB9Fndz64LdcstkNF3ztVyiTw1S9MsiQlRwvUkLT+3KBCxdzGQ== 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 poison_kmalloc_redzone helper function that poisons the redzone for kmalloc object. Drop the confusingly named ____kasan_kmalloc function and instead use poison_kmalloc_redzone along with the other required parts of ____kasan_kmalloc in the callers' code. This is a preparatory change for the following patches in this series. Signed-off-by: Andrey Konovalov --- mm/kasan/common.c | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/mm/kasan/common.c b/mm/kasan/common.c index 683d0dad32f2..ceb06d5f169f 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -302,26 +302,12 @@ void * __must_check __kasan_slab_alloc(struct kmem_cache *cache, return tagged_object; } -static inline void *____kasan_kmalloc(struct kmem_cache *cache, +static inline void poison_kmalloc_redzone(struct kmem_cache *cache, const void *object, size_t size, gfp_t flags) { unsigned long redzone_start; unsigned long redzone_end; - if (gfpflags_allow_blocking(flags)) - kasan_quarantine_reduce(); - - if (unlikely(object == NULL)) - return NULL; - - if (is_kfence_address(kasan_reset_tag(object))) - return (void *)object; - - /* - * The object has already been unpoisoned by kasan_slab_alloc() for - * kmalloc() or by kasan_krealloc() for krealloc(). - */ - /* * The redzone has byte-level precision for the generic mode. * Partially poison the last object granule to cover the unaligned @@ -345,14 +331,25 @@ static inline void *____kasan_kmalloc(struct kmem_cache *cache, if (kasan_stack_collection_enabled() && is_kmalloc_cache(cache)) kasan_save_alloc_info(cache, (void *)object, flags); - /* Keep the tag that was set by kasan_slab_alloc(). */ - return (void *)object; } void * __must_check __kasan_kmalloc(struct kmem_cache *cache, const void *object, size_t size, gfp_t flags) { - return ____kasan_kmalloc(cache, object, size, flags); + if (gfpflags_allow_blocking(flags)) + kasan_quarantine_reduce(); + + if (unlikely(object == NULL)) + return NULL; + + if (is_kfence_address(kasan_reset_tag(object))) + return (void *)object; + + /* The object has already been unpoisoned by kasan_slab_alloc(). */ + poison_kmalloc_redzone(cache, object, size, flags); + + /* Keep the tag that was set by kasan_slab_alloc(). */ + return (void *)object; } EXPORT_SYMBOL(__kasan_kmalloc); @@ -398,6 +395,9 @@ void * __must_check __kasan_krealloc(const void *object, size_t size, gfp_t flag if (unlikely(object == ZERO_SIZE_PTR)) return (void *)object; + if (is_kfence_address(kasan_reset_tag(object))) + return (void *)object; + /* * Unpoison the object's data. * Part of it might already have been unpoisoned, but it's unknown @@ -410,8 +410,10 @@ void * __must_check __kasan_krealloc(const void *object, size_t size, gfp_t flag /* Piggy-back on kmalloc() instrumentation to poison the redzone. */ if (unlikely(!slab)) return __kasan_kmalloc_large(object, size, flags); - else - return ____kasan_kmalloc(slab->slab_cache, object, size, flags); + else { + poison_kmalloc_redzone(slab->slab_cache, object, size, flags); + return (void *)object; + } } bool __kasan_mempool_poison_pages(struct page *page, unsigned int order,