hc
2023-02-13 e440ec23c5a540cdd3f7464e8779219be6fd3d95
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
 * Copyright (c) 2014, Linaro Limited
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 */
#include <linux/slab.h>
#include "tee_mutex_wait.h"
 
struct tee_mutex_wait {
   struct list_head link;
   struct completion comp;
   struct mutex mu;
   u32 wait_after;
   u32 key;
};
 
/*
 * Compares two serial numbers using Serial Number Arithmetic
 * (https://www.ietf.org/rfc/rfc1982.txt).
 */
#define TICK_GT(t1, t2) \
   (((t1) < (t2) && (t2) - (t1) > 0xFFFFFFFFu) || \
   ((t1) > (t2) && (t1) - (t2) < 0xFFFFFFFFu))
 
static struct tee_mutex_wait *tee_mutex_wait_get(struct device *dev,
               struct tee_mutex_wait_private *priv, u32 key)
{
   struct tee_mutex_wait *w;
 
   mutex_lock(&priv->mu);
 
   list_for_each_entry(w, &priv->db, link)
       if (w->key == key)
           goto out;
 
   w = kmalloc(sizeof(struct tee_mutex_wait), GFP_KERNEL);
   if (!w) {
       dev_err(dev, "kmalloc <struct tee_mutex_wait> failed\n");
       goto out;
   }
 
   init_completion(&w->comp);
   mutex_init(&w->mu);
   w->wait_after = 0;
   w->key = key;
   list_add_tail(&w->link, &priv->db);
out:
   mutex_unlock(&priv->mu);
   return w;
}
 
static void tee_mutex_wait_delete_entry(struct tee_mutex_wait *w)
{
   list_del(&w->link);
   mutex_destroy(&w->mu);
   kfree(w);
}
 
void tee_mutex_wait_delete(struct device *dev,
           struct tee_mutex_wait_private *priv,
           u32 key)
{
   struct tee_mutex_wait *w;
 
   mutex_lock(&priv->mu);
 
   list_for_each_entry(w, &priv->db, link) {
       if (w->key == key) {
           tee_mutex_wait_delete_entry(w);
           break;
       }
   }
 
   mutex_unlock(&priv->mu);
}
EXPORT_SYMBOL(tee_mutex_wait_delete);
 
void tee_mutex_wait_wakeup(struct device *dev,
           struct tee_mutex_wait_private *priv,
           u32 key, u32 wait_after)
{
   struct tee_mutex_wait *w = tee_mutex_wait_get(dev, priv, key);
 
   if (!w)
       return;
 
   mutex_lock(&w->mu);
   w->wait_after = wait_after;
   mutex_unlock(&w->mu);
   complete(&w->comp);
}
EXPORT_SYMBOL(tee_mutex_wait_wakeup);
 
void tee_mutex_wait_sleep(struct device *dev,
           struct tee_mutex_wait_private *priv,
           u32 key, u32 wait_tick)
{
   struct tee_mutex_wait *w = tee_mutex_wait_get(dev, priv, key);
   u32 wait_after;
 
   if (!w)
       return;
 
   mutex_lock(&w->mu);
   wait_after = w->wait_after;
   mutex_unlock(&w->mu);
 
   if (TICK_GT(wait_tick, wait_after))
       wait_for_completion_timeout(&w->comp, HZ);
}
EXPORT_SYMBOL(tee_mutex_wait_sleep);
 
int tee_mutex_wait_init(struct tee_mutex_wait_private *priv)
{
   mutex_init(&priv->mu);
   INIT_LIST_HEAD(&priv->db);
   return 0;
}
EXPORT_SYMBOL(tee_mutex_wait_init);
 
void tee_mutex_wait_exit(struct tee_mutex_wait_private *priv)
{
   /*
    * It's the callers responibility to ensure that no one is using
    * anything inside priv.
    */
 
   mutex_destroy(&priv->mu);
   while (!list_empty(&priv->db)) {
       struct tee_mutex_wait *w =
               list_first_entry(&priv->db,
                        struct tee_mutex_wait,
                        link);
       tee_mutex_wait_delete_entry(w);
   }
}
EXPORT_SYMBOL(tee_mutex_wait_exit);