From patchwork Mon Jan 21 17:42:20 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Davidlohr Bueso X-Patchwork-Id: 10774265 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id BC361139A for ; Mon, 21 Jan 2019 17:43:19 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A652029F0F for ; Mon, 21 Jan 2019 17:43:19 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9A56C2A6E0; Mon, 21 Jan 2019 17:43:19 +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=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 88F6829F0F for ; Mon, 21 Jan 2019 17:43:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8CE208E0009; Mon, 21 Jan 2019 12:43:17 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 899578E0001; Mon, 21 Jan 2019 12:43:17 -0500 (EST) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7B38E8E0008; Mon, 21 Jan 2019 12:43:17 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-oi1-f198.google.com (mail-oi1-f198.google.com [209.85.167.198]) by kanga.kvack.org (Postfix) with ESMTP id 4EAD38E0001 for ; Mon, 21 Jan 2019 12:43:17 -0500 (EST) Received: by mail-oi1-f198.google.com with SMTP id s204so9916588oib.11 for ; Mon, 21 Jan 2019 09:43:17 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references; bh=waI1WmmPLoTveHPlnnfdMx1fmeBn1F1egAaxgnJemTY=; b=k+OTOC129RyfjIfiwkmbE8O2jhYAdlL8+SH5tnowww/4BtHh7T4EpG+cGqR/Itw7Fm YG+mNjAIfFCSJKaqELf7fY7YWKyZnNdRGpkbwN1uFyVOewTYLNzVOw8I/dEDmUuHen+n HeJXitetSPON3JwyGoBFwLlJ30PJmD7dDGLndBwoGd1Ovucjf3j1xRj0OSAThV7oGB+H T8jeQhvMAn+1gHzAhUMyK0sbvA98vAXto/xWLPZIH9IxlCcfCBjnO8zF/m97xhtyXq2n bC1TVeHflL01Q5HU+dFdvxiZQpc/ezfjJs7r8DgeYH5UWZUGE144e251ec4L9A84s5zY hG0Q== X-Original-Authentication-Results: mx.google.com; spf=softfail (google.com: domain of transitioning dave@stgolabs.net does not designate 137.65.250.81 as permitted sender) smtp.mailfrom=dave@stgolabs.net X-Gm-Message-State: AJcUukcFBZ686sPR5Bezx77OGimDg9gewFgV8laqy0Q1cKFnCCLYHwbo hZTRS57N2v88m+TYuBS9xTbV1VRtkd57HZjjz3Y/G5/kuaRwO6CJrShaF6+5E3WhIuUF00CrnmI n+lluSoTFkGfB/6+WV6/C8neP6AwB2utBzNTpKqe5pPy78QVs3wd1t7B4lmzff18= X-Received: by 2002:aca:293:: with SMTP id 141mr6428654oic.28.1548092597024; Mon, 21 Jan 2019 09:43:17 -0800 (PST) X-Google-Smtp-Source: ALg8bN5IHk/LGWr8bEZskkT3QG41qPw2zzJi8dOoV86y04otYgHZ4IlqIOD4VhWTPyQlxIKPf91w X-Received: by 2002:aca:293:: with SMTP id 141mr6428617oic.28.1548092595959; Mon, 21 Jan 2019 09:43:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548092595; cv=none; d=google.com; s=arc-20160816; b=fFjTMcgbd2IGeOqXfgXjq71m+RZS3ozJFwdPjEhbq8kXYuPBjXKvzGpHee97180WZV jpg7kXJkiqgXY2Zkoaie+V95hqZ+/25jqt9sowVo+1/qlAcUpg6MYv+DOrOHZ8sePopo F8vFl7Gona1rJyYX2jjJeoS9qrC66B61b5d4NpvdnVM+GIbKRcgXIL1RquOz8BZMwDl6 aRl0D8AgXf9R774cHExbuajpYj9WDtKwNqr9cS1ozitqqQWpgENqKL6QJJVn74XWwrex kr03hekbj4PSKnY7EqPaiUe/E1yjG6CQql4BHk9T+5RoRrxuhCi/9UnR0WlwJQGRj/jX wMKQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from; bh=waI1WmmPLoTveHPlnnfdMx1fmeBn1F1egAaxgnJemTY=; b=I3FXG9Fe7a0aGp9A9WSbffAIa1g22W7Cbcczz248+JZHep3SaZu9JMSo70I614NXEt K/8BmyCGxILO3g7skegLg16MSom7PWSCAP6tzK6iawywPHbJHtMFydRgOCXnS2v9dNIr cIo1Qj6iRlAId9Fs5hbgh/Zodb+53CYfhnjDZ8Q/511eHCEQLzOe3lzAGPNxUvJ3AJ8O 1e4PkVWXzxeyqtUHDEwQDDdvDRQajgHlS1k8/Qi+SJJGAXcsiNsjiQWPgAIA3rdMCqnC 7XGP+o6BkwHUKzzo6e5feV8SH2OCuDtCPI/YBcKcutZ7HnVV3MU4DoBrrF1EqLK4lIoY DMyw== ARC-Authentication-Results: i=1; mx.google.com; spf=softfail (google.com: domain of transitioning dave@stgolabs.net does not designate 137.65.250.81 as permitted sender) smtp.mailfrom=dave@stgolabs.net Received: from smtp2.provo.novell.com (smtp2.provo.novell.com. [137.65.250.81]) by mx.google.com with ESMTPS id r3si6200808oif.92.2019.01.21.09.43.15 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 21 Jan 2019 09:43:15 -0800 (PST) Received-SPF: softfail (google.com: domain of transitioning dave@stgolabs.net does not designate 137.65.250.81 as permitted sender) client-ip=137.65.250.81; Authentication-Results: mx.google.com; spf=softfail (google.com: domain of transitioning dave@stgolabs.net does not designate 137.65.250.81 as permitted sender) smtp.mailfrom=dave@stgolabs.net Received: from localhost.localdomain (prv-ext-foundry1int.gns.novell.com [137.65.251.240]) by smtp2.provo.novell.com with ESMTP (TLS encrypted); Mon, 21 Jan 2019 10:43:07 -0700 From: Davidlohr Bueso To: akpm@linux-foundation.org Cc: dledford@redhat.com, jgg@mellanox.com, jack@suse.de, ira.weiny@intel.com, linux-rdma@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, dave@stgolabs.net, Davidlohr Bueso Subject: [PATCH 6/6] drivers/IB,core: reduce scope of mmap_sem Date: Mon, 21 Jan 2019 09:42:20 -0800 Message-Id: <20190121174220.10583-7-dave@stgolabs.net> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20190121174220.10583-1-dave@stgolabs.net> References: <20190121174220.10583-1-dave@stgolabs.net> X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP ib_umem_get() uses gup_longterm() and relies on the lock to stabilze the vma_list, so we cannot really get rid of mmap_sem altogether, but now that the counter is atomic, we can get of some complexity that mmap_sem brings with only pinned_vm. Reviewed-by: Ira Weiny Signed-off-by: Davidlohr Bueso --- drivers/infiniband/core/umem.c | 41 ++--------------------------------------- 1 file changed, 2 insertions(+), 39 deletions(-) diff --git a/drivers/infiniband/core/umem.c b/drivers/infiniband/core/umem.c index 678abe1afcba..b69d3efa8712 100644 --- a/drivers/infiniband/core/umem.c +++ b/drivers/infiniband/core/umem.c @@ -165,15 +165,12 @@ struct ib_umem *ib_umem_get(struct ib_udata *udata, unsigned long addr, lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; - down_write(&mm->mmap_sem); - new_pinned = atomic64_read(&mm->pinned_vm) + npages; + new_pinned = atomic64_add_return(npages, &mm->pinned_vm); if (new_pinned > lock_limit && !capable(CAP_IPC_LOCK)) { - up_write(&mm->mmap_sem); + atomic64_sub(npages, &mm->pinned_vm); ret = -ENOMEM; goto out; } - atomic64_set(&mm->pinned_vm, new_pinned); - up_write(&mm->mmap_sem); cur_base = addr & PAGE_MASK; @@ -233,9 +230,7 @@ struct ib_umem *ib_umem_get(struct ib_udata *udata, unsigned long addr, umem_release: __ib_umem_release(context->device, umem, 0); vma: - down_write(&mm->mmap_sem); atomic64_sub(ib_umem_num_pages(umem), &mm->pinned_vm); - up_write(&mm->mmap_sem); out: if (vma_list) free_page((unsigned long) vma_list); @@ -258,25 +253,12 @@ static void __ib_umem_release_tail(struct ib_umem *umem) kfree(umem); } -static void ib_umem_release_defer(struct work_struct *work) -{ - struct ib_umem *umem = container_of(work, struct ib_umem, work); - - down_write(&umem->owning_mm->mmap_sem); - atomic64_sub(ib_umem_num_pages(umem), &umem->owning_mm->pinned_vm); - up_write(&umem->owning_mm->mmap_sem); - - __ib_umem_release_tail(umem); -} - /** * ib_umem_release - release memory pinned with ib_umem_get * @umem: umem struct to release */ void ib_umem_release(struct ib_umem *umem) { - struct ib_ucontext *context = umem->context; - if (umem->is_odp) { ib_umem_odp_release(to_ib_umem_odp(umem)); __ib_umem_release_tail(umem); @@ -285,26 +267,7 @@ void ib_umem_release(struct ib_umem *umem) __ib_umem_release(umem->context->device, umem, 1); - /* - * We may be called with the mm's mmap_sem already held. This - * can happen when a userspace munmap() is the call that drops - * the last reference to our file and calls our release - * method. If there are memory regions to destroy, we'll end - * up here and not be able to take the mmap_sem. In that case - * we defer the vm_locked accounting a workqueue. - */ - if (context->closing) { - if (!down_write_trylock(&umem->owning_mm->mmap_sem)) { - INIT_WORK(&umem->work, ib_umem_release_defer); - queue_work(ib_wq, &umem->work); - return; - } - } else { - down_write(&umem->owning_mm->mmap_sem); - } atomic64_sub(ib_umem_num_pages(umem), &umem->owning_mm->pinned_vm); - up_write(&umem->owning_mm->mmap_sem); - __ib_umem_release_tail(umem); } EXPORT_SYMBOL(ib_umem_release);