Skip to content

Commit ead6660

Browse files
committed
media: dvb_frontend: only use kref after initialized
As reported by Laurent, when a DVB frontend need to register two drivers (e. g. a tuner and a demod), if the second driver fails to register (for example because it was not compiled), the error handling logic frees the frontend by calling dvb_frontend_detach(). That used to work fine, but changeset 1f862a6 ("[media] dvb_frontend: move kref to struct dvb_frontend") added a kref at struct dvb_frontend. So, now, instead of just freeing the data, the error handling do a kref_put(). That works fine only after dvb_register_frontend() succeeds. While it would be possible to add a helper function that would be initializing earlier the kref, that would require changing every single DVB frontend on non-trivial ways, and would make frontends different than other drivers. So, instead of doing that, let's focus on the real issue: only call kref_put() after kref_init(). That's easy to check, as, when the dvb frontend is successfuly registered, it will allocate its own private struct. So, if such struct is allocated, it means that it is safe to use kref_put(). If not, then nobody is using yet the frontend, and it is safe to just deallocate it. Fixes: 1f862a6 ("[media] dvb_frontend: move kref to struct dvb_frontend") Reported-by: Laurent Pinchart <[email protected]> Signed-off-by: Mauro Carvalho Chehab <[email protected]>
1 parent db6321a commit ead6660

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

drivers/media/dvb-core/dvb_frontend.c

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,22 +141,39 @@ struct dvb_frontend_private {
141141
static void dvb_frontend_invoke_release(struct dvb_frontend *fe,
142142
void (*release)(struct dvb_frontend *fe));
143143

144-
static void dvb_frontend_free(struct kref *ref)
144+
static void __dvb_frontend_free(struct dvb_frontend *fe)
145145
{
146-
struct dvb_frontend *fe =
147-
container_of(ref, struct dvb_frontend, refcount);
148146
struct dvb_frontend_private *fepriv = fe->frontend_priv;
149147

148+
if (!fepriv)
149+
return;
150+
150151
dvb_free_device(fepriv->dvbdev);
151152

152153
dvb_frontend_invoke_release(fe, fe->ops.release);
153154

154155
kfree(fepriv);
156+
fe->frontend_priv = NULL;
157+
}
158+
159+
static void dvb_frontend_free(struct kref *ref)
160+
{
161+
struct dvb_frontend *fe =
162+
container_of(ref, struct dvb_frontend, refcount);
163+
164+
__dvb_frontend_free(fe);
155165
}
156166

157167
static void dvb_frontend_put(struct dvb_frontend *fe)
158168
{
159-
kref_put(&fe->refcount, dvb_frontend_free);
169+
/*
170+
* Check if the frontend was registered, as otherwise
171+
* kref was not initialized yet.
172+
*/
173+
if (fe->frontend_priv)
174+
kref_put(&fe->refcount, dvb_frontend_free);
175+
else
176+
__dvb_frontend_free(fe);
160177
}
161178

162179
static void dvb_frontend_get(struct dvb_frontend *fe)

0 commit comments

Comments
 (0)