mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Mark Brown <broonie@kernel.org>
To: Benson Leung <bleung@google.com>,
	Guenter Roeck <groeck@chromium.org>,
	Dmitry Torokhov <dmitry.torokhov@gmail.com>
Cc: Chad Aldreda <ca@modelmiser.com>, Kees Cook <kees@kernel.org>,
	Kees Cook <kees+treewide@kernel.org>,
	Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
	Linux Next Mailing List <linux-next@vger.kernel.org>,
	Tzung-Bi Shih <tzungbi@kernel.org>
Subject: linux-next: manual merge of the chrome-platform tree with the origin tree
Date: Mon, 16 Nov 2026 19:27:09 +0000	[thread overview]
Message-ID: <avtZDbrv2YL6kfQR@sirena.org.uk> (raw)

[-- Attachment #1: Type: text/plain, Size: 4978 bytes --]

Hi all,

Today's linux-next merge of the chrome-platform tree got a conflict in:

  drivers/platform/chrome/cros_hps_i2c.c

between commit:

  4dd10fbbeb9c3 ("treewide: Convert devm_kmalloc() family to the devm_kmalloc_obj() family")

from the origin tree and commit:

  2c1aa3d09aad8 ("platform/chrome: cros_hps_i2c: Fix use-after-free in release() after unbind")

from the chrome-platform tree.

I fixed it up (see below) and can carry the fix as necessary. This
is now fixed as far as linux-next is concerned, but any non trivial
conflicts should be mentioned to your upstream maintainer when your tree
is submitted for merging.  You may also want to consider cooperating
with the maintainer of the conflicting tree to minimise any particularly
complex conflicts.

diff --combined drivers/platform/chrome/cros_hps_i2c.c
index c320489bfe1a0,436afc4f50d38..0000000000000
--- a/drivers/platform/chrome/cros_hps_i2c.c
+++ b/drivers/platform/chrome/cros_hps_i2c.c
@@@ -14,9 -14,12 +14,12 @@@
  #include <linux/fs.h>
  #include <linux/gpio/consumer.h>
  #include <linux/i2c.h>
+ #include <linux/kref.h>
  #include <linux/miscdevice.h>
  #include <linux/module.h>
+ #include <linux/mutex.h>
  #include <linux/pm_runtime.h>
+ #include <linux/slab.h>
  
  #define HPS_ACPI_ID		"GOOG0020"
  
@@@ -24,8 -27,26 +27,26 @@@ struct hps_drvdata 
  	struct i2c_client *client;
  	struct miscdevice misc_device;
  	struct gpio_desc *enable_gpio;
+ 
+ 	/* hps outlives the driver binding while files are open. */
+ 	struct kref kref;
+ 	/*
+ 	 * Protects open_count, and client against remove(). remove() sets
+ 	 * client to NULL after dropping the files' runtime PM references;
+ 	 * release() then leaves the device alone.
+ 	 */
+ 	struct mutex lock;
+ 	unsigned int open_count;
  };
  
+ static void hps_free(struct kref *kref)
+ {
+ 	struct hps_drvdata *hps = container_of(kref, struct hps_drvdata, kref);
+ 
+ 	mutex_destroy(&hps->lock);
+ 	kfree(hps);
+ }
+ 
  static void hps_set_power(struct hps_drvdata *hps, bool state)
  {
  	gpiod_set_value_cansleep(hps->enable_gpio, state);
@@@ -36,18 -57,33 +57,33 @@@ static int hps_open(struct inode *inode
  	struct hps_drvdata *hps = container_of(file->private_data,
  					       struct hps_drvdata, misc_device);
  	struct device *dev = &hps->client->dev;
+ 	int ret;
  
- 	return pm_runtime_resume_and_get(dev);
+ 	ret = pm_runtime_resume_and_get(dev);
+ 	if (ret)
+ 		return ret;
+ 
+ 	mutex_lock(&hps->lock);
+ 	hps->open_count++;
+ 	mutex_unlock(&hps->lock);
+ 
+ 	kref_get(&hps->kref);
+ 	file->private_data = hps;
+ 	return 0;
  }
  
  static int hps_release(struct inode *inode, struct file *file)
  {
- 	struct hps_drvdata *hps = container_of(file->private_data,
- 					       struct hps_drvdata, misc_device);
- 	struct device *dev = &hps->client->dev;
+ 	struct hps_drvdata *hps = file->private_data;
  
- 	pm_runtime_put(dev);
+ 	mutex_lock(&hps->lock);
+ 	if (hps->client) {
+ 		hps->open_count--;
+ 		pm_runtime_put(&hps->client->dev);
+ 	}
+ 	mutex_unlock(&hps->lock);
  
+ 	kref_put(&hps->kref, hps_free);
  	return 0;
  }
  
@@@ -62,10 -98,13 +98,13 @@@ static int hps_i2c_probe(struct i2c_cli
  	struct hps_drvdata *hps;
  	int ret;
  
- 	hps = devm_kzalloc_obj(&client->dev, *hps);
+ 	hps = kzalloc_obj(*hps);
  	if (!hps)
  		return -ENOMEM;
  
+ 	kref_init(&hps->kref);
+ 	mutex_init(&hps->lock);
+ 
  	hps->misc_device.parent = &client->dev;
  	hps->misc_device.minor = MISC_DYNAMIC_MINOR;
  	hps->misc_device.name = "cros-hps";
@@@ -83,32 -122,49 +122,49 @@@
  	if (IS_ERR(hps->enable_gpio)) {
  		ret = PTR_ERR(hps->enable_gpio);
  		dev_err(&client->dev, "failed to get enable gpio: %d\n", ret);
- 		return ret;
+ 		goto err_put;
  	}
  
  	ret = misc_register(&hps->misc_device);
  	if (ret) {
  		dev_err(&client->dev, "failed to initialize misc device: %d\n", ret);
- 		return ret;
+ 		goto err_put;
  	}
  
  	hps_set_power(hps, false);
  	pm_runtime_enable(&client->dev);
  	return 0;
+ 
+ err_put:
+ 	kref_put(&hps->kref, hps_free);
+ 	return ret;
  }
  
  static void hps_i2c_remove(struct i2c_client *client)
  {
  	struct hps_drvdata *hps = i2c_get_clientdata(client);
  
- 	pm_runtime_disable(&client->dev);
+ 	/* After misc_deregister(), no open() is running or can start. */
  	misc_deregister(&hps->misc_device);
  
+ 	/* Drop the runtime PM references of files that are still open. */
+ 	mutex_lock(&hps->lock);
+ 	while (hps->open_count) {
+ 		hps->open_count--;
+ 		pm_runtime_put_noidle(&client->dev);
+ 	}
+ 	hps->client = NULL;
+ 	mutex_unlock(&hps->lock);
+ 
+ 	pm_runtime_disable(&client->dev);
+ 
  	/*
  	 * Re-enable HPS, in order to return it to its default state
  	 * (i.e. powered on).
  	 */
  	hps_set_power(hps, true);
+ 
+ 	kref_put(&hps->kref, hps_free);
  }
  
  static int hps_suspend(struct device *dev)

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

                 reply	other threads:[~2026-11-16 19:27 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=avtZDbrv2YL6kfQR@sirena.org.uk \
    --to=broonie@kernel.org \
    --cc=bleung@google.com \
    --cc=ca@modelmiser.com \
    --cc=dmitry.torokhov@gmail.com \
    --cc=groeck@chromium.org \
    --cc=kees+treewide@kernel.org \
    --cc=kees@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-next@vger.kernel.org \
    --cc=tzungbi@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

all inboxes | Powered by JetHome®