linux/drivers/hid/hid-uclogic-core-test.c
Jinjie Ruan d45f72b3c2 HID: uclogic: Fix a work->entry not empty bug in __queue_work()
When CONFIG_HID_UCLOGIC=y and CONFIG_KUNIT_ALL_TESTS=y, launch
kernel and then the below work->entry not empty bug occurs.

In hid_test_uclogic_exec_event_hook_test(), the filter->work is not
initialized to be added to p.event_hooks->list, and then the
schedule_work() in uclogic_exec_event_hook() will call __queue_work(),
which check whether the work->entry is empty and cause the below
warning call trace.

So call INIT_WORK() with a fake work to solve the issue. After applying
this patch, the below work->entry not empty bug never occurs.

 WARNING: CPU: 0 PID: 2177 at kernel/workqueue.c:1787 __queue_work.part.0+0x780/0xad0
 Modules linked in:
 CPU: 0 PID: 2177 Comm: kunit_try_catch Tainted: G    B   W        N 6.6.0-rc2+ #30
 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.15.0-1 04/01/2014
 RIP: 0010:__queue_work.part.0+0x780/0xad0
 Code: 44 24 20 0f b6 00 84 c0 74 08 3c 03 0f 8e 52 03 00 00 f6 83 00 01 00 00 02 74 6f 4c 89 ef e8 c7 d8 f1 02 f3 90 e9 e5 f8 ff ff <0f> 0b e9 63 fc ff ff 89 e9 49 8d 57 68 4c 89 e6 4c 89 ff 83 c9 02
 RSP: 0000:ffff888102bb7ce8 EFLAGS: 00010086
 RAX: 0000000000000000 RBX: ffff888106b8e460 RCX: ffffffff84141cc7
 RDX: 1ffff11020d71c8c RSI: 0000000000000004 RDI: ffff8881001d0118
 RBP: dffffc0000000000 R08: 0000000000000001 R09: ffffed1020576f92
 R10: 0000000000000003 R11: ffff888102bb7980 R12: ffff888106b8e458
 R13: ffff888119c38800 R14: 0000000000000000 R15: ffff8881001d0100
 FS:  0000000000000000(0000) GS:ffff888119c00000(0000) knlGS:0000000000000000
 CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
 CR2: ffff888119506000 CR3: 0000000005286001 CR4: 0000000000770ef0
 DR0: ffffffff8fdd6ce0 DR1: ffffffff8fdd6ce1 DR2: ffffffff8fdd6ce3
 DR3: ffffffff8fdd6ce5 DR6: 00000000fffe0ff0 DR7: 0000000000000600
 PKRU: 55555554
 Call Trace:
  <TASK>
  ? __warn+0xc9/0x260
  ? __queue_work.part.0+0x780/0xad0
  ? report_bug+0x345/0x400
  ? handle_bug+0x3c/0x70
  ? exc_invalid_op+0x14/0x40
  ? asm_exc_invalid_op+0x16/0x20
  ? _raw_spin_lock+0x87/0xe0
  ? __queue_work.part.0+0x780/0xad0
  ? __queue_work.part.0+0x249/0xad0
  queue_work_on+0x48/0x50
  uclogic_exec_event_hook.isra.0+0xf7/0x160
  hid_test_uclogic_exec_event_hook_test+0x2f1/0x5d0
  ? try_to_wake_up+0x151/0x13e0
  ? uclogic_exec_event_hook.isra.0+0x160/0x160
  ? _raw_spin_lock_irqsave+0x8d/0xe0
  ? __sched_text_end+0xa/0xa
  ? __sched_text_end+0xa/0xa
  ? migrate_enable+0x260/0x260
  ? kunit_try_run_case_cleanup+0xe0/0xe0
  kunit_generic_run_threadfn_adapter+0x4a/0x90
  ? kunit_try_catch_throw+0x80/0x80
  kthread+0x2b5/0x380
  ? kthread_complete_and_exit+0x20/0x20
  ret_from_fork+0x2d/0x70
  ? kthread_complete_and_exit+0x20/0x20
  ret_from_fork_asm+0x11/0x20
  </TASK>

