Skip to content

Commit 47677e5

Browse files
committed
[media] em28xx: Only deallocate struct em28xx after finishing all extensions
We can't free struct em28xx while one of the extensions is still using it. So, add a kref() to control it, freeing it only after the extensions fini calls. Reviewed-by: Frank Schäfer <[email protected]> Signed-off-by: Mauro Carvalho Chehab <[email protected]>
1 parent 88e4fcd commit 47677e5

File tree

6 files changed

+56
-19
lines changed

6 files changed

+56
-19
lines changed

drivers/media/usb/em28xx/em28xx-audio.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ static int snd_em28xx_capture_open(struct snd_pcm_substream *substream)
301301
goto err;
302302
}
303303

304+
kref_get(&dev->ref);
304305
dev->adev.users++;
305306
mutex_unlock(&dev->lock);
306307

@@ -341,6 +342,7 @@ static int snd_em28xx_pcm_close(struct snd_pcm_substream *substream)
341342
substream->runtime->dma_area = NULL;
342343
}
343344
mutex_unlock(&dev->lock);
345+
kref_put(&dev->ref, em28xx_free_device);
344346

345347
return 0;
346348
}
@@ -895,6 +897,8 @@ static int em28xx_audio_init(struct em28xx *dev)
895897

896898
em28xx_info("Binding audio extension\n");
897899

900+
kref_get(&dev->ref);
901+
898902
printk(KERN_INFO "em28xx-audio.c: Copyright (C) 2006 Markus "
899903
"Rechberger\n");
900904
printk(KERN_INFO
@@ -967,7 +971,7 @@ static int em28xx_audio_fini(struct em28xx *dev)
967971
if (dev == NULL)
968972
return 0;
969973

970-
if (dev->has_alsa_audio != 1) {
974+
if (!dev->has_alsa_audio) {
971975
/* This device does not support the extension (in this case
972976
the device is expecting the snd-usb-audio module or
973977
doesn't have analog audio support at all) */
@@ -986,6 +990,7 @@ static int em28xx_audio_fini(struct em28xx *dev)
986990
dev->adev.sndcard = NULL;
987991
}
988992

993+
kref_put(&dev->ref, em28xx_free_device);
989994
return 0;
990995
}
991996

drivers/media/usb/em28xx/em28xx-cards.c

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2939,7 +2939,7 @@ static void flush_request_modules(struct em28xx *dev)
29392939
* unregisters the v4l2,i2c and usb devices
29402940
* called when the device gets disconnected or at module unload
29412941
*/
2942-
void em28xx_release_resources(struct em28xx *dev)
2942+
static void em28xx_release_resources(struct em28xx *dev)
29432943
{
29442944
/*FIXME: I2C IR should be disconnected */
29452945

@@ -2956,7 +2956,27 @@ void em28xx_release_resources(struct em28xx *dev)
29562956

29572957
mutex_unlock(&dev->lock);
29582958
};
2959-
EXPORT_SYMBOL_GPL(em28xx_release_resources);
2959+
2960+
/**
2961+
* em28xx_free_device() - Free em28xx device
2962+
*
2963+
* @ref: struct kref for em28xx device
2964+
*
2965+
* This is called when all extensions and em28xx core unregisters a device
2966+
*/
2967+
void em28xx_free_device(struct kref *ref)
2968+
{
2969+
struct em28xx *dev = kref_to_dev(ref);
2970+
2971+
em28xx_info("Freeing device\n");
2972+
2973+
if (!dev->disconnected)
2974+
em28xx_release_resources(dev);
2975+
2976+
kfree(dev->alt_max_pkt_size_isoc);
2977+
kfree(dev);
2978+
}
2979+
EXPORT_SYMBOL_GPL(em28xx_free_device);
29602980

29612981
/*
29622982
* em28xx_init_dev()
@@ -3409,6 +3429,8 @@ static int em28xx_usb_probe(struct usb_interface *interface,
34093429
dev->dvb_xfer_bulk ? "bulk" : "isoc");
34103430
}
34113431

3432+
kref_init(&dev->ref);
3433+
34123434
request_modules(dev);
34133435

34143436
/* Should be the last thing to do, to avoid newer udev's to
@@ -3453,11 +3475,7 @@ static void em28xx_usb_disconnect(struct usb_interface *interface)
34533475
em28xx_close_extension(dev);
34543476

34553477
em28xx_release_resources(dev);
3456-
3457-
if (!dev->users) {
3458-
kfree(dev->alt_max_pkt_size_isoc);
3459-
kfree(dev);
3460-
}
3478+
kref_put(&dev->ref, em28xx_free_device);
34613479
}
34623480

34633481
static int em28xx_usb_suspend(struct usb_interface *interface,

drivers/media/usb/em28xx/em28xx-dvb.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1043,7 +1043,6 @@ static int em28xx_dvb_init(struct em28xx *dev)
10431043
em28xx_info("Binding DVB extension\n");
10441044

10451045
dvb = kzalloc(sizeof(struct em28xx_dvb), GFP_KERNEL);
1046-
10471046
if (dvb == NULL) {
10481047
em28xx_info("em28xx_dvb: memory allocation failed\n");
10491048
return -ENOMEM;
@@ -1521,6 +1520,9 @@ static int em28xx_dvb_init(struct em28xx *dev)
15211520
dvb->adapter.mfe_shared = mfe_shared;
15221521

15231522
em28xx_info("DVB extension successfully initialized\n");
1523+
1524+
kref_get(&dev->ref);
1525+
15241526
ret:
15251527
em28xx_set_mode(dev, EM28XX_SUSPEND);
15261528
mutex_unlock(&dev->lock);
@@ -1577,6 +1579,7 @@ static int em28xx_dvb_fini(struct em28xx *dev)
15771579
em28xx_unregister_dvb(dvb);
15781580
kfree(dvb);
15791581
dev->dvb = NULL;
1582+
kref_put(&dev->ref, em28xx_free_device);
15801583
}
15811584

15821585
return 0;

drivers/media/usb/em28xx/em28xx-input.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,8 @@ static int em28xx_ir_init(struct em28xx *dev)
676676
return 0;
677677
}
678678

679+
kref_get(&dev->ref);
680+
679681
if (dev->board.buttons)
680682
em28xx_init_buttons(dev);
681683

@@ -816,14 +818,18 @@ static int em28xx_ir_fini(struct em28xx *dev)
816818

817819
/* skip detach on non attached boards */
818820
if (!ir)
819-
return 0;
821+
goto ref_put;
820822

821823
if (ir->rc)
822824
rc_unregister_device(ir->rc);
823825

824826
/* done */
825827
kfree(ir);
826828
dev->ir = NULL;
829+
830+
ref_put:
831+
kref_put(&dev->ref, em28xx_free_device);
832+
827833
return 0;
828834
}
829835

