Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp3869672ybz; Mon, 4 May 2020 11:12:41 -0700 (PDT) X-Google-Smtp-Source: APiQypJKLYSgYvUqbQ7HRuEZCSCfQADgrcK0vSEy8yPHH0INbeat0COZ9ZpWkkZ8kz+JhOrqD9sp X-Received: by 2002:a50:c60c:: with SMTP id k12mr15697351edg.111.1588615961788; Mon, 04 May 2020 11:12:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588615961; cv=none; d=google.com; s=arc-20160816; b=Is+QK0YModsZfXpLFn+slZk9wSlPFw4uPrSuqZnnTmMj7lKOhApOe11odu5Raw1hPN yKvjyPjBDK0riWC+Id3EKb4NBmETiqZiYCT4agMEs0xbMEreOOyQNpmpUnppvVvp39Rr IQkOb/6fAHUXy0WOVY25Jduzo7E44XYrj7zCdFcmPtGUBqw2VUQZC0KKM+jZHBLPU7u9 a5/yjICcuZMsnaM4DfXCvHWFi3ZNlF7T3wm+HT/6t2At7TLuV45TIcpDryram4xUaJ0F n+WNN+tbOb2YyDyrQddaULnjRpy9TbJHCRQl95A+fLaI0mN5pqWRrzcQkai+S/3EqL3l JZGw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=VgBl6bJUdNY2zym234g8qLpUkuQA6XipPIh805BoBf0=; b=CUmMB/CJBJtM38cUAWbwB6EdWHQ5nz51Heur/c3AUEv8K9/Nr6mTGlsucmS8hXK+iN LDJa04YEyTOrQVUYj7w8RZhWfurNHiPu/dgfGFAyqZbcHr4RoM5fPKkiQSGa6orHqbnK TOd8W+W1rqRcFB9MGc0kzEj8ZTBxZYFYziH7bAXr+c1cY1ugmLFezu39DYhlcas+hV5p JFnp7+cNAywd7oC4lfkV3n2ShlAL41CtPZrGLseWi9N/FODflqawmSq7PURpyAX0s9ES pp0pAmbtn8DyRUE/++41S6AhZ3kU44ngvj6Z5YavqtUCMw6ExWMB2On3oW+EPKarb3Ze 34YQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=oAPoai4X; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id de20si7078164edb.451.2020.05.04.11.12.17; Mon, 04 May 2020 11:12:41 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=oAPoai4X; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732027AbgEDSGu (ORCPT + 99 others); Mon, 4 May 2020 14:06:50 -0400 Received: from mail.kernel.org ([198.145.29.99]:37306 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732016AbgEDSGq (ORCPT ); Mon, 4 May 2020 14:06:46 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 110BF2073B; Mon, 4 May 2020 18:06:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1588615606; bh=nTTT4da67tY6V26UzMba9X5BQOwgU9ZiHTbAOw+bExk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=oAPoai4Xr82e9vzPDjMkjvLR69lyTTCs2gf0VixXzJFKQa/z+mXuPI1nIyYRDhNsQ 6/HGtKgwEmg80veQ2pFj8Y5RahU5xG0GE9zsmKdHbhZ+1KM3RZjwLJEnbfmcZbpTyw MzgqIyT9DJoh6dsa1yvH3TmO6jXwC3CL+oMX5DG0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Leon Romanovsky , Jason Gunthorpe Subject: [PATCH 5.6 50/73] RDMA/cm: Fix ordering of xa_alloc_cyclic() in ib_create_cm_id() Date: Mon, 4 May 2020 19:57:53 +0200 Message-Id: <20200504165509.134278419@linuxfoundation.org> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200504165501.781878940@linuxfoundation.org> References: <20200504165501.781878940@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jason Gunthorpe commit e8dc4e885c459343970b25acd9320fe9ee5492e7 upstream. xa_alloc_cyclic() is a SMP release to be paired with some later acquire during xa_load() as part of cm_acquire_id(). As such, xa_alloc_cyclic() must be done after the cm_id is fully initialized, in particular, it absolutely must be after the refcount_set(), otherwise the refcount_inc() in cm_acquire_id() may not see the set. As there are several cases where a reader will be able to use the id.local_id after cm_acquire_id in the IB_CM_IDLE state there needs to be an unfortunate split into a NULL allocate and a finalizing xa_store. Fixes: a977049dacde ("[PATCH] IB: Add the kernel CM implementation") Link: https://lore.kernel.org/r/20200310092545.251365-2-leon@kernel.org Signed-off-by: Leon Romanovsky Signed-off-by: Jason Gunthorpe Signed-off-by: Greg Kroah-Hartman --- drivers/infiniband/core/cm.c | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) --- a/drivers/infiniband/core/cm.c +++ b/drivers/infiniband/core/cm.c @@ -572,18 +572,6 @@ static int cm_init_av_by_path(struct sa_ return 0; } -static int cm_alloc_id(struct cm_id_private *cm_id_priv) -{ - int err; - u32 id; - - err = xa_alloc_cyclic_irq(&cm.local_id_table, &id, cm_id_priv, - xa_limit_32b, &cm.local_id_next, GFP_KERNEL); - - cm_id_priv->id.local_id = (__force __be32)id ^ cm.random_id_operand; - return err; -} - static u32 cm_local_id(__be32 local_id) { return (__force u32) (local_id ^ cm.random_id_operand); @@ -825,6 +813,7 @@ struct ib_cm_id *ib_create_cm_id(struct void *context) { struct cm_id_private *cm_id_priv; + u32 id; int ret; cm_id_priv = kzalloc(sizeof *cm_id_priv, GFP_KERNEL); @@ -836,9 +825,6 @@ struct ib_cm_id *ib_create_cm_id(struct cm_id_priv->id.cm_handler = cm_handler; cm_id_priv->id.context = context; cm_id_priv->id.remote_cm_qpn = 1; - ret = cm_alloc_id(cm_id_priv); - if (ret) - goto error; spin_lock_init(&cm_id_priv->lock); init_completion(&cm_id_priv->comp); @@ -847,11 +833,20 @@ struct ib_cm_id *ib_create_cm_id(struct INIT_LIST_HEAD(&cm_id_priv->altr_list); atomic_set(&cm_id_priv->work_count, -1); refcount_set(&cm_id_priv->refcount, 1); + + ret = xa_alloc_cyclic_irq(&cm.local_id_table, &id, NULL, xa_limit_32b, + &cm.local_id_next, GFP_KERNEL); + if (ret) + goto error; + cm_id_priv->id.local_id = (__force __be32)id ^ cm.random_id_operand; + xa_store_irq(&cm.local_id_table, cm_local_id(cm_id_priv->id.local_id), + cm_id_priv, GFP_KERNEL); + return &cm_id_priv->id; error: kfree(cm_id_priv); - return ERR_PTR(-ENOMEM); + return ERR_PTR(ret); } EXPORT_SYMBOL(ib_create_cm_id);