Fixes: a251d6576d ("HID: uclogic: Handle wireless device reconnection")
Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>
Reviewed-by: José Expósito <jose.exposito89@gmail.com>
Link: https://lore.kernel.org/r/20231009064245.3573397-3-ruanjinjie@huawei.com
Signed-off-by: Benjamin Tissoires <bentiss@kernel.org>
2023-10-25 16:25:47 +02:00

113 lines
2.6 KiB
C

// SPDX-License-Identifier: GPL-2.0+
/*
* HID driver for UC-Logic devices not fully compliant with HID standard
*
* Copyright (c) 2022 José Expósito <jose.exposito89@gmail.com>
*/
#include <kunit/test.h>
#include "./hid-uclogic-params.h"
#define MAX_EVENT_SIZE 12
struct uclogic_raw_event_hook_test {
u8 event[MAX_EVENT_SIZE];
size_t size;
bool expected;
};
static struct uclogic_raw_event_hook_test hook_events[] = {
{
.event = { 0xA1, 0xB2, 0xC3, 0xD4 },
.size = 4,
},
{
.event = { 0x1F, 0x2E, 0x3D, 0x4C, 0x5B, 0x6A },
.size = 6,
},
};
static struct uclogic_raw_event_hook_test test_events[] = {
{
.event = { 0xA1, 0xB2, 0xC3, 0xD4 },
.size = 4,
.expected = true,
},
{
.event = { 0x1F, 0x2E, 0x3D, 0x4C, 0x5B, 0x6A },
.size = 6,
.expected = true,
},
{
.event = { 0xA1, 0xB2, 0xC3 },
.size = 3,
.expected = false,
},
{
.event = { 0xA1, 0xB2, 0xC3, 0xD4, 0x00 },
.size = 5,
.expected = false,
},
{
.event = { 0x2E, 0x3D, 0x4C, 0x5B, 0x6A, 0x1F },
.size = 6,
.expected = false,
},
};
static void fake_work(struct work_struct *work)
{
}
static void hid_test_uclogic_exec_event_hook_test(struct kunit *test)
{
struct uclogic_params p = {0, };
struct uclogic_raw_event_hook *filter;
bool res;
int n;
/* Initialize the list of events to hook */
p.event_hooks = kunit_kzalloc(test, sizeof(*p.event_hooks), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, p.event_hooks);
INIT_LIST_HEAD(&p.event_hooks->list);
for (n = 0; n < ARRAY_SIZE(hook_events); n++) {
filter = kunit_kzalloc(test, sizeof(*filter), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, filter);
filter->size = hook_events[n].size;
filter->event = kunit_kzalloc(test, filter->size, GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, filter->event);
memcpy(filter->event, &hook_events[n].event[0], filter->size);
INIT_WORK(&filter->work, fake_work);
list_add_tail(&filter->list, &p.event_hooks->list);
}
/* Test uclogic_exec_event_hook() */
for (n = 0; n < ARRAY_SIZE(test_events); n++) {
res = uclogic_exec_event_hook(&p, &test_events[n].event[0],
test_events[n].size);
KUNIT_ASSERT_EQ(test, res, test_events[n].expected);
}
}
static struct kunit_case hid_uclogic_core_test_cases[] = {
KUNIT_CASE(hid_test_uclogic_exec_event_hook_test),
{}
};
static struct kunit_suite hid_uclogic_core_test_suite = {
.name = "hid_uclogic_core_test",
.test_cases = hid_uclogic_core_test_cases,
};
kunit_test_suite(hid_uclogic_core_test_suite);
MODULE_DESCRIPTION("KUnit tests for the UC-Logic driver");
MODULE_LICENSE("GPL");
MODULE_AUTHOR("José Expósito <jose.exposito89@gmail.com>");