|
|
@@ -9,15 +9,13 @@
|
|
|
#include <linux/idr.h>
|
|
|
#include <linux/nvmem-provider.h>
|
|
|
#include <linux/pm_runtime.h>
|
|
|
+#include <linux/sched/signal.h>
|
|
|
#include <linux/sizes.h>
|
|
|
#include <linux/slab.h>
|
|
|
#include <linux/vmalloc.h>
|
|
|
|
|
|
#include "tb.h"
|
|
|
|
|
|
-/* Switch authorization from userspace is serialized by this lock */
|
|
|
-static DEFINE_MUTEX(switch_lock);
|
|
|
-
|
|
|
/* Switch NVM support */
|
|
|
|
|
|
#define NVM_DEVID 0x05
|
|
|
@@ -253,8 +251,8 @@ static int tb_switch_nvm_write(void *priv, unsigned int offset, void *val,
|
|
|
struct tb_switch *sw = priv;
|
|
|
int ret = 0;
|
|
|
|
|
|
- if (mutex_lock_interruptible(&switch_lock))
|
|
|
- return -ERESTARTSYS;
|
|
|
+ if (!mutex_trylock(&sw->tb->lock))
|
|
|
+ return restart_syscall();
|
|
|
|
|
|
/*
|
|
|
* Since writing the NVM image might require some special steps,
|
|
|
@@ -274,7 +272,7 @@ static int tb_switch_nvm_write(void *priv, unsigned int offset, void *val,
|
|
|
memcpy(sw->nvm->buf + offset, val, bytes);
|
|
|
|
|
|
unlock:
|
|
|
- mutex_unlock(&switch_lock);
|
|
|
+ mutex_unlock(&sw->tb->lock);
|
|
|
|
|
|
return ret;
|
|
|
}
|
|
|
@@ -363,10 +361,7 @@ static int tb_switch_nvm_add(struct tb_switch *sw)
|
|
|
}
|
|
|
nvm->non_active = nvm_dev;
|
|
|
|
|
|
- mutex_lock(&switch_lock);
|
|
|
sw->nvm = nvm;
|
|
|
- mutex_unlock(&switch_lock);
|
|
|
-
|
|
|
return 0;
|
|
|
|
|
|
err_nvm_active:
|
|
|
@@ -383,10 +378,8 @@ static void tb_switch_nvm_remove(struct tb_switch *sw)
|
|
|
{
|
|
|
struct tb_switch_nvm *nvm;
|
|
|
|
|
|
- mutex_lock(&switch_lock);
|
|
|
nvm = sw->nvm;
|
|
|
sw->nvm = NULL;
|
|
|
- mutex_unlock(&switch_lock);
|
|
|
|
|
|
if (!nvm)
|
|
|
return;
|
|
|
@@ -717,8 +710,8 @@ static int tb_switch_set_authorized(struct tb_switch *sw, unsigned int val)
|
|
|
{
|
|
|
int ret = -EINVAL;
|
|
|
|
|
|
- if (mutex_lock_interruptible(&switch_lock))
|
|
|
- return -ERESTARTSYS;
|
|
|
+ if (!mutex_trylock(&sw->tb->lock))
|
|
|
+ return restart_syscall();
|
|
|
|
|
|
if (sw->authorized)
|
|
|
goto unlock;
|
|
|
@@ -761,7 +754,7 @@ static int tb_switch_set_authorized(struct tb_switch *sw, unsigned int val)
|
|
|
}
|
|
|
|
|
|
unlock:
|
|
|
- mutex_unlock(&switch_lock);
|
|
|
+ mutex_unlock(&sw->tb->lock);
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
|
@@ -818,15 +811,15 @@ static ssize_t key_show(struct device *dev, struct device_attribute *attr,
|
|
|
struct tb_switch *sw = tb_to_switch(dev);
|
|
|
ssize_t ret;
|
|
|
|
|
|
- if (mutex_lock_interruptible(&switch_lock))
|
|
|
- return -ERESTARTSYS;
|
|
|
+ if (!mutex_trylock(&sw->tb->lock))
|
|
|
+ return restart_syscall();
|
|
|
|
|
|
if (sw->key)
|
|
|
ret = sprintf(buf, "%*phN\n", TB_SWITCH_KEY_SIZE, sw->key);
|
|
|
else
|
|
|
ret = sprintf(buf, "\n");
|
|
|
|
|
|
- mutex_unlock(&switch_lock);
|
|
|
+ mutex_unlock(&sw->tb->lock);
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
|
@@ -843,8 +836,8 @@ static ssize_t key_store(struct device *dev, struct device_attribute *attr,
|
|
|
else if (hex2bin(key, buf, sizeof(key)))
|
|
|
return -EINVAL;
|
|
|
|
|
|
- if (mutex_lock_interruptible(&switch_lock))
|
|
|
- return -ERESTARTSYS;
|
|
|
+ if (!mutex_trylock(&sw->tb->lock))
|
|
|
+ return restart_syscall();
|
|
|
|
|
|
if (sw->authorized) {
|
|
|
ret = -EBUSY;
|
|
|
@@ -859,7 +852,7 @@ static ssize_t key_store(struct device *dev, struct device_attribute *attr,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- mutex_unlock(&switch_lock);
|
|
|
+ mutex_unlock(&sw->tb->lock);
|
|
|
return ret;
|
|
|
}
|
|
|
static DEVICE_ATTR(key, 0600, key_show, key_store);
|
|
|
@@ -905,8 +898,8 @@ static ssize_t nvm_authenticate_store(struct device *dev,
|
|
|
bool val;
|
|
|
int ret;
|
|
|
|
|
|
- if (mutex_lock_interruptible(&switch_lock))
|
|
|
- return -ERESTARTSYS;
|
|
|
+ if (!mutex_trylock(&sw->tb->lock))
|
|
|
+ return restart_syscall();
|
|
|
|
|
|
/* If NVMem devices are not yet added */
|
|
|
if (!sw->nvm) {
|
|
|
@@ -954,7 +947,7 @@ static ssize_t nvm_authenticate_store(struct device *dev,
|
|
|
}
|
|
|
|
|
|
exit_unlock:
|
|
|
- mutex_unlock(&switch_lock);
|
|
|
+ mutex_unlock(&sw->tb->lock);
|
|
|
|
|
|
if (ret)
|
|
|
return ret;
|
|
|
@@ -968,8 +961,8 @@ static ssize_t nvm_version_show(struct device *dev,
|
|
|
struct tb_switch *sw = tb_to_switch(dev);
|
|
|
int ret;
|
|
|
|
|
|
- if (mutex_lock_interruptible(&switch_lock))
|
|
|
- return -ERESTARTSYS;
|
|
|
+ if (!mutex_trylock(&sw->tb->lock))
|
|
|
+ return restart_syscall();
|
|
|
|
|
|
if (sw->safe_mode)
|
|
|
ret = -ENODATA;
|
|
|
@@ -978,7 +971,7 @@ static ssize_t nvm_version_show(struct device *dev,
|
|
|
else
|
|
|
ret = sprintf(buf, "%x.%x\n", sw->nvm->major, sw->nvm->minor);
|
|
|
|
|
|
- mutex_unlock(&switch_lock);
|
|
|
+ mutex_unlock(&sw->tb->lock);
|
|
|
|
|
|
return ret;
|
|
|
}
|