Commit 05b41494 authored by Jan Kiszka's avatar Jan Kiszka Committed by David S. Miller

CAPI: Rework locking of capidev members

Rename 'ncci_list_mtx' to 'lock', expressing that it now protects a
larger set of capidev members: the NCCI list, ap.applid (ie. the
registration of the application), and modifications of userflags.

We do not need to protect each and every check for ap.applid because,
once an application is registered, it will stay for the whole lifetime
of the device.

Also, there is no need to apply the capidev mutex during release (if
there could be concurrent users, we would crash them anyway by freeing
the device at the end of capi_release).
Signed-off-by: default avatarJan Kiszka <jan.kiszka@web.de>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent eca39dd8
...@@ -141,7 +141,7 @@ struct capidev { ...@@ -141,7 +141,7 @@ struct capidev {
struct capincci *nccis; struct capincci *nccis;
struct mutex ncci_list_mtx; struct mutex lock;
}; };
/* -------- global variables ---------------------------------------- */ /* -------- global variables ---------------------------------------- */
...@@ -574,38 +574,31 @@ static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb) ...@@ -574,38 +574,31 @@ static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
u16 datahandle; u16 datahandle;
#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */ #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
struct capincci *np; struct capincci *np;
u32 ncci;
unsigned long flags; unsigned long flags;
mutex_lock(&cdev->lock);
if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) { if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) {
u16 info = CAPIMSG_U16(skb->data, 12); // Info field u16 info = CAPIMSG_U16(skb->data, 12); // Info field
if ((info & 0xff00) == 0) { if ((info & 0xff00) == 0)
mutex_lock(&cdev->ncci_list_mtx);
capincci_alloc(cdev, CAPIMSG_NCCI(skb->data)); capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
mutex_unlock(&cdev->ncci_list_mtx);
} }
} if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND)
if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND) {
mutex_lock(&cdev->ncci_list_mtx);
capincci_alloc(cdev, CAPIMSG_NCCI(skb->data)); capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
mutex_unlock(&cdev->ncci_list_mtx);
}
spin_lock_irqsave(&workaround_lock, flags); spin_lock_irqsave(&workaround_lock, flags);
if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) { if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
skb_queue_tail(&cdev->recvqueue, skb); skb_queue_tail(&cdev->recvqueue, skb);
wake_up_interruptible(&cdev->recvwait); wake_up_interruptible(&cdev->recvwait);
spin_unlock_irqrestore(&workaround_lock, flags); goto unlock_out;
return;
} }
ncci = CAPIMSG_CONTROL(skb->data);
for (np = cdev->nccis; np && np->ncci != ncci; np = np->next) np = capincci_find(cdev, CAPIMSG_CONTROL(skb->data));
;
if (!np) { if (!np) {
printk(KERN_ERR "BUG: capi_signal: ncci not found\n"); printk(KERN_ERR "BUG: capi_signal: ncci not found\n");
skb_queue_tail(&cdev->recvqueue, skb); skb_queue_tail(&cdev->recvqueue, skb);
wake_up_interruptible(&cdev->recvwait); wake_up_interruptible(&cdev->recvwait);
spin_unlock_irqrestore(&workaround_lock, flags); goto unlock_out;
return;
} }
#ifndef CONFIG_ISDN_CAPI_MIDDLEWARE #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
...@@ -618,8 +611,7 @@ static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb) ...@@ -618,8 +611,7 @@ static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
if (!mp) { if (!mp) {
skb_queue_tail(&cdev->recvqueue, skb); skb_queue_tail(&cdev->recvqueue, skb);
wake_up_interruptible(&cdev->recvwait); wake_up_interruptible(&cdev->recvwait);
spin_unlock_irqrestore(&workaround_lock, flags); goto unlock_out;
return;
} }
if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) { if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) {
datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+4+2); datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+4+2);
...@@ -652,7 +644,9 @@ static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb) ...@@ -652,7 +644,9 @@ static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
} }
#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */ #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
unlock_out:
spin_unlock_irqrestore(&workaround_lock, flags); spin_unlock_irqrestore(&workaround_lock, flags);
mutex_unlock(&cdev->lock);
} }
/* -------- file_operations for capidev ----------------------------- */ /* -------- file_operations for capidev ----------------------------- */
...@@ -730,9 +724,9 @@ capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos ...@@ -730,9 +724,9 @@ capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos
CAPIMSG_SETAPPID(skb->data, cdev->ap.applid); CAPIMSG_SETAPPID(skb->data, cdev->ap.applid);
if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) { if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) {
mutex_lock(&cdev->ncci_list_mtx); mutex_lock(&cdev->lock);
capincci_free(cdev, CAPIMSG_NCCI(skb->data)); capincci_free(cdev, CAPIMSG_NCCI(skb->data));
mutex_unlock(&cdev->ncci_list_mtx); mutex_unlock(&cdev->lock);
} }
cdev->errcode = capi20_put_message(&cdev->ap, skb); cdev->errcode = capi20_put_message(&cdev->ap, skb);
...@@ -765,30 +759,35 @@ capi_ioctl(struct inode *inode, struct file *file, ...@@ -765,30 +759,35 @@ capi_ioctl(struct inode *inode, struct file *file,
unsigned int cmd, unsigned long arg) unsigned int cmd, unsigned long arg)
{ {
struct capidev *cdev = file->private_data; struct capidev *cdev = file->private_data;
struct capi20_appl *ap = &cdev->ap;
capi_ioctl_struct data; capi_ioctl_struct data;
int retval = -EINVAL; int retval = -EINVAL;
void __user *argp = (void __user *)arg; void __user *argp = (void __user *)arg;
switch (cmd) { switch (cmd) {
case CAPI_REGISTER: case CAPI_REGISTER:
{ mutex_lock(&cdev->lock);
if (ap->applid)
return -EEXIST;
if (cdev->ap.applid) {
retval = -EEXIST;
goto register_out;
}
if (copy_from_user(&cdev->ap.rparam, argp, if (copy_from_user(&cdev->ap.rparam, argp,
sizeof(struct capi_register_params))) sizeof(struct capi_register_params))) {
return -EFAULT; retval = -EFAULT;
goto register_out;
}
cdev->ap.private = cdev; cdev->ap.private = cdev;
cdev->ap.recv_message = capi_recv_message; cdev->ap.recv_message = capi_recv_message;
cdev->errcode = capi20_register(ap); cdev->errcode = capi20_register(&cdev->ap);
retval = (int)cdev->ap.applid;
if (cdev->errcode) { if (cdev->errcode) {
ap->applid = 0; cdev->ap.applid = 0;
return -EIO; retval = -EIO;
}
} }
return (int)ap->applid;
register_out:
mutex_unlock(&cdev->lock);
return retval;
case CAPI_GET_VERSION: case CAPI_GET_VERSION:
{ {
...@@ -887,68 +886,67 @@ capi_ioctl(struct inode *inode, struct file *file, ...@@ -887,68 +886,67 @@ capi_ioctl(struct inode *inode, struct file *file,
return 0; return 0;
case CAPI_SET_FLAGS: case CAPI_SET_FLAGS:
case CAPI_CLR_FLAGS: case CAPI_CLR_FLAGS: {
{
unsigned userflags; unsigned userflags;
if (copy_from_user(&userflags, argp,
sizeof(userflags))) if (copy_from_user(&userflags, argp, sizeof(userflags)))
return -EFAULT; return -EFAULT;
mutex_lock(&cdev->lock);
if (cmd == CAPI_SET_FLAGS) if (cmd == CAPI_SET_FLAGS)
cdev->userflags |= userflags; cdev->userflags |= userflags;
else else
cdev->userflags &= ~userflags; cdev->userflags &= ~userflags;
} mutex_unlock(&cdev->lock);
return 0; return 0;
}
case CAPI_GET_FLAGS: case CAPI_GET_FLAGS:
if (copy_to_user(argp, &cdev->userflags, if (copy_to_user(argp, &cdev->userflags,
sizeof(cdev->userflags))) sizeof(cdev->userflags)))
return -EFAULT; return -EFAULT;
return 0; return 0;
case CAPI_NCCI_OPENCOUNT: case CAPI_NCCI_OPENCOUNT: {
{
struct capincci *nccip; struct capincci *nccip;
unsigned ncci; unsigned ncci;
int count = 0; int count = 0;
if (copy_from_user(&ncci, argp, sizeof(ncci))) if (copy_from_user(&ncci, argp, sizeof(ncci)))
return -EFAULT; return -EFAULT;
mutex_lock(&cdev->ncci_list_mtx); mutex_lock(&cdev->lock);
if ((nccip = capincci_find(cdev, (u32) ncci)) == NULL) { nccip = capincci_find(cdev, (u32)ncci);
mutex_unlock(&cdev->ncci_list_mtx); if (nccip)
return 0; count = capincci_minor_opencount(nccip);
} mutex_unlock(&cdev->lock);
count += capincci_minor_opencount(nccip);
mutex_unlock(&cdev->ncci_list_mtx);
return count; return count;
} }
return 0;
#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
case CAPI_NCCI_GETUNIT: case CAPI_NCCI_GETUNIT: {
{
struct capincci *nccip; struct capincci *nccip;
struct capiminor *mp; struct capiminor *mp;
unsigned ncci; unsigned ncci;
int unit = 0; int unit = -ESRCH;
if (copy_from_user(&ncci, argp,
sizeof(ncci))) if (copy_from_user(&ncci, argp, sizeof(ncci)))
return -EFAULT; return -EFAULT;
mutex_lock(&cdev->ncci_list_mtx);
nccip = capincci_find(cdev, (u32) ncci); mutex_lock(&cdev->lock);
if (!nccip || (mp = nccip->minorp) == NULL) { nccip = capincci_find(cdev, (u32)ncci);
mutex_unlock(&cdev->ncci_list_mtx); if (nccip) {
return -ESRCH; mp = nccip->minorp;
} if (mp)
unit = mp->minor; unit = mp->minor;
mutex_unlock(&cdev->ncci_list_mtx); }
mutex_unlock(&cdev->lock);
return unit; return unit;
} }
return 0;
#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */ #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
}
default:
return -EINVAL; return -EINVAL;
}
} }
static int capi_open(struct inode *inode, struct file *file) static int capi_open(struct inode *inode, struct file *file)
...@@ -959,7 +957,7 @@ static int capi_open(struct inode *inode, struct file *file) ...@@ -959,7 +957,7 @@ static int capi_open(struct inode *inode, struct file *file)
if (!cdev) if (!cdev)
return -ENOMEM; return -ENOMEM;
mutex_init(&cdev->ncci_list_mtx); mutex_init(&cdev->lock);
skb_queue_head_init(&cdev->recvqueue); skb_queue_head_init(&cdev->recvqueue);
init_waitqueue_head(&cdev->recvwait); init_waitqueue_head(&cdev->recvwait);
file->private_data = cdev; file->private_data = cdev;
...@@ -979,15 +977,10 @@ static int capi_release(struct inode *inode, struct file *file) ...@@ -979,15 +977,10 @@ static int capi_release(struct inode *inode, struct file *file)
list_del(&cdev->list); list_del(&cdev->list);
mutex_unlock(&capidev_list_lock); mutex_unlock(&capidev_list_lock);
if (cdev->ap.applid) { if (cdev->ap.applid)
capi20_release(&cdev->ap); capi20_release(&cdev->ap);
cdev->ap.applid = 0;
}
skb_queue_purge(&cdev->recvqueue); skb_queue_purge(&cdev->recvqueue);
mutex_lock(&cdev->ncci_list_mtx);
capincci_free(cdev, 0xffffffff); capincci_free(cdev, 0xffffffff);
mutex_unlock(&cdev->ncci_list_mtx);
kfree(cdev); kfree(cdev);
return 0; return 0;
...@@ -1446,11 +1439,13 @@ static int capi20ncci_proc_show(struct seq_file *m, void *v) ...@@ -1446,11 +1439,13 @@ static int capi20ncci_proc_show(struct seq_file *m, void *v)
mutex_lock(&capidev_list_lock); mutex_lock(&capidev_list_lock);
list_for_each(l, &capidev_list) { list_for_each(l, &capidev_list) {
cdev = list_entry(l, struct capidev, list); cdev = list_entry(l, struct capidev, list);
mutex_lock(&cdev->lock);
for (np=cdev->nccis; np; np = np->next) { for (np=cdev->nccis; np; np = np->next) {
seq_printf(m, "%d 0x%x\n", seq_printf(m, "%d 0x%x\n",
cdev->ap.applid, cdev->ap.applid,
np->ncci); np->ncci);
} }
mutex_unlock(&cdev->lock);
} }
mutex_unlock(&capidev_list_lock); mutex_unlock(&capidev_list_lock);
return 0; return 0;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment