Skip to content

Commit 5a081ca

Browse files
committed
rpmsg: avoid premature deallocation of endpoints
When an inbound message arrives, the rpmsg core looks up its associated endpoint and invokes the registered callback. If a message arrives while its endpoint is being removed (because the rpmsg driver was removed, or a recovery of a remote processor has kicked in) we must ensure atomicity, i.e.: - Either the ept is removed before it is found or - The ept is found but will not be freed until the callback returns This is achieved by maintaining a per-ept reference count, which, when drops to zero, will trigger deallocation of the ept. With this in hand, it is now forbidden to directly deallocate epts once they have been added to the endpoints idr. Cc: stable <stable@vger.kernel.org> Reported-by: Fernando Guzman Lugo <fernando.lugo@ti.com> Signed-off-by: Ohad Ben-Cohen <ohad@wizery.com>
1 parent 6887a41 commit 5a081ca

2 files changed

Lines changed: 37 additions & 2 deletions

File tree

‎drivers/rpmsg/virtio_rpmsg_bus.c‎

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,26 @@ static int rpmsg_uevent(struct device *dev, struct kobj_uevent_env *env)
188188
rpdev->id.name);
189189
}
190190

191+
/**
192+
* __ept_release() - deallocate an rpmsg endpoint
193+
* @kref: the ept's reference count
194+
*
195+
* This function deallocates an ept, and is invoked when its @kref refcount
196+
* drops to zero.
197+
*
198+
* Never invoke this function directly!
199+
*/
200+
static void __ept_release(struct kref *kref)
201+
{
202+
struct rpmsg_endpoint *ept = container_of(kref, struct rpmsg_endpoint,
203+
refcount);
204+
/*
205+
* At this point no one holds a reference to ept anymore,
206+
* so we can directly free it
207+
*/
208+
kfree(ept);
209+
}
210+
191211
/* for more info, see below documentation of rpmsg_create_ept() */
192212
static struct rpmsg_endpoint *__rpmsg_create_ept(struct virtproc_info *vrp,
193213
struct rpmsg_channel *rpdev, rpmsg_rx_cb_t cb,
@@ -206,6 +226,8 @@ static struct rpmsg_endpoint *__rpmsg_create_ept(struct virtproc_info *vrp,
206226
return NULL;
207227
}
208228

229+
kref_init(&ept->refcount);
230+
209231
ept->rpdev = rpdev;
210232
ept->cb = cb;
211233
ept->priv = priv;
@@ -238,7 +260,7 @@ static struct rpmsg_endpoint *__rpmsg_create_ept(struct virtproc_info *vrp,
238260
idr_remove(&vrp->endpoints, request);
239261
free_ept:
240262
mutex_unlock(&vrp->endpoints_lock);
241-
kfree(ept);
263+
kref_put(&ept->refcount, __ept_release);
242264
return NULL;
243265
}
244266

@@ -306,7 +328,7 @@ __rpmsg_destroy_ept(struct virtproc_info *vrp, struct rpmsg_endpoint *ept)
306328
idr_remove(&vrp->endpoints, ept->addr);
307329
mutex_unlock(&vrp->endpoints_lock);
308330

309-
kfree(ept);
331+
kref_put(&ept->refcount, __ept_release);
310332
}
311333

312334
/**
@@ -790,14 +812,24 @@ static void rpmsg_recv_done(struct virtqueue *rvq)
790812

791813
/* use the dst addr to fetch the callback of the appropriate user */
792814
mutex_lock(&vrp->endpoints_lock);
815+
793816
ept = idr_find(&vrp->endpoints, msg->dst);
817+
818+
/* let's make sure no one deallocates ept while we use it */
819+
if (ept)
820+
kref_get(&ept->refcount);
821+
794822
mutex_unlock(&vrp->endpoints_lock);
795823

796824
if (ept && ept->cb)
797825
ept->cb(ept->rpdev, msg->data, msg->len, ept->priv, msg->src);
798826
else
799827
dev_warn(dev, "msg received with no recepient\n");
800828

829+
/* farewell, ept, we don't need you anymore */
830+
if (ept)
831+
kref_put(&ept->refcount, __ept_release);
832+
801833
/* publish the real size of the buffer */
802834
sg_init_one(&sg, msg, RPMSG_BUF_SIZE);
803835

‎include/linux/rpmsg.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include <linux/types.h>
3939
#include <linux/device.h>
4040
#include <linux/mod_devicetable.h>
41+
#include <linux/kref.h>
4142

4243
/* The feature bitmap for virtio rpmsg */
4344
#define VIRTIO_RPMSG_F_NS 0 /* RP supports name service notifications */
@@ -120,6 +121,7 @@ typedef void (*rpmsg_rx_cb_t)(struct rpmsg_channel *, void *, int, void *, u32);
120121
/**
121122
* struct rpmsg_endpoint - binds a local rpmsg address to its user
122123
* @rpdev: rpmsg channel device
124+
* @refcount: when this drops to zero, the ept is deallocated
123125
* @cb: rx callback handler
124126
* @addr: local rpmsg address
125127
* @priv: private data for the driver's use
@@ -140,6 +142,7 @@ typedef void (*rpmsg_rx_cb_t)(struct rpmsg_channel *, void *, int, void *, u32);
140142
*/
141143
struct rpmsg_endpoint {
142144
struct rpmsg_channel *rpdev;
145+
struct kref refcount;
143146
rpmsg_rx_cb_t cb;
144147
u32 addr;
145148
void *priv;

0 commit comments

Comments
 (0)