dcookies.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. * dcookies.c
  3. *
  4. * Copyright 2002 John Levon <levon@movementarian.org>
  5. *
  6. * Persistent cookie-path mappings. These are used by
  7. * profilers to convert a per-task EIP value into something
  8. * non-transitory that can be processed at a later date.
  9. * This is done by locking the dentry/vfsmnt pair in the
  10. * kernel until released by the tasks needing the persistent
  11. * objects. The tag is simply an unsigned long that refers
  12. * to the pair and can be looked up from userspace.
  13. */
  14. #include <linux/config.h>
  15. #include <linux/syscalls.h>
  16. #include <linux/module.h>
  17. #include <linux/slab.h>
  18. #include <linux/list.h>
  19. #include <linux/mount.h>
  20. #include <linux/dcache.h>
  21. #include <linux/mm.h>
  22. #include <linux/errno.h>
  23. #include <linux/dcookies.h>
  24. #include <asm/uaccess.h>
  25. /* The dcookies are allocated from a kmem_cache and
  26. * hashed onto a small number of lists. None of the
  27. * code here is particularly performance critical
  28. */
  29. struct dcookie_struct {
  30. struct dentry * dentry;
  31. struct vfsmount * vfsmnt;
  32. struct list_head hash_list;
  33. };
  34. static LIST_HEAD(dcookie_users);
  35. static DECLARE_MUTEX(dcookie_sem);
  36. static kmem_cache_t * dcookie_cache;
  37. static struct list_head * dcookie_hashtable;
  38. static size_t hash_size;
  39. static inline int is_live(void)
  40. {
  41. return !(list_empty(&dcookie_users));
  42. }
  43. /* The dentry is locked, its address will do for the cookie */
  44. static inline unsigned long dcookie_value(struct dcookie_struct * dcs)
  45. {
  46. return (unsigned long)dcs->dentry;
  47. }
  48. static size_t dcookie_hash(unsigned long dcookie)
  49. {
  50. return (dcookie >> L1_CACHE_SHIFT) & (hash_size - 1);
  51. }
  52. static struct dcookie_struct * find_dcookie(unsigned long dcookie)
  53. {
  54. struct dcookie_struct *found = NULL;
  55. struct dcookie_struct * dcs;
  56. struct list_head * pos;
  57. struct list_head * list;
  58. list = dcookie_hashtable + dcookie_hash(dcookie);
  59. list_for_each(pos, list) {
  60. dcs = list_entry(pos, struct dcookie_struct, hash_list);
  61. if (dcookie_value(dcs) == dcookie) {
  62. found = dcs;
  63. break;
  64. }
  65. }
  66. return found;
  67. }
  68. static void hash_dcookie(struct dcookie_struct * dcs)
  69. {
  70. struct list_head * list = dcookie_hashtable + dcookie_hash(dcookie_value(dcs));
  71. list_add(&dcs->hash_list, list);
  72. }
  73. static struct dcookie_struct * alloc_dcookie(struct dentry * dentry,
  74. struct vfsmount * vfsmnt)
  75. {
  76. struct dcookie_struct * dcs = kmem_cache_alloc(dcookie_cache, GFP_KERNEL);
  77. if (!dcs)
  78. return NULL;
  79. dentry->d_cookie = dcs;
  80. dcs->dentry = dget(dentry);
  81. dcs->vfsmnt = mntget(vfsmnt);
  82. hash_dcookie(dcs);
  83. return dcs;
  84. }
  85. /* This is the main kernel-side routine that retrieves the cookie
  86. * value for a dentry/vfsmnt pair.
  87. */
  88. int get_dcookie(struct dentry * dentry, struct vfsmount * vfsmnt,
  89. unsigned long * cookie)
  90. {
  91. int err = 0;
  92. struct dcookie_struct * dcs;
  93. down(&dcookie_sem);
  94. if (!is_live()) {
  95. err = -EINVAL;
  96. goto out;
  97. }
  98. dcs = dentry->d_cookie;
  99. if (!dcs)
  100. dcs = alloc_dcookie(dentry, vfsmnt);
  101. if (!dcs) {
  102. err = -ENOMEM;
  103. goto out;
  104. }
  105. *cookie = dcookie_value(dcs);
  106. out:
  107. up(&dcookie_sem);
  108. return err;
  109. }
  110. /* And here is where the userspace process can look up the cookie value
  111. * to retrieve the path.
  112. */
  113. asmlinkage long sys_lookup_dcookie(u64 cookie64, char __user * buf, size_t len)
  114. {
  115. unsigned long cookie = (unsigned long)cookie64;
  116. int err = -EINVAL;
  117. char * kbuf;
  118. char * path;
  119. size_t pathlen;
  120. struct dcookie_struct * dcs;
  121. /* we could leak path information to users
  122. * without dir read permission without this
  123. */
  124. if (!capable(CAP_SYS_ADMIN))
  125. return -EPERM;
  126. down(&dcookie_sem);
  127. if (!is_live()) {
  128. err = -EINVAL;
  129. goto out;
  130. }
  131. if (!(dcs = find_dcookie(cookie)))
  132. goto out;
  133. err = -ENOMEM;
  134. kbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  135. if (!kbuf)
  136. goto out;
  137. /* FIXME: (deleted) ? */
  138. path = d_path(dcs->dentry, dcs->vfsmnt, kbuf, PAGE_SIZE);
  139. if (IS_ERR(path)) {
  140. err = PTR_ERR(path);
  141. goto out_free;
  142. }
  143. err = -ERANGE;
  144. pathlen = kbuf + PAGE_SIZE - path;
  145. if (pathlen <= len) {
  146. err = pathlen;
  147. if (copy_to_user(buf, path, pathlen))
  148. err = -EFAULT;
  149. }
  150. out_free:
  151. kfree(kbuf);
  152. out:
  153. up(&dcookie_sem);
  154. return err;
  155. }
  156. static int dcookie_init(void)
  157. {
  158. struct list_head * d;
  159. unsigned int i, hash_bits;
  160. int err = -ENOMEM;
  161. dcookie_cache = kmem_cache_create("dcookie_cache",
  162. sizeof(struct dcookie_struct),
  163. 0, 0, NULL, NULL);
  164. if (!dcookie_cache)
  165. goto out;
  166. dcookie_hashtable = kmalloc(PAGE_SIZE, GFP_KERNEL);
  167. if (!dcookie_hashtable)
  168. goto out_kmem;
  169. err = 0;
  170. /*
  171. * Find the power-of-two list-heads that can fit into the allocation..
  172. * We don't guarantee that "sizeof(struct list_head)" is necessarily
  173. * a power-of-two.
  174. */
  175. hash_size = PAGE_SIZE / sizeof(struct list_head);
  176. hash_bits = 0;
  177. do {
  178. hash_bits++;
  179. } while ((hash_size >> hash_bits) != 0);
  180. hash_bits--;
  181. /*
  182. * Re-calculate the actual number of entries and the mask
  183. * from the number of bits we can fit.
  184. */
  185. hash_size = 1UL << hash_bits;
  186. /* And initialize the newly allocated array */
  187. d = dcookie_hashtable;
  188. i = hash_size;
  189. do {
  190. INIT_LIST_HEAD(d);
  191. d++;
  192. i--;
  193. } while (i);
  194. out:
  195. return err;
  196. out_kmem:
  197. kmem_cache_destroy(dcookie_cache);
  198. goto out;
  199. }
  200. static void free_dcookie(struct dcookie_struct * dcs)
  201. {
  202. dcs->dentry->d_cookie = NULL;
  203. dput(dcs->dentry);
  204. mntput(dcs->vfsmnt);
  205. kmem_cache_free(dcookie_cache, dcs);
  206. }
  207. static void dcookie_exit(void)
  208. {
  209. struct list_head * list;
  210. struct list_head * pos;
  211. struct list_head * pos2;
  212. struct dcookie_struct * dcs;
  213. size_t i;
  214. for (i = 0; i < hash_size; ++i) {
  215. list = dcookie_hashtable + i;
  216. list_for_each_safe(pos, pos2, list) {
  217. dcs = list_entry(pos, struct dcookie_struct, hash_list);
  218. list_del(&dcs->hash_list);
  219. free_dcookie(dcs);
  220. }
  221. }
  222. kfree(dcookie_hashtable);
  223. kmem_cache_destroy(dcookie_cache);
  224. }
  225. struct dcookie_user {
  226. struct list_head next;
  227. };
  228. struct dcookie_user * dcookie_register(void)
  229. {
  230. struct dcookie_user * user;
  231. down(&dcookie_sem);
  232. user = kmalloc(sizeof(struct dcookie_user), GFP_KERNEL);
  233. if (!user)
  234. goto out;
  235. if (!is_live() && dcookie_init())
  236. goto out_free;
  237. list_add(&user->next, &dcookie_users);
  238. out:
  239. up(&dcookie_sem);
  240. return user;
  241. out_free:
  242. kfree(user);
  243. user = NULL;
  244. goto out;
  245. }
  246. void dcookie_unregister(struct dcookie_user * user)
  247. {
  248. down(&dcookie_sem);
  249. list_del(&user->next);
  250. kfree(user);
  251. if (!is_live())
  252. dcookie_exit();
  253. up(&dcookie_sem);
  254. }
  255. EXPORT_SYMBOL_GPL(dcookie_register);
  256. EXPORT_SYMBOL_GPL(dcookie_unregister);
  257. EXPORT_SYMBOL_GPL(get_dcookie);