Skip to content

Commit

Permalink
gpu: host1x: Add locking in channel allocation
Browse files Browse the repository at this point in the history
Add locking around channel allocation to avoid race conditions.

Signed-off-by: Mikko Perttunen <mperttunen@nvidia.com>
Signed-off-by: Thierry Reding <treding@nvidia.com>
Link: https://patchwork.freedesktop.org/patch/msgid/20230901111510.663401-1-cyndis@kapsi.fi
  • Loading branch information
Mikko Perttunen authored and Thierry Reding committed Oct 11, 2023
1 parent f170bed commit 9764723
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
7 changes: 7 additions & 0 deletions drivers/gpu/host1x/channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ int host1x_channel_list_init(struct host1x_channel_list *chlist,
return -ENOMEM;
}

mutex_init(&chlist->lock);

return 0;
}

Expand Down Expand Up @@ -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;
}
Expand All @@ -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];
}

Expand Down
3 changes: 3 additions & 0 deletions drivers/gpu/host1x/channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

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

#include "cdma.h"

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

struct host1x_channel_list {
struct host1x_channel *channels;

struct mutex lock;
unsigned long *allocated_channels;
};

Expand Down

0 comments on commit 9764723

Please sign in to comment.