Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ded92846 authored by Andrew de Quincey's avatar Andrew de Quincey Committed by Linus Torvalds
Browse files

[PATCH] DVB: Fix locking problems and code cleanup



Fix locking problems and code cleanup

Signed-off-by: default avatarAndrew de Quincey <adq_dvb@lidskialf.net>
Signed-off-by: default avatarJohannes Stezenbach <js@linuxtv.org>
Signed-off-by: default avatarMauro Carvalho Chehab <mchehab@brturbo.com.br>
Signed-off-by: default avatarAndrew Morton <akpm@osdl.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@osdl.org>
parent 69459f3d
Loading
Loading
Loading
Loading
+23 −46
Original line number Original line Diff line number Diff line
@@ -35,7 +35,7 @@
#include <linux/moduleparam.h>
#include <linux/moduleparam.h>
#include <linux/vmalloc.h>
#include <linux/vmalloc.h>
#include <linux/delay.h>
#include <linux/delay.h>
#include <linux/rwsem.h>
#include <linux/spinlock.h>
#include <linux/sched.h>
#include <linux/sched.h>


#include "dvb_ca_en50221.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 */
	/* size of the buffer to use when talking to the CAM */
	int link_buf_size;
	int link_buf_size;


	/* semaphore for syncing access to slot structure */
	struct rw_semaphore sem;

	/* buffer for incoming packets */
	/* buffer for incoming packets */
	struct dvb_ringbuffer rx_buffer;
	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) {
	if (ebuf == NULL) {
		int buf_free;
		int buf_free;


		down_read(&ca->slot_info[slot].sem);
		if (ca->slot_info[slot].rx_buffer.data == NULL) {
		if (ca->slot_info[slot].rx_buffer.data == NULL) {
			up_read(&ca->slot_info[slot].sem);
			status = -EIO;
			status = -EIO;
			goto exit;
			goto exit;
		}
		}
		buf_free = dvb_ringbuffer_free(&ca->slot_info[slot].rx_buffer);
		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)) {
		if (buf_free < (ca->slot_info[slot].link_buf_size + DVB_RINGBUFFER_PKTHDRSIZE)) {
			status = -EAGAIN;
			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 */
	/* OK, add it to the receive buffer, or copy into external buffer if supplied */
	if (ebuf == NULL) {
	if (ebuf == NULL) {
		down_read(&ca->slot_info[slot].sem);
		if (ca->slot_info[slot].rx_buffer.data == NULL) {
		if (ca->slot_info[slot].rx_buffer.data == NULL) {
			up_read(&ca->slot_info[slot].sem);
			status = -EIO;
			status = -EIO;
			goto exit;
			goto exit;
		}
		}
		dvb_ringbuffer_pkt_write(&ca->slot_info[slot].rx_buffer, buf, bytes_read);
		dvb_ringbuffer_pkt_write(&ca->slot_info[slot].rx_buffer, buf, bytes_read);
		up_read(&ca->slot_info[slot].sem);
	} else {
	} else {
		memcpy(ebuf, buf, bytes_read);
		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__);
	dprintk("%s\n", __FUNCTION__);


	down_write(&ca->slot_info[slot].sem);
	ca->pub->slot_shutdown(ca->pub, slot);
	ca->pub->slot_shutdown(ca->pub, slot);
	ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_NONE;
	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
	/* need to wake up all processes to check if they're now
	   trying to write to a defunct CAM */
	   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:
	case DVB_CA_SLOTSTATE_RUNNING:
		if (ca->open)
		if (ca->open)
			dvb_ca_en50221_read_data(ca, slot, NULL, 0);
			dvb_ca_en50221_thread_wakeup(ca);
		break;
		break;
	}
	}
}
}
@@ -1127,6 +1114,7 @@ static int dvb_ca_en50221_thread(void *data)
					break;
					break;
				}
				}


				if (ca->slot_info[slot].rx_buffer.data == NULL) {
					rxbuf = vmalloc(RX_BUFFER_SIZE);
					rxbuf = vmalloc(RX_BUFFER_SIZE);
					if (rxbuf == NULL) {
					if (rxbuf == NULL) {
						printk("dvb_ca adapter %d: Unable to allocate CAM rx buffer :(\n", ca->dvbdev->adapter->num);
						printk("dvb_ca adapter %d: Unable to allocate CAM rx buffer :(\n", ca->dvbdev->adapter->num);
@@ -1134,9 +1122,8 @@ static int dvb_ca_en50221_thread(void *data)
						dvb_ca_en50221_thread_update_delay(ca);
						dvb_ca_en50221_thread_update_delay(ca);
						break;
						break;
					}
					}
				down_write(&ca->slot_info[slot].sem);
					dvb_ringbuffer_init(&ca->slot_info[slot].rx_buffer, rxbuf, RX_BUFFER_SIZE);
					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->pub->slot_ts_enable(ca->pub, slot);
				ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_RUNNING;
				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)
				if (!ca->open)
					continue;
					continue;


				// no need to poll if the CAM supports IRQs
				// poll slots for data
				if (ca->slot_info[slot].da_irq_supported)
					break;

				// poll mode
				pktcount = 0;
				pktcount = 0;
				while ((status = dvb_ca_en50221_read_data(ca, slot, NULL, 0)) > 0) {
				while ((status = dvb_ca_en50221_read_data(ca, slot, NULL, 0)) > 0) {
					if (!ca->open)
					if (!ca->open)
@@ -1367,12 +1350,13 @@ exit:
/**
/**
 * Condition for waking up in dvb_ca_en50221_io_read_condition
 * 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;
	int slot_count = 0;
	int slot_count = 0;
	int idx;
	int idx;
	int fraglen;
	size_t fraglen;
	int connection_id = -1;
	int connection_id = -1;
	int found = 0;
	int found = 0;
	u8 hdr[2];
	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)
		if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_RUNNING)
			goto nextslot;
			goto nextslot;


		down_read(&ca->slot_info[slot].sem);

		if (ca->slot_info[slot].rx_buffer.data == NULL) {
		if (ca->slot_info[slot].rx_buffer.data == NULL) {
			up_read(&ca->slot_info[slot].sem);
			return 0;
			return 0;
		}
		}


@@ -1403,9 +1384,6 @@ 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);
			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 = (slot + 1) % ca->slot_count;
		slot_count++;
		slot_count++;
@@ -1512,7 +1490,6 @@ static ssize_t dvb_ca_en50221_io_read(struct file *file, char __user * buf,
	status = pktlen;
	status = pktlen;


exit:
exit:
	up_read(&ca->slot_info[slot].sem);
	return status;
	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++) {
	for (i = 0; i < ca->slot_count; i++) {


		if (ca->slot_info[i].slot_state == DVB_CA_SLOTSTATE_RUNNING) {
		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) {
			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);
				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__);
	dprintk("%s\n", __FUNCTION__);


	if (dvb_ca_en50221_io_read_condition(ca, &result, &slot) == 1) {
	if (dvb_ca_en50221_io_read_condition(ca, &result, &slot) == 1) {
		up_read(&ca->slot_info[slot].sem);
		mask |= POLLIN;
		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);
	poll_wait(file, &ca->wait_queue, wait);


	if (dvb_ca_en50221_io_read_condition(ca, &result, &slot) == 1) {
	if (dvb_ca_en50221_io_read_condition(ca, &result, &slot) == 1) {
		up_read(&ca->slot_info[slot].sem);
		mask |= POLLIN;
		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;
		ca->slot_info[i].slot_state = DVB_CA_SLOTSTATE_NONE;
		atomic_set(&ca->slot_info[i].camchange_count, 0);
		atomic_set(&ca->slot_info[i].camchange_count, 0);
		ca->slot_info[i].camchange_type = DVB_CA_EN50221_CAMCHANGE_REMOVED;
		ca->slot_info[i].camchange_type = DVB_CA_EN50221_CAMCHANGE_REMOVED;
		init_rwsem(&ca->slot_info[i].sem);
	}
	}


	if (signal_pending(current)) {
	if (signal_pending(current)) {
@@ -1771,6 +1745,9 @@ void dvb_ca_en50221_release(struct dvb_ca_en50221 *pubca)


	for (i = 0; i < ca->slot_count; i++) {
	for (i = 0; i < ca->slot_count; i++) {
		dvb_ca_en50221_slot_shutdown(ca, 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);
	kfree(ca->slot_info);
	dvb_unregister_device(ca->dvbdev);
	dvb_unregister_device(ca->dvbdev);