From patchwork Thu Jan 14 19:36:30 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Konovalov X-Patchwork-Id: 12020897 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 991DAC433E0 for ; Thu, 14 Jan 2021 20:53:38 +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 6389F23877 for ; Thu, 14 Jan 2021 20:53:38 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 6389F23877 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=2HZ2Ofjt2C8R3uv56Ipff6UHp060R9mAd1sXRNf1gnk=; b=OYl59Npv0ONqisuL0T6pZFBxu OpyMlCpAaXu2Zmzl1DbUzN8jEuAEqDEaXywj7jfR6vnjkf+YvUXUkn/0AeJ7GRKl5xdTXQlCLq0pS 6vyJVTgdgIxBt4Suqnd8U0Rim65S3BTMSBBR3pKH0l1UcPR7Hd5Ulohzi+3Q1tkz8fUTPIqOFiuKQ evs9jJJrN1xOzxi6k6gLUYroQ3PPHZDhlBeDUwIIqgePwN8GWadmlD4uSI+HSVpFYUMALQIUVv0hR JROM3SebkxWZEQwiLlxf/aw/JC5wOVwV8rOeXplNUf/oUVNw+uZNAa3l4F8VAPSYCt5altQo+nAO2 fb9+0BJWg==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1l08TY-0005bN-89; Thu, 14 Jan 2021 19:40:08 +0000 Received: from mail-qv1-xf4a.google.com ([2607:f8b0:4864:20::f4a]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1l08Qg-0004MK-Sl for linux-arm-kernel@lists.infradead.org; Thu, 14 Jan 2021 19:37:22 +0000 Received: by mail-qv1-xf4a.google.com with SMTP id cc1so5473137qvb.3 for ; Thu, 14 Jan 2021 11:37:08 -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=mpVlk1+Mq66Mf4g3XdhCLFVXRKuPdNXXWU/IOGoVVBY=; b=l3zgn02fnCG02/GorQOuHum5hC7LgdsREdZg3KtQjsCdJpLVHBSEXZpZE+iWL9Unw7 F9Uroqs0bYp4/RbfxoOOkgqbmSXgTtrgphGuEFTA93qSMKgiloOwLBZQs3QpTF7Nmp2g sKCerQ7G12PWMi2HkcrkCiLDhaneDzSGSgHrDLAJ1TscgLv5lLHgrVzXCufFbPcT2nZE voBucxeaWnqMUdm8c2zPGQYLK8EfM+ixWGByvozQNXQuiByq2LFuypPugMvpqP7atzdr jpZvfhU856Qc1ZjCfbnDdn2FbCx5WJ/G05kPvVtwq3GO1lpCNinWTgcm4Ms026UlnJgQ tCLQ== 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=mpVlk1+Mq66Mf4g3XdhCLFVXRKuPdNXXWU/IOGoVVBY=; b=YwOa/w2Mp4gqCPeuBkAGo4JO0ZlU9tXWVcI899lR9iRyRoUf8A/5DJsxHBDDvNzKuI VtDEe8iixkgbnqrzlOYONolBFYqSEu8POxwtWakiJS9GrYJTW1fBJKHaso/uDmgDcql6 5ZMixm4hvuPOQSRn7A1sXTIduhjib5EbWRStAAwbUlwpq6KuD5Tl10Qbj8iY8C7u1/Bp IhhE1Ih8UhirU1fgDakyiSDQeDeLGq12fDv/GbPinJeHk4qt+Pe4j2g9RbM0/Mf9UodV 8VK21iI7fGgRA8DKS88M8yfyF7nZwMZF1QKvp22YkV7Uy6RyqOA85mnxbmi9JrNFnsl2 Dxiw== X-Gm-Message-State: AOAM5309OF9Rqb6JlNwrFH1wahVVQXn70cZr4ZRxtuzM0AlOMB5Y+29/ rIyHiPfnU0zgDGmJXSyFghqseJoIl7ECX4SU X-Google-Smtp-Source: ABdhPJyGquTeLUlZdqo6VBCEwifJl8lXrre/ob8oN7AAyol/9rBOwLOtlLOHPIxvikPOYLtv2pJ5MGos33xGzZJe X-Received: from andreyknvl3.muc.corp.google.com ([2a00:79e0:15:13:7220:84ff:fe09:7e9d]) (user=andreyknvl job=sendgmr) by 2002:a05:6214:b2f:: with SMTP id w15mr8831064qvj.8.1610653026992; Thu, 14 Jan 2021 11:37:06 -0800 (PST) Date: Thu, 14 Jan 2021 20:36:30 +0100 In-Reply-To: Message-Id: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.30.0.284.gd98b1dd5eaa7-goog Subject: [PATCH v3 14/15] kasan: add a test for kmem_cache_alloc/free_bulk From: Andrey Konovalov To: Andrew Morton , 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-20210114_143711_090085_DE4CDEB1 X-CRM114-Status: GOOD ( 14.81 ) 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 , 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 no false-positives when these functions are used. Link: https://linux-review.googlesource.com/id/I2a8bf797aecf81baeac61380c567308f319e263d Signed-off-by: Andrey Konovalov Reviewed-by: Marco Elver Reviewed-by: Alexander Potapenko --- lib/test_kasan.c | 38 +++++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/lib/test_kasan.c b/lib/test_kasan.c index ab22a653762e..a96376aa7293 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,31 @@ 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; + 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, ARRAY_SIZE(p), (void **)&p); + if (!ret) { + kunit_err(test, "Allocation failed: %s\n", __func__); + kmem_cache_destroy(cache); + return; + } + + for (i = 0; i < ARRAY_SIZE(p); i++) + p[i][0] = p[i][size - 1] = 42; + + kmem_cache_free_bulk(cache, ARRAY_SIZE(p), (void **)&p); + kmem_cache_destroy(cache); +} + static char global_array[10]; static void kasan_global_oob(struct kunit *test) @@ -961,7 +988,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),