From patchwork Tue Dec 19 22:28:55 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: 13499305 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 920EFC41535 for ; Tue, 19 Dec 2023 22:30:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E8D008D000F; Tue, 19 Dec 2023 17:30:24 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id DC4408D000D; Tue, 19 Dec 2023 17:30:24 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B784E8D000F; Tue, 19 Dec 2023 17:30:24 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id A55158D000D for ; Tue, 19 Dec 2023 17:30:24 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 81F861C0CF7 for ; Tue, 19 Dec 2023 22:30:24 +0000 (UTC) X-FDA: 81585012768.23.D7B71C4 Received: from out-182.mta0.migadu.com (out-182.mta0.migadu.com [91.218.175.182]) by imf19.hostedemail.com (Postfix) with ESMTP id E30751A000A for ; Tue, 19 Dec 2023 22:30:22 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=SkAJqCw+; spf=pass (imf19.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.182 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=1703025023; 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=+EoZxIVuEAKv7x5FH4zs492QikDHeQQIvGcyYCPoQRk=; b=0apfdUurSAfJAh81nnw2y7+Xdw3Tkq7+USExYW5l0kskG54ppG0R/mQufV4mSHeYXNSls/ NpU1XSkNJwHL13UDrigwICigMo5Fo1mJoCvB4XJQxO6Rs5/6cBnPMogmNdopiNyp0jv4xq iET3JOm6AWRrbkd4mweHzIEI2bbHTiQ= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1703025023; a=rsa-sha256; cv=none; b=gKZKGC19ykGQOzEksE/adD1XODbfcCD1wf644rySyh+jfSM6fyrI+/1LWm/Z//8ZUCwdpk MHVPELrjKBYdTEhj5vtsyxBW7cBeZHMf78X8RXfSB6lb2cOOZi5XalhhXi2FmQPsKCTllc HPWeIrHSO4K6ykvrz+JTnXkZK+MYeVs= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=SkAJqCw+; spf=pass (imf19.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.182 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=1703025021; 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=+EoZxIVuEAKv7x5FH4zs492QikDHeQQIvGcyYCPoQRk=; b=SkAJqCw+CpgpYcxkKbpNsgiVRs3jZsJLDCK6h/+n2tye0d1nsMHpkQEKaGwzu+Y3ortVD6 2V4tHBKknFNLP7uf44CoMBA1yfnNNq11AYTUObnX7PBrHpXsVtYRLuuPg5ub4gL7k2ldkm fH/a/eow85Q+6vMR0kRodOwNov6scpA= 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 11/21] kasan: introduce poison_kmalloc_large_redzone Date: Tue, 19 Dec 2023 23:28:55 +0100 Message-Id: <93317097b668519d76097fb065201b2027436e22.1703024586.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: E30751A000A X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: gxzypdqium5eyixsfz7q1wrrwsjrf7j6 X-HE-Tag: 1703025022-287385 X-HE-Meta: U2FsdGVkX1/yQCNGdIRktSmxD+tLDruiZtwH+Is+Lm9nm3F/DKkQzmo3hkzhkqRn2HWlmlrAL5G3Umkoy5f4lvDbakAtjw5EoMRR7KN+Yw3/edyE/Bqk22Caz2c/NDEsczVbqWa+/Ap5SYXjKJws2+E5r3vw/xA0FNMyuiG0BaQbxPfd0VxlUACbAvaqSlGGUmwRYNQfbsm0sZuzv53Bk2b3U4q+NuvvnxZo53S51VUyVDGSvUQsPRTyJxV6pUx6xO++CADMS+gvq4ix9WJa0rIT8bcIxICg3AW1U6XodHZGXPGp/4ZdZJ5vnmkHgoNc8SPlvM2OZUiYeCNbfsp4efCtEtypb61iG88h8cIZa2pl5pmocfM6twVzGLMN78FOsuoylGArhMnUz9Oow7bk8hsrayfuzaVtepuKp5fJOunc+TidgjFreQ7huV4oIAOew7hviruCh3paXBhbJlorFLuMZ9jYde3+Q5vnqGBy+isPkDrjoz4FCdt3b7WyPOnsRLQ2YaXPUzYjKQSgX/mub/FSGxgbnE4HeQCveaxyElZM4xK9YO/ChrBd99nMUM8SDlk+3+gG5xu5k2ywoRi6giUnzPKvqL4aiL9Iu0my/niXPiWxFwTikdSga+fQdRnmM+A+OIg/ggilT1UFUD6hLUQ3C3OtSGnMgA+7b3Bij83yguVnZt/UvDmAkg294heURssJM63Z+Qr+aX69/28uyaWgdFF+lGS+i6Zj7rO3AivkEWjLshCp1rfENANrQsiZwh1t7j6dhbSpfpI0FNbNTcbRmEbd5JEwgKVObfjrE97uz/By5kBw9oveQLDbm8J/j4D1xb8lnMUS7rFqZ3UZFTa07tGFDQ0iIoSgNdgA2bJtWzlDND1Az/Owm0CZKbWnFFzVnNFrhqTV2b25sVFRCifNAyH58pGJPoVDaslIvqRfQWeG/DKOsr0K+POOTtbzSZxVmxY9ErQ= 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 Split out a poison_kmalloc_large_redzone helper from __kasan_kmalloc_large and use it in the caller's code. This is a preparatory change for the following patches in this series. Signed-off-by: Andrey Konovalov --- mm/kasan/common.c | 41 +++++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/mm/kasan/common.c b/mm/kasan/common.c index 1217b260abc3..962805bf5f62 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -363,23 +363,12 @@ void * __must_check __kasan_kmalloc(struct kmem_cache *cache, const void *object } EXPORT_SYMBOL(__kasan_kmalloc); -void * __must_check __kasan_kmalloc_large(const void *ptr, size_t size, +static inline void poison_kmalloc_large_redzone(const void *ptr, size_t size, gfp_t flags) { unsigned long redzone_start; unsigned long redzone_end; - if (gfpflags_allow_blocking(flags)) - kasan_quarantine_reduce(); - - if (unlikely(ptr == NULL)) - return NULL; - - /* - * The object has already been unpoisoned by kasan_unpoison_pages() for - * alloc_pages() 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 @@ -389,12 +378,25 @@ void * __must_check __kasan_kmalloc_large(const void *ptr, size_t size, kasan_poison_last_granule(ptr, size); /* Poison the aligned part of the redzone. */ - redzone_start = round_up((unsigned long)(ptr + size), - KASAN_GRANULE_SIZE); + redzone_start = round_up((unsigned long)(ptr + size), KASAN_GRANULE_SIZE); redzone_end = (unsigned long)ptr + page_size(virt_to_page(ptr)); kasan_poison((void *)redzone_start, redzone_end - redzone_start, KASAN_PAGE_REDZONE, false); +} +void * __must_check __kasan_kmalloc_large(const void *ptr, size_t size, + gfp_t flags) +{ + if (gfpflags_allow_blocking(flags)) + kasan_quarantine_reduce(); + + if (unlikely(ptr == NULL)) + return NULL; + + /* The object has already been unpoisoned by kasan_unpoison_pages(). */ + poison_kmalloc_large_redzone(ptr, size, flags); + + /* Keep the tag that was set by alloc_pages(). */ return (void *)ptr; } @@ -402,6 +404,9 @@ void * __must_check __kasan_krealloc(const void *object, size_t size, gfp_t flag { struct slab *slab; + if (gfpflags_allow_blocking(flags)) + kasan_quarantine_reduce(); + if (unlikely(object == ZERO_SIZE_PTR)) return (void *)object; @@ -419,11 +424,11 @@ 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 { + poison_kmalloc_large_redzone(object, size, flags); + else poison_kmalloc_redzone(slab->slab_cache, object, size, flags); - return (void *)object; - } + + return (void *)object; } bool __kasan_mempool_poison_pages(struct page *page, unsigned int order,