summaryrefslogtreecommitdiff
path: root/gxp-eventfd.c
blob: ed0170fa899a9411048b9d0522a25f23d6ad1c1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// SPDX-License-Identifier: GPL-2.0
/*
 * GXP eventfd
 *
 * Copyright (C) 202d Google LLC
 */

#include <linux/eventfd.h>
#include <linux/refcount.h>
#include <linux/slab.h>

#include "gxp-eventfd.h"

struct gxp_eventfd {
	struct eventfd_ctx *ctx;
	refcount_t refcount;
};

struct gxp_eventfd *gxp_eventfd_create(int fd)
{
	struct gxp_eventfd *efd;
	int err;

	efd = kmalloc(sizeof(*efd), GFP_KERNEL);
	if (!efd)
		return ERR_PTR(-ENOMEM);

	efd->ctx = eventfd_ctx_fdget(fd);
	if (IS_ERR(efd->ctx)) {
		err = PTR_ERR(efd->ctx);
		goto error;
	}

	refcount_set(&efd->refcount, 1);

	return efd;

error:
	kfree(efd);
	return ERR_PTR(err);
}

bool gxp_eventfd_get(struct gxp_eventfd *eventfd)
{
	return refcount_inc_not_zero(&eventfd->refcount);
}

bool gxp_eventfd_put(struct gxp_eventfd *eventfd)
{
	bool refcount_is_zero;

	refcount_is_zero = refcount_dec_and_test(&eventfd->refcount);
	if (refcount_is_zero) {
		eventfd_ctx_put(eventfd->ctx);
		kfree(eventfd);
	}

	return refcount_is_zero;
}

bool gxp_eventfd_signal(struct gxp_eventfd *eventfd)
{
	bool ret;

	ret = gxp_eventfd_get(eventfd);
	if (ret)
		eventfd_signal(eventfd->ctx, 1);

	gxp_eventfd_put(eventfd);

	return ret;
}