From patchwork Tue Jan 15 18:12:59 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Davidlohr Bueso X-Patchwork-Id: 10764939 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 E1E3F6C5 for ; Tue, 15 Jan 2019 18:13:43 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D42A92D467 for ; Tue, 15 Jan 2019 18:13:43 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C800F2D4D8; Tue, 15 Jan 2019 18:13:43 +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 44D1B2D467 for ; Tue, 15 Jan 2019 18:13:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D66338E0009; Tue, 15 Jan 2019 13:13:41 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id C78F88E0007; 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 B406A8E0008; 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-pf1-f200.google.com (mail-pf1-f200.google.com [209.85.210.200]) by kanga.kvack.org (Postfix) with ESMTP id 682918E0006 for ; Tue, 15 Jan 2019 13:13:41 -0500 (EST) Received: by mail-pf1-f200.google.com with SMTP id f69so2571626pff.5 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=OsrANWbLtw5jX9/sTFFyY9hdWjcAlO+uaJD2IzHls/4=; b=tvt9Aik74/uQmjvjhSzoV/xZm6+HAlHlliUHH+xgTmSOlqYh9j135XDY2aaxmyuLkT 4jRE10OVpgN/qIYialXdftOrRaexmfiEMrNXnHblzxVp95ut4QQPaJnepr5MB9Z+tXYq FoGxmy4srunhVJs45vi+WXiLss15U1TXYyhGoDPvPfEKSgLGEtCzt5KUNthCkTVeOyfb 9QNZbzJ4yQGxaBKxu+YSevM0Ol6EPX2NsNOSk2N6P4aP9z6p9FB6IY+qmLQWyN72k5gz FbQ5s9/kjoLAM11OAuiYqZ9OM5H7LfVCCbz5qGMiPVjsJTTVibi8N5xCFClX0luAqYTN R5DQ== 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: AJcUukeCJPLDtoW2VoPNFq72lg8gFq76cYnLu1UXgpzxiCT0AsBPOhf6 6fc88LsBQWHNZWpjh2Oa+1Z6/+7WlerV/f2eNHJgSiWVunrNsezdMUo8sBM0ilb4jEWhs/HyKMT RSHPRsFx0SASt9FcHW5c1/j3HolxFIfl8x6SpbT9KLcvS4bjJ7OKB9Ukq2YWztCk= X-Received: by 2002:a63:5761:: with SMTP id h33mr4889887pgm.283.1547576021063; Tue, 15 Jan 2019 10:13:41 -0800 (PST) X-Google-Smtp-Source: ALg8bN48TlZG3CEO1nZUyCZwbY+5ZXYPJ31OkSswaoWEqZ78hwasQlhZkjQHhzM/eDUPbwC8QAUD X-Received: by 2002:a63:5761:: with SMTP id h33mr4889830pgm.283.1547576020269; 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=xmXTF6CkRyt41+jnHLa87CLiu8rMM9H2ZY+vZZn/g9FjK4N6hwJVeS2gv8BrfaxzcL inyqAtFt01xlQqcG+S08GP9flsmR9CnHd8wA/KLOXnUoGu7pdFLqMvAW7Kk64ohEKe1u UDNFXecOYnXQxKXtI7rTJYRXFqLzzI3/4tc58cqn5HfIYwQtVxtkwVVMhY9ljtMOWzcg eot9Rr4TMeHkLuKqr5FbhrJCESllcrVXp0R9IuWkcWquo40LNBkpwOdH/1X8nZpe/tCv mWDIB2SiJhsaD2CTkHMyvbyAyS3NRuVQKZFHeLt/KlQPPPsvAZJwR0oFfYVTB3cXWiVQ x4YQ== 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=OsrANWbLtw5jX9/sTFFyY9hdWjcAlO+uaJD2IzHls/4=; b=FXcfbiugtpPW90lNbLMpwPddz0BPonSbyeXOyneXK6mRrMVVeRviMbmAEKtZ8KqrsT f6nAGypgSIzCIUms28Q4ITpaIxSOCpO/dbrYuJLyYYD/SBBdyjLIEmgw7/q5nGRiumja 9YJMAyxascm4BXDV2Fpl/3MhOvNA+onxmV4Mo7+lZAzcjWdQD4KFCEzn6Mhxk+oF91Tq JTC3cniBnIEOKaF+2OUCvTWikflIl9ZZo5qFPbutimMkZtzdU5vEJO1vY91jpfb9VSJr O4KHMUUiB+zHcPYuesbujU5q9BLD5FF39+3iD4knLyIHY4kIHbl3GG0STqLwMyN42HTw 8Vyw== 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 u186si3679201pgd.131.2019.01.15.10.13.39 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:29 -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, benve@cisco.com, neescoba@cisco.com, pkaustub@cisco.com, Davidlohr Bueso Subject: [PATCH 5/6] drivers/IB,usnic: reduce scope of mmap_sem Date: Tue, 15 Jan 2019 10:12:59 -0800 Message-Id: <20190115181300.27547-6-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 usnic_uiom_get_pages() uses gup_longterm() so we cannot really get rid of mmap_sem altogether in the driver, but we can get rid of some complexity that mmap_sem brings with only pinned_vm. We can get rid of the wq altogether as we no longer need to defer work to unpin pages as the counter is now atomic. Cc: benve@cisco.com Cc: neescoba@cisco.com Cc: pkaustub@cisco.com Signed-off-by: Davidlohr Bueso Reviewed-by: Ira Weiny Acked-by: Parvi Kaustubhi --- drivers/infiniband/hw/usnic/usnic_ib_main.c | 2 -- drivers/infiniband/hw/usnic/usnic_uiom.c | 54 +++-------------------------- drivers/infiniband/hw/usnic/usnic_uiom.h | 1 - 3 files changed, 4 insertions(+), 53 deletions(-) diff --git a/drivers/infiniband/hw/usnic/usnic_ib_main.c b/drivers/infiniband/hw/usnic/usnic_ib_main.c index b2323a52a0dd..64bc4fda36bf 100644 --- a/drivers/infiniband/hw/usnic/usnic_ib_main.c +++ b/drivers/infiniband/hw/usnic/usnic_ib_main.c @@ -691,7 +691,6 @@ static int __init usnic_ib_init(void) out_pci_unreg: pci_unregister_driver(&usnic_ib_pci_driver); out_umem_fini: - usnic_uiom_fini(); return err; } @@ -704,7 +703,6 @@ static void __exit usnic_ib_destroy(void) unregister_inetaddr_notifier(&usnic_ib_inetaddr_notifier); unregister_netdevice_notifier(&usnic_ib_netdevice_notifier); pci_unregister_driver(&usnic_ib_pci_driver); - usnic_uiom_fini(); } MODULE_DESCRIPTION("Cisco VIC (usNIC) Verbs Driver"); diff --git a/drivers/infiniband/hw/usnic/usnic_uiom.c b/drivers/infiniband/hw/usnic/usnic_uiom.c index 22c40c432b9e..555d7bc93e72 100644 --- a/drivers/infiniband/hw/usnic/usnic_uiom.c +++ b/drivers/infiniband/hw/usnic/usnic_uiom.c @@ -47,8 +47,6 @@ #include "usnic_uiom.h" #include "usnic_uiom_interval_tree.h" -static struct workqueue_struct *usnic_uiom_wq; - #define USNIC_UIOM_PAGE_CHUNK \ ((PAGE_SIZE - offsetof(struct usnic_uiom_chunk, page_list)) /\ ((void *) &((struct usnic_uiom_chunk *) 0)->page_list[1] - \ @@ -129,7 +127,7 @@ static int usnic_uiom_get_pages(unsigned long addr, size_t size, int writable, uiomr->owning_mm = mm = current->mm; down_write(&mm->mmap_sem); - locked = npages + atomic_long_read(¤t->mm->pinned_vm); + locked = atomic_long_add_return(npages, ¤t->mm->pinned_vm); lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) { @@ -185,12 +183,11 @@ static int usnic_uiom_get_pages(unsigned long addr, size_t size, int writable, } out: - if (ret < 0) + if (ret < 0) { usnic_uiom_put_pages(chunk_list, 0); - else { - atomic_long_set(&mm->pinned_vm, locked); + atomic_long_sub(npages, ¤t->mm->pinned_vm); + } else mmgrab(uiomr->owning_mm); - } up_write(&mm->mmap_sem); free_page((unsigned long) page_list); @@ -436,43 +433,12 @@ static inline size_t usnic_uiom_num_pages(struct usnic_uiom_reg *uiomr) return PAGE_ALIGN(uiomr->length + uiomr->offset) >> PAGE_SHIFT; } -static void usnic_uiom_release_defer(struct work_struct *work) -{ - struct usnic_uiom_reg *uiomr = - container_of(work, struct usnic_uiom_reg, work); - - down_write(&uiomr->owning_mm->mmap_sem); - atomic_long_sub(usnic_uiom_num_pages(uiomr), &uiomr->owning_mm->pinned_vm); - up_write(&uiomr->owning_mm->mmap_sem); - - __usnic_uiom_release_tail(uiomr); -} - void usnic_uiom_reg_release(struct usnic_uiom_reg *uiomr, struct ib_ucontext *context) { __usnic_uiom_reg_release(uiomr->pd, uiomr, 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 to a workqueue. - */ - if (context->closing) { - if (!down_write_trylock(&uiomr->owning_mm->mmap_sem)) { - INIT_WORK(&uiomr->work, usnic_uiom_release_defer); - queue_work(usnic_uiom_wq, &uiomr->work); - return; - } - } else { - down_write(&uiomr->owning_mm->mmap_sem); - } atomic_long_sub(usnic_uiom_num_pages(uiomr), &uiomr->owning_mm->pinned_vm); - up_write(&uiomr->owning_mm->mmap_sem); - __usnic_uiom_release_tail(uiomr); } @@ -601,17 +567,5 @@ int usnic_uiom_init(char *drv_name) return -EPERM; } - usnic_uiom_wq = create_workqueue(drv_name); - if (!usnic_uiom_wq) { - usnic_err("Unable to alloc wq for drv %s\n", drv_name); - return -ENOMEM; - } - return 0; } - -void usnic_uiom_fini(void) -{ - flush_workqueue(usnic_uiom_wq); - destroy_workqueue(usnic_uiom_wq); -} diff --git a/drivers/infiniband/hw/usnic/usnic_uiom.h b/drivers/infiniband/hw/usnic/usnic_uiom.h index b86a9731071b..c88cfa087e3a 100644 --- a/drivers/infiniband/hw/usnic/usnic_uiom.h +++ b/drivers/infiniband/hw/usnic/usnic_uiom.h @@ -93,5 +93,4 @@ struct usnic_uiom_reg *usnic_uiom_reg_get(struct usnic_uiom_pd *pd, void usnic_uiom_reg_release(struct usnic_uiom_reg *uiomr, struct ib_ucontext *ucontext); int usnic_uiom_init(char *drv_name); -void usnic_uiom_fini(void); #endif /* USNIC_UIOM_H_ */