kdbus: Use sizeof_field() macro
[platform/kernel/linux-rpi.git] / ipc / kdbus / util.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 <tixxdz@opendz.org>
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_UTIL_H
16 #define __KDBUS_UTIL_H
17
18 #include <linux/dcache.h>
19 #include <linux/ioctl.h>
20 #include <linux/stddef.h>
21
22 #include <uapi/linux/kdbus.h>
23
24 /* all exported addresses are 64 bit */
25 #define KDBUS_PTR(addr) ((void __user *)(uintptr_t)(addr))
26
27 /* all exported sizes are 64 bit and data aligned to 64 bit */
28 #define KDBUS_ALIGN8(s) ALIGN((s), 8)
29 #define KDBUS_IS_ALIGNED8(s) (IS_ALIGNED(s, 8))
30
31 /**
32  * kdbus_member_set_user - write a structure member to user memory
33  * @_s:         Variable to copy from
34  * @_b:         Buffer to write to
35  * @_t:         Structure type
36  * @_m:         Member name in the passed structure
37  *
38  * Return: the result of copy_to_user()
39  */
40 #define kdbus_member_set_user(_s, _b, _t, _m)                           \
41 ({                                                                      \
42         u64 __user *_sz =                                               \
43                 (void __user *)((u8 __user *)(_b) + offsetof(_t, _m));  \
44         copy_to_user(_sz, _s, sizeof_field(_t, _m));                    \
45 })
46
47 /**
48  * kdbus_strhash - calculate a hash
49  * @str:        String
50  *
51  * Return: hash value
52  */
53 static inline unsigned int kdbus_strhash(const char *str)
54 {
55         unsigned long hash = init_name_hash(0);
56
57         while (*str)
58                 hash = partial_name_hash(*str++, hash);
59
60         return end_name_hash(hash);
61 }
62
63 int kdbus_verify_uid_prefix(const char *name, struct user_namespace *user_ns,
64                             kuid_t kuid);
65 int kdbus_sanitize_attach_flags(u64 flags, u64 *attach_flags);
66
67 int kdbus_copy_from_user(void *dest, void __user *user_ptr, size_t size);
68
69 struct kvec;
70
71 void kdbus_kvec_set(struct kvec *kvec, void *src, size_t len, u64 *total_len);
72 size_t kdbus_kvec_pad(struct kvec *kvec, u64 *len);
73
74 #endif