From: Rahul Rameshbabu <sergeantsagara@protonmail.com>
To: "Guilherme G. Piccoli" <gpiccoli@igalia.com>
Cc: djogorchock@gmail.com, linux-input@vger.kernel.org,
jikos@kernel.org, benjamin.tissoires@redhat.com,
kernel@gpiccoli.net, kernel-dev@igalia.com
Subject: Re: [PATCH] HID: nintendo: Prevent divide-by-zero on code
Date: Mon, 18 Dec 2023 20:39:45 +0000 [thread overview]
Message-ID: <87o7enxn1x.fsf@protonmail.com> (raw)
In-Reply-To: <20231205211628.993129-1-gpiccoli@igalia.com>
On Tue, 05 Dec, 2023 18:15:51 -0300 "Guilherme G. Piccoli" <gpiccoli@igalia.com> wrote:
> It was reported [0] that adding a generic joycon to the system caused
> a kernel crash on Steam Deck, with the below panic spew:
>
> divide error: 0000 [#1] PREEMPT SMP NOPTI
> [...]
> Hardware name: Valve Jupiter/Jupiter, BIOS F7A0119 10/24/2023
> RIP: 0010:nintendo_hid_event+0x340/0xcc1 [hid_nintendo]
> [...]
> Call Trace:
> [...]
> ? exc_divide_error+0x38/0x50
> ? nintendo_hid_event+0x340/0xcc1 [hid_nintendo]
> ? asm_exc_divide_error+0x1a/0x20
> ? nintendo_hid_event+0x307/0xcc1 [hid_nintendo]
> hid_input_report+0x143/0x160
> hidp_session_run+0x1ce/0x700 [hidp]
>
> Since it's a divide-by-0 error, by tracking the code for potential
> denominator issues, we've spotted 2 places in which this could happen;
> so let's guard against the possibility and log in the kernel if the
> condition happens. This is specially useful since some data that
> fills some denominators are read from the joycon HW in some cases,
> increasing the potential for flaws.
>
> [0] https://github.com/ValveSoftware/SteamOS/issues/1070
>
> Signed-off-by: Guilherme G. Piccoli <gpiccoli@igalia.com>
> ---
> drivers/hid/hid-nintendo.c | 27 ++++++++++++++++++++-------
> 1 file changed, 20 insertions(+), 7 deletions(-)
>
> diff --git a/drivers/hid/hid-nintendo.c b/drivers/hid/hid-nintendo.c
> index 138f154fecef..23f3f96c8c85 100644
> --- a/drivers/hid/hid-nintendo.c
> +++ b/drivers/hid/hid-nintendo.c
[...]
> @@ -1163,16 +1176,16 @@ static void joycon_parse_imu_report(struct joycon_ctlr *ctlr,
> JC_IMU_SAMPLES_PER_DELTA_AVG) {
> ctlr->imu_avg_delta_ms = ctlr->imu_delta_samples_sum /
> ctlr->imu_delta_samples_count;
> - /* don't ever want divide by zero shenanigans */
> - if (ctlr->imu_avg_delta_ms == 0) {
> - ctlr->imu_avg_delta_ms = 1;
> - hid_warn(ctlr->hdev,
> - "calculated avg imu delta of 0\n");
> - }
> ctlr->imu_delta_samples_count = 0;
> ctlr->imu_delta_samples_sum = 0;
> }
>
> + /* don't ever want divide by zero shenanigans */
> + if (ctlr->imu_avg_delta_ms == 0) {
> + ctlr->imu_avg_delta_ms = 1;
> + hid_warn(ctlr->hdev, "calculated avg imu delta of 0\n");
> + }
> +
Hi Guilherme,
I agree with the previous hunks you added and can see how those could
trigger the divide-by-zero issue you were seeing. However, I am not sure
if this hunk that I have left makes sense.
Reason being, is that the hid-nintendo driver has a special conditional
to prevent divide-by-zero in this case without this change.
1. If the first packet has not been received by the IMU, set
imu_avg_delta_ms to JC_IMU_DFLT_AVG_DELTA_MS.
2. Only change imu_avg_delta_ms when imu_delta_samples_count >=
JC_IMU_SAMPLES_PER_DELTA_AVG.
3. If that change leads to imu_avg_delta_ms being 0, set it to 1.
With this logic as-is, I do not see how a divide by zero could have
occurred in this specific path without your change. I might be missing
something, but I wanted to make sure to avoid integrating a hunk that
does not help.
Would it be possible to retest without this hunk?
> /* useful for debugging IMU sample rate */
> hid_dbg(ctlr->hdev,
> "imu_report: ms=%u last_ms=%u delta=%u avg_delta=%u\n",
--
Thanks,
Rahul Rameshbabu
next prev parent reply other threads:[~2023-12-18 20:39 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-12-05 21:15 [PATCH] HID: nintendo: Prevent divide-by-zero on code Guilherme G. Piccoli
2023-12-18 15:50 ` Jiri Kosina
2023-12-18 19:22 ` Guilherme G. Piccoli
[not found] ` <CACC3sbFGHHONh=orX2+VuRu1SdGXu-jhhFVE-xZe1wOBodUzpQ@mail.gmail.com>
2023-12-18 19:47 ` Jiri Kosina
2023-12-18 20:39 ` Rahul Rameshbabu [this message]
2023-12-18 21:46 ` Guilherme G. Piccoli
2023-12-18 21:56 ` Rahul Rameshbabu
2023-12-18 22:27 ` Jiri Kosina
2023-12-18 23:29 ` Guilherme G. Piccoli
2023-12-18 23:49 ` Jiri Kosina
2023-12-19 0:03 ` Guilherme G. Piccoli
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87o7enxn1x.fsf@protonmail.com \
--to=sergeantsagara@protonmail.com \
--cc=benjamin.tissoires@redhat.com \
--cc=djogorchock@gmail.com \
--cc=gpiccoli@igalia.com \
--cc=jikos@kernel.org \
--cc=kernel-dev@igalia.com \
--cc=kernel@gpiccoli.net \
--cc=linux-input@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox