From patchwork Thu Jul 25 12:50:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marco Pagani X-Patchwork-Id: 13741895 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BEC2319AA4F for ; Thu, 25 Jul 2024 12:50:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721911848; cv=none; b=uOXDSYiLjZLEne2VUJZg2+FFr/v0MHOEj4rgjFB09DSQT4nMQnZ+I4C1aWPpRtW1H2LBFeMsNVPkMOq+Jmz3VMaVBXjOvW8Ul9KPjVvPBYBsAVCGLRfw4sZJkO5LKK5CtqBX6pYj2Hu0UdV2luwv7bAwEdVYIc7teqcjTR9nAc8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721911848; c=relaxed/simple; bh=4XUqPxSx6vk33Fs5v1/69vqGU9Qu4OJRWDV/cVUixi8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=bDzvDf4Eee5mTjAjUbWkxsKbGFcw+QNNrPNs0zifYA2tnRDWIAGUjCShKG20N7JyHgHmRGGVwdDDhoJrAoy666mCL7B3MuksiPaffLOnDJtMX5XL7tZubvBGJWFaiz7Lp4Klld6d/peGYzKmkQZJRHgFIftOH7p1NtcXwbvZhNU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=S4Fy/XHm; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="S4Fy/XHm" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1721911845; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=a6VNin/3nB4iUE5Zgy3fIduRtJbFimcejG9hH+gh4os=; b=S4Fy/XHmKCm3g4TSh4T8pqv2YpyCn/OZlko68waVOpD/fbcJ6ptRd5dfB5UlbYiRI9Y8rj tsTTzp1UAQ3cRFIzEs68KUql5VuvNNBIOQ7uvKy7IDaZx+vX3awzSrFI2JVEUXueoj4tec A6sfwKo/9cAmKrIy33Bw2Ev/mr7sJs8= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-595-vAzTLjXBOxiKUtDWZppIXA-1; Thu, 25 Jul 2024 08:50:44 -0400 X-MC-Unique: vAzTLjXBOxiKUtDWZppIXA-1 Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-3685a5a765fso525862f8f.1 for ; Thu, 25 Jul 2024 05:50:44 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721911843; x=1722516643; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=a6VNin/3nB4iUE5Zgy3fIduRtJbFimcejG9hH+gh4os=; b=icI9sKo1ZXCpeS7hYZD98ME0qYp96sfrRfziBtPQGPl/yqMmBvGfJ3E1zwhsq6xkPO NOV2zVFZozu9ouy80kG3AXI+QUFEPuvT+n3g2PmkaU3Estt9uFJ2yRwFXVmaKxBtN+Zf YN2sRhji3WCdQWPNT4gCjf/OAYNZXGWFfgr00+byZgQFTxNHFHQdyfL5rwofC1zeFk5W 21XsJnxHjJcC+eahvH66P1lqm97frmDTEslZIbJFeM8lmG7NbPKi3UXV46N9Jx10Jm0o ZFojsWZgZYFa73sf2UJ7Dm1pc0lL8KzkQiDlno9X3S4jvrG8Y3yFcuRjnveGBw4bjNJQ Ifsw== X-Forwarded-Encrypted: i=1; AJvYcCVMQRkhRhTOTVyTv50+jNWeXh0fthsgJ5Jv1Pb3SkXTuaa6iNbcgSh4ugHXm+CeSOFlXvJGO4vdffd+5xulVkp2spC3a5WXvLjeYA== X-Gm-Message-State: AOJu0YyZC0a2EFeTHoe3RZnlR8ooeo8Nekf6LCmovOxk5gmsh/KwMxdu oshpTwmxikc02tF92HK1iJ7N0SJOWagIAqXnVTb20Dx2xqGAUz72S287oFkR6+PB4b1ogsDG26p maLI4VdlzD78uT8m9klnSRfXA9Kuj3cxGmgYBsib0DNoZlrHH6ObMw/ieryqgP/LcMg== X-Received: by 2002:a5d:530a:0:b0:368:334d:aad4 with SMTP id ffacd0b85a97d-36b319d0671mr1996494f8f.4.1721911843404; Thu, 25 Jul 2024 05:50:43 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGFnQtljov7mTyBnm12EsdGYTBFRa0d3qWO13EEzKuHlha1cnD0ekvmxjpnTJcwuAzA/gfxuQ== X-Received: by 2002:a5d:530a:0:b0:368:334d:aad4 with SMTP id ffacd0b85a97d-36b319d0671mr1996480f8f.4.1721911843083; Thu, 25 Jul 2024 05:50:43 -0700 (PDT) Received: from klayman.redhat.com (host-82-62-247-97.business.telecomitalia.it. [82.62.247.97]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-36b367d9393sm2092301f8f.26.2024.07.25.05.50.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Jul 2024 05:50:42 -0700 (PDT) From: Marco Pagani To: Moritz Fischer , Wu Hao , Xu Yilun , Tom Rix Cc: Marco Pagani , linux-kernel@vger.kernel.org, linux-fpga@vger.kernel.org Subject: [PATCH 1/3] Simplify and improve the fpga manager test suite using deferred actions Date: Thu, 25 Jul 2024 14:50:29 +0200 Message-ID: <20240725125031.308195-2-marpagan@redhat.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240725125031.308195-1-marpagan@redhat.com> References: <20240725125031.308195-1-marpagan@redhat.com> Precedence: bulk X-Mailing-List: linux-fpga@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Use deferred actions to simplify the test suite and avoid potential memory leaks when test cases fail. Remove unnecessary calls to kunit_device_unregister() since kunit devices are tied to the test context and released by a deferred action when the test is completed. Signed-off-by: Marco Pagani --- drivers/fpga/tests/fpga-mgr-test.c | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/drivers/fpga/tests/fpga-mgr-test.c b/drivers/fpga/tests/fpga-mgr-test.c index 125b3a4d43c6..9cb37aefbac4 100644 --- a/drivers/fpga/tests/fpga-mgr-test.c +++ b/drivers/fpga/tests/fpga-mgr-test.c @@ -44,6 +44,16 @@ struct mgr_ctx { struct mgr_stats stats; }; +/* + * Wrappers to avoid cast warnings when passing action functions directly + * to kunit_add_action(). + */ +KUNIT_DEFINE_ACTION_WRAPPER(sg_free_table_wrapper, sg_free_table, + struct sg_table *); + +KUNIT_DEFINE_ACTION_WRAPPER(fpga_image_info_free_wrapper, fpga_image_info_free, + struct fpga_image_info *); + /** * init_test_buffer() - Allocate and initialize a test image in a buffer. * @test: KUnit test context object. @@ -257,6 +267,9 @@ static void fpga_mgr_test_img_load_sgt(struct kunit *test) KUNIT_ASSERT_EQ(test, ret, 0); sg_init_one(sgt->sgl, img_buf, IMAGE_SIZE); + ret = kunit_add_action_or_reset(test, sg_free_table_wrapper, sgt); + KUNIT_ASSERT_EQ(test, ret, 0); + ctx->img_info->sgt = sgt; ret = fpga_mgr_load(ctx->mgr, ctx->img_info); @@ -273,13 +286,12 @@ static void fpga_mgr_test_img_load_sgt(struct kunit *test) KUNIT_EXPECT_EQ(test, ctx->stats.op_write_init_seq, ctx->stats.op_parse_header_seq + 1); KUNIT_EXPECT_EQ(test, ctx->stats.op_write_sg_seq, ctx->stats.op_parse_header_seq + 2); KUNIT_EXPECT_EQ(test, ctx->stats.op_write_complete_seq, ctx->stats.op_parse_header_seq + 3); - - sg_free_table(ctx->img_info->sgt); } static int fpga_mgr_test_init(struct kunit *test) { struct mgr_ctx *ctx; + int ret; ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx); @@ -294,19 +306,14 @@ static int fpga_mgr_test_init(struct kunit *test) ctx->img_info = fpga_image_info_alloc(ctx->dev); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->img_info); + ret = kunit_add_action_or_reset(test, fpga_image_info_free_wrapper, ctx->img_info); + KUNIT_ASSERT_EQ(test, ret, 0); + test->priv = ctx; return 0; } -static void fpga_mgr_test_exit(struct kunit *test) -{ - struct mgr_ctx *ctx = test->priv; - - fpga_image_info_free(ctx->img_info); - kunit_device_unregister(test, ctx->dev); -} - static struct kunit_case fpga_mgr_test_cases[] = { KUNIT_CASE(fpga_mgr_test_get), KUNIT_CASE(fpga_mgr_test_lock), @@ -318,7 +325,6 @@ static struct kunit_case fpga_mgr_test_cases[] = { static struct kunit_suite fpga_mgr_suite = { .name = "fpga_mgr", .init = fpga_mgr_test_init, - .exit = fpga_mgr_test_exit, .test_cases = fpga_mgr_test_cases, }; From patchwork Thu Jul 25 12:50:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marco Pagani X-Patchwork-Id: 13741896 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EF94519AA74 for ; Thu, 25 Jul 2024 12:50:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721911854; cv=none; b=PLEprI4lvoXqlSvEv+dcOBVGYXS8sADFB8ZlK/+ACoESwTjM0wpl+oaqY4IgIgG2Xcm5n+a20HZpTN9T3fvEQ3g5gtZeTn04yJmscH12brqxSalaeV4cFQWqWEx316GkQVTnNSHrnc9dI2B2B4QEfh4BvcVdzn7DO7kqApoqK1Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721911854; c=relaxed/simple; bh=YV909LoEI6bsi0Em/tZckNhqRD0hUSsHLMe7pCoiwxQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=HK/eHC7QbQtckUM8Sd9nRL5zeQ8XnX264nrcbIN/8vxI4dqVeGJ0pF0nm9qzeQ/O3W+VkQtX5GBl1LXct8WuHSn+LcYMxaBemQiPrOuw99JOz+gU53zmh0J5VwdPTSU8C0N2V1rIFRPStjDSkZS3+zxdD/fxEyMJG1UWFJcns/4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=fnTFa/lB; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="fnTFa/lB" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1721911851; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=EFHivbMHZeqOfhJYc/N6VPffgKBGWyLKy4zJkm5ynK8=; b=fnTFa/lBitrmqZziSbmCIRnBDDQGloINBtOE1sqNIUBi6WHERvIbWVIMo2endiHgTzuLno ucSAMz8+BT5F0DNDuPZDngps77IK7YH7VBFwVBG201r0ruZEiEWgOWjy2RESsEdgHpE4D+ EiAJV9qKpa3+x8RPB5BonjBX5G7qBWE= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-675-eahsemJ2MzulafgrZ7YEAA-1; Thu, 25 Jul 2024 08:50:50 -0400 X-MC-Unique: eahsemJ2MzulafgrZ7YEAA-1 Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-4266fbae4c6so6643925e9.0 for ; Thu, 25 Jul 2024 05:50:50 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721911849; x=1722516649; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=EFHivbMHZeqOfhJYc/N6VPffgKBGWyLKy4zJkm5ynK8=; b=gRf0/tUzbNr9E4Zo/i46oxNxvHPfVENg7iqgYrQq7CtZzKoc+UpU2tjegRURn01Fe0 m0MfpKnpUaSUJlvaO8nIa5rdRmkQPvNBr0TPSchd3ttUYKuveM4CAOUs2yYaXJGVgmiQ tixRPqWicT1yu5GuZUAu+9buFs9POvs4XhOujNIWsw21dnJPFLllcRM8wLEbZkF0CE0L 7P7XqQJAw5lWPn8gUndmtiT3xuQFJyjRkH0TSHNDtOLaNkce/DnRtGR/arr+9J8ZbIFk x9UTeEMJuVZoY8puPdcUzWFjCarqL7b35SpwirlvYnCX+9gwJ2u9jG9y6Lk5p0xjm9rT t8kA== X-Forwarded-Encrypted: i=1; AJvYcCXZse7ksarSnJrBC+LUadjkcg1J5iqFExs1U/xalDLe6CYaeE5tKl06w8TR7SiZ3lQQeowsicnmit9oJhhZ81xAOM5CgNVfvy05yw== X-Gm-Message-State: AOJu0YyQ6bAXce3LOGt7nnihRgeZR/ejGTPX3rxdnnJQDaQr1O64EbK7 /3C7+O5YsYsej/tN2L+P5vwnvsslLDQOc+6vtncPlYEw7PyAsPb9eeTyfCBzv2g1NxdsjUOwJcD ulQsqTTWXP5AGNWdjPfPPtukiwmvGllTbNCaGD/5WDDk5BWonCWSPz7zZ6w== X-Received: by 2002:a05:600c:474d:b0:426:6e8b:3dc5 with SMTP id 5b1f17b1804b1-42806be7778mr15913625e9.32.1721911849194; Thu, 25 Jul 2024 05:50:49 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF9q5uvqNSpT/5NnExscK0qCysyLDTMmr36IVw8Rt4+6jOlfp+0J3Wz3/u5fD/iD40wX4HhFg== X-Received: by 2002:a05:600c:474d:b0:426:6e8b:3dc5 with SMTP id 5b1f17b1804b1-42806be7778mr15913505e9.32.1721911848939; Thu, 25 Jul 2024 05:50:48 -0700 (PDT) Received: from klayman.redhat.com (host-82-62-247-97.business.telecomitalia.it. [82.62.247.97]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-36b367d9393sm2092301f8f.26.2024.07.25.05.50.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Jul 2024 05:50:48 -0700 (PDT) From: Marco Pagani To: Moritz Fischer , Wu Hao , Xu Yilun , Tom Rix Cc: Marco Pagani , linux-kernel@vger.kernel.org, linux-fpga@vger.kernel.org Subject: [PATCH 2/3] Simplify and improve the fpga bridge test suite using deferred actions Date: Thu, 25 Jul 2024 14:50:30 +0200 Message-ID: <20240725125031.308195-3-marpagan@redhat.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240725125031.308195-1-marpagan@redhat.com> References: <20240725125031.308195-1-marpagan@redhat.com> Precedence: bulk X-Mailing-List: linux-fpga@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Use deferred actions to simplify the test suite and avoid potential memory leaks when test cases fail. Remove unnecessary calls to kunit_device_unregister() since kunit devices are tied to the test context and released by a deferred action when the test is completed. Signed-off-by: Marco Pagani --- drivers/fpga/tests/fpga-bridge-test.c | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/drivers/fpga/tests/fpga-bridge-test.c b/drivers/fpga/tests/fpga-bridge-test.c index 2f7a24f23808..b9ab29809e96 100644 --- a/drivers/fpga/tests/fpga-bridge-test.c +++ b/drivers/fpga/tests/fpga-bridge-test.c @@ -23,6 +23,13 @@ struct bridge_ctx { struct bridge_stats stats; }; +/* + * Wrapper to avoid a cast warning when passing the action function directly + * to kunit_add_action(). + */ +KUNIT_DEFINE_ACTION_WRAPPER(fpga_bridge_unregister_wrapper, fpga_bridge_unregister, + struct fpga_bridge *); + static int op_enable_set(struct fpga_bridge *bridge, bool enable) { struct bridge_stats *stats = bridge->priv; @@ -50,6 +57,7 @@ static const struct fpga_bridge_ops fake_bridge_ops = { static struct bridge_ctx *register_test_bridge(struct kunit *test, const char *dev_name) { struct bridge_ctx *ctx; + int ret; ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx); @@ -61,13 +69,10 @@ static struct bridge_ctx *register_test_bridge(struct kunit *test, const char *d &ctx->stats); KUNIT_ASSERT_FALSE(test, IS_ERR_OR_NULL(ctx->bridge)); - return ctx; -} + ret = kunit_add_action_or_reset(test, fpga_bridge_unregister_wrapper, ctx->bridge); + KUNIT_ASSERT_EQ(test, ret, 0); -static void unregister_test_bridge(struct kunit *test, struct bridge_ctx *ctx) -{ - fpga_bridge_unregister(ctx->bridge); - kunit_device_unregister(test, ctx->dev); + return ctx; } static void fpga_bridge_test_get(struct kunit *test) @@ -141,8 +146,6 @@ static void fpga_bridge_test_get_put_list(struct kunit *test) fpga_bridges_put(&bridge_list); KUNIT_EXPECT_TRUE(test, list_empty(&bridge_list)); - - unregister_test_bridge(test, ctx_1); } static int fpga_bridge_test_init(struct kunit *test) @@ -152,11 +155,6 @@ static int fpga_bridge_test_init(struct kunit *test) return 0; } -static void fpga_bridge_test_exit(struct kunit *test) -{ - unregister_test_bridge(test, test->priv); -} - static struct kunit_case fpga_bridge_test_cases[] = { KUNIT_CASE(fpga_bridge_test_get), KUNIT_CASE(fpga_bridge_test_toggle), @@ -167,7 +165,6 @@ static struct kunit_case fpga_bridge_test_cases[] = { static struct kunit_suite fpga_bridge_suite = { .name = "fpga_bridge", .init = fpga_bridge_test_init, - .exit = fpga_bridge_test_exit, .test_cases = fpga_bridge_test_cases, }; From patchwork Thu Jul 25 12:50:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marco Pagani X-Patchwork-Id: 13741897 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F347A19AD7E for ; Thu, 25 Jul 2024 12:50:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721911861; cv=none; b=BxPqAvo1ayl+CBO98Af907IPL+80RPLEymiij0ZUuPMLwUjL/wb6MLtMRmRphKroGQ4WkxH/tew6tw4zhdB1L+EwJYU/2I8b1my54XSDADd3FPyqd7tG7CVY/iXgSAC5HkOjQyS/sYTfLtEKX/dbn6pc0ZDOZvRdxegJqT40FXc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721911861; c=relaxed/simple; bh=8tnd/VQtiNpjeTI/Si1dXPTSVhoxuAsentG0xIJwfhw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=oAR9idhWoSZFBPb3LmEjCZaA3Ql/ckla5LKfa2zDEqQ/+c5oeSjRnmxuWBfoPohj8t1eKslqGWl+okeNx40bcn2EtUvhsTt9OiBaGqnJPS2UvaNaOfS3Op3z6v+76fnGghw05oT4YkHkgyeF8JYFQL3E7NZVjO0ATmEXPCYyunI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=isjGv++Y; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="isjGv++Y" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1721911859; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ujCDSZiUUPCMMti3b2r7m+p5eUsjfxpaUTFjvUJjKxQ=; b=isjGv++YHCG5bilI7zQxFdIcIoqtYDRnMoHeO3zvV66g5Z/q1QbRydClmIo4tLhCPBst+d bYtJx2EyklvaJ2f+yPXQC3zbuS4IkPNhk+wtlD3e5NlOavgw5OvuMFEouI6Ulb53NaFjhL qSH7uAppO3q3y6WLYiSsn5sx2GlZbUg= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-451-pKTJO3lUNDq98DOu5mAPOw-1; Thu, 25 Jul 2024 08:50:57 -0400 X-MC-Unique: pKTJO3lUNDq98DOu5mAPOw-1 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-4280507dc5cso6449035e9.2 for ; Thu, 25 Jul 2024 05:50:57 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721911856; x=1722516656; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ujCDSZiUUPCMMti3b2r7m+p5eUsjfxpaUTFjvUJjKxQ=; b=wzQv9ltHai7ux0q1VEe9NvJOnRIcMeI9bzHXQtba27Gh9G/QVcn/5NF7nw6KYPzDRa SOexrzZRnySwFFZyGz/UV64KL1HWh8OyAdgx9vEgKtHDvnNuMWGmMnsebYgFgHvpokEx MJ5r/adGUS2ra7/9HkZ0vPmO2GzDoeEdKPM23PtQ/upa7pTxLPXsQ2jAq0fhj7pW9g6c g3327Vo9ZqolAuI4H4yhno5ykdhapDTUU6gNvSvC1KBq34aU1sKQRFS8LyNeEiKZ18+i aOZzTdkvUeACb/13awi9QIx1tkH7A7PhYocQwQPFl+MuDdbPI2QJbx3hM2oOhe5VAccv +7uA== X-Forwarded-Encrypted: i=1; AJvYcCX4/hBESbbIAf0kbQK431BCh7+NBEnIM5ALHO7PGzch7z1ZOV40gUyeCOJyf57k74NXVccwGowKF4OKXEaxJkhr1MQ3q8r7560rMg== X-Gm-Message-State: AOJu0YwhebLJZgOPctQH+H672f3MEG6lAkZnsJK8JgKdtGajapLbggZV CxPBnLv3S66fbksJU5vqk9Ui18c/LUxN+OTXCmZUk54Gmnd37UvcUA5OTsYsH8SFQvTlSG6mbDb jxKwOvLAh/eJgFz7VTIpqbZ/OROO+Dd9FJpi1aisMX8sxerSvdU5vGnBwzQ== X-Received: by 2002:a05:6000:a85:b0:368:7fd3:291c with SMTP id ffacd0b85a97d-36b3644426dmr1196351f8f.60.1721911856675; Thu, 25 Jul 2024 05:50:56 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEVAxLclyNlj7nM4ZL2qCkLrBuxajhLd/cD318oAsWpXooT8OH8qKqixRKTnOf6Vcohzy5f5A== X-Received: by 2002:a05:6000:a85:b0:368:7fd3:291c with SMTP id ffacd0b85a97d-36b3644426dmr1196338f8f.60.1721911856417; Thu, 25 Jul 2024 05:50:56 -0700 (PDT) Received: from klayman.redhat.com (host-82-62-247-97.business.telecomitalia.it. [82.62.247.97]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-36b367d9393sm2092301f8f.26.2024.07.25.05.50.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Jul 2024 05:50:56 -0700 (PDT) From: Marco Pagani To: Moritz Fischer , Wu Hao , Xu Yilun , Tom Rix Cc: Marco Pagani , linux-kernel@vger.kernel.org, linux-fpga@vger.kernel.org Subject: [PATCH 3/3] Simplify and improve the fpga region test suite using deferred actions Date: Thu, 25 Jul 2024 14:50:31 +0200 Message-ID: <20240725125031.308195-4-marpagan@redhat.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240725125031.308195-1-marpagan@redhat.com> References: <20240725125031.308195-1-marpagan@redhat.com> Precedence: bulk X-Mailing-List: linux-fpga@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Use deferred actions to simplify the test suite and avoid potential memory leaks when test cases fail. Remove unnecessary calls to kunit_device_unregister() since kunit devices are tied to the test context and released by a deferred action when the test is completed. Other changes: fix a typo by changing the test suite name to fpga_region in the kunit_suite struct. Signed-off-by: Marco Pagani --- drivers/fpga/tests/fpga-region-test.c | 41 ++++++++++++++++----------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/drivers/fpga/tests/fpga-region-test.c b/drivers/fpga/tests/fpga-region-test.c index bcf0651df261..6a108cafded8 100644 --- a/drivers/fpga/tests/fpga-region-test.c +++ b/drivers/fpga/tests/fpga-region-test.c @@ -35,6 +35,19 @@ struct test_ctx { struct mgr_stats mgr_stats; }; +/* + * Wrappers to avoid cast warnings when passing action functions directly + * to kunit_add_action(). + */ +KUNIT_DEFINE_ACTION_WRAPPER(fpga_image_info_free_wrapper, fpga_image_info_free, + struct fpga_image_info *); + +KUNIT_DEFINE_ACTION_WRAPPER(fpga_bridge_unregister_wrapper, fpga_bridge_unregister, + struct fpga_bridge *); + +KUNIT_DEFINE_ACTION_WRAPPER(fpga_region_unregister_wrapper, fpga_region_unregister, + struct fpga_region *); + static int op_write(struct fpga_manager *mgr, const char *buf, size_t count) { struct mgr_stats *stats = mgr->priv; @@ -111,6 +124,9 @@ static void fpga_region_test_program_fpga(struct kunit *test) img_info = fpga_image_info_alloc(ctx->mgr_dev); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, img_info); + ret = kunit_add_action_or_reset(test, fpga_image_info_free_wrapper, img_info); + KUNIT_ASSERT_EQ(test, ret, 0); + img_info->buf = img_buf; img_info->count = sizeof(img_buf); @@ -130,8 +146,6 @@ static void fpga_region_test_program_fpga(struct kunit *test) KUNIT_EXPECT_EQ(test, 2, ctx->bridge_stats.cycles_count); fpga_bridges_put(&ctx->region->bridge_list); - - fpga_image_info_free(img_info); } /* @@ -144,6 +158,7 @@ static int fpga_region_test_init(struct kunit *test) { struct test_ctx *ctx; struct fpga_region_info region_info = { 0 }; + int ret; ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx); @@ -164,6 +179,9 @@ static int fpga_region_test_init(struct kunit *test) ctx->bridge_stats.enable = true; + ret = kunit_add_action_or_reset(test, fpga_bridge_unregister_wrapper, ctx->bridge); + KUNIT_ASSERT_EQ(test, ret, 0); + ctx->region_dev = kunit_device_register(test, "fpga-region-test-dev"); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->region_dev); @@ -174,24 +192,14 @@ static int fpga_region_test_init(struct kunit *test) ctx->region = fpga_region_register_full(ctx->region_dev, ®ion_info); KUNIT_ASSERT_FALSE(test, IS_ERR_OR_NULL(ctx->region)); + ret = kunit_add_action_or_reset(test, fpga_region_unregister_wrapper, ctx->region); + KUNIT_ASSERT_EQ(test, ret, 0); + test->priv = ctx; return 0; } -static void fpga_region_test_exit(struct kunit *test) -{ - struct test_ctx *ctx = test->priv; - - fpga_region_unregister(ctx->region); - kunit_device_unregister(test, ctx->region_dev); - - fpga_bridge_unregister(ctx->bridge); - kunit_device_unregister(test, ctx->bridge_dev); - - kunit_device_unregister(test, ctx->mgr_dev); -} - static struct kunit_case fpga_region_test_cases[] = { KUNIT_CASE(fpga_region_test_class_find), KUNIT_CASE(fpga_region_test_program_fpga), @@ -199,9 +207,8 @@ static struct kunit_case fpga_region_test_cases[] = { }; static struct kunit_suite fpga_region_suite = { - .name = "fpga_mgr", + .name = "fpga_region", .init = fpga_region_test_init, - .exit = fpga_region_test_exit, .test_cases = fpga_region_test_cases, };