staging: bcm2835-camera: Replace spinlock protecting context_map with mutex [Linux 5.2.1]

staging: bcm2835-camera: Replace spinlock protecting context_map with mutex [Linux 5.2.1]

This Linux kernel change "staging: bcm2835-camera: Replace spinlock protecting context_map with mutex" is included in the Linux 5.2.1 release. This change is authored by Dave Stevenson <dave.stevenson [at] raspberrypi.org> on Sat Jun 29 14:13:17 2019 +0200. The commit for this change in Linux stable tree is fd84f7f (patch) which is from upstream commit 8dedab2. The same Linux upstream change may have been applied to various maintained Linux releases and you can find all Linux releases containing changes from upstream 8dedab2.

staging: bcm2835-camera: Replace spinlock protecting context_map with mutex

commit 8dedab2903f152aa3cee9ae3d57c828dea0d356e upstream.

The commit "staging: bcm2835-camera: Replace open-coded idr with a struct idr."
replaced an internal implementation of an idr with the standard functions
and a spinlock. idr_alloc(GFP_KERNEL) can sleep whilst calling kmem_cache_alloc
to allocate the new node, but this is not valid whilst in an atomic context
due to the spinlock.

There is no need for this to be a spinlock as a standard mutex is
sufficient.

Fixes: 950fd867c635 ("staging: bcm2835-camera: Replace open-coded idr with a struct idr.")
Signed-off-by: Dave Stevenson <[email protected]>
Signed-off-by: Stefan Wahren <[email protected]>
Acked-by: Hans Verkuil <[email protected]>
Acked-by: Mauro Carvalho Chehab <[email protected]>
Signed-off-by: Greg Kroah-Hartman <[email protected]>

There are 13 lines of Linux source code added/deleted in this change. Code changes to Linux kernel are as follows.

 drivers/staging/vc04_services/bcm2835-camera/mmal-vchiq.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/drivers/staging/vc04_services/bcm2835-camera/mmal-vchiq.c b/drivers/staging/vc04_services/bcm2835-camera/mmal-vchiq.c
index 16af735..f1bb900 100644
--- a/drivers/staging/vc04_services/bcm2835-camera/mmal-vchiq.c
+++ b/drivers/staging/vc04_services/bcm2835-camera/mmal-vchiq.c
@@ -161,7 +161,8 @@ struct vchiq_mmal_instance {
    void *bulk_scratch;

    struct idr context_map;
-   spinlock_t context_map_lock;
+   /* protect accesses to context_map */
+   struct mutex context_map_lock;

    /* component to use next */
    int component_idx;
@@ -184,10 +185,10 @@ struct vchiq_mmal_instance {
     * that when we service the VCHI reply, we can look up what
     * message is being replied to.
     */
-   spin_lock(&instance->context_map_lock);
+   mutex_lock(&instance->context_map_lock);
    handle = idr_alloc(&instance->context_map, msg_context,
               0, 0, GFP_KERNEL);
-   spin_unlock(&instance->context_map_lock);
+   mutex_unlock(&instance->context_map_lock);

    if (handle < 0) {
        kfree(msg_context);
@@ -211,9 +212,9 @@ struct vchiq_mmal_instance {
 {
    struct vchiq_mmal_instance *instance = msg_context->instance;

-   spin_lock(&instance->context_map_lock);
+   mutex_lock(&instance->context_map_lock);
    idr_remove(&instance->context_map, msg_context->handle);
-   spin_unlock(&instance->context_map_lock);
+   mutex_unlock(&instance->context_map_lock);
    kfree(msg_context);
 }

@@ -1849,7 +1850,7 @@ int vchiq_mmal_init(struct vchiq_mmal_instance **out_instance)

    instance->bulk_scratch = vmalloc(PAGE_SIZE);

-   spin_lock_init(&instance->context_map_lock);
+   mutex_init(&instance->context_map_lock);
    idr_init_base(&instance->context_map, 1);

    params.callback_param = instance;

Leave a Reply

Your email address will not be published. Required fields are marked *