Received: by 2002:a05:7412:d008:b0:f9:6acb:47ec with SMTP id bd8csp216921rdb; Tue, 19 Dec 2023 14:32:12 -0800 (PST) X-Google-Smtp-Source: AGHT+IGqzd53o6UlKQI67klG014fA2J6AgN17qHMWCJPwz9x4HoDdTtHlzg5o/2eDhXM8bfuzc8I X-Received: by 2002:aa7:9f0b:0:b0:6d2:7e5f:8c6 with SMTP id g11-20020aa79f0b000000b006d27e5f08c6mr3084836pfr.41.1703025132403; Tue, 19 Dec 2023 14:32:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703025132; cv=none; d=google.com; s=arc-20160816; b=IUYp1fSeEEhk3VS7UsaJr7ZvK/hjb9xXrHbzuyRs/DP7/aL8RUWKdF5haRZiWecMY0 M7tKXk5Eaymy47GZpA/4UfAbwaHSH/+yhRyTm+qomnqt0IuVTTNRQthpVjb4/tug8dIG dl6cz7F+78kNVP/tOqfJ2U8waUoexbi1ebS2rer5EmdQjwGxI+PY4Ap5Lyq/XUMnB8iR yx4or/16aoH+6XIGLdeN6ZiB6mmasjYF97xCNsUiA6L5svu0xYU6E9AgMIf8H3AhcvNU ivs6dmDDeNV3vfLjn/hmpSY5TOt6dYoiBrkmNm/KrfZGboYuaSluQ39VZvehGkDEAIe0 eeAw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=+EoZxIVuEAKv7x5FH4zs492QikDHeQQIvGcyYCPoQRk=; fh=AIzsiqkKUtrB7QnM+XRgAblgvDINIC5pDvD0mg8EzrU=; b=RNzAStb2Qcz8T37ljdGojcOjtjD4IBCbsOCp3nZBm+IvMvchp+XE3uFuRbjqZjV+LR SH7HTtBBAaLndZbw2iguvSoG1ZvIIQG4UJ7ryI6KafxqvFf51P8l49Xj0zf4XBPv1taT ctSjwcSr6/giC45+3Ww0YeVk2TSOPG7NXYfIklb2+oIgZpIyyFgaI32dRWsI/sP7sQ7n EaXwwWhAFLJ3UDFlVlRA4DHHSqc96MPAptOku7hcb+zuotnimEExFeDAqWLdMw+laR/n rS0az6sKacKd8nNTg8moqHIOQihLAZffuLq7ZbN8Zj1mbONXHIVTy4fI/r9Q++ZMJL30 mY4A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=SkAJqCw+; spf=pass (google.com: domain of linux-kernel+bounces-6052-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-6052-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id h16-20020a656390000000b005b86142ee56si19623165pgv.277.2023.12.19.14.32.12 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Dec 2023 14:32:12 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-6052-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=SkAJqCw+; spf=pass (google.com: domain of linux-kernel+bounces-6052-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-6052-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 14D7D285282 for ; Tue, 19 Dec 2023 22:32:12 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1B3AE3DB8D; Tue, 19 Dec 2023 22:30:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="SkAJqCw+" X-Original-To: linux-kernel@vger.kernel.org Received: from out-180.mta0.migadu.com (out-180.mta0.migadu.com [91.218.175.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A54F13D0D5 for ; Tue, 19 Dec 2023 22:30:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=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: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT 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, -- 2.25.1