Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.6 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id DB839C4727C for ; Tue, 29 Sep 2020 15:44:54 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 511F021734 for ; Tue, 29 Sep 2020 15:44:54 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="b+97w1Ft" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 511F021734 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Received: from list by lists.xenproject.org with outflank-mailman.291.772 (Exim 4.92) (envelope-from ) id 1kNHnv-0002nt-P0; Tue, 29 Sep 2020 15:44:35 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 291.772; Tue, 29 Sep 2020 15:44:35 +0000 X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kNHnv-0002nm-M5; Tue, 29 Sep 2020 15:44:35 +0000 Received: by outflank-mailman (input) for mailman id 291; Tue, 29 Sep 2020 15:44:33 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kNHnt-0002nh-Iy for xen-devel@lists.xenproject.org; Tue, 29 Sep 2020 15:44:33 +0000 Received: from mail-wm1-x329.google.com (unknown [2a00:1450:4864:20::329]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id e00a385b-a490-462a-b034-847911113086; Tue, 29 Sep 2020 15:44:32 +0000 (UTC) Received: by mail-wm1-x329.google.com with SMTP id y15so5334095wmi.0 for ; Tue, 29 Sep 2020 08:44:32 -0700 (PDT) Received: from CBGR90WXYV0 (54-240-197-233.amazon.com. [54.240.197.233]) by smtp.gmail.com with ESMTPSA id 92sm7173472wra.19.2020.09.29.08.44.30 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 29 Sep 2020 08:44:31 -0700 (PDT) Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kNHnt-0002nh-Iy for xen-devel@lists.xenproject.org; Tue, 29 Sep 2020 15:44:33 +0000 X-Inumbo-ID: e00a385b-a490-462a-b034-847911113086 Received: from mail-wm1-x329.google.com (unknown [2a00:1450:4864:20::329]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id e00a385b-a490-462a-b034-847911113086; Tue, 29 Sep 2020 15:44:32 +0000 (UTC) Received: by mail-wm1-x329.google.com with SMTP id y15so5334095wmi.0 for ; Tue, 29 Sep 2020 08:44:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:reply-to:to:cc:references:in-reply-to:subject:date:message-id :mime-version:content-transfer-encoding:content-language :thread-index; bh=5My7dn1m6VmkboSE+EbSkmIuow+NJ3bn9iaJxffgICg=; b=b+97w1Ft8VElq1NIM6glJH9KWUJ+qBykIFDzEZkU7NPgac5RuFZzxqmV5vTYsF2oRC xdwmmZblJbry63LEzM45GrOKkutuS77v7ZflW7lusR03cEsVB0uJa93tBL3WQnZdZ4NL hL65WhpnHcJKzcGURuoQ7csAaj13o9NJwgi571ZzEr2UCOJaExsk+FPJ+8mvuSK71MO/ T42HY82EEaY+B4MzQTMdvBljse4abXadMeshDj+kTwdQ50h03fxVepPkB7zFULup+qFu yZ7Z3GLVeR9j1BrQZ0IyTIT2oj+ZP5T3SOkMkECsuVoe442m/M9kS6dVz5y6+aVRXuv2 FBZg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:reply-to:to:cc:references:in-reply-to :subject:date:message-id:mime-version:content-transfer-encoding :content-language:thread-index; bh=5My7dn1m6VmkboSE+EbSkmIuow+NJ3bn9iaJxffgICg=; b=RNeyEDbQ6xjQhU/K5OJmyWlUu6ZkBrOUcYLrQcEzaBSEM7lRc28n3nI7oQsRQs+C1m sTXMlFhL0fS54v5gXvZAy/FpaPHsKlfNXCSNFGh/sofbecJej9fcwdLMHhPbv1SJPbB+ trSS8dXOdXNOevzqMea0abemt+ASLIw60qhkS99Clrwtb2xfo3NMxcmDJjrCP7KMEp72 znKk7g0x0vLg6NRhsso4IJd/C+PTDpeoV3YbOpn2L2RMbYP4ZTza5fLej67J/Eu+6IFP UcaK0DO9UToJTVOZ/yxW8YKBFeX+QRJiI22Y89x7AdLNJh0tp4iHfx+mH6U0COjatfgU x4Kg== X-Gm-Message-State: AOAM531unq1QNuyIabVwWkbF0Ga04dc2Y5wfIn5MVQtrk8yswJzTyF39 71W3ELpVOXoGr/9hsN1WlrI= X-Google-Smtp-Source: ABdhPJwsYb9YytNupf58uADGPnKgcqVcwkB63fieWlE8Gkyc4aiVMgavxEJlJwIIVI0liWrzcYqZQQ== X-Received: by 2002:a05:600c:4104:: with SMTP id j4mr5108134wmi.36.1601394271855; Tue, 29 Sep 2020 08:44:31 -0700 (PDT) Received: from CBGR90WXYV0 (54-240-197-233.amazon.com. [54.240.197.233]) by smtp.gmail.com with ESMTPSA id 92sm7173472wra.19.2020.09.29.08.44.30 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 29 Sep 2020 08:44:31 -0700 (PDT) From: Paul Durrant X-Google-Original-From: "Paul Durrant" Reply-To: To: "'Jan Beulich'" , Cc: "'Andrew Cooper'" , "'George Dunlap'" , "'Ian Jackson'" , "'Julien Grall'" , "'Wei Liu'" , "'Stefano Stabellini'" References: <0d5ffc89-4b04-3e06-e950-f0cb171c7419@suse.com> In-Reply-To: Subject: RE: [PATCH 02/12] evtchn: avoid race in get_xen_consumer() Date: Tue, 29 Sep 2020 16:44:30 +0100 Message-ID: <001f01d69677$6c03c7b0$440b5710$@xen.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Outlook 16.0 Content-Language: en-gb Thread-Index: AQHyn6bUUfstxLK2yDInWW9SM20ChgIp92gvqTZfUPA= > -----Original Message----- > From: Xen-devel On Behalf Of Jan Beulich > Sent: 28 September 2020 11:57 > To: xen-devel@lists.xenproject.org > Cc: Andrew Cooper ; George Dunlap ; Ian > Jackson ; Julien Grall ; Wei Liu ; Stefano Stabellini > > Subject: [PATCH 02/12] evtchn: avoid race in get_xen_consumer() > > There's no global lock around the updating of this global piece of data. > Make use of cmpxchg() to avoid two entities racing with their updates. > > Signed-off-by: Jan Beulich > --- > TBD: Initially I used cmpxchgptr() here, until realizing Arm doesn't > have it. It's slightly more type-safe than cmpxchg() (requiring > all arguments to actually be pointers), so I now wonder whether Arm > should gain it (perhaps simply by moving the x86 implementation to > xen/lib.h), or whether we should purge it from x86 as being > pointless. > > --- a/xen/common/event_channel.c > +++ b/xen/common/event_channel.c > @@ -57,7 +57,8 @@ > * with a pointer, we stash them dynamically in a small lookup array which > * can be indexed by a small integer. > */ > -static xen_event_channel_notification_t xen_consumers[NR_XEN_CONSUMERS]; > +static xen_event_channel_notification_t __read_mostly > + xen_consumers[NR_XEN_CONSUMERS]; > > /* Default notification action: wake up from wait_on_xen_event_channel(). */ > static void default_xen_notification_fn(struct vcpu *v, unsigned int port) > @@ -81,7 +82,7 @@ static uint8_t get_xen_consumer(xen_even > for ( i = 0; i < ARRAY_SIZE(xen_consumers); i++ ) > { > if ( xen_consumers[i] == NULL ) > - xen_consumers[i] = fn; > + (void)cmpxchg(&xen_consumers[i], NULL, fn); > if ( xen_consumers[i] == fn ) Why not use the return from cmpxchg() to determine success and break out of the loop rather than re-accessing the global array? Paul > break; > } >