All of lore.kernel.org
 help / color / mirror / Atom feed
From: Daniel Jordan <daniel.m.jordan@oracle.com>
To: akpm@linux-foundation.org
Cc: daniel.m.jordan@oracle.com, Alan Tull <atull@kernel.org>,
	Christoph Lameter <cl@linux.com>,
	Davidlohr Bueso <dave@stgolabs.net>,
	Moritz Fischer <mdf@kernel.org>, Wu Hao <hao.wu@intel.com>,
	linux-mm@kvack.org, linux-fpga@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: [PATCH 4/6] fpga/dlf/afu: drop mmap_sem now that locked_vm is atomic
Date: Tue,  2 Apr 2019 16:41:56 -0400	[thread overview]
Message-ID: <20190402204158.27582-5-daniel.m.jordan@oracle.com> (raw)
In-Reply-To: <20190402204158.27582-1-daniel.m.jordan@oracle.com>

With locked_vm now an atomic, there is no need to take mmap_sem as
writer.  Delete and refactor accordingly.

Signed-off-by: Daniel Jordan <daniel.m.jordan@oracle.com>
Cc: Alan Tull <atull@kernel.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Christoph Lameter <cl@linux.com>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Cc: Moritz Fischer <mdf@kernel.org>
Cc: Wu Hao <hao.wu@intel.com>
Cc: <linux-mm@kvack.org>
Cc: <linux-fpga@vger.kernel.org>
Cc: <linux-kernel@vger.kernel.org>
---
 drivers/fpga/dfl-afu-dma-region.c | 40 ++++++++++++-------------------
 1 file changed, 15 insertions(+), 25 deletions(-)

diff --git a/drivers/fpga/dfl-afu-dma-region.c b/drivers/fpga/dfl-afu-dma-region.c
index 08132fd9b6b7..81e3e3a71758 100644
--- a/drivers/fpga/dfl-afu-dma-region.c
+++ b/drivers/fpga/dfl-afu-dma-region.c
@@ -35,46 +35,36 @@ void afu_dma_region_init(struct dfl_feature_platform_data *pdata)
  * afu_dma_adjust_locked_vm - adjust locked memory
  * @dev: port device
  * @npages: number of pages
- * @incr: increase or decrease locked memory
  *
  * Increase or decrease the locked memory size with npages input.
  *
  * Return 0 on success.
  * Return -ENOMEM if locked memory size is over the limit and no CAP_IPC_LOCK.
  */
-static int afu_dma_adjust_locked_vm(struct device *dev, long npages, bool incr)
+static int afu_dma_adjust_locked_vm(struct device *dev, long pages)
 {
-	unsigned long locked, lock_limit;
+	unsigned long lock_limit;
 	s64 locked_vm;
 	int ret = 0;
 
 	/* the task is exiting. */
-	if (!current->mm)
+	if (!current->mm || !pages)
 		return 0;
 
-	down_write(&current->mm->mmap_sem);
-
-	locked_vm = atomic64_read(&current->mm->locked_vm);
-	if (incr) {
-		locked = locked_vm + npages;
+	locked_vm = atomic64_add_return(pages, &current->mm->locked_vm);
+	WARN_ON_ONCE(locked_vm < 0);
+	if (pages > 0 && !capable(CAP_IPC_LOCK)) {
 		lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
-
-		if (locked > lock_limit && !capable(CAP_IPC_LOCK))
+		if (locked_vm > lock_limit) {
 			ret = -ENOMEM;
-		else
-			atomic64_add(npages, &current->mm->locked_vm);
-	} else {
-		if (WARN_ON_ONCE(npages > locked_vm))
-			npages = locked_vm;
-		atomic64_sub(npages, &current->mm->locked_vm);
+			atomic64_sub(pages, &current->mm->locked_vm);
+		}
 	}
 
 	dev_dbg(dev, "[%d] RLIMIT_MEMLOCK %c%ld %lld/%lu%s\n", current->pid,
-		incr ? '+' : '-', npages << PAGE_SHIFT,
-		(s64)atomic64_read(&current->mm->locked_vm) << PAGE_SHIFT,
-		rlimit(RLIMIT_MEMLOCK), ret ? "- exceeded" : "");
-
-	up_write(&current->mm->mmap_sem);
+		(pages > 0) ? '+' : '-', pages << PAGE_SHIFT,
+		locked_vm << PAGE_SHIFT, rlimit(RLIMIT_MEMLOCK),
+		ret ? "- exceeded" : "");
 
 	return ret;
 }
