Received: by 10.223.185.116 with SMTP id b49csp2363839wrg; Thu, 15 Feb 2018 10:26:02 -0800 (PST) X-Google-Smtp-Source: AH8x225q8r6ViWEDbZAACimCKjF6bRHlf/rDuhpYUm4Rx4AtgXbkWlue1JNs1Pu1030nLr+J+4JZ X-Received: by 10.101.82.195 with SMTP id z3mr2962699pgp.308.1518719162295; Thu, 15 Feb 2018 10:26:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1518719162; cv=none; d=google.com; s=arc-20160816; b=Iq1abpnBo8SKCI0TvWJ84kI5KhuIoeR5vN2pCX3A4hZFBQ+E3dJP4IPnZvniXgHsSE ellnY+S6KWUCry97fruA4y55QiYPLSEdw3INvAawwuuYTdO2J4ipx9IsxOWlMKW0+EHi YM4gbzfIFyR9WMKYtRLXNdceJWBG+9RXIdUSMnkxNSAXPNm6SLcP5rUf8rEbrtOKlJmJ 1cn5Zmbv4x+lyEqgR1fcNx18SITA1SlF5h65nkaQAaK+J91wcwtviY1Zve6RG3jIM9ki O8kFp7fp2uFsZ6dDYpvREEQ+qI1IWnz4A4eKNG+O0q80rp3fAP/966dsl+vFgLsVLbtO PmBg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:cc:to:subject :message-id:date:from:references:in-reply-to:mime-version :arc-authentication-results; bh=Dz8lCoLVof5xcgHMhIVJH3lc9hCAEEIEE7THJRqU8Eo=; b=mhjZoDJJCWto13vc7MMGbWwJktCoyNbRChwsvLdZqCIKnZSpb72d6Uedbnk7Fjbnkm URCgRQ8XrgQsfSttAe+VGcyu9mLhpNUGn5rWDqUbKOvNozhzQ2X9KilghWf/1MVClQCz SmToMvo4rZPI/NPcqqPoDCcOEhm+L59n/+szyFwH/bd6ofr/fk+ts9rr0T3OqKBCRPts oWK28GuBaCJYu1s8wrNPXnsAKlekI6U/8CQS0m4g+LdHE5uX2VCXK9jA7UoT9+1IhPcF RVgIKafa1lJTb/sheGUbnrnnMifaBjGgU4y5CDpoewhQFvTa8a13pzNpmEq/b2NvHuK6 I/Ew== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f19-v6si3259320plr.641.2018.02.15.10.25.47; Thu, 15 Feb 2018 10:26:02 -0800 (PST) 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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1166400AbeBOSYi convert rfc822-to-8bit (ORCPT + 99 others); Thu, 15 Feb 2018 13:24:38 -0500 Received: from mail-qk0-f196.google.com ([209.85.220.196]:37646 "EHLO mail-qk0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1163301AbeBOSYg (ORCPT ); Thu, 15 Feb 2018 13:24:36 -0500 Received: by mail-qk0-f196.google.com with SMTP id c128so735395qkb.4 for ; Thu, 15 Feb 2018 10:24:35 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=obkEN1+FY15Pv1RnzBFNu6oo3TibaCnJMRvIQ5IKOOU=; b=PQaD09UAWbqtUSNBVRDBXxzuFogjTuGxWU/bmuNDnzEyltsIhdUxMh/bToN3fv3AVQ Jdas/yoFwsNixWxsgv3OYqgIVdUneu5+X5uApHLr4KbvesWM18XFlCulgmG4DrYlLhKM xBOtVOLgQB8cBs4oEJU914+HCC6QFbKiCFeQjZOO0hUTBwds7egK9lCDy2JRhagOEBQt yIxxspSwFYrDUOFhGtPdQ2ooYhKNoFkofSMx6cuTD1cCTocuuEYkAcK0oJbgEdz3Z00J CsJnqACW2PNp5rlq7obK31fuuyr9cLUkzlXbU5OadTx8b116zvv9h7CfM4RzVXiMaYqm Ilwg== X-Gm-Message-State: APf1xPD6eIyN4nxqLI+5xQe/FbvH9XbcKjS2LUTyODHPADSAAqwcmuX6 rpYTKe/wZ8Owb+s8NvaMYJYIbKWM8wUtygghoxGbRw== X-Received: by 10.55.107.70 with SMTP id g67mr1772584qkc.105.1518719075357; Thu, 15 Feb 2018 10:24:35 -0800 (PST) MIME-Version: 1.0 Received: by 10.12.146.213 with HTTP; Thu, 15 Feb 2018 10:24:34 -0800 (PST) In-Reply-To: <20180214214105-mutt-send-email-mst@kernel.org> References: <20180214141850.4017-1-marcandre.lureau@redhat.com> <20180214141850.4017-9-marcandre.lureau@redhat.com> <20180214214105-mutt-send-email-mst@kernel.org> From: =?UTF-8?B?TWFyYy1BbmRyw6kgTHVyZWF1?= Date: Thu, 15 Feb 2018 19:24:34 +0100 Message-ID: Subject: Re: [PATCH v14 8/9] fw_cfg: write vmcoreinfo details To: "Michael S. Tsirkin" Cc: Linux Kernel Mailing List , Baoquan He , Sergio Lopez Pascual , "Somlo, Gabriel" , xiaolong.ye@intel.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8BIT Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi On Thu, Feb 15, 2018 at 7:09 PM, Michael S. Tsirkin wrote: > On Wed, Feb 14, 2018 at 03:18:49PM +0100, Marc-André Lureau wrote: >> If the "etc/vmcoreinfo" fw_cfg file is present and we are not running >> the kdump kernel, write the addr/size of the vmcoreinfo ELF note. >> >> The DMA operation is expected to run synchronously with today qemu, >> but the specification states that it may become async, so we run >> "control" field check in a loop for eventual changes. >> >> Signed-off-by: Marc-André Lureau >> --- >> drivers/firmware/qemu_fw_cfg.c | 144 ++++++++++++++++++++++++++++++++++++++++- >> 1 file changed, 141 insertions(+), 3 deletions(-) >> >> diff --git a/drivers/firmware/qemu_fw_cfg.c b/drivers/firmware/qemu_fw_cfg.c >> index 37638b95cb45..69939e2529f2 100644 >> --- a/drivers/firmware/qemu_fw_cfg.c >> +++ b/drivers/firmware/qemu_fw_cfg.c >> @@ -34,11 +34,17 @@ >> #include >> #include >> #include >> +#include >> +#include >> +#include >> >> MODULE_AUTHOR("Gabriel L. Somlo "); >> MODULE_DESCRIPTION("QEMU fw_cfg sysfs support"); >> MODULE_LICENSE("GPL"); >> >> +/* fw_cfg revision attribute, in /sys/firmware/qemu_fw_cfg top-level dir. */ >> +static u32 fw_cfg_rev; >> + >> /* fw_cfg device i/o register addresses */ >> static bool fw_cfg_is_mmio; >> static phys_addr_t fw_cfg_p_base; >> @@ -59,6 +65,65 @@ static inline u16 fw_cfg_sel_endianness(u16 key) >> (u16 __force)cpu_to_le16(key); >> } >> >> +static inline bool fw_cfg_dma_enabled(void) >> +{ >> + return (fw_cfg_rev & FW_CFG_VERSION_DMA) && fw_cfg_reg_dma; >> +} >> + >> +/* qemu fw_cfg device is sync today, but spec says it may become async */ >> +static void fw_cfg_wait_for_control(struct fw_cfg_dma_access *d) >> +{ >> + do { >> + u32 ctrl = be32_to_cpu(READ_ONCE(d->control)); >> + >> + if ((ctrl & ~FW_CFG_DMA_CTL_ERROR) == 0) >> + return; >> + >> + usleep_range(50, 100); > > I would just do cpu_relax() here. ok, I didn't know that one. > >> + } while (true); >> + >> + /* do not reorder the read to d->control */ >> + rmb(); > > Hmm. I don't really understand the comment. > Is this code ever reacheable? How does it help? I thought that's what you suggested in v13 review, but true, I should replace the return with a break to reach it. Is that what you expect too? (my understanding is to make sure the READ_ONCE(control) in wait_for_control happens before READ_ONCE(control) after in dma_transfer) > >> +} >> + >> +static ssize_t fw_cfg_dma_transfer(void *address, u32 length, u32 control) >> +{ >> + phys_addr_t dma; >> + struct fw_cfg_dma_access *d = NULL; >> + ssize_t ret = length; >> + >> + d = kmalloc(sizeof(*d), GFP_KERNEL); >> + if (!d) { >> + ret = -ENOMEM; >> + goto end; >> + } >> + >> + /* fw_cfg device does not need IOMMU protection, so use physical addresses */ >> + *d = (struct fw_cfg_dma_access) { >> + .address = cpu_to_be64(address ? virt_to_phys(address) : 0), >> + .length = cpu_to_be32(length), >> + .control = cpu_to_be32(control) >> + }; >> + >> + dma = virt_to_phys(d); >> + >> + iowrite32be((u64)dma >> 32, fw_cfg_reg_dma); >> + /* force memory to sync before notifying device via MMIO */ >> + wmb(); >> + iowrite32be(dma, fw_cfg_reg_dma + 4); >> + >> + fw_cfg_wait_for_control(d); >> + >> + if (be32_to_cpu(READ_ONCE(d->control)) & FW_CFG_DMA_CTL_ERROR) { >> + ret = -EIO; >> + } >> + >> +end: >> + kfree(d); >> + >> + return ret; >> +} >> + >> /* read chunk of given fw_cfg blob (caller responsible for sanity-check) */ >> static inline void fw_cfg_read_blob(u16 key, >> void *buf, loff_t pos, size_t count) >> @@ -87,6 +152,47 @@ static inline void fw_cfg_read_blob(u16 key, >> acpi_release_global_lock(glk); >> } >> >> +#ifdef CONFIG_CRASH_CORE >> +/* write chunk of given fw_cfg blob (caller responsible for sanity-check) */ >> +static ssize_t fw_cfg_write_blob(u16 key, >> + void *buf, loff_t pos, size_t count) >> +{ >> + u32 glk = -1U; >> + acpi_status status; >> + ssize_t ret = count; >> + >> + /* If we have ACPI, ensure mutual exclusion against any potential >> + * device access by the firmware, e.g. via AML methods: >> + */ >> + status = acpi_acquire_global_lock(ACPI_WAIT_FOREVER, &glk); >> + if (ACPI_FAILURE(status) && status != AE_NOT_CONFIGURED) { >> + /* Should never get here */ >> + WARN(1, "%s: Failed to lock ACPI!\n", __func__); >> + return -EINVAL; >> + } >> + >> + mutex_lock(&fw_cfg_dev_lock); >> + if (pos == 0) { >> + ret = fw_cfg_dma_transfer(buf, count, key << 16 >> + | FW_CFG_DMA_CTL_SELECT >> + | FW_CFG_DMA_CTL_WRITE); >> + } else { >> + iowrite16(fw_cfg_sel_endianness(key), fw_cfg_reg_ctrl); >> + ret = fw_cfg_dma_transfer(NULL, pos, FW_CFG_DMA_CTL_SKIP); >> + if (ret < 0) >> + goto end; >> + ret = fw_cfg_dma_transfer(buf, count, FW_CFG_DMA_CTL_WRITE); >> + } >> + >> +end: >> + mutex_unlock(&fw_cfg_dev_lock); >> + >> + acpi_release_global_lock(glk); >> + >> + return ret; >> +} >> +#endif /* CONFIG_CRASH_CORE */ >> + >> /* clean up fw_cfg device i/o */ >> static void fw_cfg_io_cleanup(void) >> { >> @@ -185,9 +291,6 @@ static int fw_cfg_do_platform_probe(struct platform_device *pdev) >> return 0; >> } >> >> -/* fw_cfg revision attribute, in /sys/firmware/qemu_fw_cfg top-level dir. */ >> -static u32 fw_cfg_rev; >> - >> static ssize_t fw_cfg_showrev(struct kobject *k, struct attribute *a, char *buf) >> { >> return sprintf(buf, "%u\n", fw_cfg_rev); >> @@ -210,6 +313,32 @@ struct fw_cfg_sysfs_entry { >> struct list_head list; >> }; >> >> +#ifdef CONFIG_CRASH_CORE >> +static ssize_t fw_cfg_write_vmcoreinfo(const struct fw_cfg_file *f) >> +{ >> + static struct fw_cfg_vmcoreinfo *data; >> + ssize_t ret; >> + >> + data = kmalloc(sizeof(struct fw_cfg_vmcoreinfo), GFP_KERNEL); >> + if (!data) >> + return -ENOMEM; >> + >> + *data = (struct fw_cfg_vmcoreinfo) { >> + .guest_format = cpu_to_le16(FW_CFG_VMCOREINFO_FORMAT_ELF), >> + .size = cpu_to_le32(VMCOREINFO_NOTE_SIZE), >> + .paddr = cpu_to_le64(paddr_vmcoreinfo_note()) >> + }; >> + /* spare ourself reading host format support for now since we >> + * don't know what else to format - host may ignore ours >> + */ >> + ret = fw_cfg_write_blob(be16_to_cpu(f->select), data, >> + 0, sizeof(struct fw_cfg_vmcoreinfo)); >> + >> + kfree(data); >> + return ret; >> +} >> +#endif /* CONFIG_CRASH_CORE */ >> + >> /* get fw_cfg_sysfs_entry from kobject member */ >> static inline struct fw_cfg_sysfs_entry *to_entry(struct kobject *kobj) >> { >> @@ -450,6 +579,15 @@ static int fw_cfg_register_file(const struct fw_cfg_file *f) >> int err; >> struct fw_cfg_sysfs_entry *entry; >> >> +#ifdef CONFIG_CRASH_CORE >> + if (fw_cfg_dma_enabled() && >> + strcmp(f->name, FW_CFG_VMCOREINFO_FILENAME) == 0 && >> + !is_kdump_kernel()) { >> + if (fw_cfg_write_vmcoreinfo(f) < 0) >> + pr_warn("fw_cfg: failed to write vmcoreinfo"); >> + } >> +#endif >> + >> /* allocate new entry */ >> entry = kzalloc(sizeof(*entry), GFP_KERNEL); >> if (!entry) >> -- >> 2.16.1.73.g5832b7e9f2