spu_task_sync.c 13 KB

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