@@ -94,7 +84,7 @@ static int afu_dma_pin_pages(struct dfl_feature_platform_data *pdata,
 	struct device *dev = &pdata->dev->dev;
 	int ret, pinned;
 
-	ret = afu_dma_adjust_locked_vm(dev, npages, true);
+	ret = afu_dma_adjust_locked_vm(dev, npages);
 	if (ret)
 		return ret;
 
@@ -123,7 +113,7 @@ static int afu_dma_pin_pages(struct dfl_feature_platform_data *pdata,
 free_pages:
 	kfree(region->pages);
 unlock_vm:
-	afu_dma_adjust_locked_vm(dev, npages, false);
+	afu_dma_adjust_locked_vm(dev, -npages);
 	return ret;
 }
 
@@ -143,7 +133,7 @@ static void afu_dma_unpin_pages(struct dfl_feature_platform_data *pdata,
 
 	put_all_pages(region->pages, npages);
 	kfree(region->pages);
-	afu_dma_adjust_locked_vm(dev, npages, false);
+	afu_dma_adjust_locked_vm(dev, -npages);
 
 	dev_dbg(dev, "%ld pages unpinned\n", npages);
 }
-- 
2.21.0


  parent reply	other threads:[~2019-04-02 23:16 UTC|newest]

Thread overview: 63+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-04-02 20:41 [PATCH 0/6] convert locked_vm from unsigned long to atomic64_t Daniel Jordan
2019-04-02 20:41 ` Daniel Jordan
2019-04-02 20:41 ` Daniel Jordan
2019-04-02 20:41 ` Daniel Jordan
2019-04-02 20:41 ` [PATCH 1/6] mm: change locked_vm's type " Daniel Jordan
2019-04-02 20:41   ` Daniel Jordan
2019-04-02 20:41   ` Daniel Jordan
2019-04-02 22:04   ` Andrew Morton
2019-04-02 22:04     ` Andrew Morton
2019-04-02 22:04     ` Andrew Morton
2019-04-02 23:43     ` Davidlohr Bueso
2019-04-02 23:43       ` Davidlohr Bueso
2019-04-02 23:43       ` Davidlohr Bueso
2019-04-03 16:07       ` Daniel Jordan
2019-04-03 16:07         ` Daniel Jordan
2019-04-03 15:58     ` Daniel Jordan
2019-04-03 15:58       ` Daniel Jordan
2019-04-03 15:58       ` Daniel Jordan
2019-04-03  4:46   ` Christophe Leroy
2019-04-03  4:46     ` Christophe Leroy
2019-04-03 16:09     ` Daniel Jordan
2019-04-03 16:09       ` Daniel Jordan
2019-04-03 16:09       ` Daniel Jordan
2019-04-03 16:09       ` Daniel Jordan
2019-04-11  4:22   ` Alexey Kardashevskiy
2019-04-11  4:22     ` Alexey Kardashevskiy
2019-04-11  4:22     ` Alexey Kardashevskiy
2019-04-11  9:55     ` Mark Rutland
2019-04-11  9:55       ` Mark Rutland
2019-04-11  9:55       ` Mark Rutland
2019-04-11 20:28       ` Daniel Jordan
2019-04-11 20:28         ` Daniel Jordan
2019-04-11 20:28         ` Daniel Jordan
2019-04-16 23:33         ` Andrew Morton
2019-04-16 23:33           ` Andrew Morton
2019-04-16 23:33           ` Andrew Morton
2019-04-22 15:54           ` Daniel Jordan
2019-04-22 15:54             ` Daniel Jordan
2019-04-22 15:54             ` Daniel Jordan
2019-04-02 20:41 ` [PATCH 2/6] vfio/type1: drop mmap_sem now that locked_vm is atomic Daniel Jordan
2019-04-02 20:41 ` [PATCH 3/6] vfio/spapr_tce: " Daniel Jordan
2019-04-02 20:41 ` Daniel Jordan [this message]
2019-04-02 20:41 ` [PATCH 5/6] powerpc/mmu: " Daniel Jordan
2019-04-02 20:41   ` Daniel Jordan
2019-04-03  4:58   ` Christophe Leroy
2019-04-03 16:40     ` Daniel Jordan
2019-04-03 16:40       ` Daniel Jordan
2019-04-24  2:15       ` Davidlohr Bueso
2019-04-24  2:15         ` Davidlohr Bueso
2019-04-24  2:31         ` Davidlohr Bueso
2019-04-24 11:10         ` Jason Gunthorpe
2019-04-24 11:10           ` Jason Gunthorpe
2019-04-25  1:47           ` Daniel Jordan
2019-04-25  1:47             ` Daniel Jordan
2019-04-02 20:41 ` [PATCH 6/6] kvm/book3s: " Daniel Jordan
2019-04-02 20:41   ` Daniel Jordan
2019-04-02 20:41   ` Daniel Jordan
2019-04-03 12:51 ` [PATCH 0/6] convert locked_vm from unsigned long to atomic64_t Steven Sistare
2019-04-03 12:51   ` Steven Sistare
2019-04-03 12:51   ` Steven Sistare
2019-04-03 16:52   ` Daniel Jordan
2019-04-03 16:52     ` Daniel Jordan
2019-04-03 16:52     ` Daniel Jordan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190402204158.27582-5-daniel.m.jordan@oracle.com \
    --to=daniel.m.jordan@oracle.com \
    --cc=akpm@linux-foundation.org \
    --cc=atull@kernel.org \
    --cc=cl@linux.com \
    --cc=dave@stgolabs.net \
    --cc=hao.wu@intel.com \
    --cc=linux-fpga@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mdf@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.