[PATCH] DVB: Fix locking problems and code cleanup
authorAndrew de Quincey <adq_dvb@lidskialf.net>
Thu, 1 Dec 2005 08:51:49 +0000 (00:51 -0800)
committerLinus Torvalds <torvalds@g5.osdl.org>
Thu, 1 Dec 2005 23:48:58 +0000 (15:48 -0800)
Fix locking problems and code cleanup

Signed-off-by: Andrew de Quincey <adq_dvb@lidskialf.net>
Signed-off-by: Johannes Stezenbach <js@linuxtv.org>
Signed-off-by: Mauro Carvalho Chehab <mchehab@brturbo.com.br>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
drivers/media/dvb/dvb-core/dvb_ca_en50221.c

index 2aa767f9bd7dd88f797c8b9d48d485289661cb8a..cb2e7d6ba2836c5ca2549b020dbae71770edc3a4 100644 (file)
@@ -35,7 +35,7 @@
 #include <linux/moduleparam.h>
 #include <linux/vmalloc.h>
 #include <linux/delay.h>
-#include <linux/rwsem.h>
+#include <linux/spinlock.h>
 #include <linux/sched.h>
 
 #include "dvb_ca_en50221.h"
@@ -111,9 +111,6 @@ struct dvb_ca_slot {
        /* size of the buffer to use when talking to the CAM */
        int link_buf_size;
 
-       /* semaphore for syncing access to slot structure */
-       struct rw_semaphore sem;
-
        /* buffer for incoming packets */
        struct dvb_ringbuffer rx_buffer;
 
@@ -602,14 +599,11 @@ static int dvb_ca_en50221_read_data(struct dvb_ca_private *ca, int slot, u8 * eb
        if (ebuf == NULL) {
                int buf_free;
 
-               down_read(&ca->slot_info[slot].sem);
                if (ca->slot_info[slot].rx_buffer.data == NULL) {
-                       up_read(&ca->slot_info[slot].sem);
                        status = -EIO;
                        goto exit;
                }
                buf_free = dvb_ringbuffer_free(&ca->slot_info[slot].rx_buffer);
-               up_read(&ca->slot_info[slot].sem);
 
                if (buf_free < (ca->slot_info[slot].link_buf_size + DVB_RINGBUFFER_PKTHDRSIZE)) {
                        status = -EAGAIN;
@@ -680,14 +674,11 @@ static int dvb_ca_en50221_read_data(struct dvb_ca_private *ca, int slot, u8 * eb
 
        /* OK, add it to the receive buffer, or copy into external buffer if supplied */
        if (ebuf == NULL) {
-               down_read(&ca->slot_info[slot].sem);
                if (ca->slot_info[slot].rx_buffer.data == NULL) {
-                       up_read(&ca->slot_info[slot].sem);
                        status = -EIO;
                        goto exit;
                }
                dvb_ringbuffer_pkt_write(&ca->slot_info[slot].rx_buffer, buf, bytes_read);
-               up_read(&ca->slot_info[slot].sem);
        } else {
                memcpy(ebuf, buf, bytes_read);
        }
@@ -802,12 +793,8 @@ static int dvb_ca_en50221_slot_shutdown(struct dvb_ca_private *ca, int slot)
 {
        dprintk("%s\n", __FUNCTION__);
 
-       down_write(&ca->slot_info[slot].sem);
        ca->pub->slot_shutdown(ca->pub, slot);
        ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_NONE;
-       vfree(ca->slot_info[slot].rx_buffer.data);
-       ca->slot_info[slot].rx_buffer.data = NULL;
-       up_write(&ca->slot_info[slot].sem);
 
        /* need to wake up all processes to check if they're now
           trying to write to a defunct CAM */
@@ -893,7 +880,7 @@ void dvb_ca_en50221_frda_irq(struct dvb_ca_en50221 *pubca, int slot)
 
        case DVB_CA_SLOTSTATE_RUNNING:
                if (ca->open)
-                       dvb_ca_en50221_read_data(ca, slot, NULL, 0);
+                       dvb_ca_en50221_thread_wakeup(ca);
                break;
        }
 }
@@ -1127,16 +1114,16 @@ static int dvb_ca_en50221_thread(void *data)
                                        break;
                                }
 
-                               rxbuf = vmalloc(RX_BUFFER_SIZE);
-                               if (rxbuf == NULL) {
-                                       printk("dvb_ca adapter %d: Unable to allocate CAM rx buffer :(\n", ca->dvbdev->adapter->num);
-                                       ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
-                                       dvb_ca_en50221_thread_update_delay(ca);
-                                       break;
+                               if (ca->slot_info[slot].rx_buffer.data == NULL) {
+                                       rxbuf = vmalloc(RX_BUFFER_SIZE);
+                                       if (rxbuf == NULL) {
+                                               printk("dvb_ca adapter %d: Unable to allocate CAM rx buffer :(\n", ca->dvbdev->adapter->num);
+                                               ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
+                                               dvb_ca_en50221_thread_update_delay(ca);
+                                               break;
+                                       }
+                                       dvb_ringbuffer_init(&ca->slot_info[slot].rx_buffer, rxbuf, RX_BUFFER_SIZE);
                                }
-                               down_write(&ca->slot_info[slot].sem);
-                               dvb_ringbuffer_init(&ca->slot_info[slot].rx_buffer, rxbuf, RX_BUFFER_SIZE);
-                               up_write(&ca->slot_info[slot].sem);
 
                                ca->pub->slot_ts_enable(ca->pub, slot);
                                ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_RUNNING;
@@ -1148,11 +1135,7 @@ static int dvb_ca_en50221_thread(void *data)
                                if (!ca->open)
                                        continue;
 
-                               // no need to poll if the CAM supports IRQs
-                               if (ca->slot_info[slot].da_irq_supported)
-                                       break;
-
-                               // poll mode
+                               // poll slots for data
                                pktcount = 0;
                                while ((status = dvb_ca_en50221_read_data(ca, slot, NULL, 0)) > 0) {
                                        if (!ca->open)
@@ -1367,12 +1350,13 @@ exit:
 /**
  * Condition for waking up in dvb_ca_en50221_io_read_condition
  */
-static int dvb_ca_en50221_io_read_condition(struct dvb_ca_private *ca, int *result, int *_slot)
+static int dvb_ca_en50221_io_read_condition(struct dvb_ca_private *ca,
+                                           int *result, int *_slot)
 {
        int slot;
        int slot_count = 0;
        int idx;
-       int fraglen;
+       size_t fraglen;
        int connection_id = -1;
        int found = 0;
        u8 hdr[2];
@@ -1382,10 +1366,7 @@ static int dvb_ca_en50221_io_read_condition(struct dvb_ca_private *ca, int *resu
                if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_RUNNING)
                        goto nextslot;
 
-               down_read(&ca->slot_info[slot].sem);
-
                if (ca->slot_info[slot].rx_buffer.data == NULL) {
-                       up_read(&ca->slot_info[slot].sem);
                        return 0;
                }
 
@@ -1403,10 +1384,7 @@ static int dvb_ca_en50221_io_read_condition(struct dvb_ca_private *ca, int *resu
                        idx = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, idx, &fraglen);
                }
 
-               if (!found)
-                       up_read(&ca->slot_info[slot].sem);
-
-             nextslot:
+nextslot:
                slot = (slot + 1) % ca->slot_count;
                slot_count++;
        }
@@ -1511,8 +1489,7 @@ static ssize_t dvb_ca_en50221_io_read(struct file *file, char __user * buf,
                goto exit;
        status = pktlen;
 
-      exit:
-       up_read(&ca->slot_info[slot].sem);
+exit:
        return status;
 }
 
@@ -1544,11 +1521,11 @@ static int dvb_ca_en50221_io_open(struct inode *inode, struct file *file)
        for (i = 0; i < ca->slot_count; i++) {
 
                if (ca->slot_info[i].slot_state == DVB_CA_SLOTSTATE_RUNNING) {
-                       down_write(&ca->slot_info[i].sem);
                        if (ca->slot_info[i].rx_buffer.data != NULL) {
+                               /* it is safe to call this here without locks because
+                                * ca->open == 0. Data is not read in this case */
                                dvb_ringbuffer_flush(&ca->slot_info[i].rx_buffer);
                        }
-                       up_write(&ca->slot_info[i].sem);
                }
        }
 
@@ -1607,7 +1584,6 @@ static unsigned int dvb_ca_en50221_io_poll(struct file *file, poll_table * wait)
        dprintk("%s\n", __FUNCTION__);
 
        if (dvb_ca_en50221_io_read_condition(ca, &result, &slot) == 1) {
-               up_read(&ca->slot_info[slot].sem);
                mask |= POLLIN;
        }
 
@@ -1619,7 +1595,6 @@ static unsigned int dvb_ca_en50221_io_poll(struct file *file, poll_table * wait)
        poll_wait(file, &ca->wait_queue, wait);
 
        if (dvb_ca_en50221_io_read_condition(ca, &result, &slot) == 1) {
-               up_read(&ca->slot_info[slot].sem);
                mask |= POLLIN;
        }
 
@@ -1709,7 +1684,6 @@ int dvb_ca_en50221_init(struct dvb_adapter *dvb_adapter,
                ca->slot_info[i].slot_state = DVB_CA_SLOTSTATE_NONE;
                atomic_set(&ca->slot_info[i].camchange_count, 0);
                ca->slot_info[i].camchange_type = DVB_CA_EN50221_CAMCHANGE_REMOVED;
-               init_rwsem(&ca->slot_info[i].sem);
        }
 
        if (signal_pending(current)) {
@@ -1729,7 +1703,7 @@ int dvb_ca_en50221_init(struct dvb_adapter *dvb_adapter,
        ca->thread_pid = ret;
        return 0;
 
-      error:
+error:
        if (ca != NULL) {
                if (ca->dvbdev != NULL)
                        dvb_unregister_device(ca->dvbdev);
@@ -1771,6 +1745,9 @@ void dvb_ca_en50221_release(struct dvb_ca_en50221 *pubca)
 
        for (i = 0; i < ca->slot_count; i++) {
                dvb_ca_en50221_slot_shutdown(ca, i);
+               if (ca->slot_info[i].rx_buffer.data != NULL) {
+                       vfree(ca->slot_info[i].rx_buffer.data);
+               }
        }
        kfree(ca->slot_info);
        dvb_unregister_device(ca->dvbdev);