drivers/media/usb/em28xx/em28xx-video.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1837,7 +1837,6 @@ static int em28xx_v4l2_open(struct file *filp)
18371837
video_device_node_name(vdev), v4l2_type_names[fh_type],
18381838
dev->users);
18391839

1840-
18411840
if (mutex_lock_interruptible(&dev->lock))
18421841
return -ERESTARTSYS;
18431842
fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
@@ -1869,6 +1868,7 @@ static int em28xx_v4l2_open(struct file *filp)
18691868
v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
18701869
}
18711870

1871+
kref_get(&dev->ref);
18721872
dev->users++;
18731873

18741874
mutex_unlock(&dev->lock);
@@ -1926,9 +1926,8 @@ static int em28xx_v4l2_fini(struct em28xx *dev)
19261926
dev->clk = NULL;
19271927
}
19281928

1929-
if (dev->users)
1930-
em28xx_warn("Device is open ! Memory deallocation is deferred on last close.\n");
19311929
mutex_unlock(&dev->lock);
1930+
kref_put(&dev->ref, em28xx_free_device);
19321931

19331932
return 0;
19341933
}
@@ -1976,11 +1975,9 @@ static int em28xx_v4l2_close(struct file *filp)
19761975
mutex_lock(&dev->lock);
19771976

19781977
if (dev->users == 1) {
1979-
/* free the remaining resources if device is disconnected */
1980-
if (dev->disconnected) {
1981-
kfree(dev->alt_max_pkt_size_isoc);
1978+
/* No sense to try to write to the device */
1979+
if (dev->disconnected)
19821980
goto exit;
1983-
}
19841981

19851982
/* Save some power by putting tuner to sleep */
19861983
v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
@@ -2001,6 +1998,8 @@ static int em28xx_v4l2_close(struct file *filp)
20011998
exit:
20021999
dev->users--;
20032000
mutex_unlock(&dev->lock);
2001+
kref_put(&dev->ref, em28xx_free_device);
2002+
20042003
return 0;
20052004
}
20062005

@@ -2515,6 +2514,8 @@ static int em28xx_v4l2_init(struct em28xx *dev)
25152514

25162515
em28xx_info("V4L2 extension successfully initialized\n");
25172516

2517+
kref_get(&dev->ref);
2518+
25182519
mutex_unlock(&dev->lock);
25192520
return 0;
25202521

drivers/media/usb/em28xx/em28xx.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <linux/workqueue.h>
3333
#include <linux/i2c.h>
3434
#include <linux/mutex.h>
35+
#include <linux/kref.h>
3536
#include <linux/videodev2.h>
3637

3738
#include <media/videobuf2-vmalloc.h>
@@ -536,9 +537,10 @@ struct em28xx_i2c_bus {
536537
enum em28xx_i2c_algo_type algo_type;
537538
};
538539

539-
540540
/* main device struct */
541541
struct em28xx {
542+
struct kref ref;
543+
542544
/* generic device properties */
543545
char name[30]; /* name (including minor) of the device */
544546
int model; /* index in the device_data struct */
@@ -710,6 +712,8 @@ struct em28xx {
710712
struct em28xx_dvb *dvb;
711713
};
712714

715+
#define kref_to_dev(d) container_of(d, struct em28xx, ref)
716+
713717
struct em28xx_ops {
714718
struct list_head next;
715719
char *name;
@@ -771,7 +775,7 @@ extern struct em28xx_board em28xx_boards[];
771775
extern struct usb_device_id em28xx_id_table[];
772776
int em28xx_tuner_callback(void *ptr, int component, int command, int arg);
773777
void em28xx_setup_xc3028(struct em28xx *dev, struct xc2028_ctrl *ctl);
774-
void em28xx_release_resources(struct em28xx *dev);
778+
void em28xx_free_device(struct kref *ref);
775779

776780
/* Provided by em28xx-camera.c */
777781
int em28xx_detect_sensor(struct em28xx *dev);

0 commit comments

Comments
 (0)