Received: by 10.213.65.68 with SMTP id h4csp1727839imn; Mon, 19 Mar 2018 11:35:21 -0700 (PDT) X-Google-Smtp-Source: AG47ELsWCWyq1RpBjQCUUi/UHW0YJVrlYU/+wCYnrmG99Is3EOkzIaApPJNUGuIDeRgDF7cVS++8 X-Received: by 10.98.155.137 with SMTP id e9mr1434888pfk.109.1521484521507; Mon, 19 Mar 2018 11:35:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521484521; cv=none; d=google.com; s=arc-20160816; b=TA1WoIrW3CA7TGHaA5TOPaPKF0CUOvDK46TMKdkk272iBdktV8M7Es0qM19RzMimud zOtPuZZnwddDckwhaSgfVGMZ6v1/T9lXTr3mBtsuYlGyjJ8eak6maqr2Tr3mwP7vSh51 yvxB5XYYK3n+wCe4N3MZ3vgmSchF+Nz2S17P+lmB+xhMx+y/75U2Tsh7SfMfLDpVNr0i nVmxT6v72mauusEkjHlhJ/hgZqhIiOKtzitDgEsDyx0n5Kf93pbtRtcrWdMzCSL6CT1n ab6EEPiuRDi8PVdfcUlRzHedNTt2NFdL5iYsdILcjxGXwWQVQ7fWnx1BcRuHnsdp9/LW Fy+g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=QJ6VYFte9k0kNSXbR7ub+uhRD+/r/4p54RqKUImka5s=; b=vuvJeOGP5tKpp+aXFbvrrIEcmA3h6HuVj7hPEByz2HrFCttajvu+ZFEW13iMUJ8Qvt MeYWJHZyNLdAIoknJw2E2/HK7tEOgJAK39hx5kcra2iP1s3L3FmDpxP8Cpta9jYiiRNd OpXuxEb4u6XO1yXnbQDNhc6xlZXoRwuz0WF93/npNvnPN99WZxNdc3viZ8mIQ4MO/YaH SNvyqkMSmrVxqmtJxVyZKY4LPEE8nhSUorrCqkBQoLN5i6TF6UntzMGRLMDqGRVVV6Vt LyNrx276A/Nd7AF7IKweio7lLtrWq9EHkCc15LM5edox6tKRLACL4ZAzCWl+X3+z0Dwi iB3A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d14-v6si413381plj.191.2018.03.19.11.35.07; Mon, 19 Mar 2018 11:35:21 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1032190AbeCSSdQ (ORCPT + 99 others); Mon, 19 Mar 2018 14:33:16 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:54172 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1032179AbeCSSdK (ORCPT ); Mon, 19 Mar 2018 14:33:10 -0400 Received: from localhost (LFbn-1-12247-202.w90-92.abo.wanadoo.fr [90.92.61.202]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 59D1E12B9; Mon, 19 Mar 2018 18:33:09 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Tejun Heo , Dennis Dalessandro , Mike Marciniszyn , linux-rdma@vger.kernel.org, Linus Torvalds Subject: [PATCH 4.15 28/52] RDMAVT: Fix synchronization around percpu_ref Date: Mon, 19 Mar 2018 19:08:26 +0100 Message-Id: <20180319180736.648327623@linuxfoundation.org> X-Mailer: git-send-email 2.16.2 In-Reply-To: <20180319180734.976730813@linuxfoundation.org> References: <20180319180734.976730813@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.15-stable review patch. If anyone has any objections, please let me know. ------------------ From: Tejun Heo commit 74b44bbe80b4c62113ac1501482ea1ee40eb9d67 upstream. rvt_mregion uses percpu_ref for reference counting and RCU to protect accesses from lkey_table. When a rvt_mregion needs to be freed, it first gets unregistered from lkey_table and then rvt_check_refs() is called to wait for in-flight usages before the rvt_mregion is freed. rvt_check_refs() seems to have a couple issues. * It has a fast exit path which tests percpu_ref_is_zero(). However, a percpu_ref reading zero doesn't mean that the object can be released. In fact, the ->release() callback might not even have started executing yet. Proceeding with freeing can lead to use-after-free. * lkey_table is RCU protected but there is no RCU grace period in the free path. percpu_ref uses RCU internally but it's sched-RCU whose grace periods are different from regular RCU. Also, it generally isn't a good idea to depend on internal behaviors like this. To address the above issues, this patch removes the fast exit and adds an explicit synchronize_rcu(). Signed-off-by: Tejun Heo Acked-by: Dennis Dalessandro Cc: Mike Marciniszyn Cc: linux-rdma@vger.kernel.org Cc: Linus Torvalds Signed-off-by: Greg Kroah-Hartman --- drivers/infiniband/sw/rdmavt/mr.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) --- a/drivers/infiniband/sw/rdmavt/mr.c +++ b/drivers/infiniband/sw/rdmavt/mr.c @@ -489,11 +489,13 @@ static int rvt_check_refs(struct rvt_mre unsigned long timeout; struct rvt_dev_info *rdi = ib_to_rvt(mr->pd->device); - if (percpu_ref_is_zero(&mr->refcount)) - return 0; - /* avoid dma mr */ - if (mr->lkey) + if (mr->lkey) { + /* avoid dma mr */ rvt_dereg_clean_qps(mr); + /* @mr was indexed on rcu protected @lkey_table */ + synchronize_rcu(); + } + timeout = wait_for_completion_timeout(&mr->comp, 5 * HZ); if (!timeout) { rvt_pr_err(rdi,