From patchwork Tue Jan 15 18:13:00 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Davidlohr Bueso X-Patchwork-Id: 10764941 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 D74E86C5 for ; Tue, 15 Jan 2019 18:13:46 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C9A352D467 for ; Tue, 15 Jan 2019 18:13:46 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id BDBEC2D4D8; Tue, 15 Jan 2019 18:13:46 +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=unavailable 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 5D0552D467 for ; Tue, 15 Jan 2019 18:13:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 091EF8E0006; Tue, 15 Jan 2019 13:13:42 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id D63288E0008; Tue, 15 Jan 2019 13:13:41 -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 C04998E0006; Tue, 15 Jan 2019 13:13:41 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pg1-f197.google.com (mail-pg1-f197.google.com [209.85.215.197]) by kanga.kvack.org (Postfix) with ESMTP id 788D38E0007 for ; Tue, 15 Jan 2019 13:13:41 -0500 (EST) Received: by mail-pg1-f197.google.com with SMTP id q62so2168928pgq.9 for ; Tue, 15 Jan 2019 10:13:41 -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=Z5C2fSyH+OydW+iAWPs1EpjubN8brXYpZjwDPvPzivc=; b=ASBp18XfDJKfcHaImXhtg3+ptlRiYBfVL20UY51I5VTc/bzxQODTd52vtTzUk/y1HB vN8hoYHKgZmXC+mBSd+ejtXG+WmN5CtcrG3Ejk31Ek7j+PLTbTDYZfhTWllp5G+i7GrN QTSzHcbTzOAjJXXYDaFEFJaoUufUqc3gO6L0wTNcOdPtUnMWb0xkWtjBCXlqqaG7e1Sf c7LzJfQtT5esEDRI1Sh1dtwFu/3Z/EO/mer0HMmEtwt7z1sgzB7IDeqYrDm6yYaRaE+Z osNpow5FBWPU0FHqVKbdjH+W+P4m4Rjc3TizQMxZCMCgh4ss0Bdd7N9qWv9rCwzA3Orv xHXw== 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: AJcUukfjYM9BQTR4o6hvT2SJHcm6ASsqka++tR9crj5mSvwgpt8ARJxJ hOP9XsrLdIotrIKHk6ufTF8CYihpJqvYpWskmnBVaIkTvoJP42YVxgvdYEvJ2AOB6m+fIjeilJa 4q9e0kRlQrOj4Q/zRrAyFLmrWZmFI3sxBy0S5cWwHkDzjZGt5sxpEnqmZSXgeWNQ= X-Received: by 2002:a17:902:1122:: with SMTP id d31mr5388213pla.246.1547576021141; Tue, 15 Jan 2019 10:13:41 -0800 (PST) X-Google-Smtp-Source: ALg8bN5K+AZznoYzg8jmrj877Mik5NRK7V5K43RbolDf1CnrwCayUjmQgzjPOE8SIxnLDXGpm3Ep X-Received: by 2002:a17:902:1122:: with SMTP id d31mr5388160pla.246.1547576020389; Tue, 15 Jan 2019 10:13:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1547576020; cv=none; d=google.com; s=arc-20160816; b=dW6d2uK/R+fmZV7/1NUnYwM2k9nCB1xaJ2zSXrS61HuSlfO7bqkRl3OIlKUiRXky14 FVYI+R95yYQqi1OGkm5KnbIqy04pB+YeNnNfIamgw1DnsFyKZ8oods6n6Boh2WE4tmD5 Ta0wlsiGLeNggyx0kCLE59ch9OhhkNUhs2zyRTanQIOdVm9wm0COBGuq6iFf8a6Gi23e 6rXD/O5OQEFdIkbfz9DHlQ6e2VgsN1AXYHWSfxclJ4h+Vb/MS5/HHPxB9B1fgqdaOWFW bBt0Kr8F3/wnjk2xWMSvSlDAuOcXVhdVUUxBdKR9VYTPRJQogV/M9atnSsyML/lvrgjt hqCA== 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=Z5C2fSyH+OydW+iAWPs1EpjubN8brXYpZjwDPvPzivc=; b=WEunCIgMPNWLY4Iw7umTFl6vFZkDnvSoWM1JVl6CICKV8k+OHgkad1ovZuqSZ3IoJ4 L8Et0ePW1Q1J04VojxOSdP2Hs7/FR0zd2IosMy7XxEegh/5WFcqfzqf7fkViaR01/U/u 3QxQK10MTpAAbPVycKOSTbAHnKyzHUm0FmhWf+1DaxdsQRWqAcC/Pmy+Kn8eX5eCS25j vxtYByFuw54mw7MuL6wUxDj13KkxXjKp6cOWQ5F/kRZWeT+M9orshvdqCktO1oRRu8Iu PjAPDyVUNVHSNGE/OPJZuf47HW63RRHshfsem3v6JWv3PsHQ80ukC0y6bcYqJ7VTC2CY ziuA== 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 u4si3692634pga.91.2019.01.15.10.13.40 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 15 Jan 2019 10:13:40 -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); Tue, 15 Jan 2019 11:13:32 -0700 From: Davidlohr Bueso To: akpm@linux-foundation.org Cc: dledford@redhat.com, jgg@mellanox.com, linux-rdma@vger.kernel.org, linux-mm@kvack.org, dave@stgolabs.net, Davidlohr Bueso Subject: [PATCH 6/6] drivers/IB,core: reduce scope of mmap_sem Date: Tue, 15 Jan 2019 10:13:00 -0800 Message-Id: <20190115181300.27547-7-dave@stgolabs.net> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20190115181300.27547-1-dave@stgolabs.net> References: <20190115181300.27547-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. Signed-off-by: Davidlohr Bueso Reviewed-by: Ira Weiny --- 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 bf556215aa7e..baa2412bf6fb 100644 --- a/drivers/infiniband/core/umem.c +++ b/drivers/infiniband/core/umem.c @@ -160,15 +160,12 @@ struct ib_umem *ib_umem_get(struct ib_ucontext *context, unsigned long addr, lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; - down_write(&mm->mmap_sem); - new_pinned = atomic_long_read(&mm->pinned_vm) + npages; + new_pinned = atomic_long_add_return(npages, &mm->pinned_vm); if (new_pinned > lock_limit && !capable(CAP_IPC_LOCK)) { - up_write(&mm->mmap_sem); + atomic_long_sub(npages, &mm->pinned_vm); ret = -ENOMEM; goto out; } - atomic_long_set(&mm->pinned_vm, new_pinned); - up_write(&mm->mmap_sem); cur_base = addr & PAGE_MASK; @@ -228,9 +225,7 @@ struct ib_umem *ib_umem_get(struct ib_ucontext *context, unsigned long addr, umem_release: __ib_umem_release(context->device, umem, 0); vma: - down_write(&mm->mmap_sem); atomic_long_sub(ib_umem_num_pages(umem), &mm->pinned_vm); - up_write(&mm->mmap_sem); out: if (vma_list) free_page((unsigned long) vma_list); @@ -253,25 +248,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); - atomic_long_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); @@ -280,26 +262,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); - } atomic_long_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);