Path: blob/master/arch/powerpc/oprofile/cell/spu_task_sync.c
10818 views
/*1* Cell Broadband Engine OProfile Support2*3* (C) Copyright IBM Corporation 20064*5* Author: Maynard Johnson <[email protected]>6*7* This program is free software; you can redistribute it and/or8* modify it under the terms of the GNU General Public License9* as published by the Free Software Foundation; either version10* 2 of the License, or (at your option) any later version.11*/1213/* The purpose of this file is to handle SPU event task switching14* and to record SPU context information into the OProfile15* event buffer.16*17* Additionally, the spu_sync_buffer function is provided as a helper18* 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/slab.h>29#include <linux/spinlock.h>30#include "pr_util.h"3132#define RELEASE_ALL 99993334static DEFINE_SPINLOCK(buffer_lock);35static DEFINE_SPINLOCK(cache_lock);36static int num_spu_nodes;37int spu_prof_num_nodes;3839struct spu_buffer spu_buff[MAX_NUMNODES * SPUS_PER_NODE];40struct delayed_work spu_work;41static unsigned max_spu_buff;4243static void spu_buff_add(unsigned long int value, int spu)44{45/* spu buff is a circular buffer. Add entries to the46* head. Head is the index to store the next value.47* The buffer is full when there is one available entry48* in the queue, i.e. head and tail can't be equal.49* That way we can tell the difference between the50* buffer being full versus empty.51*52* ASSUPTION: the buffer_lock is held when this function53* is called to lock the buffer, head and tail.54*/55int full = 1;5657if (spu_buff[spu].head >= spu_buff[spu].tail) {58if ((spu_buff[spu].head - spu_buff[spu].tail)59< (max_spu_buff - 1))60full = 0;6162} else if (spu_buff[spu].tail > spu_buff[spu].head) {63if ((spu_buff[spu].tail - spu_buff[spu].head)64> 1)65full = 0;66}6768if (!full) {69spu_buff[spu].buff[spu_buff[spu].head] = value;70spu_buff[spu].head++;7172if (spu_buff[spu].head >= max_spu_buff)73spu_buff[spu].head = 0;74} else {75/* From the user's perspective make the SPU buffer76* size management/overflow look like we are using77* per cpu buffers. The user uses the same78* per cpu parameter to adjust the SPU buffer size.79* Increment the sample_lost_overflow to inform80* the user the buffer size needs to be increased.81*/82oprofile_cpu_buffer_inc_smpl_lost();83}84}8586/* This function copies the per SPU buffers to the87* OProfile kernel buffer.88*/89void sync_spu_buff(void)90{91int spu;92unsigned long flags;93int curr_head;9495for (spu = 0; spu < num_spu_nodes; spu++) {96/* In case there was an issue and the buffer didn't97* get created skip it.98*/99if (spu_buff[spu].buff == NULL)100continue;101102/* Hold the lock to make sure the head/tail103* doesn't change while spu_buff_add() is104* deciding if the buffer is full or not.105* Being a little paranoid.106*/107spin_lock_irqsave(&buffer_lock, flags);108curr_head = spu_buff[spu].head;109spin_unlock_irqrestore(&buffer_lock, flags);110111/* Transfer the current contents to the kernel buffer.112* data can still be added to the head of the buffer.113*/114oprofile_put_buff(spu_buff[spu].buff,115spu_buff[spu].tail,116curr_head, max_spu_buff);117118spin_lock_irqsave(&buffer_lock, flags);119spu_buff[spu].tail = curr_head;120spin_unlock_irqrestore(&buffer_lock, flags);121}122123}124125static void wq_sync_spu_buff(struct work_struct *work)126{127/* move data from spu buffers to kernel buffer */128sync_spu_buff();129130/* only reschedule if profiling is not done */131if (spu_prof_running)132schedule_delayed_work(&spu_work, DEFAULT_TIMER_EXPIRE);133}134135/* Container for caching information about an active SPU task. */136struct cached_info {137struct vma_to_fileoffset_map *map;138struct spu *the_spu; /* needed to access pointer to local_store */139struct kref cache_ref;140};141142static struct cached_info *spu_info[MAX_NUMNODES * 8];143144static void destroy_cached_info(struct kref *kref)145{146struct cached_info *info;147148info = container_of(kref, struct cached_info, cache_ref);149vma_map_free(info->map);150kfree(info);151module_put(THIS_MODULE);152}153154/* Return the cached_info for the passed SPU number.155* ATTENTION: Callers are responsible for obtaining the156* cache_lock if needed prior to invoking this function.157*/158static struct cached_info *get_cached_info(struct spu *the_spu, int spu_num)159{160struct kref *ref;161struct cached_info *ret_info;162163if (spu_num >= num_spu_nodes) {164printk(KERN_ERR "SPU_PROF: "165"%s, line %d: Invalid index %d into spu info cache\n",166__func__, __LINE__, spu_num);167ret_info = NULL;168goto out;169}170if (!spu_info[spu_num] && the_spu) {171ref = spu_get_profile_private_kref(the_spu->ctx);172if (ref) {173spu_info[spu_num] = container_of(ref, struct cached_info, cache_ref);174kref_get(&spu_info[spu_num]->cache_ref);175}176}177178ret_info = spu_info[spu_num];179out:180return ret_info;181}182183184/* Looks for cached info for the passed spu. If not found, the185* cached info is created for the passed spu.186* Returns 0 for success; otherwise, -1 for error.187*/188static int189prepare_cached_spu_info(struct spu *spu, unsigned long objectId)190{191unsigned long flags;192struct vma_to_fileoffset_map *new_map;193int retval = 0;194struct cached_info *info;195196/* We won't bother getting cache_lock here since197* don't do anything with the cached_info that's returned.198*/199info = get_cached_info(spu, spu->number);200201if (info) {202pr_debug("Found cached SPU info.\n");203goto out;204}205206/* Create cached_info and set spu_info[spu->number] to point to it.207* spu->number is a system-wide value, not a per-node value.208*/209info = kzalloc(sizeof(struct cached_info), GFP_KERNEL);210if (!info) {211printk(KERN_ERR "SPU_PROF: "212"%s, line %d: create vma_map failed\n",213__func__, __LINE__);214retval = -ENOMEM;215goto err_alloc;216}217new_map = create_vma_map(spu, objectId);218if (!new_map) {219printk(KERN_ERR "SPU_PROF: "220"%s, line %d: create vma_map failed\n",221__func__, __LINE__);222retval = -ENOMEM;223goto err_alloc;224}225226pr_debug("Created vma_map\n");227info->map = new_map;228info->the_spu = spu;229kref_init(&info->cache_ref);230spin_lock_irqsave(&cache_lock, flags);231spu_info[spu->number] = info;232/* Increment count before passing off ref to SPUFS. */233kref_get(&info->cache_ref);234235/* We increment the module refcount here since SPUFS is236* responsible for the final destruction of the cached_info,237* and it must be able to access the destroy_cached_info()238* function defined in the OProfile module. We decrement239* the module refcount in destroy_cached_info.240*/241try_module_get(THIS_MODULE);242spu_set_profile_private_kref(spu->ctx, &info->cache_ref,243destroy_cached_info);244spin_unlock_irqrestore(&cache_lock, flags);245goto out;246247err_alloc:248kfree(info);249out:250return retval;251}252253/*254* NOTE: The caller is responsible for locking the255* cache_lock prior to calling this function.256*/257static int release_cached_info(int spu_index)258{259int index, end;260261if (spu_index == RELEASE_ALL) {262end = num_spu_nodes;263index = 0;264} else {265if (spu_index >= num_spu_nodes) {266printk(KERN_ERR "SPU_PROF: "267"%s, line %d: "268"Invalid index %d into spu info cache\n",269__func__, __LINE__, spu_index);270goto out;271}272end = spu_index + 1;273index = spu_index;274}275for (; index < end; index++) {276if (spu_info[index]) {277kref_put(&spu_info[index]->cache_ref,278destroy_cached_info);279spu_info[index] = NULL;280}281}282283out:284return 0;285}286287/* The source code for fast_get_dcookie was "borrowed"288* from drivers/oprofile/buffer_sync.c.289*/290291/* Optimisation. We can manage without taking the dcookie sem292* because we cannot reach this code without at least one293* dcookie user still being registered (namely, the reader294* of the event buffer).295*/296static inline unsigned long fast_get_dcookie(struct path *path)297{298unsigned long cookie;299300if (path->dentry->d_flags & DCACHE_COOKIE)301return (unsigned long)path->dentry;302get_dcookie(path, &cookie);303return cookie;304}305306/* Look up the dcookie for the task's first VM_EXECUTABLE mapping,307* which corresponds loosely to "application name". Also, determine308* the offset for the SPU ELF object. If computed offset is309* non-zero, it implies an embedded SPU object; otherwise, it's a310* separate SPU binary, in which case we retrieve it's dcookie.311* For the embedded case, we must determine if SPU ELF is embedded312* in the executable application or another file (i.e., shared lib).313* If embedded in a shared lib, we must get the dcookie and return314* that to the caller.315*/316static unsigned long317get_exec_dcookie_and_offset(struct spu *spu, unsigned int *offsetp,318unsigned long *spu_bin_dcookie,319unsigned long spu_ref)320{321unsigned long app_cookie = 0;322unsigned int my_offset = 0;323struct file *app = NULL;324struct vm_area_struct *vma;325struct mm_struct *mm = spu->mm;326327if (!mm)328goto out;329330down_read(&mm->mmap_sem);331332for (vma = mm->mmap; vma; vma = vma->vm_next) {333if (!vma->vm_file)334continue;335if (!(vma->vm_flags & VM_EXECUTABLE))336continue;337app_cookie = fast_get_dcookie(&vma->vm_file->f_path);338pr_debug("got dcookie for %s\n",339vma->vm_file->f_dentry->d_name.name);340app = vma->vm_file;341break;342}343344for (vma = mm->mmap; vma; vma = vma->vm_next) {345if (vma->vm_start > spu_ref || vma->vm_end <= spu_ref)346continue;347my_offset = spu_ref - vma->vm_start;348if (!vma->vm_file)349goto fail_no_image_cookie;350351pr_debug("Found spu ELF at %X(object-id:%lx) for file %s\n",352my_offset, spu_ref,353vma->vm_file->f_dentry->d_name.name);354*offsetp = my_offset;355break;356}357358*spu_bin_dcookie = fast_get_dcookie(&vma->vm_file->f_path);359pr_debug("got dcookie for %s\n", vma->vm_file->f_dentry->d_name.name);360361up_read(&mm->mmap_sem);362363out:364return app_cookie;365366fail_no_image_cookie:367up_read(&mm->mmap_sem);368369printk(KERN_ERR "SPU_PROF: "370"%s, line %d: Cannot find dcookie for SPU binary\n",371__func__, __LINE__);372goto out;373}374375376377/* This function finds or creates cached context information for the378* passed SPU and records SPU context information into the OProfile379* event buffer.380*/381static int process_context_switch(struct spu *spu, unsigned long objectId)382{383unsigned long flags;384int retval;385unsigned int offset = 0;386unsigned long spu_cookie = 0, app_dcookie;387388retval = prepare_cached_spu_info(spu, objectId);389if (retval)390goto out;391392/* Get dcookie first because a mutex_lock is taken in that393* code path, so interrupts must not be disabled.394*/395app_dcookie = get_exec_dcookie_and_offset(spu, &offset, &spu_cookie, objectId);396if (!app_dcookie || !spu_cookie) {397retval = -ENOENT;398goto out;399}400401/* Record context info in event buffer */402spin_lock_irqsave(&buffer_lock, flags);403spu_buff_add(ESCAPE_CODE, spu->number);404spu_buff_add(SPU_CTX_SWITCH_CODE, spu->number);405spu_buff_add(spu->number, spu->number);406spu_buff_add(spu->pid, spu->number);407spu_buff_add(spu->tgid, spu->number);408spu_buff_add(app_dcookie, spu->number);409spu_buff_add(spu_cookie, spu->number);410spu_buff_add(offset, spu->number);411412/* Set flag to indicate SPU PC data can now be written out. If413* the SPU program counter data is seen before an SPU context414* record is seen, the postprocessing will fail.415*/416spu_buff[spu->number].ctx_sw_seen = 1;417418spin_unlock_irqrestore(&buffer_lock, flags);419smp_wmb(); /* insure spu event buffer updates are written */420/* don't want entries intermingled... */421out:422return retval;423}424425/*426* This function is invoked on either a bind_context or unbind_context.427* If called for an unbind_context, the val arg is 0; otherwise,428* it is the object-id value for the spu context.429* The data arg is of type 'struct spu *'.430*/431static int spu_active_notify(struct notifier_block *self, unsigned long val,432void *data)433{434int retval;435unsigned long flags;436struct spu *the_spu = data;437438pr_debug("SPU event notification arrived\n");439if (!val) {440spin_lock_irqsave(&cache_lock, flags);441retval = release_cached_info(the_spu->number);442spin_unlock_irqrestore(&cache_lock, flags);443} else {444retval = process_context_switch(the_spu, val);445}446return retval;447}448449static struct notifier_block spu_active = {450.notifier_call = spu_active_notify,451};452453static int number_of_online_nodes(void)454{455u32 cpu; u32 tmp;456int nodes = 0;457for_each_online_cpu(cpu) {458tmp = cbe_cpu_to_node(cpu) + 1;459if (tmp > nodes)460nodes++;461}462return nodes;463}464465static int oprofile_spu_buff_create(void)466{467int spu;468469max_spu_buff = oprofile_get_cpu_buffer_size();470471for (spu = 0; spu < num_spu_nodes; spu++) {472/* create circular buffers to store the data in.473* use locks to manage accessing the buffers474*/475spu_buff[spu].head = 0;476spu_buff[spu].tail = 0;477478/*479* Create a buffer for each SPU. Can't reliably480* create a single buffer for all spus due to not481* enough contiguous kernel memory.482*/483484spu_buff[spu].buff = kzalloc((max_spu_buff485* sizeof(unsigned long)),486GFP_KERNEL);487488if (!spu_buff[spu].buff) {489printk(KERN_ERR "SPU_PROF: "490"%s, line %d: oprofile_spu_buff_create "491"failed to allocate spu buffer %d.\n",492__func__, __LINE__, spu);493494/* release the spu buffers that have been allocated */495while (spu >= 0) {496kfree(spu_buff[spu].buff);497spu_buff[spu].buff = 0;498spu--;499}500return -ENOMEM;501}502}503return 0;504}505506/* The main purpose of this function is to synchronize507* OProfile with SPUFS by registering to be notified of508* SPU task switches.509*510* NOTE: When profiling SPUs, we must ensure that only511* spu_sync_start is invoked and not the generic sync_start512* in drivers/oprofile/oprof.c. A return value of513* SKIP_GENERIC_SYNC or SYNC_START_ERROR will514* accomplish this.515*/516int spu_sync_start(void)517{518int spu;519int ret = SKIP_GENERIC_SYNC;520int register_ret;521unsigned long flags = 0;522523spu_prof_num_nodes = number_of_online_nodes();524num_spu_nodes = spu_prof_num_nodes * 8;525INIT_DELAYED_WORK(&spu_work, wq_sync_spu_buff);526527/* create buffer for storing the SPU data to put in528* the kernel buffer.529*/530ret = oprofile_spu_buff_create();531if (ret)532goto out;533534spin_lock_irqsave(&buffer_lock, flags);535for (spu = 0; spu < num_spu_nodes; spu++) {536spu_buff_add(ESCAPE_CODE, spu);537spu_buff_add(SPU_PROFILING_CODE, spu);538spu_buff_add(num_spu_nodes, spu);539}540spin_unlock_irqrestore(&buffer_lock, flags);541542for (spu = 0; spu < num_spu_nodes; spu++) {543spu_buff[spu].ctx_sw_seen = 0;544spu_buff[spu].last_guard_val = 0;545}546547/* Register for SPU events */548register_ret = spu_switch_event_register(&spu_active);549if (register_ret) {550ret = SYNC_START_ERROR;551goto out;552}553554pr_debug("spu_sync_start -- running.\n");555out:556return ret;557}558559/* Record SPU program counter samples to the oprofile event buffer. */560void spu_sync_buffer(int spu_num, unsigned int *samples,561int num_samples)562{563unsigned long long file_offset;564unsigned long flags;565int i;566struct vma_to_fileoffset_map *map;567struct spu *the_spu;568unsigned long long spu_num_ll = spu_num;569unsigned long long spu_num_shifted = spu_num_ll << 32;570struct cached_info *c_info;571572/* We need to obtain the cache_lock here because it's573* possible that after getting the cached_info, the SPU job574* corresponding to this cached_info may end, thus resulting575* in the destruction of the cached_info.576*/577spin_lock_irqsave(&cache_lock, flags);578c_info = get_cached_info(NULL, spu_num);579if (!c_info) {580/* This legitimately happens when the SPU task ends before all581* samples are recorded.582* No big deal -- so we just drop a few samples.583*/584pr_debug("SPU_PROF: No cached SPU contex "585"for SPU #%d. Dropping samples.\n", spu_num);586goto out;587}588589map = c_info->map;590the_spu = c_info->the_spu;591spin_lock(&buffer_lock);592for (i = 0; i < num_samples; i++) {593unsigned int sample = *(samples+i);594int grd_val = 0;595file_offset = 0;596if (sample == 0)597continue;598file_offset = vma_map_lookup( map, sample, the_spu, &grd_val);599600/* If overlays are used by this SPU application, the guard601* value is non-zero, indicating which overlay section is in602* use. We need to discard samples taken during the time603* period which an overlay occurs (i.e., guard value changes).604*/605if (grd_val && grd_val != spu_buff[spu_num].last_guard_val) {606spu_buff[spu_num].last_guard_val = grd_val;607/* Drop the rest of the samples. */608break;609}610611/* We must ensure that the SPU context switch has been written612* out before samples for the SPU. Otherwise, the SPU context613* information is not available and the postprocessing of the614* SPU PC will fail with no available anonymous map information.615*/616if (spu_buff[spu_num].ctx_sw_seen)617spu_buff_add((file_offset | spu_num_shifted),618spu_num);619}620spin_unlock(&buffer_lock);621out:622spin_unlock_irqrestore(&cache_lock, flags);623}624625626int spu_sync_stop(void)627{628unsigned long flags = 0;629int ret;630int k;631632ret = spu_switch_event_unregister(&spu_active);633634if (ret)635printk(KERN_ERR "SPU_PROF: "636"%s, line %d: spu_switch_event_unregister " \637"returned %d\n",638__func__, __LINE__, ret);639640/* flush any remaining data in the per SPU buffers */641sync_spu_buff();642643spin_lock_irqsave(&cache_lock, flags);644ret = release_cached_info(RELEASE_ALL);645spin_unlock_irqrestore(&cache_lock, flags);646647/* remove scheduled work queue item rather then waiting648* for every queued entry to execute. Then flush pending649* system wide buffer to event buffer.650*/651cancel_delayed_work(&spu_work);652653for (k = 0; k < num_spu_nodes; k++) {654spu_buff[k].ctx_sw_seen = 0;655656/*657* spu_sys_buff will be null if there was a problem658* allocating the buffer. Only delete if it exists.659*/660kfree(spu_buff[k].buff);661spu_buff[k].buff = 0;662}663pr_debug("spu_sync_stop -- done.\n");664return ret;665}666667668669