2.6.17-rc6 perfmon2 patch for review: new system calls
PATCH 4/16 2.6.17-rc6 perfmon2 patch for review: new system calls
This patch contains the system calls interface.
perfmon/perfmon_syscalls.c +625 -0
@@ -0,0 +1,625 @@ +/* +* perfmon_syscalls.c: perfmon2 system call interface +* +* This file implements the perfmon2 interface which +* provides access to the hardware performance counters +* of the host processor. +* +* The initial version of perfmon.c was written by +* Ganesh Venkitachalam, IBM Corp. +* +* Then it was modified for perfmon-1.x by Stephane Eranian and +* David Mosberger, Hewlett Packard Co. +* +* Version Perfmon-2.x is a complete rewrite of perfmon-1.x +* by Stephane Eranian, Hewlett Packard Co. +* +* Copyright (c) 1999-2006 Hewlett-Packard Development Company, L.P. +* Contributed by Stephane Eranian <[email protected]> +* David Mosberger-Tang <[email protected]> +* +* More information about perfmon available at: +* http://www.hpl.hp.com/research/linux/perfmon +*/ +#include <linux/kernel.h> +#include <linux/perfmon.h> +#include <asm/uaccess.h> + +int pfm_check_task_state (struct pfm_context *ctx,int check_mask, +unsigned long *flags) +{ +struct task_struct *task; +unsigned long local_flags, new_flags; +int state, old_state; + +recheck: +/* +* task is NULL for system-wide context +*/ + task = ctx->task; + state = ctx->state; + local_flags = *flags; + +PFM_DBG ("state=%d [%d] task_state=%ld check_mask=0x%x" , + state, + task ? task->pid :-1 , + task ? task->state :-1 , check_mask); + +if (state ==PFM_CTX_UNLOADED ) +return 0 ; +/* +* no command can operate on a zombie context +*/ +if (state ==PFM_CTX_ZOMBIE ) +return -EINVAL ; + +/* +* at this point, state is PFM_CTX_LOADED or PFM_CTX_MASKED +*/ + +/* +* some commands require the context to be unloaded to operate +*/ +if (check_mask &PFM_CMD_UNLOADED ) { +PFM_DBG ("state=%d, cmd needs unloaded" , state); +return -EBUSY ; + } + +/* +* self-monitoring always ok. +*/ +if (task == current) +return 0 ; + +/* +* for syswide, we accept if running on the cpu the context is bound +* to. When monitoring another thread, must wait until stopped. +*/ +if (ctx->flags.system) { +if (ctx->cpu !=smp_processor_id ()) +return -EBUSY ; +return 0 ; + } + +/* +* monitoring another thread +*/ +if (state ==PFM_CTX_MASKED && (check_mask &PFM_CMD_UNLOAD ) ==0 ) +return 0 ; +/* +* state is PFM_CTX_LOADED. +* +* We could lift this restriction for UP but it would mean that +* the user has no guarantee the task would not run between +* two successive calls to perfmonctl(). That's probably OK. +* If this user wants to ensure the task does not run, then +* the task must be stopped. +*/ +if (check_mask &PFM_CMD_STOPPED ) { +if ((task->state !=TASK_STOPPED ) + && (task->state !=TASK_TRACED )) { +PFM_DBG ("[%d] task not in stopped state" , task->pid); +return -EBUSY ; + } +/* +* task is now stopped, wait for ctxsw out +* +* This is an interesting point in the code. +* We need to unprotect the context because +* the pfm_ctxswout_thread() routines needs to grab +* the same lock. There are danger in doing +* this because it leaves a window open for +* another task to get access to the context +* and possibly change its state. The one thing +* that is not possible is for the context to disappear +* because we are protected by the VFS layer, i.e., +* get_fd()/put_fd(). +*/ + old_state = state; + +PFM_DBG ("going wait_inactive for [%d] state=%ld flags=0x%lx" , + task->pid, + task->state, + local_flags); + +spin_unlock_irqrestore (&ctx->lock, local_flags); + +wait_task_inactive (task); + +spin_lock_irqsave (&ctx->lock, new_flags); + +/* +* flags may be different than when we released the lock +*/ + *flags = new_flags; + +/* +* we must recheck to verify if state has changed +*/ +if (ctx->state != old_state) { +PFM_DBG ("old_state=%d new_state=%d" , + old_state, + ctx->state); + goto recheck; + } + } +return 0 ; +} + +int pfm_get_args (void __user *ureq,size_t sz,void **req) +{ +void *addr; + +if (unlikely (sz > pfm_controls.arg_size_max)) { +PFM_DBG ("argument too big %zu max=%zu" , + sz, + pfm_controls.arg_size_max); +return -E2BIG ; + } + + addr =kmalloc (sz,GFP_KERNEL ); +if (unlikely (addr ==NULL )) +return -ENOMEM ; + +if (copy_from_user (addr, ureq, sz)) { +kfree (addr); +return -EFAULT ; + } + *req = addr; + +return 0 ; +} + +int pfm_get_smpl_arg (pfm_uuid_t uuid,void __user *uaddr,size_t usize,void **arg, +struct pfm_smpl_fmt **fmt) +{ +struct pfm_smpl_fmt *f; +void *addr =NULL ; +size_t sz; +int ret; + +if (!pfm_use_smpl_fmt (uuid)) +return 0 ; + +/* +* find fmt and increase refcount +*/ + f =pfm_smpl_fmt_get (uuid); +if (f ==NULL ) { +PFM_DBG ("buffer format not found" ); +return -EINVAL ; + } + + sz = f->fmt_arg_size; + +/* +* usize = -1 is for IA-64 backward compatibility +*/ + ret = -EINVAL ; +if (sz != usize && usize !=-1 ) { +PFM_DBG ("invalid arg size %zu, format expects %zu" , + usize, sz); + goto error; + } + + ret = -ENOMEM ; + addr =kmalloc (sz,GFP_KERNEL ); +if (addr ==NULL ) + goto error; + + ret = -EFAULT ; +if (copy_from_user (addr, uaddr, sz)) + goto error; + + *arg = addr; + *fmt = f; +return 0 ; + +error: +kfree (addr); +pfm_smpl_fmt_put (f); +return ret; +} + +asmlinkage long sys_pfm_create_context (struct pfarg_ctx __user *ureq,void __user *uarg,size_t smpl_size) +{ +struct pfarg_ctx req; +struct pfm_context *new_ctx; +struct pfm_smpl_fmt *fmt =NULL ; +void *smpl_arg =NULL ; +int ret = -EFAULT ; + +if (copy_from_user (&req, ureq,sizeof (req))) + goto abort; + + ret =pfm_get_smpl_arg (req.ctx_smpl_buf_id, uarg, smpl_size, + &smpl_arg, &fmt); +if (ret) + goto abort; + + ret =__pfm_create_context (&req, fmt, smpl_arg,PFM_NORMAL ,NULL , &new_ctx); + +/* +* copy_user return value overrides command return value +*/ +if (!ret) { +if (copy_to_user (ureq, &req,sizeof (req))) { +pfm_undo_create_context_fd (req.ctx_fd, new_ctx); + ret = -EFAULT ; + } + } + +abort: +kfree (smpl_arg); +return ret; +} + + +asmlinkage long sys_pfm_write_pmcs (int fd,struct pfarg_pmc __user *ureq,int count) +{ +struct pfm_context *ctx; +struct pfarg_pmc *req =NULL ; +unsigned long flags; +size_t sz; +int ret; + +if (count <0 ) +return -EINVAL ; + + ctx =pfm_get_ctx (fd); +if (unlikely (ctx ==NULL )) +return -EBADF ; + + + sz = count*sizeof (*ureq); + + ret =pfm_get_args (ureq, sz, (void **)&req); +if (ret) + goto error; + +spin_lock_irqsave (&ctx->lock, flags); + + ret =pfm_check_task_state (ctx,PFM_CMD_STOPPED , &flags); +if (ret ==0 ) + ret =__pfm_write_pmcs (ctx, req, count); + +spin_unlock_irqrestore (&ctx->lock, flags); + +if (copy_to_user (ureq, req, sz)) + ret = -EFAULT ; +error: +kfree (req); +pfm_put_ctx (ctx); + +return ret; +} + +asmlinkage long sys_pfm_write_pmds (int fd,struct pfarg_pmd __user *ureq,int count) +{ +struct pfm_context *ctx; +struct pfarg_pmd *req =NULL ; +unsigned long flags; +size_t sz; +int ret; + +if (count <0 ) +return -EINVAL ; + + ctx =pfm_get_ctx (fd); +if (unlikely (ctx ==NULL )) +return -EBADF ; + + + sz = count*sizeof (*ureq); + + ret =pfm_get_args (ureq, sz, (void **)&req); +if (ret) + goto error; + +spin_lock_irqsave (&ctx->lock, flags); + + ret =pfm_check_task_state (ctx,PFM_CMD_STOPPED , &flags); +if (ret ==0 ) + ret =__pfm_write_pmds (ctx, req, count,0 ); + +spin_unlock_irqrestore (&ctx->lock, flags); + +if (copy_to_user (ureq, req, sz)) + ret = -EFAULT ; +error: +kfree (req); +pfm_put_ctx (ctx); + +return ret; +} + +asmlinkage long sys_pfm_read_pmds (int fd,struct pfarg_pmd __user *ureq,int count) +{ +struct pfm_context *ctx; +struct pfarg_pmd *req =NULL ; +unsigned long flags; +size_t sz; +int ret; + +if (count <0 ) +return -EINVAL ; + + ctx =pfm_get_ctx (fd); +if (unlikely (ctx ==NULL )) +return -EBADF ; + + sz = count*sizeof (*ureq); + + ret =pfm_get_args (ureq, sz, (void **)&req); +if (ret) + goto error; + +spin_lock_irqsave (&ctx->lock, flags); + + ret =pfm_check_task_state (ctx,PFM_CMD_STOPPED , &flags); +if (ret ==0 ) + ret =__pfm_read_pmds (ctx, req, count); + +spin_unlock_irqrestore (&ctx->lock, flags); + +if (copy_to_user (ureq, req, sz)) + ret = -EFAULT ; +error: +kfree (req); +pfm_put_ctx (ctx); + +return ret; +} + +asmlinkage long sys_pfm_restart (int fd) +{ +struct pfm_context *ctx; +unsigned long flags; +int ret =0 ; + + ctx =pfm_get_ctx (fd); +if (unlikely (ctx ==NULL )) +return -EBADF ; + +spin_lock_irqsave (&ctx->lock, flags); + + ret =pfm_check_task_state (ctx,0 , &flags); +if (ret ==0 ) + ret =__pfm_restart (ctx); + +spin_unlock_irqrestore (&ctx->lock, flags); + +pfm_put_ctx (ctx); + +return ret; +} + + +asmlinkage long sys_pfm_stop (int fd) +{ +struct pfm_context *ctx; +unsigned long flags; +int ret; + + ctx =pfm_get_ctx (fd); +if (unlikely (ctx ==NULL )) +return -EBADF ; + +spin_lock_irqsave (&ctx->lock, flags); + + ret =pfm_check_task_state (ctx,PFM_CMD_STOPPED , &flags); +if (ret ==0 ) + ret =__pfm_stop (ctx); + +spin_unlock_irqrestore (&ctx->lock, flags); + +pfm_put_ctx (ctx); + +return ret; +} + +asmlinkage long sys_pfm_start (int fd,struct pfarg_start __user *ureq) +{ +struct pfm_context *ctx; +struct pfarg_start req; +unsigned long flags; +int ret =0 ; + + ctx =pfm_get_ctx (fd); +if (ctx ==NULL ) +return -EBADF ; + +/* +* the one argument is actually optional +*/ +if (ureq &©_from_user (&req, ureq,sizeof (req))) +return -EFAULT ; + +spin_lock_irqsave (&ctx->lock, flags); + + ret =pfm_check_task_state (ctx,PFM_CMD_STOPPED , &flags); +if (ret ==0 ) + ret =__pfm_start (ctx, ureq ? &req :NULL ); + +spin_unlock_irqrestore (&ctx->lock, flags); + +pfm_put_ctx (ctx); + +return ret; +} + + + +asmlinkage long sys_pfm_load_context (int fd,struct pfarg_load __user *ureq) +{ +struct pfm_context *ctx; +unsigned long flags; +struct pfarg_load req; +int ret; + + ctx =pfm_get_ctx (fd); +if (ctx ==NULL ) +return -EBADF ; + +if (copy_from_user (&req, ureq,sizeof (req))) +return -EFAULT ; + +/* +* irqsave is required to avoid race in case context is already +* loaded or with switch timeout in the case of self-monitoring +*/ +spin_lock_irqsave (&ctx->lock, flags); + + ret =pfm_check_task_state (ctx,PFM_CMD_STOPPED , &flags); +if (ret ==0 ) + ret =__pfm_load_context (ctx, &req); + +spin_unlock_irqrestore (&ctx->lock, flags); + +pfm_put_ctx (ctx); + +return ret; +} + + +asmlinkage long sys_pfm_unload_context (int fd) +{ +struct pfm_context *ctx; +unsigned long flags; +int ret =0 ; + + ctx =pfm_get_ctx (fd); +if (ctx ==NULL ) +return -EBADF ; + +spin_lock_irqsave (&ctx->lock, flags); + + ret =pfm_check_task_state (ctx,PFM_CMD_STOPPED |PFM_CMD_UNLOAD , &flags); +if (ret ==0 ) + ret =__pfm_unload_context (ctx,0 ); + +spin_unlock_irqrestore (&ctx->lock, flags); + +pfm_put_ctx (ctx); + +return ret; +} + +asmlinkage long sys_pfm_delete_evtsets (int fd,struct pfarg_setinfo __user *ureq,int count) +{ +struct pfm_context *ctx; +struct pfarg_setinfo *req; +unsigned long flags; +size_t sz; +int ret; + +if (count <0 ) +return -EINVAL ; + + ctx =pfm_get_ctx (fd); +if (ctx ==NULL ) +return -EBADF ; + + sz = count*sizeof (*ureq); + + ret =pfm_get_args (ureq, sz, (void **)&req); +if (ret) + goto error; + +spin_lock_irqsave (&ctx->lock, flags); + + ret =pfm_check_task_state (ctx,PFM_CMD_UNLOADED , &flags); +if (ret ==0 ) + ret =__pfm_delete_evtsets (ctx, req, count); + +spin_unlock_irqrestore (&ctx->lock, flags); + +if (copy_to_user (ureq, req, sz)) + ret = -EFAULT ; + +kfree (req); + +error: +pfm_put_ctx (ctx); + +return ret; +} + +asmlinkage long sys_pfm_create_evtsets (int fd,struct pfarg_setdesc __user *ureq,int count) +{ +struct pfm_context *ctx; +struct pfarg_setdesc *req; +unsigned long flags; +size_t sz; +int ret; + +if (count <0 ) +return -EINVAL ; + + ctx =pfm_get_ctx (fd); +if (ctx ==NULL ) +return -EBADF ; + + sz = count*sizeof (*ureq); + + ret =pfm_get_args (ureq, sz, (void **)&req); +if (ret) + goto error; + +spin_lock_irqsave (&ctx->lock, flags); + + ret =pfm_check_task_state (ctx,PFM_CMD_UNLOADED , &flags); +if (ret ==0 ) + ret =__pfm_create_evtsets (ctx, req, count); + +spin_unlock_irqrestore (&ctx->lock, flags); + +if (copy_to_user (ureq, req, sz)) + ret = -EFAULT ; + +kfree (req); + +error: +pfm_put_ctx (ctx); + +return ret; +} + +asmlinkage long sys_pfm_getinfo_evtsets (int fd,struct pfarg_setinfo __user *ureq,int count) +{ +struct pfm_context *ctx; +struct pfarg_setinfo *req; +unsigned long flags; +size_t sz; +int ret; + +if (count <0 ) +return -EINVAL ; + + ctx =pfm_get_ctx (fd); +if (ctx ==NULL ) +return -EBADF ; + + sz = count*sizeof (*ureq); + + ret =pfm_get_args (ureq, sz, (void **)&req); +if (ret) + goto error; + +spin_lock_irqsave (&ctx->lock, flags); + + ret =pfm_check_task_state (ctx,0 , &flags); +if (ret ==0 ) + ret =__pfm_getinfo_evtsets (ctx, req, count); + +spin_unlock_irqrestore (&ctx->lock, flags); + +if (copy_to_user (ureq, req, sz)) + ret = -EFAULT ; + +kfree (req); +error: +pfm_put_ctx (ctx); + +return ret; +}
perfmon/perfmon_file.c +810 -0
810 lines not on this page · raw message