From patchwork Wed Mar 14 19:45:10 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tejun Heo X-Patchwork-Id: 10283447 X-Patchwork-Delegate: jgg@ziepe.ca Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 183A8602BD for ; Wed, 14 Mar 2018 19:46:48 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 067A5285F5 for ; Wed, 14 Mar 2018 19:46:48 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id EF0562860B; Wed, 14 Mar 2018 19:46:47 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6264E285F5 for ; Wed, 14 Mar 2018 19:46:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751996AbeCNTpk (ORCPT ); Wed, 14 Mar 2018 15:45:40 -0400 Received: from mail-yw0-f193.google.com ([209.85.161.193]:34896 "EHLO mail-yw0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751984AbeCNTph (ORCPT ); Wed, 14 Mar 2018 15:45:37 -0400 Received: by mail-yw0-f193.google.com with SMTP id c20so3001182ywa.2; Wed, 14 Mar 2018 12:45:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=w34qzDyrjeBqrv2qxh/nVABXhMqRc0oXDRwHwkp0Wq4=; b=W3nbMwNtJAWn5xFQaeIDLt0/TvNo+AwAFZmT9b9yl02nobDATyqetCTIYPTQKoF9v6 kSJII4tzJ62Gug8+9hzp5UGCOBQU+71w06wPeOcTF2Iue8HTbQ/9JCu+fbI+nJvs3/R9 p/uB+sZkxhu1lawbdZD6043aY8blgYivZmycSBsGl3Vy/8SdqqkKLroy5pipoTRFKUGo Oz78hPhVWalmcIcH7QYJ8hHMNcAh/5JCbJX4uto+sXCaqkUa8q5Rh7Tp6MSuTYUAkfoJ WIJybCqIY2lJw7wZ8aHLs2TgbKPn5UmHwD5+Zez8B9PrJm1+Bmbn7OTXLskEDy1/Z/Wn 8raw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=w34qzDyrjeBqrv2qxh/nVABXhMqRc0oXDRwHwkp0Wq4=; b=iQB6FvT7qkgf7EuC5VwjiQUTMTNGZG6YwkFPM/TR9ffpitMljlnpU9f/RgpaYKouZh 87jWxZcyfJ0WwjdLAStSUP22w31St5DsSYCAghVa0v0wou4RcbKCefXrJP+/obLIG+4Z JnJ02Zlf9jghB0TevpEBziTHcKetffW4aBTWbFkQOTAQ0jEBTN+/Q3joqhBqQhMdGh4/ T4bIqCg29NQFgAiXYt9dvuHuL5MFUt+YfLber9CuF7LE5wTGkGYs7k/tzncROWo2ARG0 Rm4bf2X77NjR6ytO13e0FebIPkjMatJDZzkILc+iDRbk+PyPo/eNa8LCfNWyFUESwCRj J0Eg== X-Gm-Message-State: AElRT7F57WzS8yzd8tp5IMaBTkOJ1rryCsxcBxGKPPU4P8T/idd9Xu86 5z/USEpFoLoMsrY1tdnWKpA= X-Google-Smtp-Source: AG47ELsuDqfMxzqpwY0mcStvppdPgf8eGPatuRfnXbx1vJoQMRqble7asqHfxAi8hulMrPJBsAJTtw== X-Received: by 10.129.155.4 with SMTP id s4mr2160938ywg.18.1521056736451; Wed, 14 Mar 2018 12:45:36 -0700 (PDT) Received: from localhost ([2620:10d:c091:200::6ef7]) by smtp.gmail.com with ESMTPSA id y195sm1314733ywy.103.2018.03.14.12.45.35 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 14 Mar 2018 12:45:35 -0700 (PDT) From: Tejun Heo To: torvalds@linux-foundation.org, jannh@google.com, paulmck@linux.vnet.ibm.com, bcrl@kvack.org, viro@zeniv.linux.org.uk, kent.overstreet@gmail.com Cc: security@kernel.org, linux-kernel@vger.kernel.org, kernel-team@fb.com, Tejun Heo , Mike Marciniszyn , linux-rdma@vger.kernel.org Subject: [PATCH 3/8] RDMAVT: Fix synchronization around percpu_ref Date: Wed, 14 Mar 2018 12:45:10 -0700 Message-Id: <20180314194515.1661824-3-tj@kernel.org> X-Mailer: git-send-email 2.9.5 In-Reply-To: <20180314194515.1661824-1-tj@kernel.org> References: <20180314194205.1651587-1-tj@kernel.org> <20180314194515.1661824-1-tj@kernel.org> Sender: linux-rdma-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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 --- drivers/infiniband/sw/rdmavt/mr.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/drivers/infiniband/sw/rdmavt/mr.c b/drivers/infiniband/sw/rdmavt/mr.c index 1b2e536..cc429b5 100644 --- 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_mregion *mr, const char *t) 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,