Commit 34327e9f authored by WeiXiong Liao's avatar WeiXiong Liao Committed by Kees Cook
Browse files

pstore/zone,blk: Add ftrace frontend support



Support backend for ftrace. To enable ftrace backend, just make
ftrace_size be greater than 0 and a multiple of 4096.

Signed-off-by: default avatarWeiXiong Liao <liaoweixiong@allwinnertech.com>
Link: https://lore.kernel.org/lkml/20200511233229.27745-6-keescook@chromium.org/


Co-developed-by: default avatarColin Ian King <colin.king@canonical.com>
Signed-off-by: default avatarColin Ian King <colin.king@canonical.com>
Link: https://lore.kernel.org/lkml/20200512170719.221514-1-colin.king@canonical.com


Signed-off-by: default avatarKees Cook <keescook@chromium.org>
parent cc9c4d1b
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -248,3 +248,15 @@ config PSTORE_BLK_CONSOLE_SIZE

	  NOTE that, both Kconfig and module parameters can configure
	  pstore/blk, but module parameters have priority over Kconfig.

config PSTORE_BLK_FTRACE_SIZE
	int "Size in Kbytes of ftrace log to store"
	depends on PSTORE_BLK
	depends on PSTORE_FTRACE
	default 64
	help
	  This just sets size of ftrace log (ftrace_size) for pstore/blk. The
	  size is in KB and must be a multiple of 4.

	  NOTE that, both Kconfig and module parameters can configure
	  pstore/blk, but module parameters have priority over Kconfig.
+9 −0
Original line number Diff line number Diff line
@@ -43,6 +43,14 @@ static long console_size = -1;
module_param(console_size, long, 0400);
MODULE_PARM_DESC(console_size, "console size in kbytes");

#if IS_ENABLED(CONFIG_PSTORE_FTRACE)
static long ftrace_size = CONFIG_PSTORE_BLK_FTRACE_SIZE;
#else
static long ftrace_size = -1;
#endif
module_param(ftrace_size, long, 0400);
MODULE_PARM_DESC(ftrace_size, "ftrace size in kbytes");

