1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * linux/kernel/profile.c 41da177e4SLinus Torvalds * Simple profiling. Manages a direct-mapped profile hit count buffer, 51da177e4SLinus Torvalds * with configurable resolution, support for restricting the cpus on 61da177e4SLinus Torvalds * which profiling is done, and switching between cpu time and 71da177e4SLinus Torvalds * schedule() calls via kernel command line parameters passed at boot. 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * Scheduler profiling support, Arjan van de Ven and Ingo Molnar, 101da177e4SLinus Torvalds * Red Hat, July 2004 111da177e4SLinus Torvalds * Consolidation of architecture support code for profiling, 126d49e352SNadia Yvette Chambers * Nadia Yvette Chambers, Oracle, July 2004 131da177e4SLinus Torvalds * Amortized hit count accounting via per-cpu open-addressed hashtables 146d49e352SNadia Yvette Chambers * to resolve timer interrupt livelocks, Nadia Yvette Chambers, 156d49e352SNadia Yvette Chambers * Oracle, 2004 161da177e4SLinus Torvalds */ 171da177e4SLinus Torvalds 189984de1aSPaul Gortmaker #include <linux/export.h> 191da177e4SLinus Torvalds #include <linux/profile.h> 2057c8a661SMike Rapoport #include <linux/memblock.h> 211da177e4SLinus Torvalds #include <linux/notifier.h> 221da177e4SLinus Torvalds #include <linux/mm.h> 231da177e4SLinus Torvalds #include <linux/cpumask.h> 241da177e4SLinus Torvalds #include <linux/cpu.h> 251da177e4SLinus Torvalds #include <linux/highmem.h> 2697d1f15bSArjan van de Ven #include <linux/mutex.h> 2722b8ce94SDave Hansen #include <linux/slab.h> 2822b8ce94SDave Hansen #include <linux/vmalloc.h> 293905f9adSIngo Molnar #include <linux/sched/stat.h> 303905f9adSIngo Molnar 311da177e4SLinus Torvalds #include <asm/sections.h> 327d12e780SDavid Howells #include <asm/irq_regs.h> 33e8edc6e0SAlexey Dobriyan #include <asm/ptrace.h> 341da177e4SLinus Torvalds 351da177e4SLinus Torvalds struct profile_hit { 361da177e4SLinus Torvalds u32 pc, hits; 371da177e4SLinus Torvalds }; 381da177e4SLinus Torvalds #define PROFILE_GRPSHIFT 3 391da177e4SLinus Torvalds #define PROFILE_GRPSZ (1 << PROFILE_GRPSHIFT) 401da177e4SLinus Torvalds #define NR_PROFILE_HIT (PAGE_SIZE/sizeof(struct profile_hit)) 411da177e4SLinus Torvalds #define NR_PROFILE_GRP (NR_PROFILE_HIT/PROFILE_GRPSZ) 421da177e4SLinus Torvalds 431da177e4SLinus Torvalds static atomic_t *prof_buffer; 442d186afdSPavel Skripkin static unsigned long prof_len; 452d186afdSPavel Skripkin static unsigned short int prof_shift; 4607031e14SIngo Molnar 47ece8a684SIngo Molnar int prof_on __read_mostly; 4807031e14SIngo Molnar EXPORT_SYMBOL_GPL(prof_on); 4907031e14SIngo Molnar 50c309b917SRusty Russell static cpumask_var_t prof_cpu_mask; 51ade356b9SArnd Bergmann #if defined(CONFIG_SMP) && defined(CONFIG_PROC_FS) 521da177e4SLinus Torvalds static DEFINE_PER_CPU(struct profile_hit *[2], cpu_profile_hits); 531da177e4SLinus Torvalds static DEFINE_PER_CPU(int, cpu_profile_flip); 5497d1f15bSArjan van de Ven static DEFINE_MUTEX(profile_flip_mutex); 551da177e4SLinus Torvalds #endif /* CONFIG_SMP */ 561da177e4SLinus Torvalds 5722b8ce94SDave Hansen int profile_setup(char *str) 581da177e4SLinus Torvalds { 59f3da64d1SFabian Frederick static const char schedstr[] = "schedule"; 60f3da64d1SFabian Frederick static const char kvmstr[] = "kvm"; 6135783ccbSwuchi const char *select = NULL; 621da177e4SLinus Torvalds int par; 631da177e4SLinus Torvalds 64*2d451ec0STetsuo Handa if (!strncmp(str, schedstr, strlen(schedstr))) { 651da177e4SLinus Torvalds prof_on = SCHED_PROFILING; 6635783ccbSwuchi select = schedstr; 6707031e14SIngo Molnar } else if (!strncmp(str, kvmstr, strlen(kvmstr))) { 6807031e14SIngo Molnar prof_on = KVM_PROFILING; 6935783ccbSwuchi select = kvmstr; 70dfaa9c94SWilliam Lee Irwin III } else if (get_option(&str, &par)) { 712d186afdSPavel Skripkin prof_shift = clamp(par, 0, BITS_PER_LONG - 1); 721da177e4SLinus Torvalds prof_on = CPU_PROFILING; 732d186afdSPavel Skripkin pr_info("kernel profiling enabled (shift: %u)\n", 741da177e4SLinus Torvalds prof_shift); 751da177e4SLinus Torvalds } 7635783ccbSwuchi 7735783ccbSwuchi if (select) { 7835783ccbSwuchi if (str[strlen(select)] == ',') 7935783ccbSwuchi str += strlen(select) + 1; 8035783ccbSwuchi if (get_option(&str, &par)) 8135783ccbSwuchi prof_shift = clamp(par, 0, BITS_PER_LONG - 1); 8235783ccbSwuchi pr_info("kernel %s profiling enabled (shift: %u)\n", 8335783ccbSwuchi select, prof_shift); 8435783ccbSwuchi } 8535783ccbSwuchi 861da177e4SLinus Torvalds return 1; 871da177e4SLinus Torvalds } 881da177e4SLinus Torvalds __setup("profile=", profile_setup); 891da177e4SLinus Torvalds 901da177e4SLinus Torvalds 91ce05fcc3SPaul Mundt int __ref profile_init(void) 921da177e4SLinus Torvalds { 9322b8ce94SDave Hansen int buffer_bytes; 941da177e4SLinus Torvalds if (!prof_on) 9522b8ce94SDave Hansen return 0; 961da177e4SLinus Torvalds 971da177e4SLinus Torvalds /* only text is profiled */ 981da177e4SLinus Torvalds prof_len = (_etext - _stext) >> prof_shift; 990fe6ee8fSChen Zhongjin 1000fe6ee8fSChen Zhongjin if (!prof_len) { 1010fe6ee8fSChen Zhongjin pr_warn("profiling shift: %u too large\n", prof_shift); 1020fe6ee8fSChen Zhongjin prof_on = 0; 1030fe6ee8fSChen Zhongjin return -EINVAL; 1040fe6ee8fSChen Zhongjin } 1050fe6ee8fSChen Zhongjin 10622b8ce94SDave Hansen buffer_bytes = prof_len*sizeof(atomic_t); 10722b8ce94SDave Hansen 108c309b917SRusty Russell if (!alloc_cpumask_var(&prof_cpu_mask, GFP_KERNEL)) 109c309b917SRusty Russell return -ENOMEM; 110c309b917SRusty Russell 111acd89579SHugh Dickins cpumask_copy(prof_cpu_mask, cpu_possible_mask); 112acd89579SHugh Dickins 113b62f495dSMel Gorman prof_buffer = kzalloc(buffer_bytes, GFP_KERNEL|__GFP_NOWARN); 11422b8ce94SDave Hansen if (prof_buffer) 11522b8ce94SDave Hansen return 0; 11622b8ce94SDave Hansen 117b62f495dSMel Gorman prof_buffer = alloc_pages_exact(buffer_bytes, 118b62f495dSMel Gorman GFP_KERNEL|__GFP_ZERO|__GFP_NOWARN); 11922b8ce94SDave Hansen if (prof_buffer) 12022b8ce94SDave Hansen return 0; 12122b8ce94SDave Hansen 122559fa6e7SJesper Juhl prof_buffer = vzalloc(buffer_bytes); 123559fa6e7SJesper Juhl if (prof_buffer) 12422b8ce94SDave Hansen return 0; 12522b8ce94SDave Hansen 126c309b917SRusty Russell free_cpumask_var(prof_cpu_mask); 12722b8ce94SDave Hansen return -ENOMEM; 1281da177e4SLinus Torvalds } 1291da177e4SLinus Torvalds 130ade356b9SArnd Bergmann #if defined(CONFIG_SMP) && defined(CONFIG_PROC_FS) 1311da177e4SLinus Torvalds /* 1321da177e4SLinus Torvalds * Each cpu has a pair of open-addressed hashtables for pending 1331da177e4SLinus Torvalds * profile hits. read_profile() IPI's all cpus to request them 1341da177e4SLinus Torvalds * to flip buffers and flushes their contents to prof_buffer itself. 1351da177e4SLinus Torvalds * Flip requests are serialized by the profile_flip_mutex. The sole 1361da177e4SLinus Torvalds * use of having a second hashtable is for avoiding cacheline 1371da177e4SLinus Torvalds * contention that would otherwise happen during flushes of pending 1381da177e4SLinus Torvalds * profile hits required for the accuracy of reported profile hits 1391da177e4SLinus Torvalds * and so resurrect the interrupt livelock issue. 1401da177e4SLinus Torvalds * 1411da177e4SLinus Torvalds * The open-addressed hashtables are indexed by profile buffer slot 1421da177e4SLinus Torvalds * and hold the number of pending hits to that profile buffer slot on 1431da177e4SLinus Torvalds * a cpu in an entry. When the hashtable overflows, all pending hits 1441da177e4SLinus Torvalds * are accounted to their corresponding profile buffer slots with 1451da177e4SLinus Torvalds * atomic_add() and the hashtable emptied. As numerous pending hits 1461da177e4SLinus Torvalds * may be accounted to a profile buffer slot in a hashtable entry, 1471da177e4SLinus Torvalds * this amortizes a number of atomic profile buffer increments likely 1481da177e4SLinus Torvalds * to be far larger than the number of entries in the hashtable, 1491da177e4SLinus Torvalds * particularly given that the number of distinct profile buffer 1501da177e4SLinus Torvalds * positions to which hits are accounted during short intervals (e.g. 1511da177e4SLinus Torvalds * several seconds) is usually very small. Exclusion from buffer 1521da177e4SLinus Torvalds * flipping is provided by interrupt disablement (note that for 153ece8a684SIngo Molnar * SCHED_PROFILING or SLEEP_PROFILING profile_hit() may be called from 154ece8a684SIngo Molnar * process context). 1551da177e4SLinus Torvalds * The hash function is meant to be lightweight as opposed to strong, 1561da177e4SLinus Torvalds * and was vaguely inspired by ppc64 firmware-supported inverted 1571da177e4SLinus Torvalds * pagetable hash functions, but uses a full hashtable full of finite 1581da177e4SLinus Torvalds * collision chains, not just pairs of them. 1591da177e4SLinus Torvalds * 1606d49e352SNadia Yvette Chambers * -- nyc 1611da177e4SLinus Torvalds */ 1621da177e4SLinus Torvalds static void __profile_flip_buffers(void *unused) 1631da177e4SLinus Torvalds { 1641da177e4SLinus Torvalds int cpu = smp_processor_id(); 1651da177e4SLinus Torvalds 1661da177e4SLinus Torvalds per_cpu(cpu_profile_flip, cpu) = !per_cpu(cpu_profile_flip, cpu); 1671da177e4SLinus Torvalds } 1681da177e4SLinus Torvalds 1691da177e4SLinus Torvalds static void profile_flip_buffers(void) 1701da177e4SLinus Torvalds { 1711da177e4SLinus Torvalds int i, j, cpu; 1721da177e4SLinus Torvalds 17397d1f15bSArjan van de Ven mutex_lock(&profile_flip_mutex); 1741da177e4SLinus Torvalds j = per_cpu(cpu_profile_flip, get_cpu()); 1751da177e4SLinus Torvalds put_cpu(); 17615c8b6c1SJens Axboe on_each_cpu(__profile_flip_buffers, NULL, 1); 1771da177e4SLinus Torvalds for_each_online_cpu(cpu) { 1781da177e4SLinus Torvalds struct profile_hit *hits = per_cpu(cpu_profile_hits, cpu)[j]; 1791da177e4SLinus Torvalds for (i = 0; i < NR_PROFILE_HIT; ++i) { 1801da177e4SLinus Torvalds if (!hits[i].hits) { 1811da177e4SLinus Torvalds if (hits[i].pc) 1821da177e4SLinus Torvalds hits[i].pc = 0; 1831da177e4SLinus Torvalds continue; 1841da177e4SLinus Torvalds } 1851da177e4SLinus Torvalds atomic_add(hits[i].hits, &prof_buffer[hits[i].pc]); 1861da177e4SLinus Torvalds hits[i].hits = hits[i].pc = 0; 1871da177e4SLinus Torvalds } 1881da177e4SLinus Torvalds } 18997d1f15bSArjan van de Ven mutex_unlock(&profile_flip_mutex); 1901da177e4SLinus Torvalds } 1911da177e4SLinus Torvalds 1921da177e4SLinus Torvalds static void profile_discard_flip_buffers(void) 1931da177e4SLinus Torvalds { 1941da177e4SLinus Torvalds int i, cpu; 1951da177e4SLinus Torvalds 19697d1f15bSArjan van de Ven mutex_lock(&profile_flip_mutex); 1971da177e4SLinus Torvalds i = per_cpu(cpu_profile_flip, get_cpu()); 1981da177e4SLinus Torvalds put_cpu(); 19915c8b6c1SJens Axboe on_each_cpu(__profile_flip_buffers, NULL, 1); 2001da177e4SLinus Torvalds for_each_online_cpu(cpu) { 2011da177e4SLinus Torvalds struct profile_hit *hits = per_cpu(cpu_profile_hits, cpu)[i]; 2021da177e4SLinus Torvalds memset(hits, 0, NR_PROFILE_HIT*sizeof(struct profile_hit)); 2031da177e4SLinus Torvalds } 20497d1f15bSArjan van de Ven mutex_unlock(&profile_flip_mutex); 2051da177e4SLinus Torvalds } 2061da177e4SLinus Torvalds 2076f7bd76fSRakib Mullick static void do_profile_hits(int type, void *__pc, unsigned int nr_hits) 2081da177e4SLinus Torvalds { 2091da177e4SLinus Torvalds unsigned long primary, secondary, flags, pc = (unsigned long)__pc; 2101da177e4SLinus Torvalds int i, j, cpu; 2111da177e4SLinus Torvalds struct profile_hit *hits; 2121da177e4SLinus Torvalds 2131da177e4SLinus Torvalds pc = min((pc - (unsigned long)_stext) >> prof_shift, prof_len - 1); 2141da177e4SLinus Torvalds i = primary = (pc & (NR_PROFILE_GRP - 1)) << PROFILE_GRPSHIFT; 2151da177e4SLinus Torvalds secondary = (~(pc << 1) & (NR_PROFILE_GRP - 1)) << PROFILE_GRPSHIFT; 2161da177e4SLinus Torvalds cpu = get_cpu(); 2171da177e4SLinus Torvalds hits = per_cpu(cpu_profile_hits, cpu)[per_cpu(cpu_profile_flip, cpu)]; 2181da177e4SLinus Torvalds if (!hits) { 2191da177e4SLinus Torvalds put_cpu(); 2201da177e4SLinus Torvalds return; 2211da177e4SLinus Torvalds } 222ece8a684SIngo Molnar /* 223ece8a684SIngo Molnar * We buffer the global profiler buffer into a per-CPU 224ece8a684SIngo Molnar * queue and thus reduce the number of global (and possibly 225ece8a684SIngo Molnar * NUMA-alien) accesses. The write-queue is self-coalescing: 226ece8a684SIngo Molnar */ 2271da177e4SLinus Torvalds local_irq_save(flags); 2281da177e4SLinus Torvalds do { 2291da177e4SLinus Torvalds for (j = 0; j < PROFILE_GRPSZ; ++j) { 2301da177e4SLinus Torvalds if (hits[i + j].pc == pc) { 231ece8a684SIngo Molnar hits[i + j].hits += nr_hits; 2321da177e4SLinus Torvalds goto out; 2331da177e4SLinus Torvalds } else if (!hits[i + j].hits) { 2341da177e4SLinus Torvalds hits[i + j].pc = pc; 235ece8a684SIngo Molnar hits[i + j].hits = nr_hits; 2361da177e4SLinus Torvalds goto out; 2371da177e4SLinus Torvalds } 2381da177e4SLinus Torvalds } 2391da177e4SLinus Torvalds i = (i + secondary) & (NR_PROFILE_HIT - 1); 2401da177e4SLinus Torvalds } while (i != primary); 241ece8a684SIngo Molnar 242ece8a684SIngo Molnar /* 243ece8a684SIngo Molnar * Add the current hit(s) and flush the write-queue out 244ece8a684SIngo Molnar * to the global buffer: 245ece8a684SIngo Molnar */ 246ece8a684SIngo Molnar atomic_add(nr_hits, &prof_buffer[pc]); 2471da177e4SLinus Torvalds for (i = 0; i < NR_PROFILE_HIT; ++i) { 2481da177e4SLinus Torvalds atomic_add(hits[i].hits, &prof_buffer[hits[i].pc]); 2491da177e4SLinus Torvalds hits[i].pc = hits[i].hits = 0; 2501da177e4SLinus Torvalds } 2511da177e4SLinus Torvalds out: 2521da177e4SLinus Torvalds local_irq_restore(flags); 2531da177e4SLinus Torvalds put_cpu(); 2541da177e4SLinus Torvalds } 2551da177e4SLinus Torvalds 256e722d8daSSebastian Andrzej Siewior static int profile_dead_cpu(unsigned int cpu) 2571da177e4SLinus Torvalds { 2581da177e4SLinus Torvalds struct page *page; 259e722d8daSSebastian Andrzej Siewior int i; 2601da177e4SLinus Torvalds 261ef70eff9SNathan Chancellor if (cpumask_available(prof_cpu_mask)) 262c309b917SRusty Russell cpumask_clear_cpu(cpu, prof_cpu_mask); 263e722d8daSSebastian Andrzej Siewior 264e722d8daSSebastian Andrzej Siewior for (i = 0; i < 2; i++) { 265e722d8daSSebastian Andrzej Siewior if (per_cpu(cpu_profile_hits, cpu)[i]) { 266e722d8daSSebastian Andrzej Siewior page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[i]); 267e722d8daSSebastian Andrzej Siewior per_cpu(cpu_profile_hits, cpu)[i] = NULL; 2681da177e4SLinus Torvalds __free_page(page); 2691da177e4SLinus Torvalds } 2701da177e4SLinus Torvalds } 271e722d8daSSebastian Andrzej Siewior return 0; 2721da177e4SLinus Torvalds } 273e722d8daSSebastian Andrzej Siewior 274e722d8daSSebastian Andrzej Siewior static int profile_prepare_cpu(unsigned int cpu) 275e722d8daSSebastian Andrzej Siewior { 276e722d8daSSebastian Andrzej Siewior int i, node = cpu_to_mem(cpu); 277e722d8daSSebastian Andrzej Siewior struct page *page; 278e722d8daSSebastian Andrzej Siewior 279e722d8daSSebastian Andrzej Siewior per_cpu(cpu_profile_flip, cpu) = 0; 280e722d8daSSebastian Andrzej Siewior 281e722d8daSSebastian Andrzej Siewior for (i = 0; i < 2; i++) { 282e722d8daSSebastian Andrzej Siewior if (per_cpu(cpu_profile_hits, cpu)[i]) 283e722d8daSSebastian Andrzej Siewior continue; 284e722d8daSSebastian Andrzej Siewior 285e722d8daSSebastian Andrzej Siewior page = __alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0); 286e722d8daSSebastian Andrzej Siewior if (!page) { 287e722d8daSSebastian Andrzej Siewior profile_dead_cpu(cpu); 288e722d8daSSebastian Andrzej Siewior return -ENOMEM; 2891da177e4SLinus Torvalds } 290e722d8daSSebastian Andrzej Siewior per_cpu(cpu_profile_hits, cpu)[i] = page_address(page); 291e722d8daSSebastian Andrzej Siewior 292e722d8daSSebastian Andrzej Siewior } 293e722d8daSSebastian Andrzej Siewior return 0; 294e722d8daSSebastian Andrzej Siewior } 295e722d8daSSebastian Andrzej Siewior 296e722d8daSSebastian Andrzej Siewior static int profile_online_cpu(unsigned int cpu) 297e722d8daSSebastian Andrzej Siewior { 298ef70eff9SNathan Chancellor if (cpumask_available(prof_cpu_mask)) 299e722d8daSSebastian Andrzej Siewior cpumask_set_cpu(cpu, prof_cpu_mask); 300e722d8daSSebastian Andrzej Siewior 301e722d8daSSebastian Andrzej Siewior return 0; 302e722d8daSSebastian Andrzej Siewior } 303e722d8daSSebastian Andrzej Siewior 3041da177e4SLinus Torvalds #else /* !CONFIG_SMP */ 3051da177e4SLinus Torvalds #define profile_flip_buffers() do { } while (0) 3061da177e4SLinus Torvalds #define profile_discard_flip_buffers() do { } while (0) 3071da177e4SLinus Torvalds 3086f7bd76fSRakib Mullick static void do_profile_hits(int type, void *__pc, unsigned int nr_hits) 3091da177e4SLinus Torvalds { 3101da177e4SLinus Torvalds unsigned long pc; 3111da177e4SLinus Torvalds pc = ((unsigned long)__pc - (unsigned long)_stext) >> prof_shift; 312ece8a684SIngo Molnar atomic_add(nr_hits, &prof_buffer[min(pc, prof_len - 1)]); 3131da177e4SLinus Torvalds } 3141da177e4SLinus Torvalds #endif /* !CONFIG_SMP */ 3156f7bd76fSRakib Mullick 3166f7bd76fSRakib Mullick void profile_hits(int type, void *__pc, unsigned int nr_hits) 3176f7bd76fSRakib Mullick { 3186f7bd76fSRakib Mullick if (prof_on != type || !prof_buffer) 3196f7bd76fSRakib Mullick return; 3206f7bd76fSRakib Mullick do_profile_hits(type, __pc, nr_hits); 3216f7bd76fSRakib Mullick } 322bbe1a59bSAndrew Morton EXPORT_SYMBOL_GPL(profile_hits); 323bbe1a59bSAndrew Morton 3247d12e780SDavid Howells void profile_tick(int type) 3251da177e4SLinus Torvalds { 3267d12e780SDavid Howells struct pt_regs *regs = get_irq_regs(); 3277d12e780SDavid Howells 328ef70eff9SNathan Chancellor if (!user_mode(regs) && cpumask_available(prof_cpu_mask) && 329c309b917SRusty Russell cpumask_test_cpu(smp_processor_id(), prof_cpu_mask)) 3301da177e4SLinus Torvalds profile_hit(type, (void *)profile_pc(regs)); 3311da177e4SLinus Torvalds } 3321da177e4SLinus Torvalds 3331da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 3341da177e4SLinus Torvalds #include <linux/proc_fs.h> 335583a22e7SAlexey Dobriyan #include <linux/seq_file.h> 3367c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 3371da177e4SLinus Torvalds 338583a22e7SAlexey Dobriyan static int prof_cpu_mask_proc_show(struct seq_file *m, void *v) 3391da177e4SLinus Torvalds { 340ccbd59c1STejun Heo seq_printf(m, "%*pb\n", cpumask_pr_args(prof_cpu_mask)); 341583a22e7SAlexey Dobriyan return 0; 3421da177e4SLinus Torvalds } 3431da177e4SLinus Torvalds 344583a22e7SAlexey Dobriyan static int prof_cpu_mask_proc_open(struct inode *inode, struct file *file) 3451da177e4SLinus Torvalds { 346583a22e7SAlexey Dobriyan return single_open(file, prof_cpu_mask_proc_show, NULL); 347583a22e7SAlexey Dobriyan } 348583a22e7SAlexey Dobriyan 349583a22e7SAlexey Dobriyan static ssize_t prof_cpu_mask_proc_write(struct file *file, 350583a22e7SAlexey Dobriyan const char __user *buffer, size_t count, loff_t *pos) 351583a22e7SAlexey Dobriyan { 352c309b917SRusty Russell cpumask_var_t new_value; 353583a22e7SAlexey Dobriyan int err; 3541da177e4SLinus Torvalds 355c5e3a411STetsuo Handa if (!zalloc_cpumask_var(&new_value, GFP_KERNEL)) 356c309b917SRusty Russell return -ENOMEM; 357c309b917SRusty Russell 358c309b917SRusty Russell err = cpumask_parse_user(buffer, count, new_value); 359c309b917SRusty Russell if (!err) { 360583a22e7SAlexey Dobriyan cpumask_copy(prof_cpu_mask, new_value); 361583a22e7SAlexey Dobriyan err = count; 362c309b917SRusty Russell } 363c309b917SRusty Russell free_cpumask_var(new_value); 3641da177e4SLinus Torvalds return err; 3651da177e4SLinus Torvalds } 3661da177e4SLinus Torvalds 36797a32539SAlexey Dobriyan static const struct proc_ops prof_cpu_mask_proc_ops = { 36897a32539SAlexey Dobriyan .proc_open = prof_cpu_mask_proc_open, 36997a32539SAlexey Dobriyan .proc_read = seq_read, 37097a32539SAlexey Dobriyan .proc_lseek = seq_lseek, 37197a32539SAlexey Dobriyan .proc_release = single_release, 37297a32539SAlexey Dobriyan .proc_write = prof_cpu_mask_proc_write, 373583a22e7SAlexey Dobriyan }; 374583a22e7SAlexey Dobriyan 375fbd387aeSAl Viro void create_prof_cpu_mask(void) 3761da177e4SLinus Torvalds { 3771da177e4SLinus Torvalds /* create /proc/irq/prof_cpu_mask */ 37897a32539SAlexey Dobriyan proc_create("irq/prof_cpu_mask", 0600, NULL, &prof_cpu_mask_proc_ops); 3791da177e4SLinus Torvalds } 3801da177e4SLinus Torvalds 3811da177e4SLinus Torvalds /* 3821da177e4SLinus Torvalds * This function accesses profiling information. The returned data is 3831da177e4SLinus Torvalds * binary: the sampling step and the actual contents of the profile 3841da177e4SLinus Torvalds * buffer. Use of the program readprofile is recommended in order to 3851da177e4SLinus Torvalds * get meaningful info out of these data. 3861da177e4SLinus Torvalds */ 3871da177e4SLinus Torvalds static ssize_t 3881da177e4SLinus Torvalds read_profile(struct file *file, char __user *buf, size_t count, loff_t *ppos) 3891da177e4SLinus Torvalds { 3901da177e4SLinus Torvalds unsigned long p = *ppos; 3911da177e4SLinus Torvalds ssize_t read; 3921da177e4SLinus Torvalds char *pnt; 3932d186afdSPavel Skripkin unsigned long sample_step = 1UL << prof_shift; 3941da177e4SLinus Torvalds 3951da177e4SLinus Torvalds profile_flip_buffers(); 3961da177e4SLinus Torvalds if (p >= (prof_len+1)*sizeof(unsigned int)) 3971da177e4SLinus Torvalds return 0; 3981da177e4SLinus Torvalds if (count > (prof_len+1)*sizeof(unsigned int) - p) 3991da177e4SLinus Torvalds count = (prof_len+1)*sizeof(unsigned int) - p; 4001da177e4SLinus Torvalds read = 0; 4011da177e4SLinus Torvalds 4021da177e4SLinus Torvalds while (p < sizeof(unsigned int) && count > 0) { 403064b022cSHeiko Carstens if (put_user(*((char *)(&sample_step)+p), buf)) 404064b022cSHeiko Carstens return -EFAULT; 4051da177e4SLinus Torvalds buf++; p++; count--; read++; 4061da177e4SLinus Torvalds } 4071da177e4SLinus Torvalds pnt = (char *)prof_buffer + p - sizeof(atomic_t); 4081da177e4SLinus Torvalds if (copy_to_user(buf, (void *)pnt, count)) 4091da177e4SLinus Torvalds return -EFAULT; 4101da177e4SLinus Torvalds read += count; 4111da177e4SLinus Torvalds *ppos += read; 4121da177e4SLinus Torvalds return read; 4131da177e4SLinus Torvalds } 4141da177e4SLinus Torvalds 415787dbea1SBen Dooks /* default is to not implement this call */ 416787dbea1SBen Dooks int __weak setup_profiling_timer(unsigned mult) 417787dbea1SBen Dooks { 418787dbea1SBen Dooks return -EINVAL; 419787dbea1SBen Dooks } 420787dbea1SBen Dooks 4211da177e4SLinus Torvalds /* 4221da177e4SLinus Torvalds * Writing to /proc/profile resets the counters 4231da177e4SLinus Torvalds * 4241da177e4SLinus Torvalds * Writing a 'profiling multiplier' value into it also re-sets the profiling 4251da177e4SLinus Torvalds * interrupt frequency, on architectures that support this. 4261da177e4SLinus Torvalds */ 4271da177e4SLinus Torvalds static ssize_t write_profile(struct file *file, const char __user *buf, 4281da177e4SLinus Torvalds size_t count, loff_t *ppos) 4291da177e4SLinus Torvalds { 4301da177e4SLinus Torvalds #ifdef CONFIG_SMP 4311da177e4SLinus Torvalds if (count == sizeof(int)) { 4321da177e4SLinus Torvalds unsigned int multiplier; 4331da177e4SLinus Torvalds 4341da177e4SLinus Torvalds if (copy_from_user(&multiplier, buf, sizeof(int))) 4351da177e4SLinus Torvalds return -EFAULT; 4361da177e4SLinus Torvalds 4371da177e4SLinus Torvalds if (setup_profiling_timer(multiplier)) 4381da177e4SLinus Torvalds return -EINVAL; 4391da177e4SLinus Torvalds } 4401da177e4SLinus Torvalds #endif 4411da177e4SLinus Torvalds profile_discard_flip_buffers(); 4421da177e4SLinus Torvalds memset(prof_buffer, 0, prof_len * sizeof(atomic_t)); 4431da177e4SLinus Torvalds return count; 4441da177e4SLinus Torvalds } 4451da177e4SLinus Torvalds 44697a32539SAlexey Dobriyan static const struct proc_ops profile_proc_ops = { 44797a32539SAlexey Dobriyan .proc_read = read_profile, 44897a32539SAlexey Dobriyan .proc_write = write_profile, 44997a32539SAlexey Dobriyan .proc_lseek = default_llseek, 4501da177e4SLinus Torvalds }; 4511da177e4SLinus Torvalds 452e722d8daSSebastian Andrzej Siewior int __ref create_proc_profile(void) 4531da177e4SLinus Torvalds { 4541da177e4SLinus Torvalds struct proc_dir_entry *entry; 455e722d8daSSebastian Andrzej Siewior #ifdef CONFIG_SMP 456e722d8daSSebastian Andrzej Siewior enum cpuhp_state online_state; 457e722d8daSSebastian Andrzej Siewior #endif 458e722d8daSSebastian Andrzej Siewior 459c270a817SSrivatsa S. Bhat int err = 0; 4601da177e4SLinus Torvalds 4611da177e4SLinus Torvalds if (!prof_on) 4621da177e4SLinus Torvalds return 0; 463e722d8daSSebastian Andrzej Siewior #ifdef CONFIG_SMP 464e722d8daSSebastian Andrzej Siewior err = cpuhp_setup_state(CPUHP_PROFILE_PREPARE, "PROFILE_PREPARE", 465e722d8daSSebastian Andrzej Siewior profile_prepare_cpu, profile_dead_cpu); 466e722d8daSSebastian Andrzej Siewior if (err) 467e722d8daSSebastian Andrzej Siewior return err; 468c270a817SSrivatsa S. Bhat 469e722d8daSSebastian Andrzej Siewior err = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "AP_PROFILE_ONLINE", 470e722d8daSSebastian Andrzej Siewior profile_online_cpu, NULL); 471e722d8daSSebastian Andrzej Siewior if (err < 0) 472e722d8daSSebastian Andrzej Siewior goto err_state_prep; 473e722d8daSSebastian Andrzej Siewior online_state = err; 474e722d8daSSebastian Andrzej Siewior err = 0; 475e722d8daSSebastian Andrzej Siewior #endif 476c33fff0aSDenis V. Lunev entry = proc_create("profile", S_IWUSR | S_IRUGO, 47797a32539SAlexey Dobriyan NULL, &profile_proc_ops); 4781ad82fd5SPaolo Ciarrocchi if (!entry) 479e722d8daSSebastian Andrzej Siewior goto err_state_onl; 480271a15eaSDavid Howells proc_set_size(entry, (1 + prof_len) * sizeof(atomic_t)); 481c270a817SSrivatsa S. Bhat 482e722d8daSSebastian Andrzej Siewior return err; 483e722d8daSSebastian Andrzej Siewior err_state_onl: 484e722d8daSSebastian Andrzej Siewior #ifdef CONFIG_SMP 485e722d8daSSebastian Andrzej Siewior cpuhp_remove_state(online_state); 486e722d8daSSebastian Andrzej Siewior err_state_prep: 487e722d8daSSebastian Andrzej Siewior cpuhp_remove_state(CPUHP_PROFILE_PREPARE); 488e722d8daSSebastian Andrzej Siewior #endif 489c270a817SSrivatsa S. Bhat return err; 4901da177e4SLinus Torvalds } 491c96d6660SPaul Gortmaker subsys_initcall(create_proc_profile); 4921da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 493