From patchwork Wed Aug 2 15:18:09 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Philippe Brucker X-Patchwork-Id: 9876923 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id E5B83602BC for ; Wed, 2 Aug 2017 15:16:31 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D87BD287D1 for ; Wed, 2 Aug 2017 15:16:31 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id CD7EE287DB; Wed, 2 Aug 2017 15:16:31 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 73585287D1 for ; Wed, 2 Aug 2017 15:16:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753115AbdHBPQ3 (ORCPT ); Wed, 2 Aug 2017 11:16:29 -0400 Received: from usa-sjc-mx-foss1.foss.arm.com ([217.140.101.70]:55642 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753067AbdHBPQ2 (ORCPT ); Wed, 2 Aug 2017 11:16:28 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id B07961596; Wed, 2 Aug 2017 08:16:27 -0700 (PDT) Received: from e106794-lin.localdomain (e106794-lin.cambridge.arm.com [10.1.210.31]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 723553F577; Wed, 2 Aug 2017 08:16:26 -0700 (PDT) Date: Wed, 2 Aug 2017 16:18:09 +0100 From: Jean-Philippe Brucker To: Punit Agrawal Cc: kvm@vger.kernel.org, will.deacon@arm.com, robin.murphy@arm.com, lorenzo.pieralisi@arm.com, marc.zyngier@arm.com Subject: Re: [PATCH v2 kvmtool 07/10] vfio-pci: add MSI-X support Message-ID: References: <20170622170536.14319-1-jean-philippe.brucker@arm.com> <20170622170536.14319-8-jean-philippe.brucker@arm.com> <87a83kzebr.fsf@e105922-lin.cambridge.arm.com> <87vam7w9z5.fsf@e105922-lin.cambridge.arm.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <87vam7w9z5.fsf@e105922-lin.cambridge.arm.com> FCC: imap://jean-philippe.brucker%40arm.com@outlook.office365.com/Sent X-Identity-Key: id1 X-Account-Key: account1 X-Mozilla-Draft-Info: internal/draft; vcard=0; receipt=0; DSN=0; uuencode=0; attachmentreminder=0; deliveryformat=4 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1 Content-Language: en-US Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP On 01/08/17 17:04, Punit Agrawal wrote: [...] > I think I've figured out what's going wrong. pci_for_each_cap is defined > as - > > #define pci_for_each_cap(pos, cap, hdr) \ > for ((pos) = (hdr)->capabilities & ~3; \ > (cap) = (void *)(hdr) + (pos), (pos) != 0; \ > (pos) = ((struct pci_cap_hdr *)(cap))->next & ~3) > > Here cap is being assigned before the pos != 0 check. So in the last > iteration through the loop, cap will point to the start of the PCI > header - which is then used to set "last->next = pos". Ah right, sorry about that. How about moving the pos check in front? Does the following work for you? diff --git a/include/kvm/pci.h b/include/kvm/pci.h index c5fc8254..37a65956 100644 --- a/include/kvm/pci.h +++ b/include/kvm/pci.h @@ -142,9 +142,9 @@ struct pci_device_header { enum irq_type irq_type; }; -#define pci_for_each_cap(pos, cap, hdr) \ - for ((pos) = (hdr)->capabilities & ~3; \ - (cap) = (void *)(hdr) + (pos), (pos) != 0; \ +#define pci_for_each_cap(pos, cap, hdr) \ + for ((pos) = (hdr)->capabilities & ~3; \ + (pos) != 0 && ({ (cap) = (void *)(hdr) + (pos); 1; }); \ (pos) = ((struct pci_cap_hdr *)(cap))->next & ~3) int pci__init(struct kvm *kvm);