[PATCH 2/3] i2c: Mark adapter as initialised

From: Chris Wilson
Date: Thu Jun 09 2016 - 04:54:30 EST


In order to allow drivers to call i2c_init_adapter as a separate step,
and to allow other drivers to continue skipping the call, mark the
adapter as initialised upon i2c_init_adapter() and ignore multiple
calls.

The choice of how to introduce an "already-initialised" check into the
existing i2c_add_adapter() callers is tricky as we cannot make too many
assumptions about the state of memory, i.e. whether the struct was
cleared before being passed to i2c_add_adapter. This poses an issue if
we wanted to add a boolean flag to mark when the adapter is initialised
(as that flag may be set due to previous contents of memory). Instead,
we opt to use the userspace_client_lists as that should be empty from
initialisation through to registration - but will be reported as !empty
if it has either random junk or zeroes (with a very small chance that we
fail to register an adapter that just happens to have a prior empty list
at that location in memory).

Signed-off-by: Chris Wilson <chris@xxxxxxxxxxxxxxxxxx>
Cc: Ville SyrjÃlà <ville.syrjala@xxxxxxxxxxxxxxx>
Cc: Wolfram Sang <wsa@xxxxxxxxxxxxx>
Cc: linux-i2c@xxxxxxxxxxxxxxx
Cc: linux-kernel@xxxxxxxxxxxxxxx
Cc: dri-devel@xxxxxxxxxxxxxxxxxxxxx
---
drivers/i2c/i2c-core.c | 7 +++++++
1 file changed, 7 insertions(+)

diff --git a/drivers/i2c/i2c-core.c b/drivers/i2c/i2c-core.c
index 743c38a63da1..91ff70d31ec8 100644
--- a/drivers/i2c/i2c-core.c
+++ b/drivers/i2c/i2c-core.c
@@ -1513,6 +1513,10 @@ static int __process_new_adapter(struct device_driver *d, void *data)

static int i2c_init_adapter(struct i2c_adapter *adap)
{
+ /* Only initialise the adapter once. */
+ if (list_empty(&adap->userspace_clients))
+ return 0;
+
/* Sanity checks */
if (unlikely(adap->name[0] == '\0')) {
pr_err("i2c-core: Attempt to register an adapter with "
@@ -1632,6 +1636,9 @@ out_list:
mutex_lock(&core_lock);
idr_remove(&i2c_adapter_idr, adap->nr);
mutex_unlock(&core_lock);
+
+ /* Force initialisation if this struct gets reused */
+ memset(&adap->userspace_clients, 0, sizeof(adap->userspace_clients));
return res;
}

--
2.8.1