[PATCH 1/3] gpu: host1x: Add locking in channel allocation

From: Mikko Perttunen
Date: Fri Sep 01 2023 - 07:15:45 EST


From: Mikko Perttunen <mperttunen@xxxxxxxxxx>

Add locking around channel allocation to avoid race conditions.

Signed-off-by: Mikko Perttunen <mperttunen@xxxxxxxxxx>
---
drivers/gpu/host1x/channel.c | 7 +++++++
drivers/gpu/host1x/channel.h | 3 +++
2 files changed, 10 insertions(+)

diff --git a/drivers/gpu/host1x/channel.c b/drivers/gpu/host1x/channel.c
index 2d0051d6314c..79501c957532 100644
--- a/drivers/gpu/host1x/channel.c
+++ b/drivers/gpu/host1x/channel.c
@@ -27,6 +27,8 @@ int host1x_channel_list_init(struct host1x_channel_list *chlist,
return -ENOMEM;
}

+ mutex_init(&chlist->lock);
+
return 0;
}

@@ -104,8 +106,11 @@ static struct host1x_channel *acquire_unused_channel(struct host1x *host)
unsigned int max_channels = host->info->nb_channels;
unsigned int index;

+ mutex_lock(&chlist->lock);
+
index = find_first_zero_bit(chlist->allocated_channels, max_channels);
if (index >= max_channels) {
+ mutex_unlock(&chlist->lock);
dev_err(host->dev, "failed to find free channel\n");
return NULL;
}
@@ -114,6 +119,8 @@ static struct host1x_channel *acquire_unused_channel(struct host1x *host)

set_bit(index, chlist->allocated_channels);

+ mutex_unlock(&chlist->lock);
+
return &chlist->channels[index];
}

diff --git a/drivers/gpu/host1x/channel.h b/drivers/gpu/host1x/channel.h
index 39044ff6c3aa..b23a8071fbd0 100644
--- a/drivers/gpu/host1x/channel.h
+++ b/drivers/gpu/host1x/channel.h
@@ -10,6 +10,7 @@

#include <linux/io.h>
#include <linux/kref.h>
+#include <linux/mutex.h>

#include "cdma.h"

@@ -18,6 +19,8 @@ struct host1x_channel;

struct host1x_channel_list {
struct host1x_channel *channels;
+
+ struct mutex lock;
unsigned long *allocated_channels;
};

--
2.41.0