From nobody Sat Feb 7 12:19:23 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linux.ibm.com ARC-Seal: i=1; a=rsa-sha256; t=1592283117; cv=none; d=zohomail.com; s=zohoarc; b=C0KLlS6M2e5UjPphGL9UCw6LOwmLJC7i8ltGFZ5enbZ2RhmhryDgXpuOaCSZ/y7mnJZnUQ2LC47xhBjdqbmxAZfSAPoV7IX/12SAEaxE26E2fN7OJF4749yahUE7Kubm6k7445hpBtq/OCeCpl7G1rimg05zuGuwTdv89D0gpTU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1592283117; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=Kzfb/OxERxdG+34siD4vydM5YGaaL1eoyGegwMQtiz8=; b=LuTF4ZQ2jzMItkN7UxOVu5KcT8lDaXiwoBdYMFpdByYHdeuhwxhJQPJzeRh6ZQ5LQflfEtvcnH6BB1B/j600GI2Zz6iXsSZm+y+rbNnCMRZ+OdRgj+kZHX0GO1cqx7u7QdL3qpxIZkRdyuIuuxK9Yrnu7HcckIQiZ4s6YfWu6Rk= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1592283117671845.5798467214534; Mon, 15 Jun 2020 21:51:57 -0700 (PDT) Received: from localhost ([::1]:49090 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jl3Zk-0007ro-1X for importer@patchew.org; Tue, 16 Jun 2020 00:51:56 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:55792) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jl3Yn-00071F-QO; Tue, 16 Jun 2020 00:50:57 -0400 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]:25718) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jl3Yl-0004i3-VV; Tue, 16 Jun 2020 00:50:57 -0400 Received: from pps.filterd (m0098409.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id 05G4XXfD083834; Tue, 16 Jun 2020 00:50:54 -0400 Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com with ESMTP id 31pc7q1nxr-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 16 Jun 2020 00:50:54 -0400 Received: from m0098409.ppops.net (m0098409.ppops.net [127.0.0.1]) by pps.reinject (8.16.0.36/8.16.0.36) with SMTP id 05G4orn0130439; Tue, 16 Jun 2020 00:50:53 -0400 Received: from ppma01fra.de.ibm.com (46.49.7a9f.ip4.static.sl-reverse.com [159.122.73.70]) by mx0a-001b2d01.pphosted.com with ESMTP id 31pc7q1nx6-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 16 Jun 2020 00:50:53 -0400 Received: from pps.filterd (ppma01fra.de.ibm.com [127.0.0.1]) by ppma01fra.de.ibm.com (8.16.0.42/8.16.0.42) with SMTP id 05G4jnEe023711; Tue, 16 Jun 2020 04:50:51 GMT Received: from b06avi18626390.portsmouth.uk.ibm.com (b06avi18626390.portsmouth.uk.ibm.com [9.149.26.192]) by ppma01fra.de.ibm.com with ESMTP id 31mpe7hw2b-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 16 Jun 2020 04:50:51 +0000 Received: from d06av23.portsmouth.uk.ibm.com (d06av23.portsmouth.uk.ibm.com [9.149.105.59]) by b06avi18626390.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 05G4nVD361145386 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 16 Jun 2020 04:49:31 GMT Received: from d06av23.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 4D424A404D; Tue, 16 Jun 2020 04:50:48 +0000 (GMT) Received: from d06av23.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 02D5DA4040; Tue, 16 Jun 2020 04:50:48 +0000 (GMT) Received: from tuxmaker.boeblingen.de.ibm.com (unknown [9.152.85.9]) by d06av23.portsmouth.uk.ibm.com (Postfix) with ESMTP; Tue, 16 Jun 2020 04:50:47 +0000 (GMT) From: Halil Pasic To: Matthew Rosato , Cornelia Huck , Christian Borntraeger , "Michael S. Tsirkin" , qemu-s390x@nongnu.org, qemu-devel@nongnu.org Subject: [PATCH 1/2] virtio-ccw: fix virtio_set_ind_atomic Date: Tue, 16 Jun 2020 06:50:34 +0200 Message-Id: <20200616045035.51641-2-pasic@linux.ibm.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200616045035.51641-1-pasic@linux.ibm.com> References: <20200616045035.51641-1-pasic@linux.ibm.com> X-TM-AS-GCONF: 00 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.216, 18.0.687 definitions=2020-06-15_11:2020-06-15, 2020-06-15 signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 lowpriorityscore=0 bulkscore=0 impostorscore=0 suspectscore=0 priorityscore=1501 malwarescore=0 mlxlogscore=999 adultscore=0 spamscore=0 mlxscore=0 clxscore=1015 cotscore=-2147483648 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2004280000 definitions=main-2006160033 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=148.163.156.1; envelope-from=pasic@linux.ibm.com; helo=mx0a-001b2d01.pphosted.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/06/16 00:50:54 X-ACL-Warn: Detected OS = Linux 3.x [generic] [fuzzy] X-Spam_score_int: -35 X-Spam_score: -3.6 X-Spam_bar: --- X-Spam_report: (-3.6 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H2=-1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Halil Pasic , Thomas Huth , David Hildenbrand , Richard Henderson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" The atomic_cmpxchg() loop is broken because we occasionally end up with old and _old having different values (a legit compiler can generate code that accessed *ind_addr again to pick up a value for _old instead of using the value of old that was already fetched according to the rules of the abstract machine). This means the underlying CS instruction may use a different old (_old) than the one we intended to use if atomic_cmpxchg() performed the xchg part. Let us use volatile to force the rules of the abstract machine for accesses to *ind_addr. Let us also rewrite the loop so, we that the new old is used to compute the new desired value if the xchg part is not performed. Signed-off-by: Halil Pasic Reported-by: Andre Wild Fixes: 7e7494627f ("s390x/virtio-ccw: Adapter interrupt support.") Reviewed-by: Christian Borntraeger --- hw/s390x/virtio-ccw.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/hw/s390x/virtio-ccw.c b/hw/s390x/virtio-ccw.c index c1f4bb1d33..3c988a000b 100644 --- a/hw/s390x/virtio-ccw.c +++ b/hw/s390x/virtio-ccw.c @@ -786,9 +786,10 @@ static inline VirtioCcwDevice *to_virtio_ccw_dev_fast(= DeviceState *d) static uint8_t virtio_set_ind_atomic(SubchDev *sch, uint64_t ind_loc, uint8_t to_be_set) { - uint8_t ind_old, ind_new; + uint8_t expected, actual; hwaddr len =3D 1; - uint8_t *ind_addr; + /* avoid multiple fetches */ + uint8_t volatile *ind_addr; =20 ind_addr =3D cpu_physical_memory_map(ind_loc, &len, true); if (!ind_addr) { @@ -796,14 +797,15 @@ static uint8_t virtio_set_ind_atomic(SubchDev *sch, u= int64_t ind_loc, __func__, sch->cssid, sch->ssid, sch->schid); return -1; } + actual =3D *ind_addr; do { - ind_old =3D *ind_addr; - ind_new =3D ind_old | to_be_set; - } while (atomic_cmpxchg(ind_addr, ind_old, ind_new) !=3D ind_old); - trace_virtio_ccw_set_ind(ind_loc, ind_old, ind_new); - cpu_physical_memory_unmap(ind_addr, len, 1, len); + expected =3D actual; + actual =3D atomic_cmpxchg(ind_addr, expected, expected | to_be_set= ); + } while (actual !=3D expected); + trace_virtio_ccw_set_ind(ind_loc, actual, actual | to_be_set); + cpu_physical_memory_unmap((void *)ind_addr, len, 1, len); =20 - return ind_old; + return actual; } =20 static void virtio_ccw_notify(DeviceState *d, uint16_t vector) --=20 2.17.1 From nobody Sat Feb 7 12:19:23 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linux.ibm.com ARC-Seal: i=1; a=rsa-sha256; t=1592283193; cv=none; d=zohomail.com; s=zohoarc; b=Royyau8dmmgFcW+Nn0KHnGDPh+dw6+P0JJ5nffXH2OCpi83PhCZWVEh7MU7eKetXoKueHXg55Y7pE5BgtLaDIcqV5w+z0DdAdhN+tkQAwWV1YEwxSDQBAcqaZPabK1S1vvSuMfQD8MtDJAf9tfsNPIM6HE5jQztb4dxoJkINXjU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1592283193; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=QoqlUkt2cGUoz8u9xnybIDHL+jPMAagjokQVWXcfS0E=; b=edOybPhp7Glsa5Iu58xmL8vsVS0w8TlS0I6OL+My8GGR24WyUR12ST7YnD8m01hOQ/D/r5uCvIraS2kQYvLpXQSa51Z0EoKsSaJLhTqG3Ev1/tlk6OUYnQXPwht/TADlWIYj93rDcA/QJmPfSAFOpCAIIwJ5NDtpoyn6MI82CiA= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1592283193494692.5595235507449; Mon, 15 Jun 2020 21:53:13 -0700 (PDT) Received: from localhost ([::1]:52342 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jl3ay-0000qo-CD for importer@patchew.org; Tue, 16 Jun 2020 00:53:12 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:55802) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jl3Yo-00072b-Uo; Tue, 16 Jun 2020 00:50:58 -0400 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]:30582) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jl3Yn-0004ig-5j; Tue, 16 Jun 2020 00:50:58 -0400 Received: from pps.filterd (m0098393.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id 05G4XORU142409; Tue, 16 Jun 2020 00:50:55 -0400 Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com with ESMTP id 31p5es83c1-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 16 Jun 2020 00:50:55 -0400 Received: from m0098393.ppops.net (m0098393.ppops.net [127.0.0.1]) by pps.reinject (8.16.0.36/8.16.0.36) with SMTP id 05G4YkF4146316; Tue, 16 Jun 2020 00:50:55 -0400 Received: from ppma04ams.nl.ibm.com (63.31.33a9.ip4.static.sl-reverse.com [169.51.49.99]) by mx0a-001b2d01.pphosted.com with ESMTP id 31p5es83be-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 16 Jun 2020 00:50:55 -0400 Received: from pps.filterd (ppma04ams.nl.ibm.com [127.0.0.1]) by ppma04ams.nl.ibm.com (8.16.0.42/8.16.0.42) with SMTP id 05G4jdLc008614; Tue, 16 Jun 2020 04:50:52 GMT Received: from b06cxnps3075.portsmouth.uk.ibm.com (d06relay10.portsmouth.uk.ibm.com [9.149.109.195]) by ppma04ams.nl.ibm.com with ESMTP id 31mpe7vnm5-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 16 Jun 2020 04:50:52 +0000 Received: from d06av23.portsmouth.uk.ibm.com (d06av23.portsmouth.uk.ibm.com [9.149.105.59]) by b06cxnps3075.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 05G4ooRC33095886 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 16 Jun 2020 04:50:50 GMT Received: from d06av23.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id E9D57A4040; Tue, 16 Jun 2020 04:50:49 +0000 (GMT) Received: from d06av23.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 9FFB1A4055; Tue, 16 Jun 2020 04:50:49 +0000 (GMT) Received: from tuxmaker.boeblingen.de.ibm.com (unknown [9.152.85.9]) by d06av23.portsmouth.uk.ibm.com (Postfix) with ESMTP; Tue, 16 Jun 2020 04:50:49 +0000 (GMT) From: Halil Pasic To: Matthew Rosato , Cornelia Huck , Christian Borntraeger , "Michael S. Tsirkin" , qemu-s390x@nongnu.org, qemu-devel@nongnu.org Subject: [PATCH 2/2] s390x/pci: fix set_ind_atomic Date: Tue, 16 Jun 2020 06:50:35 +0200 Message-Id: <20200616045035.51641-3-pasic@linux.ibm.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200616045035.51641-1-pasic@linux.ibm.com> References: <20200616045035.51641-1-pasic@linux.ibm.com> X-TM-AS-GCONF: 00 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.216, 18.0.687 definitions=2020-06-15_11:2020-06-15, 2020-06-15 signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 phishscore=0 bulkscore=0 mlxlogscore=999 adultscore=0 lowpriorityscore=0 mlxscore=0 malwarescore=0 spamscore=0 impostorscore=0 suspectscore=0 clxscore=1015 priorityscore=1501 cotscore=-2147483648 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2004280000 definitions=main-2006160028 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=148.163.156.1; envelope-from=pasic@linux.ibm.com; helo=mx0a-001b2d01.pphosted.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/06/16 00:50:54 X-ACL-Warn: Detected OS = Linux 3.x [generic] [fuzzy] X-Spam_score_int: -35 X-Spam_score: -3.6 X-Spam_bar: --- X-Spam_report: (-3.6 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H2=-1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Halil Pasic , Thomas Huth , David Hildenbrand , Richard Henderson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" The atomic_cmpxchg() loop is broken because we occasionally end up with old and _old having different values (a legit compiler can generate code that accessed *ind_addr again to pick up a value for _old instead of using the value of old that was already fetched according to the rules of the abstract machine). This means the underlying CS instruction may use a different old (_old) than the one we intended to use if atomic_cmpxchg() performed the xchg part. Let us use volatile to force the rules of the abstract machine for accesses to *ind_addr. Let us also rewrite the loop so, we that the new old is used to compute the new desired value if the xchg part is not performed. Signed-off-by: Halil Pasic Fixes: 8cba80c3a0 ("s390: Add PCI bus support") Reported-by: Christian Borntraeger Reviewed-by: Christian Borntraeger --- hw/s390x/s390-pci-bus.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/hw/s390x/s390-pci-bus.c b/hw/s390x/s390-pci-bus.c index c4a4259f0c..80eb957a91 100644 --- a/hw/s390x/s390-pci-bus.c +++ b/hw/s390x/s390-pci-bus.c @@ -637,22 +637,24 @@ static AddressSpace *s390_pci_dma_iommu(PCIBus *bus, = void *opaque, int devfn) =20 static uint8_t set_ind_atomic(uint64_t ind_loc, uint8_t to_be_set) { - uint8_t ind_old, ind_new; + uint8_t expected, actual; hwaddr len =3D 1; - uint8_t *ind_addr; + /* avoid multiple fetches */ + uint8_t volatile *ind_addr; =20 ind_addr =3D cpu_physical_memory_map(ind_loc, &len, true); if (!ind_addr) { s390_pci_generate_error_event(ERR_EVENT_AIRERR, 0, 0, 0, 0); return -1; } + actual =3D *ind_addr; do { - ind_old =3D *ind_addr; - ind_new =3D ind_old | to_be_set; - } while (atomic_cmpxchg(ind_addr, ind_old, ind_new) !=3D ind_old); - cpu_physical_memory_unmap(ind_addr, len, 1, len); + expected =3D actual; + actual =3D atomic_cmpxchg(ind_addr, expected, expected | to_be_set= ); + } while (actual !=3D expected); + cpu_physical_memory_unmap((void *)ind_addr, len, 1, len); =20 - return ind_old; + return actual; } =20 static void s390_msi_ctrl_write(void *opaque, hwaddr addr, uint64_t data, --=20 2.17.1