From patchwork Tue Jan 18 21:10:16 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Igor V. Kovalenko" X-Patchwork-Id: 79364 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 51709B7102 for ; Wed, 19 Jan 2011 08:36:24 +1100 (EST) Received: from localhost ([127.0.0.1]:49931 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PfJDx-0006ZX-Et for incoming@patchwork.ozlabs.org; Tue, 18 Jan 2011 16:36:21 -0500 Received: from [140.186.70.92] (port=39121 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PfIrx-0002J6-R5 for qemu-devel@nongnu.org; Tue, 18 Jan 2011 16:15:42 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1PfIom-00041u-0O for qemu-devel@nongnu.org; Tue, 18 Jan 2011 16:13:32 -0500 Received: from mail-ey0-f173.google.com ([209.85.215.173]:53535) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1PfIol-00041c-PU for qemu-devel@nongnu.org; Tue, 18 Jan 2011 16:10:19 -0500 Received: by eyg7 with SMTP id 7so60032eyg.4 for ; Tue, 18 Jan 2011 13:10:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:subject:to:from:date:message-id:user-agent :mime-version:content-type:content-transfer-encoding; bh=1wzEV2hbMwucVBPwwpZdsCdcdju73KppYVSAXo56FX0=; b=fh589D3SIosfWXM/2H/hg3c0+p/0lQcAcqiJM4X4oMESLslRxdQ5BPBrcc3L1aaBTR 1Ik/mtwlh+2kCpiY8bKS8xuVOPj41Hdg+bZJaFUpssKzjRZnu5+6RzZLGAkY6lUHSgT7 pFYYbAQ72IOqPh9ywvPqcxIp4gxyJ+lH4RRzo= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=subject:to:from:date:message-id:user-agent:mime-version :content-type:content-transfer-encoding; b=XqVvxPKTuitomBUFVUgFSAcL98r97IdFRDoqIQ58lnpQKwY0nrRGYly5xlx4o8bVJR WYZtFkwnUCCeI0ozr9x2CPAs8Fd9f83XNSLmiviVf227naKy3xGz332urf9MBq3/+zq3 EXzkptGZl6DJfvY0RsozXs6uA9KkC16PZ7dNM= Received: by 10.213.114.82 with SMTP id d18mr1600291ebq.61.1295385018342; Tue, 18 Jan 2011 13:10:18 -0800 (PST) Received: from skyserv (host-87-255-18-173.bigtelecom.ru [87.255.18.173]) by mx.google.com with ESMTPS id t5sm4894769eeh.8.2011.01.18.13.10.17 (version=TLSv1/SSLv3 cipher=RC4-MD5); Tue, 18 Jan 2011 13:10:18 -0800 (PST) Received: from localhost ([127.0.0.1] helo=[192.168.1.2]) by skyserv with esmtp (Exim 4.73) (envelope-from ) id 1PfIoi-0002ZF-He for qemu-devel@nongnu.org; Wed, 19 Jan 2011 00:10:16 +0300 To: qemu-devel@nongnu.org From: "Igor V. Kovalenko" Date: Wed, 19 Jan 2011 00:10:16 +0300 Message-ID: <20110118211016.9850.63478.stgit@skyserv> User-Agent: StGit/0.15 MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2) Subject: [Qemu-devel] [PATCH] pcibus_get_dev_path: correct pci device path construction X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: Igor V. Kovalenko - fix snprintf off by one pci domain and slot number formatting snprintf calls require extra space for trailing null character without this change devices are assigned the same path name which triggers assertion in vmstate_register_with_alias_id - while iterating over devices from root pci device use PCI_SLOT and PCI_FUNC of each device on the path instead of always extracting PCI_FUNC of original device Signed-off-by: Igor V. Kovalenko --- hw/pci.c | 28 ++++++++++++++++++++-------- 1 files changed, 20 insertions(+), 8 deletions(-) diff --git a/hw/pci.c b/hw/pci.c index 8d0e3df..182ee25 100644 --- a/hw/pci.c +++ b/hw/pci.c @@ -2036,6 +2036,8 @@ static char *pcibus_get_dev_path(DeviceState *dev) int slot_len = strlen(":SS.F"); int path_len; char *path, *p; + PCIDevice** pci_path; + int i; /* Calculate # of slots on path between device and root. */; slot_depth = 0; @@ -2045,21 +2047,31 @@ static char *pcibus_get_dev_path(DeviceState *dev) path_len = domain_len + slot_len * slot_depth; - /* Allocate memory, fill in the terminating null byte. */ + /* Allocate memory. String will be null-terminated by snprintf calls. */ path = malloc(path_len + 1 /* For '\0' */); - path[path_len] = '\0'; /* First field is the domain. */ - snprintf(path, domain_len, "%04x:00", pci_find_domain(d->bus)); + snprintf(path, domain_len+1, "%04x:00", pci_find_domain(d->bus)); + + /* Store pci devices on the path walking up from device to root. + * We need them later in the reverse order, last to first. */ + pci_path = qemu_malloc(slot_depth * sizeof(PCIDevice *)); - /* Fill in slot numbers. We walk up from device to root, so need to print - * them in the reverse order, last to first. */ - p = path + path_len; + i = slot_depth; for (t = d; t; t = t->bus->parent_dev) { - p -= slot_len; - snprintf(p, slot_len, ":%02x.%x", PCI_SLOT(t->devfn), PCI_FUNC(d->devfn)); + pci_path[--i] = t; } + /* Fill in slot numbers using stored path from root pci device. */ + p = path + domain_len; + for (i = 0; i < slot_depth; ++i) { + t = pci_path[i]; + snprintf(p + i * slot_len, slot_len+1, + ":%02x.%x", PCI_SLOT(t->devfn), PCI_FUNC(t->devfn)); + } + + qemu_free(pci_path); + return path; }