Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp329910ybk; Sat, 9 May 2020 04:16:50 -0700 (PDT) X-Google-Smtp-Source: APiQypILAj7QdJIUR8uTWQpcYGW1GNq4iCcxDQIPvT7O+wWCLshF4As1jH0QWI2mjcOYw6mx0MMZ X-Received: by 2002:a17:906:1292:: with SMTP id k18mr5627048ejb.132.1589023009968; Sat, 09 May 2020 04:16:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589023009; cv=none; d=google.com; s=arc-20160816; b=Lb3ULRcVf2Rl6IqAKPwd5dXl3SUqdnL4yT2F3zRjMMh+DaGbmKFLs/MsmwHq3WugzQ ORFY501iMFefI9YMH347sH4TGAN4OI3IAUO1NjDOX+6dBnxDOhg/+JBYwRCbjw1PeBzo 9wCxgdIknaFR33R9Hqr3PbGFQ5jXvq/A59mVFsFmmBVZs/n0qKQ8NTzkALn3nGTIA+8q XDyP4k7JRkL/8i3rH8sQKNB/XWrQ3LRoQqC8AGdUMIKHU0Hu4zE4KJR4NCzvztbbH+ob wFZmB386lZOcHDqdnF9pif4kx9YXthmI2ObU/1t5ihWCL26xjFSghtOH5DLip0Pgbsfp ZHAA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from; bh=7cM+LtOSl3T3hkoRi0DeLX/1VYM0+Ot9vSN4hG6kRn0=; b=eMJY2Ot9svEScJhqvRRczCvYMA9tI3nYNUMcRoh3MiCsAjusmfqfMcJ/2lysW/Y2qf uhFkWG7dGhskVP589OLKqZe7kElPexerZeanLgiaHzGscXrq0g+MbMZcf/d4CA4l66zW +hcWkWfF0DYrSCJRV/L04WCwvZrcIW57wuFQQkvFLV6V8aPB96xJ1t3skFKmYNQoIAn0 Pum9I9r9j9v115I8SsuFzhVRo5t1IHhi+rRTjSa4zcpjlmWp2+puPGPqkF3WoiDd5E5k 8RzWWoE9XYx+fpEYOO5ArEhtZuny4RwJJsr8uuJbS5aFxmO1Gw2ZMPANc5CPctRBdfRh 7RIg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h5si2547192ejq.430.2020.05.09.04.16.26; Sat, 09 May 2020 04:16:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728370AbgEILO6 (ORCPT + 99 others); Sat, 9 May 2020 07:14:58 -0400 Received: from smtp2207-205.mail.aliyun.com ([121.197.207.205]:38474 "EHLO smtp2207-205.mail.aliyun.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728272AbgEILOz (ORCPT ); Sat, 9 May 2020 07:14:55 -0400 X-Alimail-AntiSpam: AC=CONTINUE;BC=0.07436282|-1;CH=green;DM=|CONTINUE|false|;DS=CONTINUE|ham_system_inform|0.0409161-0.000200955-0.958883;FP=0|0|0|0|0|-1|-1|-1;HT=e02c03300;MF=liaoweixiong@allwinnertech.com;NM=1;PH=DS;RN=20;RT=20;SR=0;TI=SMTPD_---.HVopGr-_1589022874; Received: from PC-liaoweixiong.allwinnertech.com(mailfrom:liaoweixiong@allwinnertech.com fp:SMTPD_---.HVopGr-_1589022874) by smtp.aliyun-inc.com(10.147.44.129); Sat, 09 May 2020 19:14:47 +0800 From: WeiXiong Liao To: Kees Cook , Anton Vorontsov , Colin Cross , Tony Luck , Jonathan Corbet , Miquel Raynal , Richard Weinberger , Vignesh Raghavendra , Petr Mladek , Sergey Senozhatsky , Steven Rostedt , Mauro Carvalho Chehab , "David S. Miller" , Rob Herring , Greg Kroah-Hartman , Pavel Tatashin Cc: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mtd@lists.infradead.org, WeiXiong Liao Subject: [PATCH v5 07/12] pstore/blk: Add ftrace frontend support Date: Sat, 9 May 2020 19:14:09 +0800 Message-Id: <1589022854-19821-8-git-send-email-liaoweixiong@allwinnertech.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1589022854-19821-1-git-send-email-liaoweixiong@allwinnertech.com> References: <1589022854-19821-1-git-send-email-liaoweixiong@allwinnertech.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Support backend for ftrace. To enable ftrace backend, just make ftrace_size be greater than 0 and a multiple of 4096. Signed-off-by: WeiXiong Liao Link: https://lore.kernel.org/r/1585126506-18635-7-git-send-email-liaoweixiong@allwinnertech.com Signed-off-by: Kees Cook --- fs/pstore/Kconfig | 12 ++++ fs/pstore/blk.c | 9 +++ fs/pstore/zone.c | 170 ++++++++++++++++++++++++++++++++++++++++++++ include/linux/pstore_zone.h | 2 + 4 files changed, 193 insertions(+) diff --git a/fs/pstore/Kconfig b/fs/pstore/Kconfig index f1484f751c5e..16a0440d8d5a 100644 --- a/fs/pstore/Kconfig +++ b/fs/pstore/Kconfig @@ -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 ftarce to store" + depends on PSTORE_BLK + depends on PSTORE_FTRACE + default 64 + help + This just sets size of ftrace (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. diff --git a/fs/pstore/blk.c b/fs/pstore/blk.c index acdeb5d7b5f3..84ca98923d8a 100644 --- a/fs/pstore/blk.c +++ b/fs/pstore/blk.c @@ -40,6 +40,14 @@ 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. * @@ -143,6 +151,7 @@ static int psblk_register_do(struct psblk_device *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; diff --git a/fs/pstore/zone.c b/fs/pstore/zone.c index 427b458aa675..a51594f95347 100644 --- a/fs/pstore/zone.c +++ b/fs/pstore/zone.c @@ -88,11 +88,14 @@ struct pstore_zone { * @opszs: oops/panic storage zones * @ppsz: pmsg storage zone * @cpsz: console storage zone + * @fpszs: ftrace storage zones * @oops_max_cnt: max count of @opszs * @oops_read_cnt: counter to read oops zone * @oops_write_cnt: counter to write * @pmsg_read_cnt: counter to read pmsg zone * @console_read_cnt: counter to read console zone + * @ftrace_max_cnt: max count of @fpszs + * @ftrace_read_cnt: counter to read ftrace zone * @oops_counter: counter to oops * @panic_counter: counter to panic * @recovered: whether finish recovering data from storage @@ -105,11 +108,14 @@ struct psz_context { struct pstore_zone **opszs; struct pstore_zone *ppsz; struct pstore_zone *cpsz; + struct pstore_zone **fpszs; unsigned int oops_max_cnt; unsigned int oops_read_cnt; unsigned int oops_write_cnt; unsigned int pmsg_read_cnt; unsigned int console_read_cnt; + unsigned int ftrace_max_cnt; + unsigned int ftrace_read_cnt; /* * the counter should be recovered when recover. * It records the oops/panic times after burning rather than booting. @@ -312,6 +318,8 @@ static void psz_flush_all_dirty_zones(struct work_struct *work) ret |= psz_flush_dirty_zone(cxt->cpsz); if (cxt->opszs) ret |= psz_flush_dirty_zones(cxt->opszs, cxt->oops_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)); } @@ -548,6 +556,31 @@ static int psz_recover_zone(struct psz_context *cxt, struct pstore_zone *zone) 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 @@ -575,6 +608,10 @@ static inline int psz_recovery(struct psz_context *cxt) if (ret) goto recover_fail; + ret = psz_recover_zones(cxt, cxt->fpszs, cxt->ftrace_max_cnt); + if (ret) + goto recover_fail; + pr_debug("recover end!\n"); atomic_set(&cxt->recovered, 1); return 0; @@ -591,6 +628,7 @@ static int psz_pstore_open(struct pstore_info *psi) cxt->oops_read_cnt = 0; cxt->pmsg_read_cnt = 0; cxt->console_read_cnt = 0; + cxt->ftrace_read_cnt = 0; return 0; } @@ -657,6 +695,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; } } @@ -799,6 +841,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; } @@ -814,6 +863,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; @@ -888,6 +945,98 @@ static ssize_t psz_oops_read(struct pstore_zone *zone, return size + hlen; } +static int psz_ftrace_combine(char *src1_buf, size_t src1_size, + char *src2_buf, size_t src2_size, + char **dest_buf, size_t *dest_size) +{ + size_t src1_off, src2_off, total; + size_t src1_idx = 0, src2_idx = 0, merged_idx = 0; + void *merged_buf; + struct pstore_ftrace_record *mrec, *s1rec, *s2rec; + size_t record_size = sizeof(struct pstore_ftrace_record); + + src1_off = src1_size % record_size; + src1_size -= src1_off; + + src2_off = src2_size % record_size; + src2_size -= src2_off; + + total = src1_size + src2_size; + merged_buf = kmalloc(total, GFP_KERNEL); + if (!merged_buf) + return -ENOMEM; + + s1rec = (struct pstore_ftrace_record *)(src1_buf + src1_off); + s2rec = (struct pstore_ftrace_record *)(src2_buf + src2_off); + mrec = (struct pstore_ftrace_record *)(merged_buf); + + while (src1_size > 0 && src2_size > 0) { + u64 s1_ts, s2_ts; + + s1_ts = pstore_ftrace_read_timestamp(&s1rec[src1_idx]); + s2_ts = pstore_ftrace_read_timestamp(&s2rec[src2_idx]); + if (s1_ts < s2_ts) { + mrec[merged_idx++] = s1rec[src1_idx++]; + src1_size -= record_size; + } else { + mrec[merged_idx++] = s2rec[src2_idx++]; + src2_size -= record_size; + } + } + + while (src1_size > 0) { + mrec[merged_idx++] = s1rec[src1_idx++]; + src1_size -= record_size; + } + + while (src2_size > 0) { + mrec[merged_idx++] = s2rec[src2_idx++]; + src2_size -= record_size; + } + + *dest_buf = merged_buf; + *dest_size = total; + return 0; +} + +/* try to combine all ftrace zones */ +static ssize_t psz_ftrace_read(struct pstore_zone *zone, + struct pstore_record *record) +{ + struct psz_context *cxt = record->psi->data; + struct psz_buffer *buf; + char *dest; + size_t dest_size; + 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 = psz_ftrace_combine(record->buf, record->size, + (char *)buf->data, atomic_read(&buf->datalen), + &dest, &dest_size); + if (unlikely(ret)) + return ret; + + kfree(record->buf); + record->buf = dest; + record->size = dest_size; + +out: + 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) { @@ -938,6 +1087,9 @@ static ssize_t psz_pstore_read(struct pstore_record *record) readop = psz_oops_read; record->id = cxt->oops_read_cnt - 1; break; + case PSTORE_TYPE_FTRACE: + readop = psz_ftrace_read; + break; case PSTORE_TYPE_CONSOLE: fallthrough; case PSTORE_TYPE_PMSG: @@ -1090,6 +1242,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 int psz_alloc_zones(struct psz_context *cxt) @@ -1114,6 +1268,16 @@ 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); + goto free_out; + } + cxt->opszs = psz_init_zones(PSTORE_TYPE_DMESG, &off, info->total_size - off_size, info->kmsg_size, &cxt->oops_max_cnt); @@ -1176,6 +1340,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 @@ -1204,6 +1369,7 @@ int register_pstore_zone(struct pstore_zone_info *info) pr_debug("\toops 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) { @@ -1240,6 +1406,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); diff --git a/include/linux/pstore_zone.h b/include/linux/pstore_zone.h index da294e6d7661..94f441b8b616 100644 --- a/include/linux/pstore_zone.h +++ b/include/linux/pstore_zone.h @@ -19,6 +19,7 @@ * @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; psz_read_op read; psz_write_op write; psz_write_op panic_write; -- 1.9.1