Received: by 2002:a05:6358:5282:b0:b5:90e7:25cb with SMTP id g2csp3621634rwa; Tue, 23 Aug 2022 07:35:03 -0700 (PDT) X-Google-Smtp-Source: AA6agR49wB0yxLW8mn28x8Fca7zTwrxWYJV6wNPzC27rCCZhKwoUkpBJaLwnMKO2JkiMfjHFR2aE X-Received: by 2002:a17:902:720c:b0:172:a1ff:8c21 with SMTP id ba12-20020a170902720c00b00172a1ff8c21mr24618916plb.159.1661265303206; Tue, 23 Aug 2022 07:35:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661265303; cv=none; d=google.com; s=arc-20160816; b=shvbIc+qRQcK8MbYh7ViXJkhH++GaClnAbb/S14t2gusj6bouXnALdi5+nfRbYOfA8 WxyKeLj5ZoU3UGSKftnx84ZwWVTYZ3LdV4W7CXixl8Hj0359pVJHE9JDufHCXvoEjI8M nDZevdrqHKCQ2KxrkTD3zdFRYSovN6gXaEwMxAOwKgKS9B5QJH/WyT6YXOQbiRetkJxc Nse3Z1Vsj2pFSzPMVic9iPxlCYnZFXVCw1tUdqIcFn16bKH3FQwht2Ep8JPfkQOH8U5y C9GGsGZ8wjmPv94E7BaIiT6DDdz2n9Kk9WkrmFdFK8VWwWfoGADs5Bg2LKbw7FodDgDg +iag== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:references:in-reply-to :message-id:date:subject:cc:to:from; bh=m8L9TiNfuO61GbkYD4zyf08aimPnGMR4uF7vad0BOXI=; b=noFUetInddfo59nxMHCrcgthJ2Uhdcx0erpPaV7Exr1D7XrK+PzaIkHMeZL85eTJ4L LxbydlC1c6pNiaZq3cvFkr0ni8s08N0zzQ1xRSGqZWtNAO3DNjiUzI7OZDTHcnltwr/T ubPUeuMCVSiNC+cgXUzO3lLpYdW9Ta48IdDLc1Q4ojCw67x5ClaaNMb6PgIM6IQ0dNJZ VxpJj/hvlN1fUdRvN1e8WHcUbEMe4a5kricJSON5mYEPTB4zIZ/BirZRlj6pyy3WOfTh jwLWVr8fQcdgjRGEyWgJwhCzOnUwzJyKLBO4gKc9KXA6D5UjyPqOWghM5AOIq9qb7vXu voDA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x5-20020a1709027c0500b001715a939a89si5997618pll.430.2022.08.23.07.34.51; Tue, 23 Aug 2022 07:35:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242635AbiHWOTP (ORCPT + 99 others); Tue, 23 Aug 2022 10:19:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58360 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242102AbiHWOSy (ORCPT ); Tue, 23 Aug 2022 10:18:54 -0400 X-Greylist: delayed 530 seconds by postgrey-1.37 at lindbergh.monkeyblade.net; Tue, 23 Aug 2022 04:32:42 PDT Received: from azure-sdnproxy.icoremail.net (azure-sdnproxy.icoremail.net [52.187.6.220]) by lindbergh.monkeyblade.net (Postfix) with SMTP id 4F66727C609; Tue, 23 Aug 2022 04:32:41 -0700 (PDT) Received: from ubuntu.localdomain (unknown [218.12.19.15]) by mail-app2 (Coremail) with SMTP id by_KCgDnqrA5uARjm7mFAw--.5982S3; Tue, 23 Aug 2022 19:21:42 +0800 (CST) From: Duoming Zhou To: linux-kernel@vger.kernel.org, gregkh@linuxfoundation.org, briannorris@chromium.org Cc: johannes@sipsolutions.net, rafael@kernel.org, amitkarwar@gmail.com, ganapathi017@gmail.com, sharvari.harisangam@nxp.com, huxinming820@gmail.com, kvalo@kernel.org, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, linux-wireless@vger.kernel.org, netdev@vger.kernel.org, Duoming Zhou Subject: [PATCH v8 1/2] devcoredump: add new APIs to support migration of users from old device coredump related APIs Date: Tue, 23 Aug 2022 19:21:26 +0800 Message-Id: <9e5b72d4fee30704fbd067342ee69b769931318b.1661252818.git.duoming@zju.edu.cn> X-Mailer: git-send-email 2.17.1 In-Reply-To: References: In-Reply-To: References: X-CM-TRANSID: by_KCgDnqrA5uARjm7mFAw--.5982S3 X-Coremail-Antispam: 1UD129KBjvJXoW3Xr48KF13XFWxJF4rKw1ftFb_yoW3CF1fpF W8Gay5KrWUGrnrWr93Zr47WFy5Gws7Cas3X34Sk347Kan3AwnxKFWDZFyay34Dt3s5AFy8 GF98Aa4SkryvyF7anT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUBE14x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_Jr4l82xGYIkIc2 x26xkF7I0E14v26r4j6ryUM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2z4x0 Y4vE2Ix0cI8IcVAFwI0_tr0E3s1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr1j6F4UJw A2z4x0Y4vEx4A2jsIE14v26rxl6s0DM28EF7xvwVC2z280aVCY1x0267AKxVW0oVCq3wAS 0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7IYx2 IY67AKxVWUJVWUGwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S6xCaFVCjc4AY6r1j6r4UM4x0 Y48IcxkI7VAKI48JM4x0x7Aq67IIx4CEVc8vx2IErcIFxwACI402YVCY1x02628vn2kIc2 xKxwCF04k20xvY0x0EwIxGrwCFx2IqxVCFs4IE7xkEbVWUJVW8JwC20s026c02F40E14v2 6r1j6r18MI8I3I0E7480Y4vE14v26r106r1rMI8E67AF67kF1VAFwI0_GFv_WrylIxkGc2 Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVWUJVWUCwCI42IY6xIIjxv20xvEc7CjxVAFwI0_ Gr0_Cr1lIxAIcVCF04k26cxKx2IYs7xG6r1j6r1xMIIF0xvEx4A2jsIE14v26r1j6r4UMI IF0xvEx4A2jsIEc7CjxVAFwI0_Gr0_Gr1UYxBIdaVFxhVjvjDU0xZFpf9x0JU4T5dUUUUU = X-CM-SenderInfo: qssqjiasttq6lmxovvfxof0/1tbiAgULAVZdtbI0NQAUsU X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The dev_coredumpv(), dev_coredumpm() and dev_coredumpsg() could not be used in atomic context, because they call kvasprintf_const() and kstrdup() with GFP_KERNEL parameter. The process is shown below: dev_coredumpv(.., gfp_t gfp) dev_coredumpm(.., gfp_t gfp) dev_set_name kobject_set_name_vargs kvasprintf_const(GFP_KERNEL, ...); //may sleep kstrdup(s, GFP_KERNEL); //may sleep This patch adds new APIs that remove gfp_t parameter of dev_coredumpv(), dev_coredumpm() and dev_coredumpsg() in order to show they could not be used in atomic context. These new APIs will ultimately replace the dev_coredumpv(), dev_coredumpm() and dev_coredumpsg() when there are no users of the old APIs. Fixes: 833c95456a70 ("device coredump: add new device coredump class") Signed-off-by: Duoming Zhou --- Changes in v8: - add new APIs to prepare migration of users from old device coredump related APIs. drivers/base/devcoredump.c | 116 ++++++++++++++++++++++++++++++++++++ include/linux/devcoredump.h | 34 +++++++++++ 2 files changed, 150 insertions(+) diff --git a/drivers/base/devcoredump.c b/drivers/base/devcoredump.c index f4d794d6bb8..728457b12ce 100644 --- a/drivers/base/devcoredump.c +++ b/drivers/base/devcoredump.c @@ -185,6 +185,21 @@ void dev_coredumpv(struct device *dev, void *data, size_t datalen, } EXPORT_SYMBOL_GPL(dev_coredumpv); +/** + * dev_core_dumpv - create device coredump with vmalloc data + * @dev: the struct device for the crashed device + * @data: vmalloc data containing the device coredump + * @datalen: length of the data + * + * This function takes ownership of the vmalloc'ed data and will free + * it when it is no longer used. See dev_core_dumpm() for more information. + */ +void dev_core_dumpv(struct device *dev, void *data, size_t datalen) +{ + dev_core_dumpm(dev, NULL, data, datalen, devcd_readv, devcd_freev); +} +EXPORT_SYMBOL_GPL(dev_core_dumpv); + static int devcd_match_failing(struct device *dev, const void *failing) { struct devcd_entry *devcd = dev_to_devcd(dev); @@ -312,6 +327,87 @@ void dev_coredumpm(struct device *dev, struct module *owner, } EXPORT_SYMBOL_GPL(dev_coredumpm); +/** + * dev_core_dumpm - 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 + * @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_core_dumpm(struct device *dev, struct module *owner, + 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)) +{ + static atomic_t devcd_count = ATOMIC_INIT(0); + struct devcd_entry *devcd; + struct device *existing; + + if (devcd_disabled) + goto free; + + existing = class_find_device(&devcd_class, NULL, dev, + devcd_match_failing); + if (existing) { + put_device(existing); + goto free; + } + + if (!try_module_get(owner)) + goto free; + + devcd = kzalloc(sizeof(*devcd), GFP_KERNEL); + 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); + + dev_set_name(&devcd->devcd_dev, "devcd%d", + atomic_inc_return(&devcd_count)); + devcd->devcd_dev.class = &devcd_class; + + if (device_add(&devcd->devcd_dev)) + goto put_device; + + /* + * These should normally not fail, but there is no problem + * continuing without the links, so just warn instead of + * failing. + */ + if (sysfs_create_link(&devcd->devcd_dev.kobj, &dev->kobj, + "failing_device") || + sysfs_create_link(&dev->kobj, &devcd->devcd_dev.kobj, + "devcoredump")) + dev_warn(dev, "devcoredump create_link failed\n"); + + INIT_DELAYED_WORK(&devcd->del_wk, devcd_del); + schedule_delayed_work(&devcd->del_wk, DEVCD_TIMEOUT); + + return; + put_device: + put_device(&devcd->devcd_dev); + put_module: + module_put(owner); + free: + free(data); +} +EXPORT_SYMBOL_GPL(dev_core_dumpm); + /** * dev_coredumpsg - create device coredump that uses scatterlist as data * parameter @@ -333,6 +429,26 @@ void dev_coredumpsg(struct device *dev, struct scatterlist *table, } EXPORT_SYMBOL_GPL(dev_coredumpsg); +/** + * dev_core_dumpsg - create device coredump that uses scatterlist as data + * parameter + * @dev: the struct device for the crashed device + * @table: the dump data + * @datalen: length of the data + * + * 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 + * it will free the data. + */ +void dev_core_dumpsg(struct device *dev, struct scatterlist *table, + size_t datalen) +{ + dev_core_dumpm(dev, NULL, table, datalen, devcd_read_from_sgtable, + devcd_free_sgtable); +} +EXPORT_SYMBOL_GPL(dev_core_dumpsg); + static int __init devcoredump_init(void) { return class_register(&devcd_class); diff --git a/include/linux/devcoredump.h b/include/linux/devcoredump.h index c008169ed2c..113fe63800a 100644 --- a/include/linux/devcoredump.h +++ b/include/linux/devcoredump.h @@ -55,14 +55,26 @@ static inline void _devcd_free_sgtable(struct scatterlist *table) void dev_coredumpv(struct device *dev, void *data, size_t datalen, gfp_t gfp); +void dev_core_dumpv(struct device *dev, void *data, size_t datalen); + 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_core_dumpm(struct device *dev, struct module *owner, + 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)); + void dev_coredumpsg(struct device *dev, struct scatterlist *table, size_t datalen, gfp_t gfp); + +void dev_core_dumpsg(struct device *dev, struct scatterlist *table, + size_t datalen); + #else static inline void dev_coredumpv(struct device *dev, void *data, size_t datalen, gfp_t gfp) @@ -70,6 +82,12 @@ static inline void dev_coredumpv(struct device *dev, void *data, vfree(data); } +static inline void dev_core_dumpv(struct device *dev, void *data, + size_t datalen) +{ + vfree(data); +} + static inline void dev_coredumpm(struct device *dev, struct module *owner, void *data, size_t datalen, gfp_t gfp, @@ -80,11 +98,27 @@ dev_coredumpm(struct device *dev, struct module *owner, free(data); } +static inline void +dev_core_dumpm(struct device *dev, struct module *owner, + 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)) +{ + free(data); +} + static inline void dev_coredumpsg(struct device *dev, struct scatterlist *table, size_t datalen, gfp_t gfp) { _devcd_free_sgtable(table); } + +static inline void dev_core_dumpsg(struct device *dev, struct scatterlist *table, + size_t datalen) +{ + _devcd_free_sgtable(table); +} #endif /* CONFIG_DEV_COREDUMP */ #endif /* __DEVCOREDUMP_H */ -- 2.17.1