Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp54027pxy; Wed, 21 Apr 2021 18:17:39 -0700 (PDT) X-Google-Smtp-Source: ABdhPJznPHnropyghf/QWDEPtix6oaOJQOfCoZXUsnJG7FGOq0MzYVuatm7Q4a+hGAY043WYCpQp X-Received: by 2002:a63:f40b:: with SMTP id g11mr950742pgi.133.1619054259736; Wed, 21 Apr 2021 18:17:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619054259; cv=none; d=google.com; s=arc-20160816; b=AMx0H/Oqe2Ndti6+GK2DvTE2hAWkBeUpjb7wFu5c3sGo5EXV1CJoM//Qd8RCJrlf2e u4KNCryIbvzuavCU4YB5tGt6pZ/7/ID5QGI3EJkgEvlX8g2WabcuiiJQSXxX8zs/+nML pg1FeScEbLYiegJ+rWHIwLWjzP3uot/nrMUDBF2MdOZ+nTKfU0QkVDcJ35n9UN0ol5/q p5IuIx5lpBQDLBaMMdnT3dR+1zrMjXuTveHPyCNb3avl4RBb0zUvOSXXTf8WwdGxvmSx nUxvwfIYFHBfjGO9jaHGXPOSAXeTuj4EVg5odDW//LjwZe4F2NaYhcLY57ScxricAYUK 19PQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:mime-version:message-id:date :dkim-signature; bh=FvAswNxnPHsrYsB29334aDx0CJGCBFJ87PYQGSoXC7U=; b=Rx/ntr3khWkbyD3t2U+jbxIFRBnQPOW1yveKD6i41vS7xyyk9ZFgGhq2jeaS3c+Aex pohHKnmbMBKgF2dJVi7kuEHuu+Kpm9xTlMG6k1J0bo2htUn2szF7wwQDY4uXEeyOpOmq Epyn1rtOJImY+SfI16vrvSejUPda3UiQhik1VemsdCHFq6zQ96MR6mfP/YmS05Egex2k Xp2v1PQ0atYOy9/RB6XCZEjM5nfcKmDbP18ePTyaNYytQD8jODtn8bIsuMYfnQC2bKzn E38yc9AOD5xS47MD5u0aOag5KL4+6CW9Ktr48ydsY03i8Sh9QXS9UIhzKYFxef5zYXI4 XX1g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=Jml0dfSb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id n4si1683188pgd.318.2021.04.21.18.17.28; Wed, 21 Apr 2021 18:17:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=Jml0dfSb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235723AbhDUSc7 (ORCPT + 99 others); Wed, 21 Apr 2021 14:32:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55708 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231685AbhDUSc4 (ORCPT ); Wed, 21 Apr 2021 14:32:56 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 85D1FC06138A for ; Wed, 21 Apr 2021 11:32:22 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id z8-20020a2566480000b02904e0f6f67f42so17063891ybm.15 for ; Wed, 21 Apr 2021 11:32:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc; bh=FvAswNxnPHsrYsB29334aDx0CJGCBFJ87PYQGSoXC7U=; b=Jml0dfSb6fz47ZIEY22eqphvQEi8jUZhPeaUH3yBL4lYpGNTOxCsF3JqbFPN6YZ/FN u4kSvuOnoXcMkdH6vnguAyqWSlh+kk+5KtLPxDqphU1SbOWmB6jK4BQv5TwH7fd0Hueu tWqQMaDeLwsa03LzDzHv34/Vhahm0e+cah9sva3WQ5fO50VCeCrURAsp+zW2v/GBWnXQ 9j6Kho/ORIiadlzNR+WOjMZdnVwLU2HDIEIlBxwCIdqLBrE0DcdOhRYwqR9pFeMSXnq+ rNsf1SLYxaUXxyyC+Y7QeArAAiVuIkqIVLBZmsUe5m7L8MLzBML6u7G2tAzTOiZj3oVB 3e5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=FvAswNxnPHsrYsB29334aDx0CJGCBFJ87PYQGSoXC7U=; b=RFksRFeBcARgfQDSV/jmXtlukkyskWKkR1/l+WDyREVXHVCEVV1EP0pfR4EFFdQ82N aeP+LauZesIu0hncV8BvMq3xMH5cql7Zzd4g6uCNBo1r54Jssz1aUGUI4Tuw8FLRW0ky g1snDYTZTHG+cSVBb0BJ9UXo5rV3JImjUqqDOBOvHBIoVGeeCcbDtlAZlazWw1VEDghc yyp0O0KuTc5pRVLrGZYu4iVLRf+KN2jGcNXIP7a9eMXJkWiQQhmKd99lLY5r7INUqhce zN0e9FhkFyu5fWXAC4ShkBD8QF7WlgJiF38u6VVjvowPNjth11U7+UWkIdfUC6iVdo7B or8g== X-Gm-Message-State: AOAM533GS07wqjndV2aDcntz+fC7F+H6pVIv+DA4qor7IG/QOw8/e6L4 J9X+O8YPvTEKLJ8nxgTWf9DX/TIj3mzX5g== X-Received: from dlatypov.svl.corp.google.com ([2620:15c:2cd:202:9524:9c1f:6fff:a9ad]) (user=dlatypov job=sendgmr) by 2002:a25:3f04:: with SMTP id m4mr35514845yba.350.1619029941836; Wed, 21 Apr 2021 11:32:21 -0700 (PDT) Date: Wed, 21 Apr 2021 11:32:10 -0700 Message-Id: <20210421183210.2557462-1-dlatypov@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.31.1.498.g6c1eba8ee3d-goog Subject: [PATCH 1/2] kunit: introduce kunit_kmalloc_array/kunit_kcalloc() helpers From: Daniel Latypov To: brendanhiggins@google.com, davidgow@google.com Cc: linux-kernel@vger.kernel.org, kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, skhan@linuxfoundation.org, Daniel Latypov Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add in: * kunit_kmalloc_array() and wire up kunit_kmalloc() to be a special case of it. * kunit_kcalloc() for symmetry with kunit_kzalloc() This should using KUnit more natural by making it more similar to the existing *alloc() APIs. And while we shouldn't necessarily be writing unit tests where overflow should be a concern, it can't hurt to be safe. Signed-off-by: Daniel Latypov --- include/kunit/test.h | 36 ++++++++++++++++++++++++++++++++---- lib/kunit/test.c | 22 ++++++++++++---------- 2 files changed, 44 insertions(+), 14 deletions(-) diff --git a/include/kunit/test.h b/include/kunit/test.h index 49601c4b98b8..7fa0de4af977 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -577,16 +577,30 @@ static inline int kunit_destroy_named_resource(struct kunit *test, void kunit_remove_resource(struct kunit *test, struct kunit_resource *res); /** - * kunit_kmalloc() - Like kmalloc() except the allocation is *test managed*. + * kunit_kmalloc_array() - Like kmalloc_array() except the allocation is *test managed*. * @test: The test context object. + * @n: number of elements. * @size: The size in bytes of the desired memory. * @gfp: flags passed to underlying kmalloc(). * - * Just like `kmalloc(...)`, except the allocation is managed by the test case + * Just like `kmalloc_array(...)`, except the allocation is managed by the test case * and is automatically cleaned up after the test case concludes. See &struct * kunit_resource for more information. */ -void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp); +void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t flags); + +/** + * kunit_kmalloc() - Like kmalloc() except the allocation is *test managed*. + * @test: The test context object. + * @size: The size in bytes of the desired memory. + * @gfp: flags passed to underlying kmalloc(). + * + * See kmalloc() and kunit_kmalloc_array() for more information. + */ +static inline void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp) +{ + return kunit_kmalloc_array(test, 1, size, gfp); +} /** * kunit_kfree() - Like kfree except for allocations managed by KUnit. @@ -601,13 +615,27 @@ void kunit_kfree(struct kunit *test, const void *ptr); * @size: The size in bytes of the desired memory. * @gfp: flags passed to underlying kmalloc(). * - * See kzalloc() and kunit_kmalloc() for more information. + * See kzalloc() and kunit_kmalloc_array() for more information. */ static inline void *kunit_kzalloc(struct kunit *test, size_t size, gfp_t gfp) { return kunit_kmalloc(test, size, gfp | __GFP_ZERO); } +/** + * kunit_kzalloc() - Just like kunit_kmalloc_array(), but zeroes the allocation. + * @test: The test context object. + * @n: number of elements. + * @size: The size in bytes of the desired memory. + * @gfp: flags passed to underlying kmalloc(). + * + * See kcalloc() and kunit_kmalloc_array() for more information. + */ +static inline void *kunit_kcalloc(struct kunit *test, size_t n, size_t size, gfp_t flags) +{ + return kunit_kmalloc_array(test, n, size, flags | __GFP_ZERO); +} + void kunit_cleanup(struct kunit *test); void kunit_log_append(char *log, const char *fmt, ...); diff --git a/lib/kunit/test.c b/lib/kunit/test.c index ec9494e914ef..052fccf69eef 100644 --- a/lib/kunit/test.c +++ b/lib/kunit/test.c @@ -540,41 +540,43 @@ int kunit_destroy_resource(struct kunit *test, kunit_resource_match_t match, } EXPORT_SYMBOL_GPL(kunit_destroy_resource); -struct kunit_kmalloc_params { +struct kunit_kmalloc_array_params { + size_t n; size_t size; gfp_t gfp; }; -static int kunit_kmalloc_init(struct kunit_resource *res, void *context) +static int kunit_kmalloc_array_init(struct kunit_resource *res, void *context) { - struct kunit_kmalloc_params *params = context; + struct kunit_kmalloc_array_params *params = context; - res->data = kmalloc(params->size, params->gfp); + res->data = kmalloc_array(params->n, params->size, params->gfp); if (!res->data) return -ENOMEM; return 0; } -static void kunit_kmalloc_free(struct kunit_resource *res) +static void kunit_kmalloc_array_free(struct kunit_resource *res) { kfree(res->data); } -void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp) +void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp) { - struct kunit_kmalloc_params params = { + struct kunit_kmalloc_array_params params = { .size = size, + .n = n, .gfp = gfp }; return kunit_alloc_resource(test, - kunit_kmalloc_init, - kunit_kmalloc_free, + kunit_kmalloc_array_init, + kunit_kmalloc_array_free, gfp, ¶ms); } -EXPORT_SYMBOL_GPL(kunit_kmalloc); +EXPORT_SYMBOL_GPL(kunit_kmalloc_array); void kunit_kfree(struct kunit *test, const void *ptr) { base-commit: 16fc44d6387e260f4932e9248b985837324705d8 -- 2.31.1.498.g6c1eba8ee3d-goog