blob: b6619ae9a3e0cbdf24ab2de2e33929e187d436f7 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * S/390 debug facility
4 *
Mikhail Zaslonko0990d832020-05-05 10:34:52 +02005 * Copyright IBM Corp. 1999, 2020
Michael Holzheu3ab121a2012-03-11 11:59:32 -04006 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Author(s): Michael Holzheu (holzheu@de.ibm.com),
Heiko Carstens496da0d2017-10-13 09:06:29 +02008 * Holger Smolinski (Holger.Smolinski@de.ibm.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * Bugreports to: <Linux390@de.ibm.com>
11 */
12
Michael Holzheuc5612c12008-12-25 13:39:43 +010013#define KMSG_COMPONENT "s390dbf"
14#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/stddef.h>
17#include <linux/kernel.h>
18#include <linux/errno.h>
19#include <linux/slab.h>
20#include <linux/ctype.h>
André Goddard Rosae7d28602009-12-14 18:01:06 -080021#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/sysctl.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080023#include <linux/uaccess.h>
Paul Gortmaker3994a522017-02-09 15:20:23 -050024#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/init.h>
Michael Holzheu66a464d2005-06-25 14:55:33 -070026#include <linux/fs.h>
27#include <linux/debugfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29#include <asm/debug.h>
30
31#define DEBUG_PROLOG_ENTRY -1
32
Michael Holzheu66a464d2005-06-25 14:55:33 -070033#define ALL_AREAS 0 /* copy all debug areas */
34#define NO_AREAS 1 /* copy no debug areas */
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036/* typedefs */
37
38typedef struct file_private_info {
39 loff_t offset; /* offset of last read in file */
Heiko Carstens496da0d2017-10-13 09:06:29 +020040 int act_area; /* number of last formated area */
41 int act_page; /* act page in given area */
42 int act_entry; /* last formated entry (offset */
43 /* relative to beginning of last */
44 /* formated page) */
45 size_t act_entry_offset; /* up to this offset we copied */
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 /* in last read the last formated */
47 /* entry to userland */
48 char temp_buf[2048]; /* buffer for output */
Heiko Carstens496da0d2017-10-13 09:06:29 +020049 debug_info_t *debug_info_org; /* original debug information */
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 debug_info_t *debug_info_snap; /* snapshot of debug information */
51 struct debug_view *view; /* used view of debug info */
52} file_private_info_t;
53
Heiko Carstens496da0d2017-10-13 09:06:29 +020054typedef struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 char *string;
Heiko Carstens496da0d2017-10-13 09:06:29 +020056 /*
57 * This assumes that all args are converted into longs
58 * on L/390 this is the case for all types of parameter
59 * except of floats, and long long (32 bit)
Michael Holzheu66a464d2005-06-25 14:55:33 -070060 *
61 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 long args[0];
63} debug_sprintf_entry_t;
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065/* internal function prototyes */
66
67static int debug_init(void);
68static ssize_t debug_output(struct file *file, char __user *user_buf,
Heiko Carstens496da0d2017-10-13 09:06:29 +020069 size_t user_len, loff_t *offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070static ssize_t debug_input(struct file *file, const char __user *user_buf,
Heiko Carstens496da0d2017-10-13 09:06:29 +020071 size_t user_len, loff_t *offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072static int debug_open(struct inode *inode, struct file *file);
73static int debug_close(struct inode *inode, struct file *file);
Cornelia Huck5cbbf162008-05-15 16:52:35 +020074static debug_info_t *debug_info_create(const char *name, int pages_per_area,
Heiko Carstens496da0d2017-10-13 09:06:29 +020075 int nr_areas, int buf_size, umode_t mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076static void debug_info_get(debug_info_t *);
77static void debug_info_put(debug_info_t *);
Heiko Carstens496da0d2017-10-13 09:06:29 +020078static int debug_prolog_level_fn(debug_info_t *id,
79 struct debug_view *view, char *out_buf);
80static int debug_input_level_fn(debug_info_t *id, struct debug_view *view,
81 struct file *file, const char __user *user_buf,
82 size_t user_buf_size, loff_t *offset);
83static int debug_prolog_pages_fn(debug_info_t *id,
84 struct debug_view *view, char *out_buf);
85static int debug_input_pages_fn(debug_info_t *id, struct debug_view *view,
86 struct file *file, const char __user *user_buf,
87 size_t user_buf_size, loff_t *offset);
88static int debug_input_flush_fn(debug_info_t *id, struct debug_view *view,
89 struct file *file, const char __user *user_buf,
90 size_t user_buf_size, loff_t *offset);
91static int debug_hex_ascii_format_fn(debug_info_t *id, struct debug_view *view,
92 char *out_buf, const char *in_buf);
Heiko Carstens496da0d2017-10-13 09:06:29 +020093static int debug_sprintf_format_fn(debug_info_t *id, struct debug_view *view,
94 char *out_buf, debug_sprintf_entry_t *curr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96/* globals */
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098struct debug_view debug_hex_ascii_view = {
99 "hex_ascii",
100 NULL,
101 &debug_dflt_header_fn,
102 &debug_hex_ascii_format_fn,
103 NULL,
104 NULL
105};
Heiko Carstensd5274362012-07-24 14:27:53 +0200106EXPORT_SYMBOL(debug_hex_ascii_view);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100108static struct debug_view debug_level_view = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 "level",
110 &debug_prolog_level_fn,
111 NULL,
112 NULL,
113 &debug_input_level_fn,
114 NULL
115};
116
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100117static struct debug_view debug_pages_view = {
Michael Holzheu66a464d2005-06-25 14:55:33 -0700118 "pages",
119 &debug_prolog_pages_fn,
120 NULL,
121 NULL,
122 &debug_input_pages_fn,
123 NULL
124};
125
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100126static struct debug_view debug_flush_view = {
Heiko Carstens496da0d2017-10-13 09:06:29 +0200127 "flush",
128 NULL,
129 NULL,
130 NULL,
131 &debug_input_flush_fn,
132 NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133};
134
135struct debug_view debug_sprintf_view = {
136 "sprintf",
137 NULL,
138 &debug_dflt_header_fn,
Heiko Carstens496da0d2017-10-13 09:06:29 +0200139 (debug_format_proc_t *)&debug_sprintf_format_fn,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 NULL,
141 NULL
142};
Heiko Carstensd5274362012-07-24 14:27:53 +0200143EXPORT_SYMBOL(debug_sprintf_view);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100145/* used by dump analysis tools to determine version of debug feature */
Heiko Carstensa8061702008-04-17 07:46:26 +0200146static unsigned int __used debug_feature_version = __DEBUG_FEATURE_VERSION;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148/* static globals */
149
Heiko Carstens496da0d2017-10-13 09:06:29 +0200150static debug_info_t *debug_area_first;
151static debug_info_t *debug_area_last;
Christoph Hellwige11f0d02007-05-31 17:38:03 +0200152static DEFINE_MUTEX(debug_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154static int initialized;
Michael Holzheu3ab121a2012-03-11 11:59:32 -0400155static int debug_critical;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800157static const struct file_operations debug_file_ops = {
Heiko Carstens496da0d2017-10-13 09:06:29 +0200158 .owner = THIS_MODULE,
159 .read = debug_output,
160 .write = debug_input,
161 .open = debug_open,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 .release = debug_close,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200163 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164};
165
Michael Holzheu66a464d2005-06-25 14:55:33 -0700166static struct dentry *debug_debugfs_root_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168/* functions */
169
170/*
Michael Holzheu66a464d2005-06-25 14:55:33 -0700171 * debug_areas_alloc
172 * - Debug areas are implemented as a threedimensonal array:
173 * areas[areanumber][pagenumber][pageoffset]
174 */
175
Heiko Carstens496da0d2017-10-13 09:06:29 +0200176static debug_entry_t ***debug_areas_alloc(int pages_per_area, int nr_areas)
Michael Holzheu66a464d2005-06-25 14:55:33 -0700177{
Heiko Carstens496da0d2017-10-13 09:06:29 +0200178 debug_entry_t ***areas;
179 int i, j;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700180
Kees Cook6da2ec52018-06-12 13:55:00 -0700181 areas = kmalloc_array(nr_areas, sizeof(debug_entry_t **), GFP_KERNEL);
Michael Holzheu66a464d2005-06-25 14:55:33 -0700182 if (!areas)
183 goto fail_malloc_areas;
184 for (i = 0; i < nr_areas; i++) {
Christian Borntraeger827c4912020-03-31 05:57:23 -0400185 /* GFP_NOWARN to avoid user triggerable WARN, we handle fails */
Kees Cook6da2ec52018-06-12 13:55:00 -0700186 areas[i] = kmalloc_array(pages_per_area,
187 sizeof(debug_entry_t *),
Christian Borntraeger827c4912020-03-31 05:57:23 -0400188 GFP_KERNEL | __GFP_NOWARN);
Heiko Carstens496da0d2017-10-13 09:06:29 +0200189 if (!areas[i])
Michael Holzheu66a464d2005-06-25 14:55:33 -0700190 goto fail_malloc_areas2;
Heiko Carstens496da0d2017-10-13 09:06:29 +0200191 for (j = 0; j < pages_per_area; j++) {
Eric Sesterhennfb630512006-03-24 03:15:31 -0800192 areas[i][j] = kzalloc(PAGE_SIZE, GFP_KERNEL);
Heiko Carstens496da0d2017-10-13 09:06:29 +0200193 if (!areas[i][j]) {
194 for (j--; j >= 0 ; j--)
Michael Holzheu66a464d2005-06-25 14:55:33 -0700195 kfree(areas[i][j]);
Michael Holzheu66a464d2005-06-25 14:55:33 -0700196 kfree(areas[i]);
197 goto fail_malloc_areas2;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700198 }
199 }
200 }
201 return areas;
202
203fail_malloc_areas2:
Heiko Carstens496da0d2017-10-13 09:06:29 +0200204 for (i--; i >= 0; i--) {
205 for (j = 0; j < pages_per_area; j++)
Michael Holzheu66a464d2005-06-25 14:55:33 -0700206 kfree(areas[i][j]);
Michael Holzheu66a464d2005-06-25 14:55:33 -0700207 kfree(areas[i]);
208 }
209 kfree(areas);
210fail_malloc_areas:
211 return NULL;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700212}
213
Michael Holzheu66a464d2005-06-25 14:55:33 -0700214/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 * debug_info_alloc
216 * - alloc new debug-info
217 */
Heiko Carstens496da0d2017-10-13 09:06:29 +0200218static debug_info_t *debug_info_alloc(const char *name, int pages_per_area,
219 int nr_areas, int buf_size, int level,
220 int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
Heiko Carstens496da0d2017-10-13 09:06:29 +0200222 debug_info_t *rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 /* alloc everything */
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800225 rc = kmalloc(sizeof(debug_info_t), GFP_KERNEL);
Heiko Carstens496da0d2017-10-13 09:06:29 +0200226 if (!rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 goto fail_malloc_rc;
Eric Sesterhennfb630512006-03-24 03:15:31 -0800228 rc->active_entries = kcalloc(nr_areas, sizeof(int), GFP_KERNEL);
Heiko Carstens496da0d2017-10-13 09:06:29 +0200229 if (!rc->active_entries)
Michael Holzheu66a464d2005-06-25 14:55:33 -0700230 goto fail_malloc_active_entries;
Eric Sesterhennfb630512006-03-24 03:15:31 -0800231 rc->active_pages = kcalloc(nr_areas, sizeof(int), GFP_KERNEL);
Heiko Carstens496da0d2017-10-13 09:06:29 +0200232 if (!rc->active_pages)
Michael Holzheu66a464d2005-06-25 14:55:33 -0700233 goto fail_malloc_active_pages;
Heiko Carstens496da0d2017-10-13 09:06:29 +0200234 if ((mode == ALL_AREAS) && (pages_per_area != 0)) {
Michael Holzheu66a464d2005-06-25 14:55:33 -0700235 rc->areas = debug_areas_alloc(pages_per_area, nr_areas);
Heiko Carstens496da0d2017-10-13 09:06:29 +0200236 if (!rc->areas)
Michael Holzheu66a464d2005-06-25 14:55:33 -0700237 goto fail_malloc_areas;
238 } else {
239 rc->areas = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 }
241
242 /* initialize members */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 spin_lock_init(&rc->lock);
Michael Holzheu66a464d2005-06-25 14:55:33 -0700244 rc->pages_per_area = pages_per_area;
Heiko Carstens496da0d2017-10-13 09:06:29 +0200245 rc->nr_areas = nr_areas;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700246 rc->active_area = 0;
Heiko Carstens496da0d2017-10-13 09:06:29 +0200247 rc->level = level;
248 rc->buf_size = buf_size;
249 rc->entry_size = sizeof(debug_entry_t) + buf_size;
Jean Delvare20cb9e72007-03-19 13:18:53 +0100250 strlcpy(rc->name, name, sizeof(rc->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 memset(rc->views, 0, DEBUG_MAX_VIEWS * sizeof(struct debug_view *));
Heiko Carstens496da0d2017-10-13 09:06:29 +0200252 memset(rc->debugfs_entries, 0, DEBUG_MAX_VIEWS * sizeof(struct dentry *));
Elena Reshetovaefc0c212017-03-02 12:23:45 +0100253 refcount_set(&(rc->ref_count), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 return rc;
256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257fail_malloc_areas:
Michael Holzheu66a464d2005-06-25 14:55:33 -0700258 kfree(rc->active_pages);
259fail_malloc_active_pages:
260 kfree(rc->active_entries);
261fail_malloc_active_entries:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 kfree(rc);
263fail_malloc_rc:
264 return NULL;
265}
266
267/*
Michael Holzheu66a464d2005-06-25 14:55:33 -0700268 * debug_areas_free
269 * - free all debug areas
270 */
Heiko Carstens496da0d2017-10-13 09:06:29 +0200271static void debug_areas_free(debug_info_t *db_info)
Michael Holzheu66a464d2005-06-25 14:55:33 -0700272{
Heiko Carstens496da0d2017-10-13 09:06:29 +0200273 int i, j;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700274
Heiko Carstens496da0d2017-10-13 09:06:29 +0200275 if (!db_info->areas)
Michael Holzheu66a464d2005-06-25 14:55:33 -0700276 return;
277 for (i = 0; i < db_info->nr_areas; i++) {
Heiko Carstens496da0d2017-10-13 09:06:29 +0200278 for (j = 0; j < db_info->pages_per_area; j++)
Michael Holzheu66a464d2005-06-25 14:55:33 -0700279 kfree(db_info->areas[i][j]);
Michael Holzheu66a464d2005-06-25 14:55:33 -0700280 kfree(db_info->areas[i]);
281 }
282 kfree(db_info->areas);
283 db_info->areas = NULL;
284}
285
286/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 * debug_info_free
288 * - free memory debug-info
289 */
Heiko Carstens496da0d2017-10-13 09:06:29 +0200290static void debug_info_free(debug_info_t *db_info)
291{
Michael Holzheu66a464d2005-06-25 14:55:33 -0700292 debug_areas_free(db_info);
293 kfree(db_info->active_entries);
294 kfree(db_info->active_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 kfree(db_info);
296}
297
298/*
299 * debug_info_create
300 * - create new debug-info
301 */
302
Heiko Carstens496da0d2017-10-13 09:06:29 +0200303static debug_info_t *debug_info_create(const char *name, int pages_per_area,
304 int nr_areas, int buf_size, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
Heiko Carstens496da0d2017-10-13 09:06:29 +0200306 debug_info_t *rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Heiko Carstens496da0d2017-10-13 09:06:29 +0200308 rc = debug_info_alloc(name, pages_per_area, nr_areas, buf_size,
309 DEBUG_DEFAULT_LEVEL, ALL_AREAS);
310 if (!rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 goto out;
312
Michael Holzheu9637c3f2008-04-17 07:46:18 +0200313 rc->mode = mode & ~S_IFMT;
314
Michael Holzheu66a464d2005-06-25 14:55:33 -0700315 /* create root directory */
Heiko Carstens496da0d2017-10-13 09:06:29 +0200316 rc->debugfs_root_entry = debugfs_create_dir(rc->name,
317 debug_debugfs_root_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319 /* append new element to linked list */
Heiko Carstens496da0d2017-10-13 09:06:29 +0200320 if (!debug_area_first) {
321 /* first element in list */
322 debug_area_first = rc;
323 rc->prev = NULL;
324 } else {
325 /* append element to end of list */
326 debug_area_last->next = rc;
327 rc->prev = debug_area_last;
328 }
329 debug_area_last = rc;
330 rc->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Elena Reshetovaefc0c212017-03-02 12:23:45 +0100332 refcount_set(&rc->ref_count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333out:
334 return rc;
335}
336
337/*
338 * debug_info_copy
339 * - copy debug-info
340 */
Heiko Carstens496da0d2017-10-13 09:06:29 +0200341static debug_info_t *debug_info_copy(debug_info_t *in, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
Heiko Carstens496da0d2017-10-13 09:06:29 +0200343 unsigned long flags;
344 debug_info_t *rc;
345 int i, j;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700346
Michael Holzheu942eaab2005-09-03 15:57:58 -0700347 /* get a consistent copy of the debug areas */
348 do {
349 rc = debug_info_alloc(in->name, in->pages_per_area,
350 in->nr_areas, in->buf_size, in->level, mode);
351 spin_lock_irqsave(&in->lock, flags);
Heiko Carstens496da0d2017-10-13 09:06:29 +0200352 if (!rc)
Michael Holzheu942eaab2005-09-03 15:57:58 -0700353 goto out;
354 /* has something changed in the meantime ? */
Heiko Carstens496da0d2017-10-13 09:06:29 +0200355 if ((rc->pages_per_area == in->pages_per_area) &&
356 (rc->nr_areas == in->nr_areas)) {
Michael Holzheu942eaab2005-09-03 15:57:58 -0700357 break;
358 }
359 spin_unlock_irqrestore(&in->lock, flags);
360 debug_info_free(rc);
361 } while (1);
362
Julia Lawallacfa9222008-12-25 13:39:30 +0100363 if (mode == NO_AREAS)
Heiko Carstens496da0d2017-10-13 09:06:29 +0200364 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Heiko Carstens496da0d2017-10-13 09:06:29 +0200366 for (i = 0; i < in->nr_areas; i++) {
367 for (j = 0; j < in->pages_per_area; j++)
368 memcpy(rc->areas[i][j], in->areas[i][j], PAGE_SIZE);
369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370out:
Heiko Carstens496da0d2017-10-13 09:06:29 +0200371 spin_unlock_irqrestore(&in->lock, flags);
372 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
374
375/*
376 * debug_info_get
377 * - increments reference count for debug-info
378 */
Heiko Carstens496da0d2017-10-13 09:06:29 +0200379static void debug_info_get(debug_info_t *db_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
381 if (db_info)
Elena Reshetovaefc0c212017-03-02 12:23:45 +0100382 refcount_inc(&db_info->ref_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
384
385/*
386 * debug_info_put:
387 * - decreases reference count for debug-info and frees it if necessary
388 */
Heiko Carstens496da0d2017-10-13 09:06:29 +0200389static void debug_info_put(debug_info_t *db_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
391 int i;
392
393 if (!db_info)
394 return;
Elena Reshetovaefc0c212017-03-02 12:23:45 +0100395 if (refcount_dec_and_test(&db_info->ref_count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
Michael Holzheu66a464d2005-06-25 14:55:33 -0700397 if (!db_info->views[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 continue;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700399 debugfs_remove(db_info->debugfs_entries[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 }
Michael Holzheu66a464d2005-06-25 14:55:33 -0700401 debugfs_remove(db_info->debugfs_root_entry);
Heiko Carstens496da0d2017-10-13 09:06:29 +0200402 if (db_info == debug_area_first)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 debug_area_first = db_info->next;
Heiko Carstens496da0d2017-10-13 09:06:29 +0200404 if (db_info == debug_area_last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 debug_area_last = db_info->prev;
Heiko Carstens496da0d2017-10-13 09:06:29 +0200406 if (db_info->prev)
407 db_info->prev->next = db_info->next;
408 if (db_info->next)
409 db_info->next->prev = db_info->prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 debug_info_free(db_info);
411 }
412}
413
414/*
415 * debug_format_entry:
416 * - format one debug entry and return size of formated data
417 */
Heiko Carstens496da0d2017-10-13 09:06:29 +0200418static int debug_format_entry(file_private_info_t *p_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
Heiko Carstens496da0d2017-10-13 09:06:29 +0200420 debug_info_t *id_snap = p_info->debug_info_snap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 struct debug_view *view = p_info->view;
422 debug_entry_t *act_entry;
423 size_t len = 0;
Heiko Carstens496da0d2017-10-13 09:06:29 +0200424
425 if (p_info->act_entry == DEBUG_PROLOG_ENTRY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 /* print prolog */
Heiko Carstens496da0d2017-10-13 09:06:29 +0200427 if (view->prolog_proc)
428 len += view->prolog_proc(id_snap, view, p_info->temp_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 goto out;
430 }
Michael Holzheu66a464d2005-06-25 14:55:33 -0700431 if (!id_snap->areas) /* this is true, if we have a prolog only view */
432 goto out; /* or if 'pages_per_area' is 0 */
Heiko Carstens496da0d2017-10-13 09:06:29 +0200433 act_entry = (debug_entry_t *) ((char *)id_snap->areas[p_info->act_area]
434 [p_info->act_page] + p_info->act_entry);
435
Mikhail Zaslonko0990d832020-05-05 10:34:52 +0200436 if (act_entry->clock == 0LL)
Heiko Carstens496da0d2017-10-13 09:06:29 +0200437 goto out; /* empty entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 if (view->header_proc)
Michael Holzheu66a464d2005-06-25 14:55:33 -0700439 len += view->header_proc(id_snap, view, p_info->act_area,
Heiko Carstens496da0d2017-10-13 09:06:29 +0200440 act_entry, p_info->temp_buf + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 if (view->format_proc)
Michael Holzheu66a464d2005-06-25 14:55:33 -0700442 len += view->format_proc(id_snap, view, p_info->temp_buf + len,
Heiko Carstens496da0d2017-10-13 09:06:29 +0200443 DEBUG_DATA(act_entry));
Michael Holzheu66a464d2005-06-25 14:55:33 -0700444out:
Heiko Carstens496da0d2017-10-13 09:06:29 +0200445 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
447
448/*
449 * debug_next_entry:
450 * - goto next entry in p_info
451 */
Heiko Carstens496da0d2017-10-13 09:06:29 +0200452static inline int debug_next_entry(file_private_info_t *p_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
Michael Holzheu66a464d2005-06-25 14:55:33 -0700454 debug_info_t *id;
455
456 id = p_info->debug_info_snap;
Heiko Carstens496da0d2017-10-13 09:06:29 +0200457 if (p_info->act_entry == DEBUG_PROLOG_ENTRY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 p_info->act_entry = 0;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700459 p_info->act_page = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 goto out;
461 }
Heiko Carstens496da0d2017-10-13 09:06:29 +0200462 if (!id->areas)
Michael Holzheu66a464d2005-06-25 14:55:33 -0700463 return 1;
464 p_info->act_entry += id->entry_size;
465 /* switch to next page, if we reached the end of the page */
Heiko Carstens496da0d2017-10-13 09:06:29 +0200466 if (p_info->act_entry > (PAGE_SIZE - id->entry_size)) {
Michael Holzheu66a464d2005-06-25 14:55:33 -0700467 /* next page */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 p_info->act_entry = 0;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700469 p_info->act_page += 1;
Heiko Carstens496da0d2017-10-13 09:06:29 +0200470 if ((p_info->act_page % id->pages_per_area) == 0) {
Michael Holzheu66a464d2005-06-25 14:55:33 -0700471 /* next area */
Heiko Carstens496da0d2017-10-13 09:06:29 +0200472 p_info->act_area++;
473 p_info->act_page = 0;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700474 }
Heiko Carstens496da0d2017-10-13 09:06:29 +0200475 if (p_info->act_area >= id->nr_areas)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 return 1;
477 }
478out:
Heiko Carstens496da0d2017-10-13 09:06:29 +0200479 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480}
481
482/*
483 * debug_output:
484 * - called for user read()
485 * - copies formated debug entries to the user buffer
486 */
Heiko Carstens496da0d2017-10-13 09:06:29 +0200487static ssize_t debug_output(struct file *file, /* file descriptor */
488 char __user *user_buf, /* user buffer */
489 size_t len, /* length of buffer */
490 loff_t *offset) /* offset in the file */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
492 size_t count = 0;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700493 size_t entry_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 file_private_info_t *p_info;
495
Heiko Carstens496da0d2017-10-13 09:06:29 +0200496 p_info = (file_private_info_t *) file->private_data;
497 if (*offset != p_info->offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 return -EPIPE;
Heiko Carstens496da0d2017-10-13 09:06:29 +0200499 if (p_info->act_area >= p_info->debug_info_snap->nr_areas)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 entry_offset = p_info->act_entry_offset;
Heiko Carstens496da0d2017-10-13 09:06:29 +0200502 while (count < len) {
Michael Holzheu66a464d2005-06-25 14:55:33 -0700503 int formatted_line_residue;
Heiko Carstens496da0d2017-10-13 09:06:29 +0200504 int formatted_line_size;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700505 int user_buf_residue;
506 size_t copy_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Michael Holzheu66a464d2005-06-25 14:55:33 -0700508 formatted_line_size = debug_format_entry(p_info);
509 formatted_line_residue = formatted_line_size - entry_offset;
510 user_buf_residue = len-count;
511 copy_size = min(user_buf_residue, formatted_line_residue);
Heiko Carstens496da0d2017-10-13 09:06:29 +0200512 if (copy_size) {
Michael Holzheu66a464d2005-06-25 14:55:33 -0700513 if (copy_to_user(user_buf + count, p_info->temp_buf
Heiko Carstens496da0d2017-10-13 09:06:29 +0200514 + entry_offset, copy_size))
Michael Holzheu66a464d2005-06-25 14:55:33 -0700515 return -EFAULT;
516 count += copy_size;
517 entry_offset += copy_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
Heiko Carstens496da0d2017-10-13 09:06:29 +0200519 if (copy_size == formatted_line_residue) {
Michael Holzheu66a464d2005-06-25 14:55:33 -0700520 entry_offset = 0;
Heiko Carstens496da0d2017-10-13 09:06:29 +0200521 if (debug_next_entry(p_info))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 goto out;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 }
525out:
Heiko Carstens496da0d2017-10-13 09:06:29 +0200526 p_info->offset = *offset + count;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700527 p_info->act_entry_offset = entry_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 *offset = p_info->offset;
529 return count;
530}
531
532/*
533 * debug_input:
534 * - called for user write()
535 * - calls input function of view
536 */
Heiko Carstens496da0d2017-10-13 09:06:29 +0200537static ssize_t debug_input(struct file *file, const char __user *user_buf,
538 size_t length, loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 file_private_info_t *p_info;
Heiko Carstens496da0d2017-10-13 09:06:29 +0200541 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Christoph Hellwige11f0d02007-05-31 17:38:03 +0200543 mutex_lock(&debug_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 p_info = ((file_private_info_t *) file->private_data);
Heiko Carstens496da0d2017-10-13 09:06:29 +0200545 if (p_info->view->input_proc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 rc = p_info->view->input_proc(p_info->debug_info_org,
547 p_info->view, file, user_buf,
548 length, offset);
Heiko Carstens496da0d2017-10-13 09:06:29 +0200549 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 rc = -EPERM;
Heiko Carstens496da0d2017-10-13 09:06:29 +0200551 }
Christoph Hellwige11f0d02007-05-31 17:38:03 +0200552 mutex_unlock(&debug_mutex);
Heiko Carstens496da0d2017-10-13 09:06:29 +0200553 return rc; /* number of input characters */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554}
555
556/*
557 * debug_open:
558 * - called for user open()
559 * - copies formated output to private_data area of the file
560 * handle
561 */
Heiko Carstens496da0d2017-10-13 09:06:29 +0200562static int debug_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 debug_info_t *debug_info, *debug_info_snapshot;
Heiko Carstens496da0d2017-10-13 09:06:29 +0200565 file_private_info_t *p_info;
566 int i, rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Christoph Hellwige11f0d02007-05-31 17:38:03 +0200568 mutex_lock(&debug_mutex);
Al Viro496ad9a2013-01-23 17:07:38 -0500569 debug_info = file_inode(file)->i_private;
Michael Holzheu942eaab2005-09-03 15:57:58 -0700570 /* find debug view */
571 for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
572 if (!debug_info->views[i])
573 continue;
Heiko Carstens496da0d2017-10-13 09:06:29 +0200574 else if (debug_info->debugfs_entries[i] == file->f_path.dentry)
575 goto found; /* found view ! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 }
577 /* no entry found */
578 rc = -EINVAL;
579 goto out;
580
Michael Holzheu66a464d2005-06-25 14:55:33 -0700581found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Heiko Carstens496da0d2017-10-13 09:06:29 +0200583 /* Make snapshot of current debug areas to get it consistent. */
Michael Holzheu66a464d2005-06-25 14:55:33 -0700584 /* To copy all the areas is only needed, if we have a view which */
585 /* formats the debug areas. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Heiko Carstens496da0d2017-10-13 09:06:29 +0200587 if (!debug_info->views[i]->format_proc && !debug_info->views[i]->header_proc)
Michael Holzheu66a464d2005-06-25 14:55:33 -0700588 debug_info_snapshot = debug_info_copy(debug_info, NO_AREAS);
Heiko Carstens496da0d2017-10-13 09:06:29 +0200589 else
Michael Holzheu66a464d2005-06-25 14:55:33 -0700590 debug_info_snapshot = debug_info_copy(debug_info, ALL_AREAS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Heiko Carstens496da0d2017-10-13 09:06:29 +0200592 if (!debug_info_snapshot) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 rc = -ENOMEM;
594 goto out;
595 }
Heiko Carstens496da0d2017-10-13 09:06:29 +0200596 p_info = kmalloc(sizeof(file_private_info_t), GFP_KERNEL);
597 if (!p_info) {
Michael Holzheu58ace9f2009-03-26 15:24:50 +0100598 debug_info_free(debug_info_snapshot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 rc = -ENOMEM;
600 goto out;
601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 p_info->offset = 0;
603 p_info->debug_info_snap = debug_info_snapshot;
Heiko Carstens496da0d2017-10-13 09:06:29 +0200604 p_info->debug_info_org = debug_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 p_info->view = debug_info->views[i];
606 p_info->act_area = 0;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700607 p_info->act_page = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 p_info->act_entry = DEBUG_PROLOG_ENTRY;
609 p_info->act_entry_offset = 0;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700610 file->private_data = p_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 debug_info_get(debug_info);
Martin Schwidefsky58ea91c2010-05-17 10:00:07 +0200612 nonseekable_open(inode, file);
Michael Holzheu66a464d2005-06-25 14:55:33 -0700613out:
Christoph Hellwige11f0d02007-05-31 17:38:03 +0200614 mutex_unlock(&debug_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 return rc;
616}
617
618/*
619 * debug_close:
620 * - called for user close()
621 * - deletes private_data area of the file handle
622 */
Heiko Carstens496da0d2017-10-13 09:06:29 +0200623static int debug_close(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
625 file_private_info_t *p_info;
Heiko Carstens496da0d2017-10-13 09:06:29 +0200626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 p_info = (file_private_info_t *) file->private_data;
Heiko Carstens496da0d2017-10-13 09:06:29 +0200628 if (p_info->debug_info_snap)
Michael Holzheu66a464d2005-06-25 14:55:33 -0700629 debug_info_free(p_info->debug_info_snap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 debug_info_put(p_info->debug_info_org);
631 kfree(file->private_data);
Heiko Carstens496da0d2017-10-13 09:06:29 +0200632 return 0; /* success */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633}
634
Steffen Maier0328e512019-07-03 12:19:48 +0200635/**
636 * debug_register_mode() - creates and initializes debug area.
637 *
638 * @name: Name of debug log (e.g. used for debugfs entry)
639 * @pages_per_area: Number of pages, which will be allocated per area
640 * @nr_areas: Number of debug areas
641 * @buf_size: Size of data area in each debug entry
642 * @mode: File mode for debugfs files. E.g. S_IRWXUGO
643 * @uid: User ID for debugfs files. Currently only 0 is supported.
644 * @gid: Group ID for debugfs files. Currently only 0 is supported.
645 *
646 * Return:
647 * - Handle for generated debug area
648 * - %NULL if register failed
649 *
650 * Allocates memory for a debug log.
651 * Must not be called within an interrupt handler.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 */
Cornelia Huck5cbbf162008-05-15 16:52:35 +0200653debug_info_t *debug_register_mode(const char *name, int pages_per_area,
Al Virof4ae40a62011-07-24 04:33:43 -0400654 int nr_areas, int buf_size, umode_t mode,
Cornelia Huck5cbbf162008-05-15 16:52:35 +0200655 uid_t uid, gid_t gid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
657 debug_info_t *rc = NULL;
658
Michael Holzheu9637c3f2008-04-17 07:46:18 +0200659 /* Since debugfs currently does not support uid/gid other than root, */
660 /* we do not allow gid/uid != 0 until we get support for that. */
661 if ((uid != 0) || (gid != 0))
Joe Perchesbaebc702016-03-03 20:49:57 -0800662 pr_warn("Root becomes the owner of all s390dbf files in sysfs\n");
Stoyan Gaydarov6aa0d3a2009-03-26 15:24:47 +0100663 BUG_ON(!initialized);
Christoph Hellwige11f0d02007-05-31 17:38:03 +0200664 mutex_lock(&debug_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Heiko Carstens496da0d2017-10-13 09:06:29 +0200666 /* create new debug_info */
Michael Holzheu9637c3f2008-04-17 07:46:18 +0200667 rc = debug_info_create(name, pages_per_area, nr_areas, buf_size, mode);
Heiko Carstens496da0d2017-10-13 09:06:29 +0200668 if (!rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 goto out;
670 debug_register_view(rc, &debug_level_view);
Heiko Carstens496da0d2017-10-13 09:06:29 +0200671 debug_register_view(rc, &debug_flush_view);
Michael Holzheu66a464d2005-06-25 14:55:33 -0700672 debug_register_view(rc, &debug_pages_view);
673out:
Heiko Carstens496da0d2017-10-13 09:06:29 +0200674 if (!rc)
Michael Holzheuc5612c12008-12-25 13:39:43 +0100675 pr_err("Registering debug feature %s failed\n", name);
Christoph Hellwige11f0d02007-05-31 17:38:03 +0200676 mutex_unlock(&debug_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 return rc;
678}
Michael Holzheu9637c3f2008-04-17 07:46:18 +0200679EXPORT_SYMBOL(debug_register_mode);
680
Steffen Maier0328e512019-07-03 12:19:48 +0200681/**
682 * debug_register() - creates and initializes debug area with default file mode.
683 *
684 * @name: Name of debug log (e.g. used for debugfs entry)
685 * @pages_per_area: Number of pages, which will be allocated per area
686 * @nr_areas: Number of debug areas
687 * @buf_size: Size of data area in each debug entry
688 *
689 * Return:
690 * - Handle for generated debug area
691 * - %NULL if register failed
692 *
693 * Allocates memory for a debug log.
694 * The debugfs file mode access permissions are read and write for user.
695 * Must not be called within an interrupt handler.
Michael Holzheu9637c3f2008-04-17 07:46:18 +0200696 */
Cornelia Huck5cbbf162008-05-15 16:52:35 +0200697debug_info_t *debug_register(const char *name, int pages_per_area,
698 int nr_areas, int buf_size)
Michael Holzheu9637c3f2008-04-17 07:46:18 +0200699{
700 return debug_register_mode(name, pages_per_area, nr_areas, buf_size,
701 S_IRUSR | S_IWUSR, 0, 0);
702}
Heiko Carstensd5274362012-07-24 14:27:53 +0200703EXPORT_SYMBOL(debug_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Steffen Maier0328e512019-07-03 12:19:48 +0200705/**
706 * debug_unregister() - give back debug area.
707 *
708 * @id: handle for debug log
709 *
710 * Return:
711 * none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 */
Heiko Carstens496da0d2017-10-13 09:06:29 +0200713void debug_unregister(debug_info_t *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
715 if (!id)
Heiko Carstens496da0d2017-10-13 09:06:29 +0200716 return;
Christoph Hellwige11f0d02007-05-31 17:38:03 +0200717 mutex_lock(&debug_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 debug_info_put(id);
Christoph Hellwige11f0d02007-05-31 17:38:03 +0200719 mutex_unlock(&debug_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720}
Heiko Carstensd5274362012-07-24 14:27:53 +0200721EXPORT_SYMBOL(debug_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723/*
Michael Holzheu66a464d2005-06-25 14:55:33 -0700724 * debug_set_size:
725 * - set area size (number of pages) and number of areas
726 */
Heiko Carstens496da0d2017-10-13 09:06:29 +0200727static int debug_set_size(debug_info_t *id, int nr_areas, int pages_per_area)
Michael Holzheu66a464d2005-06-25 14:55:33 -0700728{
Heiko Carstens496da0d2017-10-13 09:06:29 +0200729 debug_entry_t ***new_areas;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700730 unsigned long flags;
Heiko Carstens496da0d2017-10-13 09:06:29 +0200731 int rc = 0;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700732
Heiko Carstens496da0d2017-10-13 09:06:29 +0200733 if (!id || (nr_areas <= 0) || (pages_per_area < 0))
Michael Holzheu66a464d2005-06-25 14:55:33 -0700734 return -EINVAL;
Heiko Carstens496da0d2017-10-13 09:06:29 +0200735 if (pages_per_area > 0) {
Michael Holzheu66a464d2005-06-25 14:55:33 -0700736 new_areas = debug_areas_alloc(pages_per_area, nr_areas);
Heiko Carstens496da0d2017-10-13 09:06:29 +0200737 if (!new_areas) {
Michael Holzheuc5612c12008-12-25 13:39:43 +0100738 pr_info("Allocating memory for %i pages failed\n",
739 pages_per_area);
Michael Holzheu66a464d2005-06-25 14:55:33 -0700740 rc = -ENOMEM;
741 goto out;
742 }
743 } else {
744 new_areas = NULL;
745 }
Heiko Carstens496da0d2017-10-13 09:06:29 +0200746 spin_lock_irqsave(&id->lock, flags);
Michael Holzheu66a464d2005-06-25 14:55:33 -0700747 debug_areas_free(id);
748 id->areas = new_areas;
749 id->nr_areas = nr_areas;
750 id->pages_per_area = pages_per_area;
751 id->active_area = 0;
Heiko Carstens496da0d2017-10-13 09:06:29 +0200752 memset(id->active_entries, 0, sizeof(int)*id->nr_areas);
Michael Holzheu66a464d2005-06-25 14:55:33 -0700753 memset(id->active_pages, 0, sizeof(int)*id->nr_areas);
Heiko Carstens496da0d2017-10-13 09:06:29 +0200754 spin_unlock_irqrestore(&id->lock, flags);
755 pr_info("%s: set new size (%i pages)\n", id->name, pages_per_area);
Michael Holzheu66a464d2005-06-25 14:55:33 -0700756out:
757 return rc;
758}
759
Steffen Maier0328e512019-07-03 12:19:48 +0200760/**
761 * debug_set_level() - Sets new actual debug level if new_level is valid.
762 *
763 * @id: handle for debug log
764 * @new_level: new debug level
765 *
766 * Return:
767 * none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 */
Heiko Carstens496da0d2017-10-13 09:06:29 +0200769void debug_set_level(debug_info_t *id, int new_level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
771 unsigned long flags;
Heiko Carstens496da0d2017-10-13 09:06:29 +0200772
773 if (!id)
774 return;
775 spin_lock_irqsave(&id->lock, flags);
776 if (new_level == DEBUG_OFF_LEVEL) {
777 id->level = DEBUG_OFF_LEVEL;
778 pr_info("%s: switched off\n", id->name);
779 } else if ((new_level > DEBUG_MAX_LEVEL) || (new_level < 0)) {
Michael Holzheuc5612c12008-12-25 13:39:43 +0100780 pr_info("%s: level %i is out of range (%i - %i)\n",
Heiko Carstens496da0d2017-10-13 09:06:29 +0200781 id->name, new_level, 0, DEBUG_MAX_LEVEL);
782 } else {
783 id->level = new_level;
784 }
785 spin_unlock_irqrestore(&id->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786}
Heiko Carstensd5274362012-07-24 14:27:53 +0200787EXPORT_SYMBOL(debug_set_level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789/*
790 * proceed_active_entry:
791 * - set active entry to next in the ring buffer
792 */
Heiko Carstens496da0d2017-10-13 09:06:29 +0200793static inline void proceed_active_entry(debug_info_t *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
Michael Holzheu66a464d2005-06-25 14:55:33 -0700795 if ((id->active_entries[id->active_area] += id->entry_size)
Heiko Carstens496da0d2017-10-13 09:06:29 +0200796 > (PAGE_SIZE - id->entry_size)) {
Michael Holzheu66a464d2005-06-25 14:55:33 -0700797 id->active_entries[id->active_area] = 0;
798 id->active_pages[id->active_area] =
799 (id->active_pages[id->active_area] + 1) %
800 id->pages_per_area;
801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802}
803
804/*
805 * proceed_active_area:
806 * - set active area to next in the ring buffer
807 */
Heiko Carstens496da0d2017-10-13 09:06:29 +0200808static inline void proceed_active_area(debug_info_t *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
810 id->active_area++;
811 id->active_area = id->active_area % id->nr_areas;
812}
813
814/*
815 * get_active_entry:
816 */
Heiko Carstens496da0d2017-10-13 09:06:29 +0200817static inline debug_entry_t *get_active_entry(debug_info_t *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
Michael Holzheu66a464d2005-06-25 14:55:33 -0700819 return (debug_entry_t *) (((char *) id->areas[id->active_area]
Heiko Carstens496da0d2017-10-13 09:06:29 +0200820 [id->active_pages[id->active_area]]) +
821 id->active_entries[id->active_area]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822}
823
824/*
825 * debug_finish_entry:
826 * - set timestamp, caller address, cpu number etc.
827 */
828
Heiko Carstens496da0d2017-10-13 09:06:29 +0200829static inline void debug_finish_entry(debug_info_t *id, debug_entry_t *active,
830 int level, int exception)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831{
Mikhail Zaslonko0990d832020-05-05 10:34:52 +0200832 unsigned char clk[STORE_CLOCK_EXT_SIZE];
833 unsigned long timestamp;
834
835 get_tod_clock_ext(clk);
836 timestamp = *(unsigned long *) &clk[0] >> 4;
837 timestamp -= TOD_UNIX_EPOCH >> 12;
838 active->clock = timestamp;
839 active->cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 active->caller = __builtin_return_address(0);
Mikhail Zaslonko0990d832020-05-05 10:34:52 +0200841 active->exception = exception;
842 active->level = level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 proceed_active_entry(id);
Heiko Carstens496da0d2017-10-13 09:06:29 +0200844 if (exception)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 proceed_active_area(id);
846}
847
Heiko Carstens496da0d2017-10-13 09:06:29 +0200848static int debug_stoppable = 1;
849static int debug_active = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851#define CTL_S390DBF_STOPPABLE 5678
852#define CTL_S390DBF_ACTIVE 5679
853
854/*
855 * proc handler for the running debug_active sysctl
856 * always allow read, allow write only if debug_stoppable is set or
857 * if debug_active is already off
858 */
Heiko Carstens496da0d2017-10-13 09:06:29 +0200859static int s390dbf_procactive(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +0200860 void *buffer, size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861{
862 if (!write || debug_stoppable || !debug_active)
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700863 return proc_dointvec(table, write, buffer, lenp, ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 else
865 return 0;
866}
867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868static struct ctl_table s390dbf_table[] = {
869 {
Heiko Carstens496da0d2017-10-13 09:06:29 +0200870 .procname = "debug_stoppable",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 .data = &debug_stoppable,
872 .maxlen = sizeof(int),
Heiko Carstens496da0d2017-10-13 09:06:29 +0200873 .mode = S_IRUGO | S_IWUSR,
874 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 },
Heiko Carstens496da0d2017-10-13 09:06:29 +0200876 {
877 .procname = "debug_active",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 .data = &debug_active,
879 .maxlen = sizeof(int),
Heiko Carstens496da0d2017-10-13 09:06:29 +0200880 .mode = S_IRUGO | S_IWUSR,
881 .proc_handler = s390dbf_procactive,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 },
Eric W. Biedermanb05fd352009-04-03 04:36:34 -0700883 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884};
885
886static struct ctl_table s390dbf_dir_table[] = {
887 {
Heiko Carstens496da0d2017-10-13 09:06:29 +0200888 .procname = "s390dbf",
889 .maxlen = 0,
890 .mode = S_IRUGO | S_IXUGO,
891 .child = s390dbf_table,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 },
Eric W. Biedermanb05fd352009-04-03 04:36:34 -0700893 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894};
895
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100896static struct ctl_table_header *s390dbf_sysctl_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Steffen Maier0328e512019-07-03 12:19:48 +0200898/**
899 * debug_stop_all() - stops the debug feature if stopping is allowed.
900 *
901 * Return:
902 * - none
903 *
904 * Currently used in case of a kernel oops.
905 */
Heiko Carstens496da0d2017-10-13 09:06:29 +0200906void debug_stop_all(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
908 if (debug_stoppable)
909 debug_active = 0;
910}
Heiko Carstensd5274362012-07-24 14:27:53 +0200911EXPORT_SYMBOL(debug_stop_all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Steffen Maier0328e512019-07-03 12:19:48 +0200913/**
914 * debug_set_critical() - event/exception functions try lock instead of spin.
915 *
916 * Return:
917 * - none
918 *
919 * Currently used in case of stopping all CPUs but the current one.
920 * Once in this state, functions to write a debug entry for an
921 * event or exception no longer spin on the debug area lock,
922 * but only try to get it and fail if they do not get the lock.
923 */
Michael Holzheu3ab121a2012-03-11 11:59:32 -0400924void debug_set_critical(void)
925{
926 debug_critical = 1;
927}
928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929/*
930 * debug_event_common:
931 * - write debug entry with given size
932 */
Heiko Carstens496da0d2017-10-13 09:06:29 +0200933debug_entry_t *debug_event_common(debug_info_t *id, int level, const void *buf,
934 int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 debug_entry_t *active;
Heiko Carstens496da0d2017-10-13 09:06:29 +0200937 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Michael Holzheu66a464d2005-06-25 14:55:33 -0700939 if (!debug_active || !id->areas)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 return NULL;
Michael Holzheu3ab121a2012-03-11 11:59:32 -0400941 if (debug_critical) {
942 if (!spin_trylock_irqsave(&id->lock, flags))
943 return NULL;
Heiko Carstens496da0d2017-10-13 09:06:29 +0200944 } else {
Michael Holzheu3ab121a2012-03-11 11:59:32 -0400945 spin_lock_irqsave(&id->lock, flags);
Heiko Carstens496da0d2017-10-13 09:06:29 +0200946 }
Sebastian Ott94158e52017-10-09 17:49:38 +0200947 do {
948 active = get_active_entry(id);
Sebastian Ott94158e52017-10-09 17:49:38 +0200949 memcpy(DEBUG_DATA(active), buf, min(len, id->buf_size));
Sebastian Ott0dcd91a2017-10-12 13:57:26 +0200950 if (len < id->buf_size)
951 memset((DEBUG_DATA(active)) + len, 0, id->buf_size - len);
Sebastian Ott94158e52017-10-09 17:49:38 +0200952 debug_finish_entry(id, active, level, 0);
953 len -= id->buf_size;
954 buf += id->buf_size;
955 } while (len > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
Sebastian Ott94158e52017-10-09 17:49:38 +0200957 spin_unlock_irqrestore(&id->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 return active;
959}
Heiko Carstensd5274362012-07-24 14:27:53 +0200960EXPORT_SYMBOL(debug_event_common);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
962/*
963 * debug_exception_common:
964 * - write debug entry with given size and switch to next debug area
965 */
Heiko Carstens496da0d2017-10-13 09:06:29 +0200966debug_entry_t *debug_exception_common(debug_info_t *id, int level,
967 const void *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 debug_entry_t *active;
Heiko Carstens496da0d2017-10-13 09:06:29 +0200970 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Michael Holzheu66a464d2005-06-25 14:55:33 -0700972 if (!debug_active || !id->areas)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 return NULL;
Michael Holzheu3ab121a2012-03-11 11:59:32 -0400974 if (debug_critical) {
975 if (!spin_trylock_irqsave(&id->lock, flags))
976 return NULL;
Heiko Carstens496da0d2017-10-13 09:06:29 +0200977 } else {
Michael Holzheu3ab121a2012-03-11 11:59:32 -0400978 spin_lock_irqsave(&id->lock, flags);
Heiko Carstens496da0d2017-10-13 09:06:29 +0200979 }
Sebastian Ott94158e52017-10-09 17:49:38 +0200980 do {
981 active = get_active_entry(id);
Sebastian Ott94158e52017-10-09 17:49:38 +0200982 memcpy(DEBUG_DATA(active), buf, min(len, id->buf_size));
Sebastian Ott0dcd91a2017-10-12 13:57:26 +0200983 if (len < id->buf_size)
984 memset((DEBUG_DATA(active)) + len, 0, id->buf_size - len);
Sebastian Ott94158e52017-10-09 17:49:38 +0200985 debug_finish_entry(id, active, level, len <= id->buf_size);
986 len -= id->buf_size;
987 buf += id->buf_size;
988 } while (len > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Sebastian Ott94158e52017-10-09 17:49:38 +0200990 spin_unlock_irqrestore(&id->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 return active;
992}
Heiko Carstensd5274362012-07-24 14:27:53 +0200993EXPORT_SYMBOL(debug_exception_common);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
995/*
996 * counts arguments in format string for sprintf view
997 */
Heiko Carstens496da0d2017-10-13 09:06:29 +0200998static inline int debug_count_numargs(char *string)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999{
Heiko Carstens496da0d2017-10-13 09:06:29 +02001000 int numargs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Heiko Carstens496da0d2017-10-13 09:06:29 +02001002 while (*string) {
1003 if (*string++ == '%')
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 numargs++;
1005 }
Heiko Carstens496da0d2017-10-13 09:06:29 +02001006 return numargs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007}
1008
1009/*
1010 * debug_sprintf_event:
1011 */
Heiko Carstens496da0d2017-10-13 09:06:29 +02001012debug_entry_t *__debug_sprintf_event(debug_info_t *id, int level, char *string, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 debug_sprintf_entry_t *curr_event;
1015 debug_entry_t *active;
Heiko Carstens496da0d2017-10-13 09:06:29 +02001016 unsigned long flags;
1017 int numargs, idx;
1018 va_list ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Michael Holzheu66a464d2005-06-25 14:55:33 -07001020 if (!debug_active || !id->areas)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 return NULL;
Heiko Carstens496da0d2017-10-13 09:06:29 +02001022 numargs = debug_count_numargs(string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Michael Holzheu3ab121a2012-03-11 11:59:32 -04001024 if (debug_critical) {
1025 if (!spin_trylock_irqsave(&id->lock, flags))
1026 return NULL;
Heiko Carstens496da0d2017-10-13 09:06:29 +02001027 } else {
Michael Holzheu3ab121a2012-03-11 11:59:32 -04001028 spin_lock_irqsave(&id->lock, flags);
Heiko Carstens496da0d2017-10-13 09:06:29 +02001029 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 active = get_active_entry(id);
Heiko Carstens496da0d2017-10-13 09:06:29 +02001031 curr_event = (debug_sprintf_entry_t *) DEBUG_DATA(active);
1032 va_start(ap, string);
1033 curr_event->string = string;
1034 for (idx = 0; idx < min(numargs, (int)(id->buf_size / sizeof(long)) - 1); idx++)
1035 curr_event->args[idx] = va_arg(ap, long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 va_end(ap);
1037 debug_finish_entry(id, active, level, 0);
1038 spin_unlock_irqrestore(&id->lock, flags);
1039
1040 return active;
1041}
Christian Borntraeger832a7712014-12-01 09:16:45 +01001042EXPORT_SYMBOL(__debug_sprintf_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
1044/*
1045 * debug_sprintf_exception:
1046 */
Heiko Carstens496da0d2017-10-13 09:06:29 +02001047debug_entry_t *__debug_sprintf_exception(debug_info_t *id, int level, char *string, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 debug_sprintf_entry_t *curr_event;
1050 debug_entry_t *active;
Heiko Carstens496da0d2017-10-13 09:06:29 +02001051 unsigned long flags;
1052 int numargs, idx;
1053 va_list ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Michael Holzheu66a464d2005-06-25 14:55:33 -07001055 if (!debug_active || !id->areas)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 return NULL;
1057
Heiko Carstens496da0d2017-10-13 09:06:29 +02001058 numargs = debug_count_numargs(string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Michael Holzheu3ab121a2012-03-11 11:59:32 -04001060 if (debug_critical) {
1061 if (!spin_trylock_irqsave(&id->lock, flags))
1062 return NULL;
Heiko Carstens496da0d2017-10-13 09:06:29 +02001063 } else {
Michael Holzheu3ab121a2012-03-11 11:59:32 -04001064 spin_lock_irqsave(&id->lock, flags);
Heiko Carstens496da0d2017-10-13 09:06:29 +02001065 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 active = get_active_entry(id);
Heiko Carstens496da0d2017-10-13 09:06:29 +02001067 curr_event = (debug_sprintf_entry_t *)DEBUG_DATA(active);
1068 va_start(ap, string);
1069 curr_event->string = string;
1070 for (idx = 0; idx < min(numargs, (int)(id->buf_size / sizeof(long)) - 1); idx++)
1071 curr_event->args[idx] = va_arg(ap, long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 va_end(ap);
1073 debug_finish_entry(id, active, level, 1);
1074 spin_unlock_irqrestore(&id->lock, flags);
1075
1076 return active;
1077}
Christian Borntraeger832a7712014-12-01 09:16:45 +01001078EXPORT_SYMBOL(__debug_sprintf_exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Steffen Maier0328e512019-07-03 12:19:48 +02001080/**
1081 * debug_register_view() - registers new debug view and creates debugfs
1082 * dir entry
1083 *
1084 * @id: handle for debug log
1085 * @view: pointer to debug view struct
1086 *
1087 * Return:
1088 * - 0 : ok
1089 * - < 0: Error
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 */
Heiko Carstens496da0d2017-10-13 09:06:29 +02001091int debug_register_view(debug_info_t *id, struct debug_view *view)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
Heiko Carstens496da0d2017-10-13 09:06:29 +02001093 unsigned long flags;
1094 struct dentry *pde;
1095 umode_t mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 int rc = 0;
1097 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
1099 if (!id)
1100 goto out;
Michael Holzheu9637c3f2008-04-17 07:46:18 +02001101 mode = (id->mode | S_IFREG) & ~S_IXUGO;
1102 if (!(view->prolog_proc || view->format_proc || view->header_proc))
1103 mode &= ~(S_IRUSR | S_IRGRP | S_IROTH);
1104 if (!view->input_proc)
1105 mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
Michael Holzheu66a464d2005-06-25 14:55:33 -07001106 pde = debugfs_create_file(view->name, mode, id->debugfs_root_entry,
Heiko Carstens496da0d2017-10-13 09:06:29 +02001107 id, &debug_file_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 spin_lock_irqsave(&id->lock, flags);
1109 for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
Michael Holzheu66a464d2005-06-25 14:55:33 -07001110 if (!id->views[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 break;
1112 }
1113 if (i == DEBUG_MAX_VIEWS) {
Michael Holzheuc5612c12008-12-25 13:39:43 +01001114 pr_err("Registering view %s/%s would exceed the maximum "
1115 "number of views %i\n", id->name, view->name, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 rc = -1;
Michael Holzheu66a464d2005-06-25 14:55:33 -07001117 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 id->views[i] = view;
Michael Holzheu66a464d2005-06-25 14:55:33 -07001119 id->debugfs_entries[i] = pde;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 }
1121 spin_unlock_irqrestore(&id->lock, flags);
Michael Holzheu5a334c02012-12-20 17:30:52 +01001122 if (rc)
1123 debugfs_remove(pde);
Michael Holzheu66a464d2005-06-25 14:55:33 -07001124out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 return rc;
1126}
Heiko Carstensd5274362012-07-24 14:27:53 +02001127EXPORT_SYMBOL(debug_register_view);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Steffen Maier0328e512019-07-03 12:19:48 +02001129/**
1130 * debug_unregister_view() - unregisters debug view and removes debugfs
1131 * dir entry
1132 *
1133 * @id: handle for debug log
1134 * @view: pointer to debug view struct
1135 *
1136 * Return:
1137 * - 0 : ok
1138 * - < 0: Error
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 */
Heiko Carstens496da0d2017-10-13 09:06:29 +02001140int debug_unregister_view(debug_info_t *id, struct debug_view *view)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141{
Michael Holzheu5a334c02012-12-20 17:30:52 +01001142 struct dentry *dentry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 unsigned long flags;
Michael Holzheu5a334c02012-12-20 17:30:52 +01001144 int i, rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
1146 if (!id)
1147 goto out;
1148 spin_lock_irqsave(&id->lock, flags);
1149 for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
1150 if (id->views[i] == view)
1151 break;
1152 }
Heiko Carstens496da0d2017-10-13 09:06:29 +02001153 if (i == DEBUG_MAX_VIEWS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 rc = -1;
Heiko Carstens496da0d2017-10-13 09:06:29 +02001155 } else {
Michael Holzheu5a334c02012-12-20 17:30:52 +01001156 dentry = id->debugfs_entries[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 id->views[i] = NULL;
Michael Holzheu5a334c02012-12-20 17:30:52 +01001158 id->debugfs_entries[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 }
1160 spin_unlock_irqrestore(&id->lock, flags);
Michael Holzheu5a334c02012-12-20 17:30:52 +01001161 debugfs_remove(dentry);
Michael Holzheu66a464d2005-06-25 14:55:33 -07001162out:
1163 return rc;
1164}
Heiko Carstensd5274362012-07-24 14:27:53 +02001165EXPORT_SYMBOL(debug_unregister_view);
Michael Holzheu66a464d2005-06-25 14:55:33 -07001166
Heiko Carstens496da0d2017-10-13 09:06:29 +02001167static inline char *debug_get_user_string(const char __user *user_buf,
1168 size_t user_len)
Michael Holzheu66a464d2005-06-25 14:55:33 -07001169{
Heiko Carstens496da0d2017-10-13 09:06:29 +02001170 char *buffer;
Michael Holzheu66a464d2005-06-25 14:55:33 -07001171
1172 buffer = kmalloc(user_len + 1, GFP_KERNEL);
1173 if (!buffer)
1174 return ERR_PTR(-ENOMEM);
1175 if (copy_from_user(buffer, user_buf, user_len) != 0) {
1176 kfree(buffer);
1177 return ERR_PTR(-EFAULT);
1178 }
1179 /* got the string, now strip linefeed. */
1180 if (buffer[user_len - 1] == '\n')
1181 buffer[user_len - 1] = 0;
1182 else
1183 buffer[user_len] = 0;
Heiko Carstens496da0d2017-10-13 09:06:29 +02001184 return buffer;
Michael Holzheu66a464d2005-06-25 14:55:33 -07001185}
1186
Heiko Carstens496da0d2017-10-13 09:06:29 +02001187static inline int debug_get_uint(char *buf)
Michael Holzheu66a464d2005-06-25 14:55:33 -07001188{
1189 int rc;
1190
André Goddard Rosae7d28602009-12-14 18:01:06 -08001191 buf = skip_spaces(buf);
Michael Holzheu66a464d2005-06-25 14:55:33 -07001192 rc = simple_strtoul(buf, &buf, 10);
Heiko Carstens496da0d2017-10-13 09:06:29 +02001193 if (*buf)
Michael Holzheu66a464d2005-06-25 14:55:33 -07001194 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 return rc;
1196}
1197
1198/*
1199 * functions for debug-views
1200 ***********************************
1201*/
1202
1203/*
1204 * prints out actual debug level
1205 */
1206
Heiko Carstens496da0d2017-10-13 09:06:29 +02001207static int debug_prolog_pages_fn(debug_info_t *id, struct debug_view *view,
1208 char *out_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209{
Michael Holzheu66a464d2005-06-25 14:55:33 -07001210 return sprintf(out_buf, "%i\n", id->pages_per_area);
1211}
1212
1213/*
1214 * reads new size (number of pages per debug area)
1215 */
1216
Heiko Carstens496da0d2017-10-13 09:06:29 +02001217static int debug_input_pages_fn(debug_info_t *id, struct debug_view *view,
1218 struct file *file, const char __user *user_buf,
1219 size_t user_len, loff_t *offset)
Michael Holzheu66a464d2005-06-25 14:55:33 -07001220{
Heiko Carstens496da0d2017-10-13 09:06:29 +02001221 int rc, new_pages;
Michael Holzheu66a464d2005-06-25 14:55:33 -07001222 char *str;
Michael Holzheu66a464d2005-06-25 14:55:33 -07001223
1224 if (user_len > 0x10000)
Heiko Carstens496da0d2017-10-13 09:06:29 +02001225 user_len = 0x10000;
1226 if (*offset != 0) {
Michael Holzheu66a464d2005-06-25 14:55:33 -07001227 rc = -EPIPE;
1228 goto out;
1229 }
Heiko Carstens496da0d2017-10-13 09:06:29 +02001230 str = debug_get_user_string(user_buf, user_len);
1231 if (IS_ERR(str)) {
Michael Holzheu66a464d2005-06-25 14:55:33 -07001232 rc = PTR_ERR(str);
1233 goto out;
1234 }
1235 new_pages = debug_get_uint(str);
Heiko Carstens496da0d2017-10-13 09:06:29 +02001236 if (new_pages < 0) {
Michael Holzheu66a464d2005-06-25 14:55:33 -07001237 rc = -EINVAL;
1238 goto free_str;
1239 }
Heiko Carstens496da0d2017-10-13 09:06:29 +02001240 rc = debug_set_size(id, id->nr_areas, new_pages);
1241 if (rc != 0) {
Michael Holzheu66a464d2005-06-25 14:55:33 -07001242 rc = -EINVAL;
1243 goto free_str;
1244 }
1245 rc = user_len;
1246free_str:
1247 kfree(str);
1248out:
1249 *offset += user_len;
1250 return rc; /* number of input characters */
1251}
1252
1253/*
1254 * prints out actual debug level
1255 */
Heiko Carstens496da0d2017-10-13 09:06:29 +02001256static int debug_prolog_level_fn(debug_info_t *id, struct debug_view *view,
1257 char *out_buf)
Michael Holzheu66a464d2005-06-25 14:55:33 -07001258{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 int rc = 0;
1260
Heiko Carstens496da0d2017-10-13 09:06:29 +02001261 if (id->level == DEBUG_OFF_LEVEL)
1262 rc = sprintf(out_buf, "-\n");
1263 else
Michael Holzheu66a464d2005-06-25 14:55:33 -07001264 rc = sprintf(out_buf, "%i\n", id->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 return rc;
1266}
1267
1268/*
1269 * reads new debug level
1270 */
Heiko Carstens496da0d2017-10-13 09:06:29 +02001271static int debug_input_level_fn(debug_info_t *id, struct debug_view *view,
1272 struct file *file, const char __user *user_buf,
1273 size_t user_len, loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274{
Heiko Carstens496da0d2017-10-13 09:06:29 +02001275 int rc, new_level;
Michael Holzheu66a464d2005-06-25 14:55:33 -07001276 char *str;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
Michael Holzheu66a464d2005-06-25 14:55:33 -07001278 if (user_len > 0x10000)
Heiko Carstens496da0d2017-10-13 09:06:29 +02001279 user_len = 0x10000;
1280 if (*offset != 0) {
Michael Holzheu66a464d2005-06-25 14:55:33 -07001281 rc = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 goto out;
1283 }
Heiko Carstens496da0d2017-10-13 09:06:29 +02001284 str = debug_get_user_string(user_buf, user_len);
1285 if (IS_ERR(str)) {
Michael Holzheu66a464d2005-06-25 14:55:33 -07001286 rc = PTR_ERR(str);
1287 goto out;
1288 }
Heiko Carstens496da0d2017-10-13 09:06:29 +02001289 if (str[0] == '-') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 debug_set_level(id, DEBUG_OFF_LEVEL);
Michael Holzheu66a464d2005-06-25 14:55:33 -07001291 rc = user_len;
1292 goto free_str;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 } else {
Michael Holzheu66a464d2005-06-25 14:55:33 -07001294 new_level = debug_get_uint(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 }
Heiko Carstens496da0d2017-10-13 09:06:29 +02001296 if (new_level < 0) {
Joe Perchesbaebc702016-03-03 20:49:57 -08001297 pr_warn("%s is not a valid level for a debug feature\n", str);
Michael Holzheu66a464d2005-06-25 14:55:33 -07001298 rc = -EINVAL;
1299 } else {
1300 debug_set_level(id, new_level);
1301 rc = user_len;
1302 }
1303free_str:
1304 kfree(str);
1305out:
1306 *offset += user_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 return rc; /* number of input characters */
1308}
1309
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310/*
1311 * flushes debug areas
1312 */
Heiko Carstens496da0d2017-10-13 09:06:29 +02001313static void debug_flush(debug_info_t *id, int area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314{
Heiko Carstens496da0d2017-10-13 09:06:29 +02001315 unsigned long flags;
1316 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
Heiko Carstens496da0d2017-10-13 09:06:29 +02001318 if (!id || !id->areas)
1319 return;
1320 spin_lock_irqsave(&id->lock, flags);
1321 if (area == DEBUG_FLUSH_ALL) {
1322 id->active_area = 0;
1323 memset(id->active_entries, 0, id->nr_areas * sizeof(int));
1324 for (i = 0; i < id->nr_areas; i++) {
Michael Holzheu66a464d2005-06-25 14:55:33 -07001325 id->active_pages[i] = 0;
Heiko Carstens496da0d2017-10-13 09:06:29 +02001326 for (j = 0; j < id->pages_per_area; j++)
1327 memset(id->areas[i][j], 0, PAGE_SIZE);
Michael Holzheu66a464d2005-06-25 14:55:33 -07001328 }
Heiko Carstens496da0d2017-10-13 09:06:29 +02001329 } else if (area >= 0 && area < id->nr_areas) {
1330 id->active_entries[area] = 0;
Michael Holzheu66a464d2005-06-25 14:55:33 -07001331 id->active_pages[area] = 0;
Heiko Carstens496da0d2017-10-13 09:06:29 +02001332 for (i = 0; i < id->pages_per_area; i++)
1333 memset(id->areas[area][i], 0, PAGE_SIZE);
1334 }
1335 spin_unlock_irqrestore(&id->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336}
1337
1338/*
Heiko Carstens496da0d2017-10-13 09:06:29 +02001339 * view function: flushes debug areas
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 */
Heiko Carstens496da0d2017-10-13 09:06:29 +02001341static int debug_input_flush_fn(debug_info_t *id, struct debug_view *view,
1342 struct file *file, const char __user *user_buf,
1343 size_t user_len, loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344{
Heiko Carstens496da0d2017-10-13 09:06:29 +02001345 char input_buf[1];
1346 int rc = user_len;
Michael Holzheu66a464d2005-06-25 14:55:33 -07001347
1348 if (user_len > 0x10000)
Heiko Carstens496da0d2017-10-13 09:06:29 +02001349 user_len = 0x10000;
1350 if (*offset != 0) {
Michael Holzheu66a464d2005-06-25 14:55:33 -07001351 rc = -EPIPE;
Heiko Carstens496da0d2017-10-13 09:06:29 +02001352 goto out;
Michael Holzheu66a464d2005-06-25 14:55:33 -07001353 }
Heiko Carstens496da0d2017-10-13 09:06:29 +02001354 if (copy_from_user(input_buf, user_buf, 1)) {
1355 rc = -EFAULT;
1356 goto out;
1357 }
1358 if (input_buf[0] == '-') {
1359 debug_flush(id, DEBUG_FLUSH_ALL);
1360 goto out;
1361 }
1362 if (isdigit(input_buf[0])) {
1363 int area = ((int) input_buf[0] - (int) '0');
1364
1365 debug_flush(id, area);
1366 goto out;
1367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Michael Holzheuc5612c12008-12-25 13:39:43 +01001369 pr_info("Flushing debug data failed because %c is not a valid "
1370 "area\n", input_buf[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Michael Holzheu66a464d2005-06-25 14:55:33 -07001372out:
Heiko Carstens496da0d2017-10-13 09:06:29 +02001373 *offset += user_len;
1374 return rc; /* number of input characters */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375}
1376
1377/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 * prints debug data in hex/ascii format
1379 */
Heiko Carstens496da0d2017-10-13 09:06:29 +02001380static int debug_hex_ascii_format_fn(debug_info_t *id, struct debug_view *view,
1381 char *out_buf, const char *in_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382{
1383 int i, rc = 0;
1384
Heiko Carstens496da0d2017-10-13 09:06:29 +02001385 for (i = 0; i < id->buf_size; i++)
1386 rc += sprintf(out_buf + rc, "%02x ", ((unsigned char *) in_buf)[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 rc += sprintf(out_buf + rc, "| ");
1388 for (i = 0; i < id->buf_size; i++) {
1389 unsigned char c = in_buf[i];
Heiko Carstens496da0d2017-10-13 09:06:29 +02001390
Michael Holzheu3ab121a2012-03-11 11:59:32 -04001391 if (isascii(c) && isprint(c))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 rc += sprintf(out_buf + rc, "%c", c);
Michael Holzheu3ab121a2012-03-11 11:59:32 -04001393 else
1394 rc += sprintf(out_buf + rc, ".");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 }
1396 rc += sprintf(out_buf + rc, "\n");
1397 return rc;
1398}
1399
1400/*
1401 * prints header for debug entry
1402 */
Heiko Carstens496da0d2017-10-13 09:06:29 +02001403int debug_dflt_header_fn(debug_info_t *id, struct debug_view *view,
1404 int area, debug_entry_t *entry, char *out_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405{
Mikhail Zaslonko0990d832020-05-05 10:34:52 +02001406 unsigned long sec, usec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 unsigned long caller;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 unsigned int level;
Heiko Carstens496da0d2017-10-13 09:06:29 +02001409 char *except_str;
1410 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Mikhail Zaslonko0990d832020-05-05 10:34:52 +02001412 level = entry->level;
1413 sec = entry->clock;
Martin Schwidefskyea417aa2016-11-16 09:48:41 +01001414 usec = do_div(sec, USEC_PER_SEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
Mikhail Zaslonko0990d832020-05-05 10:34:52 +02001416 if (entry->exception)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 except_str = "*";
1418 else
1419 except_str = "-";
Heiko Carstens9cb1cce2016-01-18 13:12:19 +01001420 caller = (unsigned long) entry->caller;
Mikhail Zaslonko0990d832020-05-05 10:34:52 +02001421 rc += sprintf(out_buf, "%02i %011ld:%06lu %1u %1s %04u %pK ",
Martin Schwidefskyea417aa2016-11-16 09:48:41 +01001422 area, sec, usec, level, except_str,
Mikhail Zaslonko0990d832020-05-05 10:34:52 +02001423 entry->cpu, (void *)caller);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 return rc;
1425}
Heiko Carstensd5274362012-07-24 14:27:53 +02001426EXPORT_SYMBOL(debug_dflt_header_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
1428/*
1429 * prints debug data sprintf-formated:
1430 * debug_sprinf_event/exception calls must be used together with this view
1431 */
1432
1433#define DEBUG_SPRINTF_MAX_ARGS 10
1434
Heiko Carstens496da0d2017-10-13 09:06:29 +02001435static int debug_sprintf_format_fn(debug_info_t *id, struct debug_view *view,
1436 char *out_buf, debug_sprintf_entry_t *curr_event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437{
Heiko Carstens496da0d2017-10-13 09:06:29 +02001438 int num_longs, num_used_args = 0, i, rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 int index[DEBUG_SPRINTF_MAX_ARGS];
1440
1441 /* count of longs fit into one entry */
Heiko Carstens496da0d2017-10-13 09:06:29 +02001442 num_longs = id->buf_size / sizeof(long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Heiko Carstens496da0d2017-10-13 09:06:29 +02001444 if (num_longs < 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 goto out; /* bufsize of entry too small */
Heiko Carstens496da0d2017-10-13 09:06:29 +02001446 if (num_longs == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 /* no args, we use only the string */
1448 strcpy(out_buf, curr_event->string);
1449 rc = strlen(curr_event->string);
1450 goto out;
1451 }
1452
1453 /* number of arguments used for sprintf (without the format string) */
Heiko Carstens496da0d2017-10-13 09:06:29 +02001454 num_used_args = min(DEBUG_SPRINTF_MAX_ARGS, (num_longs - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Heiko Carstens496da0d2017-10-13 09:06:29 +02001456 memset(index, 0, DEBUG_SPRINTF_MAX_ARGS * sizeof(int));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
Heiko Carstens496da0d2017-10-13 09:06:29 +02001458 for (i = 0; i < num_used_args; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 index[i] = i;
1460
Heiko Carstens496da0d2017-10-13 09:06:29 +02001461 rc = sprintf(out_buf, curr_event->string, curr_event->args[index[0]],
1462 curr_event->args[index[1]], curr_event->args[index[2]],
1463 curr_event->args[index[3]], curr_event->args[index[4]],
1464 curr_event->args[index[5]], curr_event->args[index[6]],
1465 curr_event->args[index[7]], curr_event->args[index[8]],
1466 curr_event->args[index[9]]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 return rc;
1469}
1470
1471/*
Heiko Carstensd5274362012-07-24 14:27:53 +02001472 * debug_init:
1473 * - is called exactly once to initialize the debug feature
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 */
Heiko Carstensd5274362012-07-24 14:27:53 +02001475static int __init debug_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476{
Heiko Carstensd5274362012-07-24 14:27:53 +02001477 s390dbf_sysctl_header = register_sysctl_table(s390dbf_dir_table);
1478 mutex_lock(&debug_mutex);
1479 debug_debugfs_root_entry = debugfs_create_dir(DEBUG_DIR_ROOT, NULL);
1480 initialized = 1;
1481 mutex_unlock(&debug_mutex);
1482 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483}
Michael Holzheu66a464d2005-06-25 14:55:33 -07001484postcore_initcall(debug_init);