Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp4273340yba; Sun, 12 May 2019 08:56:04 -0700 (PDT) X-Google-Smtp-Source: APXvYqy8MxCOz2idHgaiz4iabmtI36QYhNhINBgImgA9xqpfdUrRgvpf6ygEUzQdVjavHVV27yw0 X-Received: by 2002:a63:f710:: with SMTP id x16mr12273915pgh.216.1557676564778; Sun, 12 May 2019 08:56:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1557676564; cv=none; d=google.com; s=arc-20160816; b=YhMKkk+9ogJ3BwVkibQMTWnSHhuRCqCE4FMFd+8gd7XciTEGFIpFcHzngAXeLcxyod LJ/kYzY2iwyD9oTwrvp28uMt3UzirS6fk1PMComsFcQF8EHNXp5xwsgjxDL4PvETqIVy +c6mQFoQZCPJ6I42CkWdWd5mi1MjN4nIXuFHAHQG6Nxmo4Bkuciw/48Econ+BRrcdhgr xEaIkL1lQ+XeixJi4YLwPFfUXc1/vDEJj2tw0tTIpzsZd3J7wuvgs86ua3bHb49zEhIq qZ+r8/x2+8MQwjhRdzqhNkSL+fHCDgFhh5Z3Bv8SVJSV3/3x91QGzwq/Hh0DIlDwyzN0 5eWQ== 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=/T6slGtirlNBQjteyXLnYngCOCai+AykFmC6WhR/XFI=; b=i7RIN9F74IxPb5T96asD6q79h+k+oBLQWAtjeryOuV/d26Va96n9EcguvFRUuD2NNh AjypzvSZdPQbbgzuClySMGckpazBv84gneFBV6BP3AKBRsf2WTNifB7wfxqsaivMQsoa +x0mhsnvm3nJF6B8GN3O0xFRPGBXI3SdW8Szs+ayOOKA82OFSakmKM6qRpw1sgDWCNiw UD0uR2JCM0P3nAh/1aE1mTLBgIHf+ldERhzPx/DNs9A1kNraJhaeXbSwzZ2MCWAByTR1 GaUG1avJ91V/9NWBc+9mvfsRJem+V+puXnA7T8NzlOdL7h4jIsXsn7LYXspGPAg2MdnC Xgtw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="uqQJQ/mp"; 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 y1si13995730pll.412.2019.05.12.08.55.49; Sun, 12 May 2019 08:56:04 -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="uqQJQ/mp"; 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 S1726885AbfELPyt (ORCPT + 99 others); Sun, 12 May 2019 11:54:49 -0400 Received: from mail-pg1-f196.google.com ([209.85.215.196]:46636 "EHLO mail-pg1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726478AbfELPys (ORCPT ); Sun, 12 May 2019 11:54:48 -0400 Received: by mail-pg1-f196.google.com with SMTP id t187so5425657pgb.13 for ; Sun, 12 May 2019 08:54:48 -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=/T6slGtirlNBQjteyXLnYngCOCai+AykFmC6WhR/XFI=; b=uqQJQ/mp3DME/+Jc3Kmcqp44PDgILdA79aeHJzxG25Y8z0QKTf7Iotv2uur33liLp3 6RkElbQAlehqBrfSsnlM4nfAhlpd2LmmijI/6+2Ij1jqvITSy8TniYq9YpE+A0hndvfv 7cn0flFrTAIfyLYiTQE59wZsVzg1F5KJKPn3b01g2I7lUFYz63p68Xk+lZviL7vPCbeP RTQULhDVjkoVUnyQzso6rgkzMFoaBZ1aotz/1a4cg2bGZHhFmStUscb1O3UK1SlROuOH nbdItSOg1K6VTtheOlkxrQtx0jfFhNyU7p1UzCL51WnQlnypb+diPW+RtdAx14YuOn2e ranA== 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=/T6slGtirlNBQjteyXLnYngCOCai+AykFmC6WhR/XFI=; b=Bm/cz5o8wCjuPQMFfgsELiU6PZpU78BpgDDe70uOrxgH216hIfN2mwS5rkomAG/6m+ EjS/kQm/YfKk4t+9YC26ElSshV4Ik+y+dJZ8YTOyenO+Ozd0YsSQlNx+NXC4ZzcdS7LX vvVIKga5D/nlYk+d09Af89GflBv0lIoZYk8uO6e7ivRBPtuzUXGx9pik6zJtVVSPZU/x +6T9SMy9Ehr6yKtZFXUIMhl+14oQ70DoUliCkD6oA5eV5EdnlnJcDMF5XXm2AufLiBGl 30zP4eKC+KGSTxJamMdSCkpnG3pTUZ1wrqsRLgOWCZpX23JFHaOGsymcfJYmPbyp6pSY 6Vfw== X-Gm-Message-State: APjAAAVaJC4iIcu6mAs3sdsisHJN9FsAhqntmeWIEyFRZNXVZ0hIfjBQ RykkiZeXJUkhww55vSWS93I= X-Received: by 2002:a62:ee05:: with SMTP id e5mr26294810pfi.117.1557676487619; Sun, 12 May 2019 08:54:47 -0700 (PDT) Received: from mita-MS-7A45.lan ([240f:34:212d:1:918e:f7e4:1728:3f45]) by smtp.gmail.com with ESMTPSA id v2sm4470058pgr.2.2019.05.12.08.54.44 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 12 May 2019 08:54:47 -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 , Minwoo Im , Kenneth Heitke Subject: [PATCH v3 3/7] devcoredump: allow to create several coredump files in one device Date: Mon, 13 May 2019 00:54:13 +0900 Message-Id: <1557676457-4195-4-git-send-email-akinobu.mita@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1557676457-4195-1-git-send-email-akinobu.mita@gmail.com> References: <1557676457-4195-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 Cc: Minwoo Im Cc: Kenneth Heitke Signed-off-by: Akinobu Mita --- * v3 - No change since v2 drivers/base/devcoredump.c | 155 ++++++++++++++++++++++++++++++-------------- include/linux/devcoredump.h | 33 ++++++++++ 2 files changed, 139 insertions(+), 49 deletions(-) diff --git a/drivers/base/devcoredump.c b/drivers/base/devcoredump.c index e42d0b5..4dd6dba 100644 --- a/drivers/base/devcoredump.c +++ b/drivers/base/devcoredump.c @@ -25,16 +25,20 @@ 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 module *owner; struct delayed_work del_wk; struct device *failing_dev; + int num_files; + struct devcd_file files[]; }; static struct devcd_entry *dev_to_devcd(struct device *dev) @@ -45,8 +49,14 @@ 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); + } - devcd->free(devcd->data); module_put(devcd->owner); /* @@ -64,9 +74,14 @@ 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 +90,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 +109,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 +154,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 +230,55 @@ 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(struct_size(devcd, files, num_files), gfp); + if (!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 +293,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 +309,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 +332,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