Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions kernel/locking/semaphore.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <linux/semaphore.h>
#include <linux/spinlock.h>
#include <linux/ftrace.h>
#include <linux/slab.h> // Required for kmalloc() and kfree()

static noinline void __down(struct semaphore *sem);
static noinline int __down_interruptible(struct semaphore *sem);
Expand Down Expand Up @@ -205,11 +206,14 @@ static inline int __sched __down_common(struct semaphore *sem, long state,
long timeout)
{
struct task_struct *task = current;
struct semaphore_waiter waiter;
struct semaphore_waiter *waiter;
waiter = kmalloc(sizeof(*waiter), GFP_KERNEL);
if (!waiter)
return -ENOMEM; // Handle allocation failure

list_add_tail(&waiter.list, &sem->wait_list);
waiter.task = task;
waiter.up = false;
list_add_tail(&waiter->list, &sem->wait_list);
waiter->task = task;
waiter->up = false;

for (;;) {
if (signal_pending_state(state, task))
Expand All @@ -220,16 +224,21 @@ static inline int __sched __down_common(struct semaphore *sem, long state,
raw_spin_unlock_irq(&sem->lock);
timeout = schedule_timeout(timeout);
raw_spin_lock_irq(&sem->lock);
if (waiter.up)
if (waiter->up)
{
kfree(waiter);
return 0;
}
}

timed_out:
list_del(&waiter.list);
list_del(&waiter->list);
kfree(waiter);
return -ETIME;

interrupted:
list_del(&waiter.list);
list_del(&waiter->list);
kfree(waiter);
return -EINTR;
}

Expand Down