Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp4621602iob; Sun, 8 May 2022 19:32:34 -0700 (PDT) X-Google-Smtp-Source: ABdhPJylxF05M8UOBWjeHWIAwNNxn4nSZjrNwRZG1ud4hQJn+lfmkxo7oBjSaYCgyR4Kw1lUExva X-Received: by 2002:a63:5516:0:b0:3c6:53e9:30b3 with SMTP id j22-20020a635516000000b003c653e930b3mr10041348pgb.273.1652063554132; Sun, 08 May 2022 19:32:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652063554; cv=none; d=google.com; s=arc-20160816; b=eIxvZ8Nhtw4cW2qNDmy/h7tVgFt1k1snvxm/MNcSyUrRjy9ptVEsUWBc8Nqxn7qj7+ eLC3Xs9Su8W+SX78zRJn5pNNq3Z/+ZBx8sPzhZk9mqrXvxP8qw7jXglF+BqpXlQcePAY fOZkUPMQWHWPJd/zOzedRapEALfZR9vpENAgQ8DplmCGFVjwGQjN6HOTM1pq4AyyfCFk 18qseakgDqm8h+gwbVJGZAXntgeVwsb/AV3dKDm1AfPRJOgnIvcEOIJAHR3HyEBbSwdK DaQt9TzIsufPktsp6VwHlv+Pei8B4g2/fAzZgqVPiErLIB4KtXmaPH5+WaKy3gwV9sfM 4x9g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date; bh=Je9aT1rHIkX+6lOcuyMYvVnncpG8bOaXeL+zAUCNjuI=; b=wmLbp3YpexjX4UeGuhSm/ZpMpb8QRuRjl3rneS4S7SRrmWYpZ6G4t4ot1fCxdGrBED NR0nG+ZqGDOUe1hNQhHjxvB1CVCKRKLutQyyJje4K1m92hAylFhRF2X+AO0Zyb0GbMPG nGOgmPld7koj5Rmabr9DgzGAiyhAhHMxwTRwJabpP2Ng134QqC/wciz3Kyy6A1vVBv5a w1VIIUZO5UK16OMKUJE5ux5XZrDUs66Yclo7Q4ZZ7tUtNgZCgm/uYaaNUY6dQqDYGXP6 1E2X9a/Ot8rMFSGd+k86KpQu9nT/NL8B/qfsS8Ajypse1lx+v2Sqvv/mmCEDs4TuG6cg JaFw== 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:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id bb11-20020a170902bc8b00b0015d56a3ccc8si8838069plb.71.2022.05.08.19.32.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 May 2022 19:32:34 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id D7B1871D82; Sun, 8 May 2022 19:31:23 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232947AbiEHMfN (ORCPT + 99 others); Sun, 8 May 2022 08:35:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37556 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232146AbiEHMfI (ORCPT ); Sun, 8 May 2022 08:35:08 -0400 X-Greylist: delayed 19087 seconds by postgrey-1.37 at lindbergh.monkeyblade.net; Sun, 08 May 2022 05:31:18 PDT Received: from bmailout2.hostsharing.net (bmailout2.hostsharing.net [83.223.78.240]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 85757BF53; Sun, 8 May 2022 05:31:18 -0700 (PDT) Received: from h08.hostsharing.net (h08.hostsharing.net [IPv6:2a01:37:1000::53df:5f1c:0]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "*.hostsharing.net", Issuer "RapidSSL TLS DV RSA Mixed SHA256 2020 CA-1" (verified OK)) by bmailout2.hostsharing.net (Postfix) with ESMTPS id 7E21628004991; Sun, 8 May 2022 14:31:16 +0200 (CEST) Received: by h08.hostsharing.net (Postfix, from userid 100393) id 72166119437; Sun, 8 May 2022 14:31:16 +0200 (CEST) Date: Sun, 8 May 2022 14:31:16 +0200 From: Lukas Wunner To: Pavel Machek Cc: Greg Kroah-Hartman , linux-kernel@vger.kernel.org, stable@vger.kernel.org, Liguang Zhang , Bjorn Helgaas Subject: Re: [PATCH 5.10 126/599] PCI: pciehp: Clear cmd_busy bit in polling mode Message-ID: <20220508123116.GA27352@wunner.de> References: <20220405070258.802373272@linuxfoundation.org> <20220405070302.589741179@linuxfoundation.org> <20220409081314.GA19452@amd> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220409081314.GA19452@amd> User-Agent: Mutt/1.10.1 (2018-07-13) X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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 On Sat, Apr 09, 2022 at 10:13:15AM +0200, Pavel Machek wrote: > > From: Liguang Zhang > > > > Writes to a Downstream Port's Slot Control register are PCIe hotplug > > "commands." If the Port supports Command Completed events, software must > > wait for a command to complete before writing to Slot Control again. > > > > pcie_do_write_cmd() sets ctrl->cmd_busy when it writes to Slot Control. If > > software notification is enabled, i.e., PCI_EXP_SLTCTL_HPIE and > > PCI_EXP_SLTCTL_CCIE are set, ctrl->cmd_busy is cleared by pciehp_isr(). > > > > But when software notification is disabled, as it is when pcie_init() > > powers off an empty slot, pcie_wait_cmd() uses pcie_poll_cmd() to poll for > > command completion, and it neglects to clear ctrl->cmd_busy, which leads to > > spurious timeouts: > > I'm pretty sure this fixes the problem, but... it is still not fully > correct. > > > +++ b/drivers/pci/hotplug/pciehp_hpc.c > > @@ -98,6 +98,8 @@ static int pcie_poll_cmd(struct controll > > if (slot_status & PCI_EXP_SLTSTA_CC) { > > pcie_capability_write_word(pdev, PCI_EXP_SLTSTA, > > PCI_EXP_SLTSTA_CC); > > + ctrl->cmd_busy = 0; > > + smp_mb(); > > return 1; > > } > > Is the memory barrier neccessary? I don't see corresponding memory > barrier for reading. > > If it is neccessary, should we have WRITE_ONCE at the very least, or > probably normal atomic operations? The cmd_busy flag is set by pcie_do_write_cmd() before writing the Slot Control register and it is then cleared by pciehp_isr(). The purpose of the memory barriers is to ensure that order. IOW, we want to avoid a scenario where the write to cmd_busy in pcie_do_write_cmd() hasn't been committed to memory yet, the Slot Control write is performed, an interrupt occurs and is handled, the interrupt handler writes cmd_busy = 0 and only then is the cmd_busy = 1 write in pcie_do_write_cmd() committed to memory. That said, you're right that such a scenario is impossible if cmd_busy is cleared by the synchronous pcie_poll_cmd() instead of the asynchronous pciehp_isr(). Care to submit a patch to remove the memory barrier in this single location? A WRITE_ONCE() (i.e. a mere compiler barrier instead of a proper cacheline flush) is not sufficient to avoid the above scenario. An atomic bitop would work, but wouldn't offer advantages compared to the status quo. Thanks, Lukas