firewire: cdev: add KUnit test for bus reset event UAPI structure

The bus reset event structure exposed through the UAPI has a different
size depending on the target architecture. Extra care is required when
copying it to/from userspace.

Add a KUnit test for the structure to ensure that it has the expected
size and offsets.

Link: https://lore.kernel.org/r/20260920071501.77785-6-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
This commit is contained in:
Takashi Sakamoto 2026-09-20 16:15:01 +09:00
parent 259ee8e255
commit ab2a823af3
2 changed files with 24 additions and 1 deletions

View File

@ -524,7 +524,10 @@ static int ioctl_get_info(struct client *client, union ioctl_arg *arg)
memset(&bus_reset, 0, sizeof(bus_reset));
fill_bus_reset_event(&bus_reset, client);
/* unaligned size of bus_reset is 36 bytes */
// This structure has 4 bytes of trailing padding under the System V ABI
// on most architectures (due to 8-byte alignment of the long long type),
// except for Intel386 (where long long type is aligned to 4 bytes). In
// either case, the effective length is 36 bytes.
if (copy_to_user(u64_to_uptr(a->bus_reset), &bus_reset, 36))
return -EFAULT;
}

View File

@ -72,11 +72,31 @@ static void structure_layout_event_phy_packet2(struct kunit *test)
KUNIT_EXPECT_EQ(test, 24, offsetof(struct fw_cdev_event_phy_packet2, data));
}
static void structure_layout_event_bus_reset(struct kunit *test)
{
#if defined(CONFIG_X86_32)
// 4 bytes alignment for aggregate type including 8 bytes storage types.
KUNIT_EXPECT_EQ(test, 36, sizeof(struct fw_cdev_event_bus_reset));
#else
KUNIT_EXPECT_EQ(test, 40, sizeof(struct fw_cdev_event_bus_reset));
#endif
KUNIT_EXPECT_EQ(test, 0, offsetof(struct fw_cdev_event_bus_reset, closure));
KUNIT_EXPECT_EQ(test, 8, offsetof(struct fw_cdev_event_bus_reset, type));
KUNIT_EXPECT_EQ(test, 12, offsetof(struct fw_cdev_event_bus_reset, node_id));
KUNIT_EXPECT_EQ(test, 16, offsetof(struct fw_cdev_event_bus_reset, local_node_id));
KUNIT_EXPECT_EQ(test, 20, offsetof(struct fw_cdev_event_bus_reset, bm_node_id));
KUNIT_EXPECT_EQ(test, 24, offsetof(struct fw_cdev_event_bus_reset, irm_node_id));
KUNIT_EXPECT_EQ(test, 28, offsetof(struct fw_cdev_event_bus_reset, root_node_id));
KUNIT_EXPECT_EQ(test, 32, offsetof(struct fw_cdev_event_bus_reset, generation));
}
static struct kunit_case structure_layout_test_cases[] = {
KUNIT_CASE(structure_layout_event_response),
KUNIT_CASE(structure_layout_event_request3),
KUNIT_CASE(structure_layout_event_response2),
KUNIT_CASE(structure_layout_event_phy_packet2),
KUNIT_CASE(structure_layout_event_bus_reset),
{}
};