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: 12017295 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=-16.6 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_INVALID,DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham 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 88737C433E0 for ; Wed, 13 Jan 2021 16:22:30 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 357DB2313C for ; Wed, 13 Jan 2021 16:22:30 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 357DB2313C Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 9783A8D007F; Wed, 13 Jan 2021 11:22:21 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8D76D8D007E; Wed, 13 Jan 2021 11:22:21 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7A1A48D007F; Wed, 13 Jan 2021 11:22:21 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0103.hostedemail.com [216.40.44.103]) by kanga.kvack.org (Postfix) with ESMTP id 62BB28D007E for ; Wed, 13 Jan 2021 11:22:21 -0500 (EST) Received: from smtpin28.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 1FBA0824556B for ; Wed, 13 Jan 2021 16:22:21 +0000 (UTC) X-FDA: 77701269282.28.mask19_3a14aaf2751f Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin28.hostedemail.com (Postfix) with ESMTP id 59DB26D6D for ; Wed, 13 Jan 2021 16:22:20 +0000 (UTC) X-HE-Tag: mask19_3a14aaf2751f X-Filterd-Recvd-Size: 5982 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) by imf25.hostedemail.com (Postfix) with ESMTP for ; Wed, 13 Jan 2021 16:22:19 +0000 (UTC) Received: by mail-wm1-f74.google.com with SMTP id f187so1031709wme.3 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=IX7Z7b+sIM6I600rZIMoHUrB0hej4EKSv2Tor5XwEy/MkRsktrravIbPzUQBEKHBOc VELULKWOOsEoTLQ8DVKBtfEeFKFACtMpuf19VTz1PiqB8Bv0zy/umYBTRhmByIcLJ2kv QW5IHJSYy9vC86+QI4mnvISqud2K7KV/Y2vj4Y3Tsz/OOabyTcembV6DHhaAkxuGvGb0 panYhGQqNmFA2H0jV6gLOQyErVSwpbxHkjPMsfbzf3yGuA92BZne6xLiSBcj1A/kpNso VufD/4H8RLBl4NLm0noNtfdQG07WqumwnnF+MFdAVw1d7pwnB5vBY/BcOmhnwsgJBnP8 oUmg== X-Gm-Message-State: AOAM531ypDMpdVAFsoRXfvESBVk1cpuumf8zbav5TDElBQBEUK5pIhIN hrt8LY47o9gHjXlZDhsPAeVkpxdH+0vbD5jw 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 Cc: Andrew Morton , Will Deacon , Andrey Ryabinin , Peter Collingbourne , Evgenii Stepanov , Branislav Rankov , Kevin Brodsky , kasan-dev@googlegroups.com, linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov 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: 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),