firmware loader: Fix the concurrent request_firmware() race for kref_get/put
authorChuansheng Liu <chuansheng.liu@intel.com>
Fri, 9 Nov 2012 17:27:22 +0000 (01:27 +0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 14 Nov 2012 23:04:23 +0000 (15:04 -0800)
There is one race that both request_firmware() with the same
firmware name.

The race scenerio is as below:
CPU1                                                  CPU2
request_firmware() -->
_request_firmware_load() return err                   another request_firmware() is coming -->
_request_firmware_cleanup is called -->               _request_firmware_prepare -->
release_firmware --->                                 fw_lookup_and_allocate_buf -->
                                                      spin_lock(&fwc->lock)
...                                                   __fw_lookup_buf() return true
fw_free_buf() will be called -->                      ...
kref_put -->
decrease the refcount to 0
                                                      kref_get(&tmp->ref) ==> it will trigger warning
                                                                              due to refcount == 0
__fw_free_buf() -->
...                                                   spin_unlock(&fwc->lock)
spin_lock(&fwc->lock)
list_del(&buf->list)
spin_unlock(&fwc->lock)
kfree(buf)
                                                      After that, the freed buf will be used.

The key race is decreasing refcount to 0 and list_del is not protected together by
fwc->lock, and it is possible another thread try to get it between refcount==0
and list_del.

Fix it here to protect it together.

Acked-by: Ming Lei <ming.lei@canonical.com>
Signed-off-by: liu chuansheng <chuansheng.liu@intel.com>
Cc: stable <stable@vger.kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/base/firmware_class.c

index b44ed35ac38d1cba7f58c8be3e10f4ad5f757087..be5f7aae75fc66e386baa292e27ed07812f9529f 100644 (file)
@@ -246,7 +246,6 @@ static void __fw_free_buf(struct kref *ref)
                 __func__, buf->fw_id, buf, buf->data,
                 (unsigned int)buf->size);
 
-       spin_lock(&fwc->lock);
        list_del(&buf->list);
        spin_unlock(&fwc->lock);
 
@@ -263,7 +262,10 @@ static void __fw_free_buf(struct kref *ref)
 
 static void fw_free_buf(struct firmware_buf *buf)
 {
-       kref_put(&buf->ref, __fw_free_buf);
+       struct firmware_cache *fwc = buf->fwc;
+       spin_lock(&fwc->lock);
+       if (!kref_put(&buf->ref, __fw_free_buf))
+               spin_unlock(&fwc->lock);
 }
 
 /* direct firmware loading support */