From patchwork Wed Jan 13 16:21:40 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Konovalov X-Patchwork-Id: 12017327 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-17.0 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 43C04C433E0 for ; Wed, 13 Jan 2021 16:28:10 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id C569B23370 for ; Wed, 13 Jan 2021 16:28:09 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org C569B23370 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:To:From:Subject:References:Mime-Version:Message-Id: In-Reply-To:Date:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=ocicvQ72Tk1qB2W8sIBQ3YRdRhzgFpNXK3RR4gdRbwg=; b=NevlUcvTS6Hp19tmErEen5F2+ U2P2yb3zzEaJBg7f9SYL4L0dGnORjSsUrV77q/ZeLPE1Mj7jKzjC4ddDqn4MrqLZzJ/oMPMG/50pI UzacexmqGrrTc4Cf6QUvk6nCScuKmXjVzRdQXAjBMVciagAERcoUaIxMQ4LFfaaz9+mFBsxkggH6e sc0vekP8Q/yj53cVhjDWkmyNKEqr2iDHvcj8HY7W05uXue9JR7hYUnjjdhu1TQqUML5aUoW/NoaIj 4rObOOaoT8k6OYxaPoLNI6cJHdlV9lP0bjyg3YxDxesXhfUz73Io9OWJ7XqaUku0Imm7RuLGrfgsn m1TD36JkA==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kzixj-0000Pn-Sr; Wed, 13 Jan 2021 16:25:35 +0000 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kzix7-00005s-Ib for linux-arm-kernel@merlin.infradead.org; Wed, 13 Jan 2021 16:24:57 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Type:Cc:To:From:Subject: References:Mime-Version:Message-Id:In-Reply-To:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=F9RPqmmRw2Yulu4z1uWbEkv56znu+VxUo16IevqT5w0=; b=l6XgalPzBdzKbhCR6/AY2JVYlA lqJwYilaaFastxh2iMSicmMPA3tkpHGK2nbtuQfCt3Rloz9IfX3W7LDfGSJ3YDVD4Fu1P7qshaR8N LQ9JTK7EA9EZvCsSjiiVM33aQW+XowgDd18KyMAlIukKV+o2/8Y3CvVjpYGq+OJ/SMC2Mp7n1VVMu nyuInL+X1Jaw4N9TZNyHim6LXlMJjIkYnBR1UqCaengCqLlZ1wqNYJufngAbe0N+cBOWMqHcq0jHP 0ngEULqK2228j/rSa4Y4ig2kHxPrvG9PZVBNI2fAJrnvRcf5iIQg3AnL5Y5lb2U4a1KPEru3iEpNI 8U1fePGw==; Received: from mail-wm1-x349.google.com ([2a00:1450:4864:20::349]) by casper.infradead.org with esmtps (Exim 4.94 #2 (Red Hat Linux)) id 1kziuw-006SNp-GR for linux-arm-kernel@lists.infradead.org; Wed, 13 Jan 2021 16:24:09 +0000 Received: by mail-wm1-x349.google.com with SMTP id x20so1030018wmc.0 for ; Wed, 13 Jan 2021 08:22:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=F9RPqmmRw2Yulu4z1uWbEkv56znu+VxUo16IevqT5w0=; b=WhXPKI7ka1PLsjpj9a1T3weEwxQ9LvJWks0+YFb00OhSvQC/EH0LdHWIwXnhlAzsAC LiGbUHWbpgahZqXRXhrQVQuPUYfHZc9HKOs3NRhPQNt8GTc2UZqQ5c6uFISJqXUvEJqy ewAG16gjFqiAHwmLniCt16/Fkc4q1Whks66SxS3XPsBq3eUwEo48WFXK0ER50OKScjAe GAXL74wFdc8bC/Efq5Zvm/yvWLMZA80M0TWPT7adRxArIoTdLYAMvgI06/vzVoL1xDGS dYrwxb4FDvNk9jTHQDpWcL4z/s32aZv30dBKMM3f/pXjvn17HpyRCv9nphNKV3AqzPs1 WXGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=F9RPqmmRw2Yulu4z1uWbEkv56znu+VxUo16IevqT5w0=; b=cEQU9nIG4uVWay2WfS1oG1y/ZOii6VyEMxKdDn/dSLcabl1Oi6GlUx6Df9qtfwFysn OkUZ1BxHkPzmabzRsqm50YWdfFo+JlxJsgdOR0nncRe4xurHJ5gCDjHPSaMyd5nHawFY srDIvLyO8DfH2lHt0j5voY2tkb3QukX4ZfV9o6doJeszuR9GRChR0VCSzin1fd3Oy07H nZHj8lZaMgzFK0NQdzHIcb/s+wLWH9NYFt7Xq5iZe481NNgGMMn363y23p9I4yQ1MLih s1DQNcxjTdPVmS1TVgxJVq0sbupf42JCTLy/LNz1gsHZjntfm4Hgd1NdRJaVZBu/J13b 3fNA== X-Gm-Message-State: AOAM532VsK9W0TVwWThTMyLyH1mGiDvECiE4dVH/oiSu+3B84eL5VBqa VH2l7PqSiUDBTSDxG9ib8MaSV9MYLl5bTSG6 X-Google-Smtp-Source: ABdhPJwUx1QOs+UqfzWHVhJ4l6FtbhFlt9a/4UJ2shESq0YyK1lhktj3AH0Y5/JRqWIf9rrtsCQ5ejOwFlyvUDY5 X-Received: from andreyknvl3.muc.corp.google.com ([2a00:79e0:15:13:7220:84ff:fe09:7e9d]) (user=andreyknvl job=sendgmr) by 2002:a1c:c308:: with SMTP id t8mr85582wmf.22.1610554938684; Wed, 13 Jan 2021 08:22:18 -0800 (PST) Date: Wed, 13 Jan 2021 17:21:40 +0100 In-Reply-To: Message-Id: <0e994d67a05cbf23b3c6186a862b5d22cad2ca7b.1610554432.git.andreyknvl@google.com> Mime-Version: 1.0 References: X-Mailer: git-send-email 2.30.0.284.gd98b1dd5eaa7-goog Subject: [PATCH v2 13/14] kasan: add a test for kmem_cache_alloc/free_bulk From: Andrey Konovalov To: Catalin Marinas , Vincenzo Frascino , Dmitry Vyukov , Alexander Potapenko , Marco Elver X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210113_162409_103537_49334A47 X-CRM114-Status: GOOD ( 13.76 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Branislav Rankov , Andrey Konovalov , Kevin Brodsky , Will Deacon , linux-kernel@vger.kernel.org, kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arm-kernel@lists.infradead.org, Andrey Ryabinin , Andrew Morton , Peter Collingbourne , Evgenii Stepanov Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Add a test for kmem_cache_alloc/free_bulk to make sure there are now false-positives when these functions are used. Link: https://linux-review.googlesource.com/id/I2a8bf797aecf81baeac61380c567308f319e263d Signed-off-by: Andrey Konovalov --- lib/test_kasan.c | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/lib/test_kasan.c b/lib/test_kasan.c index 5e3d054e5b8c..d9f9a93922d5 100644 --- a/lib/test_kasan.c +++ b/lib/test_kasan.c @@ -479,10 +479,11 @@ static void kmem_cache_oob(struct kunit *test) { char *p; size_t size = 200; - struct kmem_cache *cache = kmem_cache_create("test_cache", - size, 0, - 0, NULL); + struct kmem_cache *cache; + + cache = kmem_cache_create("test_cache", size, 0, 0, NULL); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache); + p = kmem_cache_alloc(cache, GFP_KERNEL); if (!p) { kunit_err(test, "Allocation failed: %s\n", __func__); @@ -491,11 +492,12 @@ static void kmem_cache_oob(struct kunit *test) } KUNIT_EXPECT_KASAN_FAIL(test, *p = p[size + OOB_TAG_OFF]); + kmem_cache_free(cache, p); kmem_cache_destroy(cache); } -static void memcg_accounted_kmem_cache(struct kunit *test) +static void kmem_cache_accounted(struct kunit *test) { int i; char *p; @@ -522,6 +524,32 @@ static void memcg_accounted_kmem_cache(struct kunit *test) kmem_cache_destroy(cache); } +static void kmem_cache_bulk(struct kunit *test) +{ + struct kmem_cache *cache; + size_t size = 200; + size_t p_size = 10; + char *p[10]; + bool ret; + int i; + + cache = kmem_cache_create("test_cache", size, 0, 0, NULL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache); + + ret = kmem_cache_alloc_bulk(cache, GFP_KERNEL, p_size, (void **)&p); + if (!ret) { + kunit_err(test, "Allocation failed: %s\n", __func__); + kmem_cache_destroy(cache); + return; + } + + for (i = 0; i < p_size; i++) + p[i][0] = p[i][size - 1] = 42; + + kmem_cache_free_bulk(cache, p_size, (void **)&p); + kmem_cache_destroy(cache); +} + static char global_array[10]; static void kasan_global_oob(struct kunit *test) @@ -961,7 +989,8 @@ static struct kunit_case kasan_kunit_test_cases[] = { KUNIT_CASE(kfree_via_page), KUNIT_CASE(kfree_via_phys), KUNIT_CASE(kmem_cache_oob), - KUNIT_CASE(memcg_accounted_kmem_cache), + KUNIT_CASE(kmem_cache_accounted), + KUNIT_CASE(kmem_cache_bulk), KUNIT_CASE(kasan_global_oob), KUNIT_CASE(kasan_stack_oob), KUNIT_CASE(kasan_alloca_oob_left),