/*
 * blkdev - the block device to use for pstore storage
 *
@@ -152,6 +160,7 @@ static int psblk_register_do(struct pstore_device_info *dev)
	verify_size(kmsg_size, 4096, dev->flags & PSTORE_FLAGS_DMESG);
	verify_size(pmsg_size, 4096, dev->flags & PSTORE_FLAGS_PMSG);
	verify_size(console_size, 4096, dev->flags & PSTORE_FLAGS_CONSOLE);
	verify_size(ftrace_size, 4096, dev->flags & PSTORE_FLAGS_FTRACE);
#undef verify_size

	pstore_zone_info->total_size = dev->total_size;
+113 −1
Original line number Diff line number Diff line
@@ -92,11 +92,14 @@ struct pstore_zone {
 * @kpszs: kmsg dump storage zones
 * @ppsz: pmsg storage zone
 * @cpsz: console storage zone
 * @fpszs: ftrace storage zones
 * @kmsg_max_cnt: max count of @kpszs
 * @kmsg_read_cnt: counter of total read kmsg dumps
 * @kmsg_write_cnt: counter of total kmsg dump writes
 * @pmsg_read_cnt: counter of total read pmsg zone
 * @console_read_cnt: counter of total read console zone
 * @ftrace_max_cnt: max count of @fpszs
 * @ftrace_read_cnt: counter of max read ftrace zone
 * @oops_counter: counter of oops dumps
 * @panic_counter: counter of panic dumps
 * @recovered: whether finished recovering data from storage
@@ -109,11 +112,14 @@ struct psz_context {
	struct pstore_zone **kpszs;
	struct pstore_zone *ppsz;
	struct pstore_zone *cpsz;
	struct pstore_zone **fpszs;
	unsigned int kmsg_max_cnt;
	unsigned int kmsg_read_cnt;
	unsigned int kmsg_write_cnt;
	unsigned int pmsg_read_cnt;
	unsigned int console_read_cnt;
	unsigned int ftrace_max_cnt;
	unsigned int ftrace_read_cnt;
	/*
	 * These counters should be calculated during recovery.
	 * It records the oops/panic times after crashes rather than boots.
@@ -314,6 +320,8 @@ static void psz_flush_all_dirty_zones(struct work_struct *work)
		ret |= psz_flush_dirty_zone(cxt->cpsz);
	if (cxt->kpszs)
		ret |= psz_flush_dirty_zones(cxt->kpszs, cxt->kmsg_max_cnt);
	if (cxt->fpszs)
		ret |= psz_flush_dirty_zones(cxt->fpszs, cxt->ftrace_max_cnt);
	if (ret && cxt->pstore_zone_info)
		schedule_delayed_work(&psz_cleaner, msecs_to_jiffies(1000));
}
@@ -550,6 +558,31 @@ free_oldbuf:
	return ret;
}

static int psz_recover_zones(struct psz_context *cxt,
		struct pstore_zone **zones, unsigned int cnt)
{
	int ret;
	unsigned int i;
	struct pstore_zone *zone;

	if (!zones)
		return 0;

	for (i = 0; i < cnt; i++) {
		zone = zones[i];
		if (unlikely(!zone))
			continue;
		ret = psz_recover_zone(cxt, zone);
		if (ret)
			goto recover_fail;
	}

	return 0;
recover_fail:
	pr_debug("recover %s[%u] failed\n", zone->name, i);
	return ret;
}

/**
 * psz_recovery() - recover data from storage
 * @cxt: the context of pstore/zone
@@ -574,6 +607,10 @@ static inline int psz_recovery(struct psz_context *cxt)
		goto out;

	ret = psz_recover_zone(cxt, cxt->cpsz);
	if (ret)
		goto out;

	ret = psz_recover_zones(cxt, cxt->fpszs, cxt->ftrace_max_cnt);

out:
	if (unlikely(ret))
@@ -592,6 +629,7 @@ static int psz_pstore_open(struct pstore_info *psi)
	cxt->kmsg_read_cnt = 0;
	cxt->pmsg_read_cnt = 0;
	cxt->console_read_cnt = 0;
	cxt->ftrace_read_cnt = 0;
	return 0;
}

@@ -658,6 +696,10 @@ static int psz_pstore_erase(struct pstore_record *record)
		return psz_record_erase(cxt, cxt->ppsz);
	case PSTORE_TYPE_CONSOLE:
		return psz_record_erase(cxt, cxt->cpsz);
	case PSTORE_TYPE_FTRACE:
		if (record->id >= cxt->ftrace_max_cnt)
			return -EINVAL;
		return psz_record_erase(cxt, cxt->fpszs[record->id]);
	default: return -EINVAL;
	}
}
@@ -802,6 +844,13 @@ static int notrace psz_pstore_write(struct pstore_record *record)
		return psz_record_write(cxt->cpsz, record);
	case PSTORE_TYPE_PMSG:
		return psz_record_write(cxt->ppsz, record);
	case PSTORE_TYPE_FTRACE: {
		int zonenum = smp_processor_id();

		if (!cxt->fpszs)
			return -ENOSPC;
		return psz_record_write(cxt->fpszs[zonenum], record);
	}
	default:
		return -EINVAL;
	}
@@ -817,6 +866,14 @@ static struct pstore_zone *psz_read_next_zone(struct psz_context *cxt)
			return zone;
	}

	if (cxt->ftrace_read_cnt < cxt->ftrace_max_cnt)
		/*
		 * No need psz_old_ok(). Let psz_ftrace_read() do so for
		 * combination. psz_ftrace_read() should traverse over
		 * all zones in case of some zone without data.
		 */
		return cxt->fpszs[cxt->ftrace_read_cnt++];

	if (cxt->pmsg_read_cnt == 0) {
		cxt->pmsg_read_cnt++;
		zone = cxt->ppsz;
@@ -891,6 +948,38 @@ static ssize_t psz_kmsg_read(struct pstore_zone *zone,
	return size + hlen;
}

/* try to combine all ftrace zones */
static ssize_t psz_ftrace_read(struct pstore_zone *zone,
		struct pstore_record *record)
{
	struct psz_context *cxt;
	struct psz_buffer *buf;
	int ret;

	if (!zone || !record)
		return -ENOSPC;

	if (!psz_old_ok(zone))
		goto out;

	buf = (struct psz_buffer *)zone->oldbuf;
	if (!buf)
		return -ENOMSG;

	ret = pstore_ftrace_combine_log(&record->buf, &record->size,
			(char *)buf->data, atomic_read(&buf->datalen));
	if (unlikely(ret))
		return ret;

out:
	cxt = record->psi->data;
	if (cxt->ftrace_read_cnt < cxt->ftrace_max_cnt)
		/* then, read next ftrace zone */
		return -ENOMSG;
	record->id = 0;
	return record->size ? record->size : -ENOMSG;
}

static ssize_t psz_record_read(struct pstore_zone *zone,
		struct pstore_record *record)
{
@@ -941,6 +1030,9 @@ next_zone:
		readop = psz_kmsg_read;
		record->id = cxt->kmsg_read_cnt - 1;
		break;
	case PSTORE_TYPE_FTRACE:
		readop = psz_ftrace_read;
		break;
	case PSTORE_TYPE_CONSOLE:
		fallthrough;
	case PSTORE_TYPE_PMSG:
@@ -1005,6 +1097,8 @@ static void psz_free_all_zones(struct psz_context *cxt)
		psz_free_zone(&cxt->ppsz);
	if (cxt->cpsz)
		psz_free_zone(&cxt->cpsz);
	if (cxt->fpszs)
		psz_free_zones(&cxt->fpszs, &cxt->ftrace_max_cnt);
}

static struct pstore_zone *psz_init_zone(enum pstore_type_id type,
@@ -1115,6 +1209,17 @@ static int psz_alloc_zones(struct psz_context *cxt)
		goto free_out;
	}

	off_size += info->ftrace_size;
	cxt->fpszs = psz_init_zones(PSTORE_TYPE_FTRACE, &off,
			info->ftrace_size,
			info->ftrace_size / nr_cpu_ids,
			&cxt->ftrace_max_cnt);
	if (IS_ERR(cxt->fpszs)) {
		err = PTR_ERR(cxt->fpszs);
		cxt->fpszs = NULL;
		goto free_out;
	}

	cxt->kpszs = psz_init_zones(PSTORE_TYPE_DMESG, &off,
			info->total_size - off_size,
			info->kmsg_size, &cxt->kmsg_max_cnt);
@@ -1149,7 +1254,8 @@ int register_pstore_zone(struct pstore_zone_info *info)
		return -EINVAL;
	}

	if (!info->kmsg_size && !info->pmsg_size && !info->console_size) {
	if (!info->kmsg_size && !info->pmsg_size && !info->console_size &&
	    !info->ftrace_size) {
		pr_warn("at least one record size must be non-zero\n");
		return -EINVAL;
	}
@@ -1173,6 +1279,7 @@ int register_pstore_zone(struct pstore_zone_info *info)
	check_size(kmsg_size, SECTOR_SIZE);
	check_size(pmsg_size, SECTOR_SIZE);
	check_size(console_size, SECTOR_SIZE);
	check_size(ftrace_size, SECTOR_SIZE);

#undef check_size

@@ -1200,6 +1307,7 @@ int register_pstore_zone(struct pstore_zone_info *info)
	pr_debug("\tkmsg size : %ld Bytes\n", info->kmsg_size);
	pr_debug("\tpmsg size : %ld Bytes\n", info->pmsg_size);
	pr_debug("\tconsole size : %ld Bytes\n", info->console_size);
	pr_debug("\tftrace size : %ld Bytes\n", info->ftrace_size);

	err = psz_alloc_zones(cxt);
	if (err) {
@@ -1237,6 +1345,10 @@ int register_pstore_zone(struct pstore_zone_info *info)
		cxt->pstore.flags |= PSTORE_FLAGS_CONSOLE;
		pr_cont(" console");
	}
	if (info->ftrace_size) {
		cxt->pstore.flags |= PSTORE_FLAGS_FTRACE;
		pr_cont(" ftrace");
	}
	pr_cont("\n");

	err = pstore_register(&cxt->pstore);
+2 −0
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ typedef ssize_t (*pstore_zone_write_op)(const char *, size_t, loff_t);
 * @max_reason: Maximum kmsg dump reason to store.
 * @pmsg_size:	The size of pmsg zone which is the same as @kmsg_size.
 * @console_size:The size of console zone which is the same as @kmsg_size.
 * @ftrace_size:The size of ftrace zone which is the same as @kmsg_size.
 * @read:	The general read operation. Both of the function parameters
 *		@size and @offset are relative value to storage.
 *		On success, the number of bytes should be returned, others
@@ -37,6 +38,7 @@ struct pstore_zone_info {
	int max_reason;
	unsigned long pmsg_size;
	unsigned long console_size;
	unsigned long ftrace_size;
	pstore_zone_read_op read;
	pstore_zone_write_op write;
	pstore_zone_write_op panic_write;