Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp6986118yba; Thu, 2 May 2019 02:00:58 -0700 (PDT) X-Google-Smtp-Source: APXvYqw2ghpG3i1Z8YFH4mwoz3hpkWNEJxSSG1dG3p59syFYhl3s+41V1bDQ4/KybDCnAEZwQwYN X-Received: by 2002:a17:902:12f:: with SMTP id 44mr2514119plb.193.1556787657997; Thu, 02 May 2019 02:00:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1556787657; cv=none; d=google.com; s=arc-20160816; b=hoyXtoc9DjBT3JPKibqCgcUxMlj9GQtyuP0K20WN5AehbFQavthsD07fSS+Hexvf3+ VdpkuU7Bgz/4NfV2b0ADwQOFGl3YY/9dCAIc0bzvT4/XpikEVACBGDIkHnJp4NWTqpwV bkdNJ0Jh3T/B1xuGk7m1oU99Ab4A6euE/F4VreR9kvqQMSVDSLHH2lWa04Lc3El8xIh3 CsnaiNW+1Vs5y8tA7zZTKIM4bgIC5gig3KJ6aX+Gx52O3Gary0P3Zlj4/EePPElQ3XT9 6TnUKiBkEyQe3FltOYBunNQa1b3TXcJtaiIG4AvEF7XSOIw2uNpLEyS6GpiG+ZhZBU7v D/3Q== 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:dkim-signature; bh=CGcoNMFIuA2c7vDEz3xrp/PFtO6Q2y11E1/I7HpJMZA=; b=IfadsasPlDfdD2LAlivqozMzXJKNGQEFYqztFwZ4k62G4ruWigcPYu0SzuERSdD0V8 6aMBi/ccdgpxoqMCpIGMYkapfex4I/qBHJI6vSQ6k0rqEUVcNPo4w/ErWQ2thW1sel25 tZRf27wm1/cIuDFdgI3CcfvHBrPU+D1Fm+1X7dJFbgr5zA+2oi4b/XcWk3u2prJyHzob Tk0Y9rJzUnCd+yeY0ekRX854l/fFYD/r4C1DzcHskZwWDoSzI4InQGT6n4hF/xhGOBZB RdpLBRue1FTq2r5hHcNCo6cj+V7vvevNlpTC9paDKcffo03DxIVdXIDGB4kGmwGJxJeU 1bSg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=OD0BswP4; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v22si43325102pfm.151.2019.05.02.02.00.42; Thu, 02 May 2019 02:00:57 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=OD0BswP4; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726510AbfEBI7o (ORCPT + 99 others); Thu, 2 May 2019 04:59:44 -0400 Received: from mail-pg1-f194.google.com ([209.85.215.194]:35073 "EHLO mail-pg1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726020AbfEBI7n (ORCPT ); Thu, 2 May 2019 04:59:43 -0400 Received: by mail-pg1-f194.google.com with SMTP id h1so769629pgs.2 for ; Thu, 02 May 2019 01:59:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=CGcoNMFIuA2c7vDEz3xrp/PFtO6Q2y11E1/I7HpJMZA=; b=OD0BswP4PdkegmHLHavWbB8wIX/zxSfYJprS7PUvkY/mp5cPw899NU45XrDAcFbdXk CmuBzm1Qf9/WtkaPqIhXGO1wnXzr7e35Y42eQ//fYuMq7NRAIbJ2TWzAEABxe5uLSoyf kpW3PTQNUDUzVnZYgnvjOvnigKwA1bcyoguc6pRD1tW6D2/izizACkcSXOfSV/+Y4xGi twnzMfQRVdUthUGfA0uTkyelC5ygSDLg9IqV0BctDscxwFbnJgwqpiA6X98/EFobo7zn HC5romyhn47c8RaZLbGgvjVbO5t4j1H/S0HBhijh56NupPYSoZo1LikRnrGSQvIZyVS5 on+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=CGcoNMFIuA2c7vDEz3xrp/PFtO6Q2y11E1/I7HpJMZA=; b=X4z6SSpNihkw0mADfwBj8DyWFtRoyCDDStrZ+naV2pDZTiyr34VGOoQxvrMv6ys5q7 TRJkQhx5h7X+ti81I66Yh2VxDSFxv26fzvXTxS83zxCmiCetqFeVyiB/VQwaQZIHhzYP 1hhxfhhB+wuuJ0pMf9IN7xKEyCpwvmTGFccrk0H8BIyH4yEIlgl2ILRUtdIQ/5BRKx8R G5VB6NIEDIg2V+a2rU0WG9kHl2zHK8nLp9EKmhW9YeNVRDch0iU8nbwhubFsoqEnpLZr d6cyL9nN9k01hgc/tGpZ+aBTtVWLDlVn9ZgZN6G7k4hUKhalSEGeYFPbr8TOkrYPd7JM WEWA== X-Gm-Message-State: APjAAAWuhb1zKmySsTkZBa2yo3U/9jZjVysnJY2yV6TSCR9scMfDpEQX Rpdn4rwOGa3z+oJ9A2s2ViA= X-Received: by 2002:a63:f315:: with SMTP id l21mr2700948pgh.417.1556787582342; Thu, 02 May 2019 01:59:42 -0700 (PDT) Received: from localhost.localdomain ([240f:34:212d:1:1b24:991b:df50:ea3f]) by smtp.gmail.com with ESMTPSA id z7sm74960831pgh.81.2019.05.02.01.59.39 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 02 May 2019 01:59:41 -0700 (PDT) From: Akinobu Mita To: linux-nvme@lists.infradead.org, linux-kernel@vger.kernel.org Cc: Akinobu Mita , Johannes Berg , Keith Busch , Jens Axboe , Christoph Hellwig , Sagi Grimberg Subject: [PATCH 2/4] devcoredump: allow to create several coredump files in one device Date: Thu, 2 May 2019 17:59:19 +0900 Message-Id: <1556787561-5113-3-git-send-email-akinobu.mita@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1556787561-5113-1-git-send-email-akinobu.mita@gmail.com> References: <1556787561-5113-1-git-send-email-akinobu.mita@gmail.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The device coredump mechanism currently allows drivers to create only a single coredump file. If there are several binary blobs to dump, we need to define a binary format or conver to text format in order to put them into a single coredump file. This provides a new function that allows drivers to create several device coredump files in one crashed device. Cc: Johannes Berg Cc: Keith Busch Cc: Jens Axboe Cc: Christoph Hellwig Cc: Sagi Grimberg Signed-off-by: Akinobu Mita --- drivers/base/devcoredump.c | 162 ++++++++++++++++++++++++++++++-------------- include/linux/devcoredump.h | 33 +++++++++ 2 files changed, 146 insertions(+), 49 deletions(-) diff --git a/drivers/base/devcoredump.c b/drivers/base/devcoredump.c index 3c960a6..30ddc5e 100644 --- a/drivers/base/devcoredump.c +++ b/drivers/base/devcoredump.c @@ -25,14 +25,18 @@ static bool devcd_disabled; /* if data isn't read by userspace after 5 minutes then delete it */ #define DEVCD_TIMEOUT (HZ * 60 * 5) -struct devcd_entry { - struct device devcd_dev; - void *data; - size_t datalen; - struct module *owner; +struct devcd_file { + struct bin_attribute bin_attr; ssize_t (*read)(char *buffer, loff_t offset, size_t count, void *data, size_t datalen); void (*free)(void *data); +}; + +struct devcd_entry { + struct device devcd_dev; + struct devcd_file *files; + int num_files; + struct module *owner; struct delayed_work del_wk; struct device *failing_dev; }; @@ -45,8 +49,15 @@ static struct devcd_entry *dev_to_devcd(struct device *dev) static void devcd_dev_release(struct device *dev) { struct devcd_entry *devcd = dev_to_devcd(dev); + int i; + + for (i = 0; i < devcd->num_files; i++) { + struct devcd_file *file = &devcd->files[i]; + + file->free(file->bin_attr.private); + } + kfree(devcd->files); - devcd->free(devcd->data); module_put(devcd->owner); /* @@ -64,9 +75,15 @@ static void devcd_dev_release(struct device *dev) static void devcd_del(struct work_struct *wk) { struct devcd_entry *devcd; + int i; devcd = container_of(wk, struct devcd_entry, del_wk.work); + for (i = 0; i < devcd->num_files; i++) { + device_remove_bin_file(&devcd->devcd_dev, + &devcd->files[i].bin_attr); + } + device_del(&devcd->devcd_dev); put_device(&devcd->devcd_dev); } @@ -75,10 +92,11 @@ static ssize_t devcd_data_read(struct file *filp, struct kobject *kobj, struct bin_attribute *bin_attr, char *buffer, loff_t offset, size_t count) { - struct device *dev = kobj_to_dev(kobj); - struct devcd_entry *devcd = dev_to_devcd(dev); + struct devcd_file *file = + container_of(bin_attr, struct devcd_file, bin_attr); - return devcd->read(buffer, offset, count, devcd->data, devcd->datalen); + return file->read(buffer, offset, count, bin_attr->private, + bin_attr->size); } static ssize_t devcd_data_write(struct file *filp, struct kobject *kobj, @@ -93,25 +111,6 @@ static ssize_t devcd_data_write(struct file *filp, struct kobject *kobj, return count; } -static struct bin_attribute devcd_attr_data = { - .attr = { .name = "data", .mode = S_IRUSR | S_IWUSR, }, - .size = 0, - .read = devcd_data_read, - .write = devcd_data_write, -}; - -static struct bin_attribute *devcd_dev_bin_attrs[] = { - &devcd_attr_data, NULL, -}; - -static const struct attribute_group devcd_dev_group = { - .bin_attrs = devcd_dev_bin_attrs, -}; - -static const struct attribute_group *devcd_dev_groups[] = { - &devcd_dev_group, NULL, -}; - static int devcd_free(struct device *dev, void *data) { struct devcd_entry *devcd = dev_to_devcd(dev); @@ -157,7 +156,6 @@ static struct class devcd_class = { .name = "devcoredump", .owner = THIS_MODULE, .dev_release = devcd_dev_release, - .dev_groups = devcd_dev_groups, .class_groups = devcd_class_groups, }; @@ -234,30 +232,60 @@ static ssize_t devcd_read_from_sgtable(char *buffer, loff_t offset, offset); } +static struct devcd_entry *devcd_alloc(struct dev_coredumpm_bulk_data *files, + int num_files, gfp_t gfp) +{ + struct devcd_entry *devcd; + int i; + + devcd = kzalloc(sizeof(*devcd), gfp); + if (!devcd) + return NULL; + + devcd->files = kcalloc(num_files, sizeof(devcd->files[0]), gfp); + if (!devcd->files) { + kfree(devcd); + return NULL; + } + devcd->num_files = num_files; + + for (i = 0; i < devcd->num_files; i++) { + struct devcd_file *file = &devcd->files[i]; + + sysfs_bin_attr_init(&file->bin_attr); + file->bin_attr.attr.name = files[i].name; + + file->bin_attr.attr.mode = 0600; + file->bin_attr.size = files[i].datalen; + file->bin_attr.private = files[i].data; + file->bin_attr.read = devcd_data_read; + file->bin_attr.write = devcd_data_write; + + file->read = files[i].read; + file->free = files[i].free; + } + + return devcd; +} + /** - * dev_coredumpm - create device coredump with read/free methods + * dev_coredumpm_bulk - create a number of device coredump files * @dev: the struct device for the crashed device * @owner: the module that contains the read/free functions, use %THIS_MODULE - * @data: data cookie for the @read/@free functions - * @datalen: length of the data * @gfp: allocation flags - * @read: function to read from the given buffer - * @free: function to free the given buffer + * @files: the configuration of device coredump files + * @num_files: the number of device coredump files to create * - * Creates a new device coredump for the given device. If a previous one hasn't - * been read yet, the new coredump is discarded. The data lifetime is determined - * by the device coredump framework and when it is no longer needed the @free - * function will be called to free the data. + * This function allows drivers to create several device coredump files in + * one crashed device. */ -void dev_coredumpm(struct device *dev, struct module *owner, - void *data, size_t datalen, gfp_t gfp, - ssize_t (*read)(char *buffer, loff_t offset, size_t count, - void *data, size_t datalen), - void (*free)(void *data)) +void dev_coredumpm_bulk(struct device *dev, struct module *owner, gfp_t gfp, + struct dev_coredumpm_bulk_data *files, int num_files) { static atomic_t devcd_count = ATOMIC_INIT(0); struct devcd_entry *devcd; struct device *existing; + int i; if (devcd_disabled) goto free; @@ -272,15 +300,11 @@ void dev_coredumpm(struct device *dev, struct module *owner, if (!try_module_get(owner)) goto free; - devcd = kzalloc(sizeof(*devcd), gfp); + devcd = devcd_alloc(files, num_files, gfp); if (!devcd) goto put_module; devcd->owner = owner; - devcd->data = data; - devcd->datalen = datalen; - devcd->read = read; - devcd->free = free; devcd->failing_dev = get_device(dev); device_initialize(&devcd->devcd_dev); @@ -292,6 +316,12 @@ void dev_coredumpm(struct device *dev, struct module *owner, if (device_add(&devcd->devcd_dev)) goto put_device; + for (i = 0; i < devcd->num_files; i++) { + if (device_create_bin_file(&devcd->devcd_dev, + &devcd->files[i].bin_attr)) + /* nothing - some files will be missing */; + } + if (sysfs_create_link(&devcd->devcd_dev.kobj, &dev->kobj, "failing_device")) /* nothing - symlink will be missing */; @@ -309,7 +339,41 @@ void dev_coredumpm(struct device *dev, struct module *owner, put_module: module_put(owner); free: - free(data); + for (i = 0; i < num_files; i++) + files[i].free(files[i].data); +} +EXPORT_SYMBOL_GPL(dev_coredumpm_bulk); + +/** + * dev_coredumpm - create device coredump with read/free methods + * @dev: the struct device for the crashed device + * @owner: the module that contains the read/free functions, use %THIS_MODULE + * @data: data cookie for the @read/@free functions + * @datalen: length of the data + * @gfp: allocation flags + * @read: function to read from the given buffer + * @free: function to free the given buffer + * + * Creates a new device coredump for the given device. If a previous one hasn't + * been read yet, the new coredump is discarded. The data lifetime is determined + * by the device coredump framework and when it is no longer needed the @free + * function will be called to free the data. + */ +void dev_coredumpm(struct device *dev, struct module *owner, + void *data, size_t datalen, gfp_t gfp, + ssize_t (*read)(char *buffer, loff_t offset, size_t count, + void *data, size_t datalen), + void (*free)(void *data)) +{ + struct dev_coredumpm_bulk_data bulk_data = { + .name = "data", + .data = data, + .datalen = datalen, + .read = read, + .free = free, + }; + + dev_coredumpm_bulk(dev, owner, gfp, &bulk_data, 1); } EXPORT_SYMBOL_GPL(dev_coredumpm); diff --git a/include/linux/devcoredump.h b/include/linux/devcoredump.h index 269521f..9addb6f 100644 --- a/include/linux/devcoredump.h +++ b/include/linux/devcoredump.h @@ -65,6 +65,26 @@ static inline void _devcd_free_sgtable(struct scatterlist *table) kfree(delete_iter); } +/** + * struct dev_coredumpm_bulk_data - Data used for dev_coredumpm_bulk + * + * @name: coredump file name + * @data: data cookie for the @read/@free functions + * @datalen: length of the data + * @read: function to read from the given buffer + * @free: function to free the given buffer + * + * An array of this structure is passed as argument to dev_coredumpm_bulk, and + * used to describe each device coredump. + */ +struct dev_coredumpm_bulk_data { + char *name; + void *data; + size_t datalen; + ssize_t (*read)(char *buffer, loff_t offset, size_t count, + void *data, size_t datalen); + void (*free)(void *data); +}; #ifdef CONFIG_DEV_COREDUMP void dev_coredumpv(struct device *dev, void *data, size_t datalen, @@ -76,6 +96,9 @@ void dev_coredumpm(struct device *dev, struct module *owner, void *data, size_t datalen), void (*free)(void *data)); +void dev_coredumpm_bulk(struct device *dev, struct module *owner, gfp_t gfp, + struct dev_coredumpm_bulk_data *files, int num_files); + void dev_coredumpsg(struct device *dev, struct scatterlist *table, size_t datalen, gfp_t gfp); #else @@ -95,6 +118,16 @@ dev_coredumpm(struct device *dev, struct module *owner, free(data); } +static inline +void dev_coredumpm_bulk(struct device *dev, struct module *owner, gfp_t gfp, + struct dev_coredumpm_bulk_data *files, int num_files) +{ + int i; + + for (i = 0; i < num_files; i++) + files[i].free(files[i].data); +} + static inline void dev_coredumpsg(struct device *dev, struct scatterlist *table, size_t datalen, gfp_t gfp) { -- 2.7.4