usb: gadget: f_fs: Prevent panic due to failure of huge size buffer allocation
[platform/kernel/linux-rpi.git] / ipc / kdbus / connection.h
1 /*
2  * Copyright (C) 2013-2015 Kay Sievers
3  * Copyright (C) 2013-2015 Greg Kroah-Hartman <gregkh@linuxfoundation.org>
4  * Copyright (C) 2013-2015 Daniel Mack <daniel@zonque.org>
5  * Copyright (C) 2013-2015 David Herrmann <dh.herrmann@gmail.com>
6  * Copyright (C) 2013-2015 Linux Foundation
7  * Copyright (C) 2014-2015 Djalal Harouni
8  *
9  * kdbus is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU Lesser General Public License as published by the
11  * Free Software Foundation; either version 2.1 of the License, or (at
12  * your option) any later version.
13  */
14
15 #ifndef __KDBUS_CONNECTION_H
16 #define __KDBUS_CONNECTION_H
17
18 #include <linux/atomic.h>
19 #include <linux/kref.h>
20 #include <linux/lockdep.h>
21 #include <linux/path.h>
22
23 #include "limits.h"
24 #include "metadata.h"
25 #include "pool.h"
26 #include "queue.h"
27 #include "util.h"
28
29 #define KDBUS_HELLO_SPECIAL_CONN        (KDBUS_HELLO_ACTIVATOR | \
30                                          KDBUS_HELLO_POLICY_HOLDER | \
31                                          KDBUS_HELLO_MONITOR)
32
33 struct kdbus_name_entry;
34 struct kdbus_quota;
35 struct kdbus_staging;
36
37 /**
38  * struct kdbus_conn - connection to a bus
39  * @kref:               Reference count
40  * @active:             Active references to the connection
41  * @id:                 Connection ID
42  * @flags:              KDBUS_HELLO_* flags
43  * @attach_flags_send:  KDBUS_ATTACH_* flags for sending
44  * @attach_flags_recv:  KDBUS_ATTACH_* flags for receiving
45  * @description:        Human-readable connection description, used for
46  *                      debugging. This field is only set when the
47  *                      connection is created.
48  * @ep:                 The endpoint this connection belongs to
49  * @lock:               Connection data lock
50  * @hentry:             Entry in ID <-> connection map
51  * @ep_entry:           Entry in endpoint
52  * @monitor_entry:      Entry in monitor, if the connection is a monitor
53  * @reply_list:         List of connections this connection should
54  *                      reply to
55  * @work:               Delayed work to handle timeouts
56  *                      activator for
57  * @match_db:           Subscription filter to broadcast messages
58  * @meta_proc:          Process metadata of connection creator, or NULL
59  * @meta_fake:          Faked metadata, or NULL
60  * @pool:               The user's buffer to receive messages
61  * @user:               Owner of the connection
62  * @cred:               The credentials of the connection at creation time
63  * @pid:                Pid at creation time
64  * @root_path:          Root path at creation time
65  * @request_count:      Number of pending requests issued by this
66  *                      connection that are waiting for replies from
67  *                      other peers
68  * @lost_count:         Number of lost broadcast messages
69  * @wait:               Wake up this endpoint
70  * @queue:              The message queue associated with this connection
71  * @quota:              Array of per-user quota indexed by user->id
72  * @n_quota:            Number of elements in quota array
73  * @names_list:         List of well-known names
74  * @name_count:         Number of owned well-known names
75  * @privileged:         Whether this connection is privileged on the domain
76  * @owner:              Owned by the same user as the bus owner
77  */
78 struct kdbus_conn {
79         struct kref kref;
80         atomic_t active;
81 #ifdef CONFIG_DEBUG_LOCK_ALLOC
82         struct lockdep_map dep_map;
83 #endif
84         u64 id;
85         u64 flags;
86         atomic64_t attach_flags_send;
87         atomic64_t attach_flags_recv;
88         const char *description;
89         struct kdbus_ep *ep;
90         struct mutex lock;
91         struct hlist_node hentry;
92         struct list_head ep_entry;
93         struct list_head monitor_entry;
94         struct list_head reply_list;
95         struct delayed_work work;
96         struct kdbus_match_db *match_db;
97         struct kdbus_meta_proc *meta_proc;
98         struct kdbus_meta_fake *meta_fake;
99         struct kdbus_pool *pool;
100         struct kdbus_user *user;
101         const struct cred *cred;
102         struct pid *pid;
103         struct path root_path;
104         atomic_t request_count;
105         atomic_t lost_count;
106         wait_queue_head_t wait;
107         struct kdbus_queue queue;
108
109         struct kdbus_quota *quota;
110         unsigned int n_quota;
111
112         /* protected by registry->rwlock */
113         struct list_head names_list;
114         unsigned int name_count;
115
116         bool privileged:1;
117         bool owner:1;
118 };
119
120 struct kdbus_conn *kdbus_conn_ref(struct kdbus_conn *conn);
121 struct kdbus_conn *kdbus_conn_unref(struct kdbus_conn *conn);
122 bool kdbus_conn_active(const struct kdbus_conn *conn);
123 int kdbus_conn_acquire(struct kdbus_conn *conn);
124 void kdbus_conn_release(struct kdbus_conn *conn);
125 int kdbus_conn_disconnect(struct kdbus_conn *conn, bool ensure_queue_empty);
126 bool kdbus_conn_has_name(struct kdbus_conn *conn, const char *name);
127 int kdbus_conn_quota_inc(struct kdbus_conn *c, struct kdbus_user *u,
128                          size_t memory, size_t fds);
129 void kdbus_conn_quota_dec(struct kdbus_conn *c, struct kdbus_user *u,
130                           size_t memory, size_t fds);
131 void kdbus_conn_lost_message(struct kdbus_conn *c);
132 int kdbus_conn_entry_insert(struct kdbus_conn *conn_src,
133                             struct kdbus_conn *conn_dst,
134                             struct kdbus_staging *staging,
135                             struct kdbus_reply *reply,
136                             const struct kdbus_name_entry *name);
137 void kdbus_conn_move_messages(struct kdbus_conn *conn_dst,
138                               struct kdbus_conn *conn_src,
139                               u64 name_id);
140
141 /* policy */
142 bool kdbus_conn_policy_own_name(struct kdbus_conn *conn,
143                                 const struct cred *conn_creds,
144                                 const char *name);
145 bool kdbus_conn_policy_talk(struct kdbus_conn *conn,
146                             const struct cred *conn_creds,
147                             struct kdbus_conn *to);
148 bool kdbus_conn_policy_see_name_unlocked(struct kdbus_conn *conn,
149                                          const struct cred *curr_creds,
150                                          const char *name);
151 bool kdbus_conn_policy_see_notification(struct kdbus_conn *conn,
152                                         const struct cred *curr_creds,
153                                         const struct kdbus_msg *msg);
154
155 /* command dispatcher */
156 struct kdbus_conn *kdbus_cmd_hello(struct kdbus_ep *ep, struct file *file,
157                                    void __user *argp);
158 int kdbus_cmd_byebye_unlocked(struct kdbus_conn *conn, void __user *argp);
159 int kdbus_cmd_conn_info(struct kdbus_conn *conn, void __user *argp);
160 int kdbus_cmd_update(struct kdbus_conn *conn, void __user *argp);
161 int kdbus_cmd_send(struct kdbus_conn *conn, struct file *f, void __user *argp);
162 int kdbus_cmd_recv(struct kdbus_conn *conn, void __user *argp);
163 int kdbus_cmd_free(struct kdbus_conn *conn, void __user *argp);
164
165 /**
166  * kdbus_conn_is_ordinary() - Check if connection is ordinary
167  * @conn:               The connection to check
168  *
169  * Return: Non-zero if the connection is an ordinary connection
170  */
171 static inline int kdbus_conn_is_ordinary(const struct kdbus_conn *conn)
172 {
173         return !(conn->flags & KDBUS_HELLO_SPECIAL_CONN);
174 }
175
176 /**
177  * kdbus_conn_is_activator() - Check if connection is an activator
178  * @conn:               The connection to check
179  *
180  * Return: Non-zero if the connection is an activator
181  */
182 static inline int kdbus_conn_is_activator(const struct kdbus_conn *conn)
183 {
184         return conn->flags & KDBUS_HELLO_ACTIVATOR;
185 }
186
187 /**
188  * kdbus_conn_is_policy_holder() - Check if connection is a policy holder
189  * @conn:               The connection to check
190  *
191  * Return: Non-zero if the connection is a policy holder
192  */
193 static inline int kdbus_conn_is_policy_holder(const struct kdbus_conn *conn)
194 {
195         return conn->flags & KDBUS_HELLO_POLICY_HOLDER;
196 }
197
198 /**
199  * kdbus_conn_is_monitor() - Check if connection is a monitor
200  * @conn:               The connection to check
201  *
202  * Return: Non-zero if the connection is a monitor
203  */
204 static inline int kdbus_conn_is_monitor(const struct kdbus_conn *conn)
205 {
206         return conn->flags & KDBUS_HELLO_MONITOR;
207 }
208
209 /**
210  * kdbus_conn_lock2() - Lock two connections
211  * @a:          connection A to lock or NULL
212  * @b:          connection B to lock or NULL
213  *
214  * Lock two connections at once. As we need to have a stable locking order, we
215  * always lock the connection with lower memory address first.
216  */
217 static inline void kdbus_conn_lock2(struct kdbus_conn *a, struct kdbus_conn *b)
218 {
219         if (a < b) {
220                 if (a)
221                         mutex_lock(&a->lock);
222                 if (b && b != a)
223                         mutex_lock_nested(&b->lock, !!a);
224         } else {
225                 if (b)
226                         mutex_lock(&b->lock);
227                 if (a && a != b)
228                         mutex_lock_nested(&a->lock, !!b);
229         }
230 }
231
232 /**
233  * kdbus_conn_unlock2() - Unlock two connections
234  * @a:          connection A to unlock or NULL
235  * @b:          connection B to unlock or NULL
236  *
237  * Unlock two connections at once. See kdbus_conn_lock2().
238  */
239 static inline void kdbus_conn_unlock2(struct kdbus_conn *a,
240                                       struct kdbus_conn *b)
241 {
242         if (a)
243                 mutex_unlock(&a->lock);
244         if (b && b != a)
245                 mutex_unlock(&b->lock);
246 }
247
248 /**
249  * kdbus_conn_assert_active() - lockdep assert on active lock
250  * @conn:       connection that shall be active
251  *
252  * This verifies via lockdep that the caller holds an active reference to the
253  * given connection.
254  */
255 static inline void kdbus_conn_assert_active(struct kdbus_conn *conn)
256 {
257         lockdep_assert_held(conn);
258 }
259
260 #endif