All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Denis Benato" <denis.benato@linux.dev>
Cc: linux-input@vger.kernel.org, dmitry.torokhov@gmail.com
Subject: Re: [PATCH v11 11/18] HID: asus: add support for btn remapping
Date: Mon, 26 Oct 2026 00:06:29 +0000	[thread overview]
Message-ID: <sashiko-outbox-184552@kernel.org> (raw)
In-Reply-To: <20261025235232.129945-12-denis.benato@linux.dev>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] HID: asus: race publishing button_entries in ally_create_button_attributes()
--

commit 72fcba2fa957c8a91f51c8bd2f81b7d77bbc08de
Author: Denis Benato <denis.benato@linux.dev>

HID: asus: add support for btn remapping

This commit adds sysfs support for remapping physical buttons and macros on
ROG Ally handheld devices, including per-button remap_index attributes and
automatic cache refreshes when the gamepad mode changes.

> diff --git a/drivers/hid/hid-asus.c b/drivers/hid/hid-asus.c
> index ed95b2cdec813..ea27495c3df66 100644
> --- a/drivers/hid/hid-asus.c
> +++ b/drivers/hid/hid-asus.c
[ ... ]
> @@ -1141,6 +1480,33 @@ static ssize_t gamepad_mode_store(struct device *dev, struct device_attribute *a
>  
>  	cfg->gamepad_mode = mode_byte;
>  
> +	/*
> +	 * When the gamepad mode changes, the hardware automatically
> +	 * resets all button mappings to its internal defaults for the
> +	 * new mode.
> +	 *
> +	 * Refresh the software cache so that subsequent
> +	 * reads from sysfs reflect what the firmware is actually
> +	 * using.
> +	 */
> +	if (mode_byte == ALLY_GAMEPAD_MODE_GAMEPAD)
> +		ally_set_default_gamepad_mapping(&cfg->button_mappings);
> +	else if (mode_byte == ALLY_GAMEPAD_MODE_KEYBOARD)
> +		ally_set_default_keyboard_mapping(&cfg->button_mappings);
> +
> +	/*
> +	 * The re-defaulted mappings are what the remap and macro files
> +	 * report: wake anyone polling them about the new values.
> +	 */
> +	if (cfg->button_entries) {
> +		for (i = 0; i < ALLY_BTN_MAX; i++) {
> +			sysfs_notify(&dev->kobj, cfg->button_entries[i].group.name,
> +				     "remap");
> +			sysfs_notify(&dev->kobj, cfg->button_entries[i].group.name,
> +				     "macro");
> +		}
> +	}
[ ... ]
> @@ -2824,15 +3759,12 @@ static void ally_btn_turbo_init_attrs(struct ally_btn_sysfs_entry *entry)
>   * Return: 0 on success, negative errno on failure
>   */
>  static int ally_create_button_attributes(struct hid_device *hdev,
> -					 struct ally_handheld *ally,
> -					 struct ally_config *cfg)
> +					  struct ally_handheld *ally,
> +					  struct ally_config *cfg)
>  {
>  	struct ally_btn_sysfs_entry *entries;
>  	int i, ret;
>  
> -	if (!cfg->turbo_support)
> -		return 0;
> -
>  	entries = kcalloc(ALLY_BTN_MAX, sizeof(*entries), GFP_KERNEL);
>  	if (!entries)
>  		return -ENOMEM;
> @@ -2845,13 +3777,53 @@ static int ally_create_button_attributes(struct hid_device *hdev,
>  		entries[i].hdev = hdev;
>  		entries[i].btn = i;
>  
> -		if (!ally_btn_get_turbo_params(cfg, i)) {
> -			hid_err(hdev, "Invalid button id %d for turbo attributes\n", i);
> -			ret = -EINVAL;
> +		if (cfg->turbo_support) {
> +			if (!ally_btn_get_turbo_params(cfg, i)) {
> +				hid_err(hdev, "Invalid button id %d for turbo attributes\n", i);
> +				ret = -EINVAL;
> +				goto err_cleanup;
> +			}
> +
> +			ally_btn_turbo_init_attrs(&entries[i]);
> +		}
> +
> +		entries[i].remap_attr = button_remap_attr_create(ally, i, false);
> +		if (!entries[i].remap_attr) {
> +			ret = -ENOMEM;
>  			goto err_cleanup;
>  		}
>  
> -		ally_btn_turbo_init_attrs(&entries[i]);
> +		entries[i].macro_attr = button_remap_attr_create(ally, i, true);
> +		if (!entries[i].macro_attr) {
> +			ret = -ENOMEM;
> +			goto err_cleanup;
> +		}
> +
> +		/* Initialize the remap_index attribute */
> +		sysfs_attr_init(&entries[i].attr_remap_index.attr);
> +		entries[i].attr_remap_index.attr.name = "remap_index";
> +		entries[i].attr_remap_index.attr.mode = 0444;
> +		entries[i].attr_remap_index.show = btn_remap_index_show;
> +		entries[i].attr_remap_index.store = NULL;
> +
> +		/* Set up attributes array based on what's supported */
> +		if (cfg->turbo_support) {
> +			entries[i].attrs[4] =
> +				&entries[i].remap_attr->dev_attr.attr;
> +			entries[i].attrs[5] =
> +				&entries[i].macro_attr->dev_attr.attr;
> +			entries[i].attrs[6] =
> +				&entries[i].attr_remap_index.attr;
> +			entries[i].attrs[7] = NULL;
> +		} else {
> +			entries[i].attrs[0] =
> +				&entries[i].remap_attr->dev_attr.attr;
> +			entries[i].attrs[1] =
> +				&entries[i].macro_attr->dev_attr.attr;
> +			entries[i].attrs[2] =
> +				&entries[i].attr_remap_index.attr;
> +			entries[i].attrs[3] = NULL;
> +		}
>  
>  		entries[i].group.name = ally_button_names[i];
>  		entries[i].group.attrs = entries[i].attrs;
> @@ -2867,12 +3839,27 @@ static int ally_create_button_attributes(struct hid_device *hdev,
>  	return 0;
>  
>  err_cleanup:
> +	/*
> +	 * Unpublish first: the readers of button_entries (like the
> +	 * gamepad_mode_store() cross-notification) hold config_mutex and
> +	 * must observe either the live array or NULL, never a freed one.
> +	 * The lock is released again before the removals, so a straggler
> +	 * callback waiting on it can drain against the not yet freed
> +	 * entries.
> +	 */
> +	scoped_guard(mutex, &cfg->config_mutex) {
> +		cfg->button_entries = NULL;
> +	}

[Severity: Medium]
Can gamepad_mode_store() observe a partially initialized cfg->button_entries
array while ally_create_button_attributes() is still running?

In hid_asus_ally_probe() -> ally_config_create(), ally->config is published
and ally_attr_groups[0] (which exposes gamepad_mode) is registered with
sysfs_create_group() before ally_create_button_attributes() is called:

drivers/hid/hid-asus.c:ally_config_create() {
	...
	spin_lock_irqsave(&ally_data_lock, flags);
	ally->config = cfg;
	spin_unlock_irqrestore(&ally_data_lock, flags);

	for (sysfs_i = 0; sysfs_i < ARRAY_SIZE(ally_attr_groups); sysfs_i++) {
		ret = sysfs_create_group(&hdev->dev.kobj,
					 &ally_attr_groups[sysfs_i]);
	...
	ret = ally_create_button_attributes(hdev, ally, cfg);
}

At the start of ally_create_button_attributes(), cfg->button_entries = entries
is assigned without holding cfg->config_mutex and before the loop initializes
entries[i].group.name and creates the sysfs groups:

drivers/hid/hid-asus.c:ally_create_button_attributes() {
	entries = kcalloc(ALLY_BTN_MAX, sizeof(*entries), GFP_KERNEL);
	if (!entries)
		return -ENOMEM;

	cfg->button_entries = entries;

	for (i = 0; i < ALLY_BTN_MAX; i++) {
		...
		entries[i].group.name = ally_button_names[i];
		...
}

If userspace writes to gamepad_mode while this loop is running (or before
err_cleanup acquires cfg->config_mutex on failure), gamepad_mode_store()
acquires cfg->config_mutex, sees a non-NULL cfg->button_entries, and iterates
over all ALLY_BTN_MAX entries, racing with the unlocked writes to
entries[i].group.name and passing NULL as the directory name to
sysfs_notify() for entries that have not been initialized yet.

Should cfg->button_entries = entries instead be published under
cfg->config_mutex only after all ALLY_BTN_MAX groups have been initialized
and created?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261025235232.129945-1-denis.benato@linux.dev?part=11

  reply	other threads:[~2026-10-26  0:06 UTC|newest]

Thread overview: 44+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-25 23:52 [PATCH v11 00/18] HID: asus: add support for ROG Ally handhelds Denis Benato
2026-10-25 23:52 ` [PATCH v11 01/18] " Denis Benato
2026-10-26  0:10   ` sashiko-bot
2026-10-26 12:38   ` Ilpo Järvinen
2026-10-25 23:52 ` [PATCH v11 02/18] HID: asus: add gamepad configuration Denis Benato
2026-10-26  0:10   ` sashiko-bot
2026-10-26 12:56   ` Ilpo Järvinen
2026-10-25 23:52 ` [PATCH v11 03/18] HID: asus: add vibration strength configuration Denis Benato
2026-10-25 23:52 ` [PATCH v11 04/18] HID: asus: add joysticks inner and outer range configuration Denis Benato
2026-10-26 13:00   ` Ilpo Järvinen
2026-10-25 23:52 ` [PATCH v11 05/18] HID: asus: add triggers " Denis Benato
2026-10-25 23:52 ` [PATCH v11 06/18] HID: asus: add joysticks anti-deadzone configuration Denis Benato
2026-10-25 23:52 ` [PATCH v11 07/18] HID: asus: add support for response curve Denis Benato
2026-10-26 13:06   ` Ilpo Järvinen
2026-10-25 23:52 ` [PATCH v11 08/18] HID: asus: add support to force feedback Denis Benato
2026-10-26  0:19   ` sashiko-bot
2026-10-26 12:36   ` Sakari Ailus
2026-10-26 18:21     ` Ilpo Järvinen
2026-10-26 18:27       ` Sakari Ailus
2026-10-26 13:13   ` Ilpo Järvinen
2026-10-25 23:52 ` [PATCH v11 09/18] HID: asus: add support for gamepad mode Denis Benato
2026-10-26 13:20   ` Ilpo Järvinen
2026-10-25 23:52 ` [PATCH v11 10/18] HID: asus: add support for turbo buttons Denis Benato
2026-10-26 13:29   ` Ilpo Järvinen
2026-10-25 23:52 ` [PATCH v11 11/18] HID: asus: add support for btn remapping Denis Benato
2026-10-26  0:06   ` sashiko-bot [this message]
2026-10-26 14:03   ` Ilpo Järvinen
2026-10-25 23:52 ` [PATCH v11 12/18] HID: asus: size feature reports from the HID report descriptor Denis Benato
2026-10-26  0:06   ` sashiko-bot
2026-10-26 13:06   ` Rebecca Mara Müller
2026-10-26 19:38     ` Denis Benato
2026-10-25 23:52 ` [PATCH v11 13/18] HID: asus: identify XG Mobile stations and bind the LED to the reported model Denis Benato
2026-10-25 23:52 ` [PATCH v11 14/18] HID: asus: expose XG Mobile identity and firmware information Denis Benato
2026-10-26  0:05   ` sashiko-bot
2026-10-26 14:10   ` Ilpo Järvinen
2026-10-25 23:52 ` [PATCH v11 15/18] HID: asus: size ally gamepad commands from the HID report descriptor Denis Benato
2026-10-26  0:10   ` sashiko-bot
2026-10-25 23:52 ` [PATCH v11 16/18] platform/x86: asus-armoury: pick the eGPU activation from the reported GPU Denis Benato
2026-10-25 23:52 ` [PATCH v11 17/18] HID: asus: report the XG Mobile GPU vendor to asus-wmi Denis Benato
2026-10-26  0:03   ` sashiko-bot
2026-10-26 14:15   ` Ilpo Järvinen
2026-10-25 23:52 ` [PATCH v11 18/18] MAINTAINERS: add maintainer for the ASUS HID driver Denis Benato
2026-10-26 14:24 ` [PATCH v11 00/18] HID: asus: add support for ROG Ally handhelds Ilpo Järvinen
2026-10-26 18:03   ` Denis Benato

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=sashiko-outbox-184552@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=denis.benato@linux.dev \
    --cc=dmitry.torokhov@gmail.com \
    --cc=linux-input@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.