From patchwork Mon Nov 6 20:10:20 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: 13447426 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 78FE5C4167D for ; Mon, 6 Nov 2023 20:11:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 39E246B0299; Mon, 6 Nov 2023 15:11:45 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 375D46B029A; Mon, 6 Nov 2023 15:11:45 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1C83E6B029B; Mon, 6 Nov 2023 15:11:45 -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 068556B0299 for ; Mon, 6 Nov 2023 15:11:45 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id B7B1280925 for ; Mon, 6 Nov 2023 20:11:44 +0000 (UTC) X-FDA: 81428624928.21.C550111 Received: from out-172.mta1.migadu.com (out-172.mta1.migadu.com [95.215.58.172]) by imf08.hostedemail.com (Postfix) with ESMTP id 0FCEB16002A for ; Mon, 6 Nov 2023 20:11:42 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=RItIssP+; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf08.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.172 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=1699301503; 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=CJgfMj/P7YPw/LlXCqtrAgQfJMC0kIuIB9F1taTDejA=; b=iYMxpQZ3ENPZzHmDwaBDcqbNkBXULMonOnRLSjWZgne+wnF7uxItOrXY6P0h1niod5opl7 g+cw+Rf8FuuLgNy/UtP+qrqicv0E1SEE20KQ1CFV12LqTD3MVywGRVeqmw/SWhXpHkQTAk NSQIwgl/988DUZ2k1Q+gjxB2o/jvPy4= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=RItIssP+; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf08.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.172 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1699301503; a=rsa-sha256; cv=none; b=EOmsxrCcrw5W6ef7kBPxFtS2AhlrLEaXAdcqwF2rYbPFP63BlWZTGA869N68iGui9YKWAj uQ0j0bNA9ZEg6JXkdb5mFSz2vjepvq5hRHn7cal0ITVEUkHtK0vivM+n8xfPJY1pJGXJrk 5EtA9Y0Ar8BV/RsucXZ97gAWpdzBfoI= 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=CJgfMj/P7YPw/LlXCqtrAgQfJMC0kIuIB9F1taTDejA=; b=RItIssP+VxjIXsAX7VSo3+5Kc1Se3hq8f7f/cbQzFTUM6RKLnsGl4CAP/yG2vvcChay10m sSL9QfjTxiEsvbgUXV80kI0/hG2Tid6pbZndkQAOOqmBYC5P+q26F0bUqJ3gTSejNUH0wQ 8h5rzVebWZwZH9+D6V1P/RwTQQDBARw= 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 11/20] kasan: introduce poison_kmalloc_large_redzone Date: Mon, 6 Nov 2023 21:10:20 +0100 Message-Id: <79f306b7713aa06876975bbc782c392087652383.1699297309.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 0FCEB16002A X-Stat-Signature: chynq75qqqads7hid3dbkzn4edwmecx1 X-Rspam-User: X-HE-Tag: 1699301502-399187 X-HE-Meta: U2FsdGVkX1+dqGxg3yDQiM6CwU/mF/5L6uQb8AvK3zdW7vDvVG4xZDlHUXv00T3h6LPqgyzJ4LlaSmA+xA68u7Q9+6G1apc2skcqCIu/ZsH8OP3sXDdd3ckMsbk7WcBjM0g0c1I1fe7qgJVZEDaEJu+YZ+BCUtd7wcknHuFaXjvb1yaH/O6iEcrl8VnLJccWQnErLV9aZoQpsZlae/EZlXColZLoHrAjpiAFfG/F6cP24zrDmBtzZFboC6SQ17n44G4rs1uVD28xzebtiu6OQcHlbLkoXLVdgb78EofoAi3cuX1gkA10wu/0wqUbLtaZDVFZdyQJ6UVr1uYQ3O/dcQ4ec3h2K7friHlKZlPqHoICQVO1DuEuSl5qKJKB2K7wQAcpoYaP8bcEHn0MCdxFlol1x6FzA1cZ9+oeVPRW/Fm/QEGdn/M+5vU1pJBFH2gbnlkbWww4OON5vqycZSTS4YVZqLzNsCZiOOPkqUay7BZZMojfDbz8OJl6TTOjVo960e9LhHyXoptyFcgWCzyNMffwpx4jCTnfL66t4lp68V+EyLmtm6Cs/sMbJfNeWx2aH8XagCb3G9V0Hs+uwl/9RChXarFe+GtqDDACXphVVwEPdnlqm9IiGdaZ+giZYrHVvjcowdpi4VYnf5avKjVvKk2PLNBo03prrAUG5lcydx3P1UNvW2ZXVMWJVTRMX0kBicEPrELjFZlckMECBqhqCCkqcCogATeRVtse8SuvhPnVxGCcAv7Lr9De1UEsNLMM9CAzEHaXBBNnEnvBQtIBPJwNJ9y/zkZCPvSOZaftGVrKUcJLdOfW8oO7gxA7ePY8m3p2TM6nV3UQAtvWjv5NL5r0Y/0ahzIg6ykeWN0qnnn7ikXsyilF1PmEGdfWYJ8XJwWa+DUXipDAkViNg1gambTyT5qEOLNqTaCSxNn/kqfi5Ko6Ut9Lnub867em1PLMwozVYpBpOP1dy4+jqE9 ykaFB6wD DVNDtCDHG0q3b3jXn6pnmbQ67FVhtf9nLkofsBTcycfG/qvuncc2O5y9rfcj6EgdhSgndRUmztDrKjNrfOJLcpC2OlWV8s476zw5amaOK5Vx0n07U9fxZB3stIrPl9DJu823RCKFM3TgiO2jj4v2oqN+gBcCEXm1G+2Mr234/6Htoj5dxeybirBE9w+ewvj9oBRl+jEd8/kAj29Y= 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 ceb06d5f169f..b50e4fbaf238 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -353,23 +353,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 @@ -379,12 +368,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; } @@ -392,6 +394,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; @@ -409,11 +414,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,