diff --git a/drivers/infiniband/core/device.c b/drivers/infiniband/core/device.c
index 57e1e177921ef04833c318406b2141efc50ffc49..60083bde3e39f9446a593b39248a63905906221d 100644
--- a/drivers/infiniband/core/device.c
+++ b/drivers/infiniband/core/device.c
@@ -189,12 +189,14 @@ static struct ib_device *__ib_device_get_by_name(const char *name)
 
 int ib_device_rename(struct ib_device *ibdev, const char *name)
 {
-	int ret = 0;
-
-	if (!strcmp(name, dev_name(&ibdev->dev)))
-		return ret;
+	int ret;
 
 	mutex_lock(&device_mutex);
+	if (!strcmp(name, dev_name(&ibdev->dev))) {
+		ret = 0;
+		goto out;
+	}
+
 	if (__ib_device_get_by_name(name)) {
 		ret = -EEXIST;
 		goto out;