Skip to content

Commit

Permalink
winesync: Introduce WINESYNC_IOC_CREATE_EVENT
Browse files Browse the repository at this point in the history
  • Loading branch information
Zebediah Figura authored and xanmod committed May 30, 2022
1 parent 9484b5b commit 18569c1
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
65 changes: 65 additions & 0 deletions drivers/misc/winesync.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
enum winesync_type {
WINESYNC_TYPE_SEM,
WINESYNC_TYPE_MUTEX,
WINESYNC_TYPE_EVENT,
};

struct winesync_obj {
Expand Down Expand Up @@ -66,6 +67,10 @@ struct winesync_obj {
__u32 owner;
bool ownerdead;
} mutex;
struct {
bool manual;
bool signaled;
} event;
} u;
};

Expand Down Expand Up @@ -199,6 +204,8 @@ static bool is_signaled(struct winesync_obj *obj, __u32 owner)
if (obj->u.mutex.owner && obj->u.mutex.owner != owner)
return false;
return obj->u.mutex.count < UINT_MAX;
case WINESYNC_TYPE_EVENT:
return obj->u.event.signaled;
}

WARN(1, "bad object type %#x\n", obj->type);
Expand Down Expand Up @@ -248,6 +255,10 @@ static void try_wake_all(struct winesync_device *dev, struct winesync_q *q,
obj->u.mutex.count++;
obj->u.mutex.owner = q->owner;
break;
case WINESYNC_TYPE_EVENT:
if (!obj->u.event.manual)
obj->u.event.signaled = false;
break;
}
}
wake_up_process(q->task);
Expand Down Expand Up @@ -315,6 +326,26 @@ static void try_wake_any_mutex(struct winesync_obj *mutex)
}
}

static void try_wake_any_event(struct winesync_obj *event)
{
struct winesync_q_entry *entry;

lockdep_assert_held(&event->lock);

list_for_each_entry(entry, &event->any_waiters, node) {
struct winesync_q *q = entry->q;

if (!event->u.event.signaled)
break;

if (atomic_cmpxchg(&q->signaled, -1, entry->index) == -1) {
if (!event->u.event.manual)
event->u.event.signaled = false;
wake_up_process(q->task);
}
}
}

static int winesync_create_sem(struct winesync_device *dev, void __user *argp)
{
struct winesync_sem_args __user *user_args = argp;
Expand Down Expand Up @@ -379,6 +410,35 @@ static int winesync_create_mutex(struct winesync_device *dev, void __user *argp)
return put_user(id, &user_args->mutex);
}

static int winesync_create_event(struct winesync_device *dev, void __user *argp)
{
struct winesync_event_args __user *user_args = argp;
struct winesync_event_args args;
struct winesync_obj *event;
__u32 id;
int ret;

if (copy_from_user(&args, argp, sizeof(args)))
return -EFAULT;

event = kzalloc(sizeof(*event), GFP_KERNEL);
if (!event)
return -ENOMEM;

init_obj(event);
event->type = WINESYNC_TYPE_EVENT;
event->u.event.manual = args.manual;
event->u.event.signaled = args.signaled;

ret = xa_alloc(&dev->objects, &id, event, xa_limit_32b, GFP_KERNEL);
if (ret < 0) {
kfree(event);
return ret;
}

return put_user(id, &user_args->event);
}

static int winesync_delete(struct winesync_device *dev, void __user *argp)
{
struct winesync_obj *obj;
Expand Down Expand Up @@ -760,6 +820,9 @@ static void try_wake_any_obj(struct winesync_obj *obj)
case WINESYNC_TYPE_MUTEX:
try_wake_any_mutex(obj);
break;
case WINESYNC_TYPE_EVENT:
try_wake_any_event(obj);
break;
}
}

Expand Down Expand Up @@ -925,6 +988,8 @@ static long winesync_char_ioctl(struct file *file, unsigned int cmd,
void __user *argp = (void __user *)parm;

switch (cmd) {
case WINESYNC_IOC_CREATE_EVENT:
return winesync_create_event(dev, argp);
case WINESYNC_IOC_CREATE_MUTEX:
return winesync_create_mutex(dev, argp);
case WINESYNC_IOC_CREATE_SEM:
Expand Down
8 changes: 8 additions & 0 deletions include/uapi/linux/winesync.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ struct winesync_mutex_args {
__u32 count;
};

struct winesync_event_args {
__u32 event;
__u32 manual;
__u32 signaled;
};

struct winesync_wait_args {
__u64 timeout;
__u64 objs;
Expand Down Expand Up @@ -51,5 +57,7 @@ struct winesync_wait_args {
struct winesync_sem_args)
#define WINESYNC_IOC_READ_MUTEX _IOWR(WINESYNC_IOC_BASE, 9, \
struct winesync_mutex_args)
#define WINESYNC_IOC_CREATE_EVENT _IOWR(WINESYNC_IOC_BASE, 10, \
struct winesync_event_args)

#endif

0 comments on commit 18569c1

Please sign in to comment.