spu_task_sync.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /*
  2. * Cell Broadband Engine OProfile Support
  3. *
  4. * (C) Copyright IBM Corporation 2006
  5. *
  6. * Author: Maynard Johnson <maynardj@us.ibm.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the License, or (at your option) any later version.
  12. */
  13. /* The purpose of this file is to handle SPU event task switching
  14. * and to record SPU context information into the OProfile
  15. * event buffer.
  16. *
  17. * Additionally, the spu_sync_buffer function is provided as a helper
  18. * for recoding actual SPU program counter samples to the event buffer.
  19. */
  20. #include <linux/dcookies.h>
  21. #include <linux/kref.h>
  22. #include <linux/mm.h>
  23. #include <linux/fs.h>
  24. #include <linux/module.h>
  25. #include <linux/notifier.h>
  26. #include <linux/numa.h>
  27. #include <linux/oprofile.h>
  28. #include <linux/spinlock.h>
  29. #include "pr_util.h"
  30. #define RELEASE_ALL 9999
  31. static DEFINE_SPINLOCK(buffer_lock);
  32. static DEFINE_SPINLOCK(cache_lock);
  33. static int num_spu_nodes;
  34. int spu_prof_num_nodes;
  35. int last_guard_val[MAX_NUMNODES * 8];
  36. /* Container for caching information about an active SPU task. */
  37. struct cached_info {
  38. struct vma_to_fileoffset_map *map;
  39. struct spu *the_spu; /* needed to access pointer to local_store */
  40. struct kref cache_ref;
  41. };
  42. static struct cached_info *spu_info[MAX_NUMNODES * 8];
  43. static void destroy_cached_info(struct kref *kref)
  44. {
  45. struct cached_info *info;
  46. info = container_of(kref, struct cached_info, cache_ref);
  47. vma_map_free(info->map);
  48. kfree(info);
  49. module_put(THIS_MODULE);
  50. }
  51. /* Return the cached_info for the passed SPU number.
  52. * ATTENTION: Callers are responsible for obtaining the
  53. * cache_lock if needed prior to invoking this function.
  54. */
  55. static struct cached_info *get_cached_info(struct spu *the_spu, int spu_num)
  56. {
  57. struct kref *ref;
  58. struct cached_info *ret_info;
  59. if (spu_num >= num_spu_nodes) {
  60. printk(KERN_ERR "SPU_PROF: "
  61. "%s, line %d: Invalid index %d into spu info cache\n",
  62. __func__, __LINE__, spu_num);
  63. ret_info = NULL;
  64. goto out;
  65. }
  66. if (!spu_info[spu_num] && the_spu) {
  67. ref = spu_get_profile_private_kref(the_spu->ctx);
  68. if (ref) {
  69. spu_info[spu_num] = container_of(ref, struct cached_info, cache_ref);
  70. kref_get(&spu_info[spu_num]->cache_ref);
  71. }
  72. }
  73. ret_info = spu_info[spu_num];
  74. out:
  75. return ret_info;
  76. }
  77. /* Looks for cached info for the passed spu. If not found, the
  78. * cached info is created for the passed spu.
  79. * Returns 0 for success; otherwise, -1 for error.
  80. */
  81. static int
  82. prepare_cached_spu_info(struct spu *spu, unsigned long objectId)
  83. {
  84. unsigned long flags;
  85. struct vma_to_fileoffset_map *new_map;
  86. int retval = 0;
  87. struct cached_info *info;
  88. /* We won't bother getting cache_lock here since
  89. * don't do anything with the cached_info that's returned.
  90. */
  91. info = get_cached_info(spu, spu->number);
  92. if (info) {
  93. pr_debug("Found cached SPU info.\n");
  94. goto out;
  95. }
  96. /* Create cached_info and set spu_info[spu->number] to point to it.
  97. * spu->number is a system-wide value, not a per-node value.
  98. */
  99. info = kzalloc(sizeof(struct cached_info), GFP_KERNEL);
  100. if (!info) {
  101. printk(KERN_ERR "SPU_PROF: "
  102. "%s, line %d: create vma_map failed\n",
  103. __func__, __LINE__);
  104. retval = -ENOMEM;
  105. goto err_alloc;
  106. }
  107. new_map = create_vma_map(spu, objectId);
  108. if (!new_map) {
  109. printk(KERN_ERR "SPU_PROF: "
  110. "%s, line %d: create vma_map failed\n",
  111. __func__, __LINE__);
  112. retval = -ENOMEM;
  113. goto err_alloc;
  114. }
  115. pr_debug("Created vma_map\n");
  116. info->map = new_map;
  117. info->the_spu = spu;
  118. kref_init(&info->cache_ref);
  119. spin_lock_irqsave(&cache_lock, flags);
  120. spu_info[spu->number] = info;
  121. /* Increment count before passing off ref to SPUFS. */
  122. kref_get(&info->cache_ref);
  123. /* We increment the module refcount here since SPUFS is
  124. * responsible for the final destruction of the cached_info,
  125. * and it must be able to access the destroy_cached_info()
  126. * function defined in the OProfile module. We decrement
  127. * the module refcount in destroy_cached_info.
  128. */
  129. try_module_get(THIS_MODULE);
  130. spu_set_profile_private_kref(spu->ctx, &info->cache_ref,
  131. destroy_cached_info);
  132. spin_unlock_irqrestore(&cache_lock, flags);
  133. goto out;
  134. err_alloc:
  135. kfree(info);
  136. out:
  137. return retval;
  138. }
  139. /*
  140. * NOTE: The caller is responsible for locking the
  141. * cache_lock prior to calling this function.
  142. */
  143. static int release_cached_info(int spu_index)
  144. {
  145. int index, end;
  146. if (spu_index == RELEASE_ALL) {
  147. end = num_spu_nodes;
  148. index = 0;
  149. } else {
  150. if (spu_index >= num_spu_nodes) {
  151. printk(KERN_ERR "SPU_PROF: "
  152. "%s, line %d: "
  153. "Invalid index %d into spu info cache\n",
  154. __func__, __LINE__, spu_index);
  155. goto out;
  156. }
  157. end = spu_index + 1;
  158. index = spu_index;
  159. }
  160. for (; index < end; index++) {
  161. if (spu_info[index]) {
  162. kref_put(&spu_info[index]->cache_ref,
  163. destroy_cached_info);
  164. spu_info[index] = NULL;
  165. }
  166. }
  167. out:
  168. return 0;
  169. }
  170. /* The source code for fast_get_dcookie was "borrowed"
  171. * from drivers/oprofile/buffer_sync.c.
  172. */
  173. /* Optimisation. We can manage without taking the dcookie sem
  174. * because we cannot reach this code without at least one
  175. * dcookie user still being registered (namely, the reader
  176. * of the event buffer).
  177. */
  178. static inline unsigned long fast_get_dcookie(struct path *path)
  179. {
  180. unsigned long cookie;
  181. if (path->dentry->d_cookie)
  182. return (unsigned long)path->dentry;
  183. get_dcookie(path, &cookie);
  184. return cookie;
  185. }
  186. /* Look up the dcookie for the task's first VM_EXECUTABLE mapping,
  187. * which corresponds loosely to "application name". Also, determine
  188. * the offset for the SPU ELF object. If computed offset is
  189. * non-zero, it implies an embedded SPU object; otherwise, it's a
  190. * separate SPU binary, in which case we retrieve it's dcookie.
  191. * For the embedded case, we must determine if SPU ELF is embedded
  192. * in the executable application or another file (i.e., shared lib).
  193. * If embedded in a shared lib, we must get the dcookie and return
  194. * that to the caller.
  195. */
  196. static unsigned long
  197. get_exec_dcookie_and_offset(struct spu *spu, unsigned int *offsetp,
  198. unsigned long *spu_bin_dcookie,
  199. unsigned long spu_ref)
  200. {
  201. unsigned long app_cookie = 0;
  202. unsigned int my_offset = 0;
  203. struct file *app = NULL;
  204. struct vm_area_struct *vma;
  205. struct mm_struct *mm = spu->mm;
  206. if (!mm)
  207. goto out;
  208. down_read(&mm->mmap_sem);
  209. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  210. if (!vma->vm_file)
  211. continue;
  212. if (!(vma->vm_flags & VM_EXECUTABLE))
  213. continue;
  214. app_cookie = fast_get_dcookie(&vma->vm_file->f_path);
  215. pr_debug("got dcookie for %s\n",
  216. vma->vm_file->f_dentry->d_name.name);
  217. app = vma->vm_file;
  218. break;
  219. }
  220. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  221. if (vma->vm_start > spu_ref || vma->vm_end <= spu_ref)
  222. continue;
  223. my_offset = spu_ref - vma->vm_start;
  224. if (!vma->vm_file)
  225. goto fail_no_image_cookie;
  226. pr_debug("Found spu ELF at %X(object-id:%lx) for file %s\n",
  227. my_offset, spu_ref,
  228. vma->vm_file->f_dentry->d_name.name);
  229. *offsetp = my_offset;
  230. break;
  231. }
  232. *spu_bin_dcookie = fast_get_dcookie(&vma->vm_file->f_path);
  233. pr_debug("got dcookie for %s\n", vma->vm_file->f_dentry->d_name.name);
  234. up_read(&mm->mmap_sem);
  235. out:
  236. return app_cookie;
  237. fail_no_image_cookie:
  238. up_read(&mm->mmap_sem);
  239. printk(KERN_ERR "SPU_PROF: "
  240. "%s, line %d: Cannot find dcookie for SPU binary\n",
  241. __func__, __LINE__);
  242. goto out;
  243. }
  244. /* This function finds or creates cached context information for the
  245. * passed SPU and records SPU context information into the OProfile
  246. * event buffer.
  247. */
  248. static int process_context_switch(struct spu *spu, unsigned long objectId)
  249. {
  250. unsigned long flags;
  251. int retval;
  252. unsigned int offset = 0;
  253. unsigned long spu_cookie = 0, app_dcookie;
  254. retval = prepare_cached_spu_info(spu, objectId);
  255. if (retval)
  256. goto out;
  257. /* Get dcookie first because a mutex_lock is taken in that
  258. * code path, so interrupts must not be disabled.
  259. */
  260. app_dcookie = get_exec_dcookie_and_offset(spu, &offset, &spu_cookie, objectId);
  261. if (!app_dcookie || !spu_cookie) {
  262. retval = -ENOENT;
  263. goto out;
  264. }
  265. /* Record context info in event buffer */
  266. spin_lock_irqsave(&buffer_lock, flags);
  267. add_event_entry(ESCAPE_CODE);
  268. add_event_entry(SPU_CTX_SWITCH_CODE);
  269. add_event_entry(spu->number);
  270. add_event_entry(spu->pid);
  271. add_event_entry(spu->tgid);
  272. add_event_entry(app_dcookie);
  273. add_event_entry(spu_cookie);
  274. add_event_entry(offset);
  275. spin_unlock_irqrestore(&buffer_lock, flags);
  276. smp_wmb(); /* insure spu event buffer updates are written */
  277. /* don't want entries intermingled... */
  278. out:
  279. return retval;
  280. }
  281. /*
  282. * This function is invoked on either a bind_context or unbind_context.
  283. * If called for an unbind_context, the val arg is 0; otherwise,
  284. * it is the object-id value for the spu context.
  285. * The data arg is of type 'struct spu *'.
  286. */
  287. static int spu_active_notify(struct notifier_block *self, unsigned long val,
  288. void *data)
  289. {
  290. int retval;
  291. unsigned long flags;
  292. struct spu *the_spu = data;
  293. pr_debug("SPU event notification arrived\n");
  294. if (!val) {
  295. spin_lock_irqsave(&cache_lock, flags);
  296. retval = release_cached_info(the_spu->number);
  297. spin_unlock_irqrestore(&cache_lock, flags);
  298. } else {
  299. retval = process_context_switch(the_spu, val);
  300. }
  301. return retval;
  302. }
  303. static struct notifier_block spu_active = {
  304. .notifier_call = spu_active_notify,
  305. };
  306. static int number_of_online_nodes(void)
  307. {
  308. u32 cpu; u32 tmp;
  309. int nodes = 0;
  310. for_each_online_cpu(cpu) {
  311. tmp = cbe_cpu_to_node(cpu) + 1;
  312. if (tmp > nodes)
  313. nodes++;
  314. }
  315. return nodes;
  316. }
  317. /* The main purpose of this function is to synchronize
  318. * OProfile with SPUFS by registering to be notified of
  319. * SPU task switches.
  320. *
  321. * NOTE: When profiling SPUs, we must ensure that only
  322. * spu_sync_start is invoked and not the generic sync_start
  323. * in drivers/oprofile/oprof.c. A return value of
  324. * SKIP_GENERIC_SYNC or SYNC_START_ERROR will
  325. * accomplish this.
  326. */
  327. int spu_sync_start(void)
  328. {
  329. int k;
  330. int ret = SKIP_GENERIC_SYNC;
  331. int register_ret;
  332. unsigned long flags = 0;
  333. spu_prof_num_nodes = number_of_online_nodes();
  334. num_spu_nodes = spu_prof_num_nodes * 8;
  335. spin_lock_irqsave(&buffer_lock, flags);
  336. add_event_entry(ESCAPE_CODE);
  337. add_event_entry(SPU_PROFILING_CODE);
  338. add_event_entry(num_spu_nodes);
  339. spin_unlock_irqrestore(&buffer_lock, flags);
  340. /* Register for SPU events */
  341. register_ret = spu_switch_event_register(&spu_active);
  342. if (register_ret) {
  343. ret = SYNC_START_ERROR;
  344. goto out;
  345. }
  346. for (k = 0; k < (MAX_NUMNODES * 8); k++)
  347. last_guard_val[k] = 0;
  348. pr_debug("spu_sync_start -- running.\n");
  349. out:
  350. return ret;
  351. }
  352. /* Record SPU program counter samples to the oprofile event buffer. */
  353. void spu_sync_buffer(int spu_num, unsigned int *samples,
  354. int num_samples)
  355. {
  356. unsigned long long file_offset;
  357. unsigned long flags;
  358. int i;
  359. struct vma_to_fileoffset_map *map;
  360. struct spu *the_spu;
  361. unsigned long long spu_num_ll = spu_num;
  362. unsigned long long spu_num_shifted = spu_num_ll << 32;
  363. struct cached_info *c_info;
  364. /* We need to obtain the cache_lock here because it's
  365. * possible that after getting the cached_info, the SPU job
  366. * corresponding to this cached_info may end, thus resulting
  367. * in the destruction of the cached_info.
  368. */
  369. spin_lock_irqsave(&cache_lock, flags);
  370. c_info = get_cached_info(NULL, spu_num);
  371. if (!c_info) {
  372. /* This legitimately happens when the SPU task ends before all
  373. * samples are recorded.
  374. * No big deal -- so we just drop a few samples.
  375. */
  376. pr_debug("SPU_PROF: No cached SPU contex "
  377. "for SPU #%d. Dropping samples.\n", spu_num);
  378. goto out;
  379. }
  380. map = c_info->map;
  381. the_spu = c_info->the_spu;
  382. spin_lock(&buffer_lock);
  383. for (i = 0; i < num_samples; i++) {
  384. unsigned int sample = *(samples+i);
  385. int grd_val = 0;
  386. file_offset = 0;
  387. if (sample == 0)
  388. continue;
  389. file_offset = vma_map_lookup( map, sample, the_spu, &grd_val);
  390. /* If overlays are used by this SPU application, the guard
  391. * value is non-zero, indicating which overlay section is in
  392. * use. We need to discard samples taken during the time
  393. * period which an overlay occurs (i.e., guard value changes).
  394. */
  395. if (grd_val && grd_val != last_guard_val[spu_num]) {
  396. last_guard_val[spu_num] = grd_val;
  397. /* Drop the rest of the samples. */
  398. break;
  399. }
  400. add_event_entry(file_offset | spu_num_shifted);
  401. }
  402. spin_unlock(&buffer_lock);
  403. out:
  404. spin_unlock_irqrestore(&cache_lock, flags);
  405. }
  406. int spu_sync_stop(void)
  407. {
  408. unsigned long flags = 0;
  409. int ret = spu_switch_event_unregister(&spu_active);
  410. if (ret) {
  411. printk(KERN_ERR "SPU_PROF: "
  412. "%s, line %d: spu_switch_event_unregister returned %d\n",
  413. __func__, __LINE__, ret);
  414. goto out;
  415. }
  416. spin_lock_irqsave(&cache_lock, flags);
  417. ret = release_cached_info(RELEASE_ALL);
  418. spin_unlock_irqrestore(&cache_lock, flags);
  419. out:
  420. pr_debug("spu_sync_stop -- done.\n");
  421. return ret;
  422. }