1833 lines · 45.3 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0/* *   S/390 debug facility * *    Copyright IBM Corp. 1999, 2020 * *    Author(s): Michael Holzheu (holzheu@de.ibm.com), *		 Holger Smolinski (Holger.Smolinski@de.ibm.com) * *    Bugreports to: <Linux390@de.ibm.com> */ #define pr_fmt(fmt) "s390dbf: " fmt #include <linux/stddef.h>#include <linux/kernel.h>#include <linux/errno.h>#include <linux/slab.h>#include <linux/ctype.h>#include <linux/string.h>#include <linux/sysctl.h>#include <linux/uaccess.h>#include <linux/export.h>#include <linux/init.h>#include <linux/fs.h>#include <linux/math.h>#include <linux/minmax.h>#include <linux/debugfs.h>#include <linux/glob.h>#include <linux/stringify.h> #include <asm/debug.h> #define DEBUG_PROLOG_ENTRY -1 #define ALL_AREAS 0 /* copy all debug areas */#define NO_AREAS  1 /* copy no debug areas */ /* typedefs */ typedef struct file_private_info {	loff_t offset;			/* offset of last read in file */	int    act_area;		/* number of last formatted area */	int    act_page;		/* act page in given area */	int    act_entry;		/* last formatted entry (offset */					/* relative to beginning of last */					/* formatted page) */	size_t act_entry_offset;	/* up to this offset we copied */					/* in last read the last formatted */					/* entry to userland */	char   temp_buf[2048];		/* buffer for output */	debug_info_t *debug_info_org;	/* original debug information */	debug_info_t *debug_info_snap;	/* snapshot of debug information */	struct debug_view *view;	/* used view of debug info */} file_private_info_t; typedef struct {	char *string;	/*	 * This assumes that all args are converted into longs	 * on L/390 this is the case for all types of parameter	 * except of floats, and long long (32 bit)	 *	 */	long args[];} debug_sprintf_entry_t; /* internal function prototypes */ static int debug_init(void);static ssize_t debug_output(struct file *file, char __user *user_buf,			    size_t user_len, loff_t *offset);static ssize_t debug_input(struct file *file, const char __user *user_buf,			   size_t user_len, loff_t *offset);static int debug_open(struct inode *inode, struct file *file);static int debug_close(struct inode *inode, struct file *file);static debug_info_t *debug_info_create(const char *name, int pages_per_area,				       int nr_areas, int buf_size, umode_t mode);static void debug_info_get(debug_info_t *);static void debug_info_put(debug_info_t *);static int debug_prolog_level_fn(debug_info_t *id,				 struct debug_view *view, char *out_buf,				 size_t out_buf_size);static int debug_input_level_fn(debug_info_t *id, struct debug_view *view,				struct file *file, const char __user *user_buf,				size_t user_buf_size, loff_t *offset);static int debug_prolog_pages_fn(debug_info_t *id,				 struct debug_view *view, char *out_buf,				 size_t out_buf_size);static int debug_input_pages_fn(debug_info_t *id, struct debug_view *view,				struct file *file, const char __user *user_buf,				size_t user_buf_size, loff_t *offset);static int debug_input_flush_fn(debug_info_t *id, struct debug_view *view,				struct file *file, const char __user *user_buf,				size_t user_buf_size, loff_t *offset);static int debug_hex_ascii_format_fn(debug_info_t *id, struct debug_view *view,				     char *out_buf, size_t out_buf_size,				     const char *in_buf);static void debug_areas_swap(debug_info_t *a, debug_info_t *b);static void debug_events_append(debug_info_t *dest, debug_info_t *src); /* globals */ struct debug_view debug_hex_ascii_view = {	"hex_ascii",	NULL,	&debug_dflt_header_fn,	&debug_hex_ascii_format_fn,	NULL,	NULL};EXPORT_SYMBOL(debug_hex_ascii_view); static struct debug_view debug_level_view = {	"level",	&debug_prolog_level_fn,	NULL,	NULL,	&debug_input_level_fn,	NULL}; static struct debug_view debug_pages_view = {	"pages",	&debug_prolog_pages_fn,	NULL,	NULL,	&debug_input_pages_fn,	NULL}; static struct debug_view debug_flush_view = {	"flush",	NULL,	NULL,	NULL,	&debug_input_flush_fn,	NULL}; struct debug_view debug_sprintf_view = {	"sprintf",	NULL,	&debug_dflt_header_fn,	&debug_sprintf_format_fn,	NULL,	NULL};EXPORT_SYMBOL(debug_sprintf_view); /* used by dump analysis tools to determine version of debug feature */static unsigned int __used debug_feature_version = __DEBUG_FEATURE_VERSION; /* static globals */ static debug_info_t *debug_area_first;static debug_info_t *debug_area_last;static DEFINE_MUTEX(debug_mutex);extern debug_info_t *__s390dbf_info[], *__s390dbf_info_end[]; static int initialized;static int debug_critical; static const struct file_operations debug_file_ops = {	.owner	 = THIS_MODULE,	.read	 = debug_output,	.write	 = debug_input,	.open	 = debug_open,	.release = debug_close,}; static struct dentry *debug_debugfs_root_entry; /* List of debug area parameters to override */#define PARAM_UNSET	-2#define PARAM_NUM	16static struct debug_param_t {	char name[DEBUG_MAX_NAME_LEN + 1];	int level;	int pages;} debug_param[PARAM_NUM];static int debug_param_num; /* functions */static void debug_get_param(const char *name, int *level, int *pages){	struct debug_param_t *p;	int i; 	for (i = 0; i < debug_param_num; i++) {		p = &debug_param[i];		if (!glob_match(p->name, name))			continue;		if (level && p->level != PARAM_UNSET) {			pr_info("%s: override level to %d\n", name, p->level);			*level = p->level;		}		if (pages && p->pages != PARAM_UNSET) {			pr_info("%s: override pages to %d\n", name, p->pages);			*pages = p->pages;		}	}} #define LVL_LEN		10#define LVL_FMT		"%" __stringify(LVL_LEN) "[^:]"#define NAME_FMT	"%" __stringify(DEBUG_MAX_NAME_LEN) "[^:]" static bool __init s390dbf_parse_one(const char *arg, struct debug_param_t *param){	struct debug_param_t p = { { 0 }, PARAM_UNSET, PARAM_UNSET };	char level[LVL_LEN + 1] = { 0 }; 	/* arg: <name|pattern>:[<level>|-]:[<pages>] */	if (sscanf(arg, NAME_FMT ":" LVL_FMT ":%d", p.name, level, &p.pages) > 1) {		if (strcmp(level, "-") == 0)			p.level = DEBUG_OFF_LEVEL;		else if (kstrtoint(level, 0, &p.level) != 0)			return false;	} else if (sscanf(arg, NAME_FMT "::%d", p.name, &p.pages) != 2) {		return false;	} 	if (p.level != PARAM_UNSET && p.level != DEBUG_OFF_LEVEL &&	    (p.level < 0 || p.level > DEBUG_MAX_LEVEL))		return false;	if (p.pages != PARAM_UNSET && p.pages < 0)		return false;	*param = p; 	return true;} static int __init s390dbf_parse(char *arg){	debug_info_t **id;	int i, rc = 0; 	while (arg && debug_param_num < PARAM_NUM) {		if (s390dbf_parse_one(arg, &debug_param[debug_param_num]))			debug_param_num++;		else			rc = -EINVAL;		arg = strchr(arg, ',');		if (arg)			arg++;	} 	/*	 * Apply level to static debug areas, delay buffer size changes until	 * regular memory allocations are possible.	 */	for (i = 0, id = __s390dbf_info; &id[i] < __s390dbf_info_end; i++)		debug_get_param(id[i]->name, &id[i]->level, NULL); 	return rc;}early_param("s390dbf", s390dbf_parse); /* * debug_areas_alloc * - Debug areas are implemented as a threedimensonal array: *   areas[areanumber][pagenumber][pageoffset] */ static debug_entry_t ***debug_areas_alloc(int pages_per_area, int nr_areas){	debug_entry_t ***areas;	int i, j; 	areas = kmalloc_objs(debug_entry_t **, nr_areas);	if (!areas)		goto fail_malloc_areas;	for (i = 0; i < nr_areas; i++) {		/* GFP_NOWARN to avoid user triggerable WARN, we handle fails */		areas[i] = kmalloc_objs(debug_entry_t *, pages_per_area,					GFP_KERNEL | __GFP_NOWARN);		if (!areas[i])			goto fail_malloc_areas2;		for (j = 0; j < pages_per_area; j++) {			areas[i][j] = kzalloc(PAGE_SIZE, GFP_KERNEL);			if (!areas[i][j]) {				for (j--; j >= 0 ; j--)					kfree(areas[i][j]);				kfree(areas[i]);				goto fail_malloc_areas2;			}		}	}	return areas; fail_malloc_areas2:	for (i--; i >= 0; i--) {		for (j = 0; j < pages_per_area; j++)			kfree(areas[i][j]);		kfree(areas[i]);	}	kfree(areas);fail_malloc_areas:	return NULL;} /* * debug_info_alloc * - alloc new debug-info */static debug_info_t *debug_info_alloc(const char *name, int pages_per_area,				      int nr_areas, int buf_size, int level,				      int mode){	debug_info_t *rc; 	/* alloc everything */	rc = kmalloc_obj(debug_info_t);	if (!rc)		goto fail_malloc_rc;	rc->active_entries = kzalloc_objs(int, nr_areas);	if (!rc->active_entries)		goto fail_malloc_active_entries;	rc->active_pages = kzalloc_objs(int, nr_areas);	if (!rc->active_pages)		goto fail_malloc_active_pages;	if ((mode == ALL_AREAS) && (pages_per_area != 0)) {		rc->areas = debug_areas_alloc(pages_per_area, nr_areas);		if (!rc->areas)			goto fail_malloc_areas;	} else {		rc->areas = NULL;	} 	/* initialize members */	raw_spin_lock_init(&rc->lock);	rc->pages_per_area = pages_per_area;	rc->nr_areas	   = nr_areas;	rc->active_area    = 0;	rc->level	   = level;	rc->buf_size	   = buf_size;	rc->entry_size	   = sizeof(debug_entry_t) + buf_size;	strscpy(rc->name, name);	memset(rc->views, 0, DEBUG_MAX_VIEWS * sizeof(struct debug_view *));	memset(rc->debugfs_entries, 0, DEBUG_MAX_VIEWS * sizeof(struct dentry *));	refcount_set(&(rc->ref_count), 0); 	return rc; fail_malloc_areas:	kfree(rc->active_pages);fail_malloc_active_pages:	kfree(rc->active_entries);fail_malloc_active_entries:	kfree(rc);fail_malloc_rc:	return NULL;} /* * debug_areas_free * - free all debug areas */static void debug_areas_free(debug_info_t *db_info){	int i, j; 	if (!db_info->areas)		return;	for (i = 0; i < db_info->nr_areas; i++) {		for (j = 0; j < db_info->pages_per_area; j++)			kfree(db_info->areas[i][j]);		kfree(db_info->areas[i]);	}	kfree(db_info->areas);	db_info->areas = NULL;} /* * debug_info_free * - free memory debug-info */static void debug_info_free(debug_info_t *db_info){	debug_areas_free(db_info);	kfree(db_info->active_entries);	kfree(db_info->active_pages);	kfree(db_info);} /* * debug_info_create * - create new debug-info */ static debug_info_t *debug_info_create(const char *name, int pages_per_area,				       int nr_areas, int buf_size, umode_t mode){	int level = DEBUG_DEFAULT_LEVEL;	debug_info_t *rc; 	debug_get_param(name, &level, &pages_per_area);	rc = debug_info_alloc(name, pages_per_area, nr_areas, buf_size, level, ALL_AREAS);	if (!rc)		goto out; 	rc->mode = mode & ~S_IFMT;	refcount_set(&rc->ref_count, 1);out:	return rc;} /* * debug_info_copy * - copy debug-info */static debug_info_t *debug_info_copy(debug_info_t *in, int mode){	unsigned long flags;	debug_info_t *rc;	int i, j; 	/* get a consistent copy of the debug areas */	do {		rc = debug_info_alloc(in->name, in->pages_per_area,			in->nr_areas, in->buf_size, in->level, mode);		raw_spin_lock_irqsave(&in->lock, flags);		if (!rc)			goto out;		/* has something changed in the meantime ? */		if ((rc->pages_per_area == in->pages_per_area) &&		    (rc->nr_areas == in->nr_areas)) {			break;		}		raw_spin_unlock_irqrestore(&in->lock, flags);		debug_info_free(rc);	} while (1); 	if (mode == NO_AREAS)		goto out; 	for (i = 0; i < in->nr_areas; i++) {		for (j = 0; j < in->pages_per_area; j++)			memcpy(rc->areas[i][j], in->areas[i][j], PAGE_SIZE);		rc->active_pages[i] = in->active_pages[i];		rc->active_entries[i] = in->active_entries[i];	}	rc->active_area = in->active_area;out:	raw_spin_unlock_irqrestore(&in->lock, flags);	return rc;} /* * debug_info_get * - increments reference count for debug-info */static void debug_info_get(debug_info_t *db_info){	if (db_info)		refcount_inc(&db_info->ref_count);} /* * debug_info_put: * - decreases reference count for debug-info and frees it if necessary */static void debug_info_put(debug_info_t *db_info){	if (!db_info)		return;	if (refcount_dec_and_test(&db_info->ref_count))		debug_info_free(db_info);} /* * debug_format_entry: * - format one debug entry and return size of formatted data */static int debug_format_entry(file_private_info_t *p_info){	debug_info_t *id_snap	= p_info->debug_info_snap;	struct debug_view *view = p_info->view;	debug_entry_t *act_entry;	size_t len = 0; 	if (p_info->act_entry == DEBUG_PROLOG_ENTRY) {		/* print prolog */		if (view->prolog_proc) {			len += view->prolog_proc(id_snap, view, p_info->temp_buf,						 sizeof(p_info->temp_buf));		}		goto out;	}	if (!id_snap->areas) /* this is true, if we have a prolog only view */		goto out;    /* or if 'pages_per_area' is 0 */	act_entry = (debug_entry_t *) ((char *)id_snap->areas[p_info->act_area]				       [p_info->act_page] + p_info->act_entry); 	if (act_entry->clock == 0LL)		goto out; /* empty entry */	if (view->header_proc) {		len += view->header_proc(id_snap, view, p_info->act_area,					 act_entry, p_info->temp_buf + len,

Lines 1–500 of 1833.