From patchwork Fri Jan 15 17:52:51 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Konovalov X-Patchwork-Id: 12023585 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 82AD9C433DB for ; Fri, 15 Jan 2021 17:57:37 +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 26F8022262 for ; Fri, 15 Jan 2021 17:57:37 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 26F8022262 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=JdnYxoJXdeM1iHqIa3o6a0hsTeUNcrB7TUtJP5lhWKA=; b=DIgPl5fMC940sfDywjTRvjq3y BCrlgVx1l9NjKFrX3wdAhyyejG6HQD2PApOj/QFrQG9nZ2O88H6DrLqpPTEDwuz69F3ch74PnDDJm QFGmTQjqucsdfo8LbgfRQwTXk8H9yUO1RjKY0WpA9zcuirmvDb9vKkamrAHfVmlGRQnWlQImBfnPk aZf9nL6pRtpeMtVVUN6o8LZCv67BJK7FwL2R2K3bR4yQae3FBevZZoI/89XrACGetQTHsqI3nkumS Ar6vP9APZaD6yLW5wO2T3HaxNszGLioH0mtOfIUZniOx1O++IHQID0r7Ck+TmW7rwJvPbGtw3AQir TdH1+mWIg==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1l0TKK-0004Xm-03; Fri, 15 Jan 2021 17:56:00 +0000 Received: from mail-wr1-x44a.google.com ([2a00:1450:4864:20::44a]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1l0TI0-0003aX-Iq for linux-arm-kernel@lists.infradead.org; Fri, 15 Jan 2021 17:53:59 +0000 Received: by mail-wr1-x44a.google.com with SMTP id u29so4482735wru.6 for ; Fri, 15 Jan 2021 09:53:34 -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=KKvTna9pOG94VdqPVmZRxWtkege1Cyuqg2hRO1ClK+0=; b=ed3FebypFmJx/L1iuiWNaLMG07hyk3w2HThAWfe4DA6P0l6iiWWdLglY+RugS8O3x2 8wi5BH/PlnIMZ7Za8gNVHK59XI0kibCPJPsviTht+qATHJv+zUQaQAOo4AMD6J+aPFtx yT/cXmmvlg3q4J4t4N8foKtC82zZnW55Z9nKA0gh4TuEjuwv1C+94wUT5NhDamdRO0kJ yxabOTkDgEjUXjdBoGIDsRBVJNzXwSDA5vTrytu6Lb7+7VfDbXJeAQIgn8QFGWywwIzt tqV9KJl4xrtHmGlFext79AI3i47CDDdRBBENU5OfB7CZGAc363k/tr6ZNqAgeQ3MMZ3F qDNA== 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=KKvTna9pOG94VdqPVmZRxWtkege1Cyuqg2hRO1ClK+0=; b=NLTxadGiilZqNDe8JRitOJj1bOUPDcV5RT2Xc40R4+EM6TN4nYCvE2wWVh3I1llGnN qvPyazUaFgANQnkj8zy5Lv2X1x787a9BN5xm/zEtboxvdVIDRTp13H0Hn6sypK0zCLGE WPcpO0lhGYUJKnBokq6cYs+RuZF7+cqhB0f4omGAulTFRvvnzZquCKk1c+3TVpD7Z9F+ zCiEtsew1bvG5uTt45v8VrD3949CdwZVG4HssKFM1UuL5nWLYs8xnaVu90RgkbwiG4Ub CQapB4Ht9Oj+20QlRNG6fe90OZcN3NPNVkpgOWFgII2ipMMfvuvT6vFhXG/eORvbgKFN pHvQ== X-Gm-Message-State: AOAM530ag+z5K4JVi2HAXY9678OJOqzRbWcxb/I1OpKodccwrUSsKlBU 3AmfgbfZTubPw9KdnuwKJtSu/UmHpOhFg/0b X-Google-Smtp-Source: ABdhPJyVftmTjOwG8sQ/qHZqkBUIVpPntUaupbXGD/60w3ULfDb6BFCbT5DhRAHKYT1DG0qLEs4DoB2QgTHuIzaj X-Received: from andreyknvl3.muc.corp.google.com ([2a00:79e0:15:13:7220:84ff:fe09:7e9d]) (user=andreyknvl job=sendgmr) by 2002:a05:6000:11c1:: with SMTP id i1mr14588719wrx.16.1610733213887; Fri, 15 Jan 2021 09:53:33 -0800 (PST) Date: Fri, 15 Jan 2021 18:52:51 +0100 In-Reply-To: Message-Id: <418122ebe4600771ac81e9ca6eab6740cf8dcfa1.1610733117.git.andreyknvl@google.com> Mime-Version: 1.0 References: X-Mailer: git-send-email 2.30.0.284.gd98b1dd5eaa7-goog Subject: [PATCH v4 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-20210115_125336_820157_2D0B94D8 X-CRM114-Status: GOOD ( 14.70 ) 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 Reviewed-by: Marco Elver Reviewed-by: Alexander Potapenko Signed-off-by: Andrey Konovalov --- 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..4ba7461210fd 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),