Received: by 2002:a05:7412:8521:b0:e2:908c:2ebd with SMTP id t33csp2375560rdf; Mon, 6 Nov 2023 12:12:31 -0800 (PST) X-Google-Smtp-Source: AGHT+IE6Pmp9gzeJKuZ8HodSsPwvjRZGAro4eCpeNxxcxgTeh6xGQikQIBJYrsLiTtigvvyFgXQK X-Received: by 2002:a05:6e02:1d89:b0:357:fa1b:48d with SMTP id h9-20020a056e021d8900b00357fa1b048dmr847923ila.25.1699301551245; Mon, 06 Nov 2023 12:12:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699301551; cv=none; d=google.com; s=arc-20160816; b=X1xq4RfOu7pfMZWGknET2eNmVXojTpvYXFQDBVrKWcnUELFuy+wPzoCLDH9tyfPQsR K/4s68Y2AkI6DlpHDxMaagQlSzC6Ula1i1eF1fL18Zmw5hCFQAr3+JKFPCfjxLdU+u4R q5y2wdPPt1ykL/06okIq+liZiDqE6DMFOwNG80mg/9VL6dfvJv9gLMXVzlmR+PGNhH7h PrcgNFB4DOi0dCcQbsLuLkFg9WIKpCcljbXhs2fyyOtA9SRY1pZMQAQUIhkbTeA5WX9D EgasH/P8IU5e4VxxOvjImO5HGdWEiijM9rliOpIY0WsV8ENUeQ7M//eEr9ieOjrilQBp EJgg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=CJgfMj/P7YPw/LlXCqtrAgQfJMC0kIuIB9F1taTDejA=; fh=n5KN85tQTomq0Sa/nFFC9xbnc77mxkBzF8HoolO/QOw=; b=w6rnERPrFsA008lcojUUDniD8pkwGMAy9eQmFyQH5FUVi+m+fklU0WJdYyyVsZDkWK /NTuhv2krJf9B1TVKZsApc+p5dRqCkaenKoBqAK43sH9Krbu9hVM8KbQjSIw6SqGQpkK cXx7PBYy1+ZuZKVQdJwbzh98gRfoNmrRxfZbb3XBbATHLIHxm3mxtMSOfdCcwNQQuu94 o3ls9qejayfLaVcXn6j/x5pG7egPGg7QmLlYY4QQSAv2GbORhWtSVJeqE8hUY0JfXF03 dBgEEt0GQ2nTbhNHOLGs9KeWGKlgwnAxVmbAs5aHBydpwl+Jhi8E7W1z3t7sHwOsWil/ uGIQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=RItIssP+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id s6-20020a056a00178600b006bddca27b44si9009877pfg.233.2023.11.06.12.12.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Nov 2023 12:12:31 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=RItIssP+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 802758032A23; Mon, 6 Nov 2023 12:12:28 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233016AbjKFUL7 (ORCPT + 99 others); Mon, 6 Nov 2023 15:11:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41174 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232987AbjKFULq (ORCPT ); Mon, 6 Nov 2023 15:11:46 -0500 Received: from out-178.mta1.migadu.com (out-178.mta1.migadu.com [95.215.58.178]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3BFAED77 for ; Mon, 6 Nov 2023 12:11:43 -0800 (PST) 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 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Mon, 06 Nov 2023 12:12:28 -0800 (PST) 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, -- 2.25.1