Received: by 2002:ac0:946b:0:0:0:0:0 with SMTP id j40csp4219384imj; Tue, 12 Feb 2019 11:54:30 -0800 (PST) X-Google-Smtp-Source: AHgI3IZlTRwil0qi1xZalg1gZRrG+ZbBZ/JQB7UVyzBPyLDtkS/ARxdrcEPMzJIFUhRBPfZ4xPYM X-Received: by 2002:a17:902:b48d:: with SMTP id y13mr5528581plr.273.1550001269987; Tue, 12 Feb 2019 11:54:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1550001269; cv=none; d=google.com; s=arc-20160816; b=OWZUGRv2F8XIEaUfTy1BjHKgYsQKIQ9JwJfrDlcnin9TqrK8gDK7SI9ONhD18Ca4h5 kaJkroJw+euhFVpu80ziTUsaECWrrtYkRfkKkHw8ZO6BheQPlEIdbhdFNZGnV73sSXn9 3Z6FfxYlUpI4gKIGTdltE/Xa2+UFpr82tJ3zxHufyFCkPmttzdHbaNGDOUzVBI90X0v8 UHzFVTK+Ys1LT5t61fiC38EqXjz7x6bnXV2LiY0eJlD76bGDNEh6kdrwH3fxVPCyI62S iIscF69Bk0nGQqhG3k5yK7hTOzblo3G/JBJ/15sjS3RN980fi0vhdpg2RmrS52SQueGL VToQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date; bh=/k1iyzfDcvu7lH9PXGXFz9zsyg69wRbygDedX0js/C8=; b=SvMTJxrGKJHIgl61YWvEZzNVByziobPmbb4eWCYeEV6ODazjldBXtRwaGcec6ajuiY vGLN5aUQI1ob/GbhCXWgtKl/NawbElN4abbNWimCIao/BsXeSif00PRX+7z/Ix+oxXw8 kMYnhnV0hLeQTXBxo1Rwk17RNzSChQPqrtIYOj8NKZATX4oqz8BbZkMY5uYSTHVmctog IO+L++kP17ViSH9uix65pUogi7Y/IUJxSt99UsI0WGvRMby5PZSVS4zJYBw8VyJlN5k1 uf61bNJdBvqygANao0O8IryyFjiy6zHQpqjKMY87NrlbJ4yo/2FGL0fkBNFfXxImIIGX ZhdQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t22si757812pfj.198.2019.02.12.11.54.13; Tue, 12 Feb 2019 11:54:29 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731134AbfBLS44 (ORCPT + 99 others); Tue, 12 Feb 2019 13:56:56 -0500 Received: from mx1.redhat.com ([209.132.183.28]:44568 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726550AbfBLS44 (ORCPT ); Tue, 12 Feb 2019 13:56:56 -0500 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 9C9083DE2F; Tue, 12 Feb 2019 18:56:54 +0000 (UTC) Received: from w520.home (ovpn-116-24.phx2.redhat.com [10.3.116.24]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4EC985C22F; Tue, 12 Feb 2019 18:56:53 +0000 (UTC) Date: Tue, 12 Feb 2019 11:56:52 -0700 From: Alex Williamson To: Alexey Kardashevskiy Cc: Daniel Jordan , jgg@ziepe.ca, akpm@linux-foundation.org, dave@stgolabs.net, jack@suse.cz, cl@linux.com, linux-mm@kvack.org, kvm@vger.kernel.org, kvm-ppc@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-fpga@vger.kernel.org, linux-kernel@vger.kernel.org, paulus@ozlabs.org, benh@kernel.crashing.org, mpe@ellerman.id.au, hao.wu@intel.com, atull@kernel.org, mdf@kernel.org Subject: Re: [PATCH 2/5] vfio/spapr_tce: use pinned_vm instead of locked_vm to account pinned pages Message-ID: <20190212115652.6cf9a20b@w520.home> In-Reply-To: References: <20190211224437.25267-1-daniel.m.jordan@oracle.com> <20190211224437.25267-3-daniel.m.jordan@oracle.com> MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Tue, 12 Feb 2019 18:56:55 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, 12 Feb 2019 17:56:18 +1100 Alexey Kardashevskiy wrote: > On 12/02/2019 09:44, Daniel Jordan wrote: > > Beginning with bc3e53f682d9 ("mm: distinguish between mlocked and pinned > > pages"), locked and pinned pages are accounted separately. The SPAPR > > TCE VFIO IOMMU driver accounts pinned pages to locked_vm; use pinned_vm > > instead. > > > > pinned_vm recently became atomic and so no longer relies on mmap_sem > > held as writer: delete. > > > > Signed-off-by: Daniel Jordan > > --- > > Documentation/vfio.txt | 6 +-- > > drivers/vfio/vfio_iommu_spapr_tce.c | 64 ++++++++++++++--------------- > > 2 files changed, 33 insertions(+), 37 deletions(-) > > > > diff --git a/Documentation/vfio.txt b/Documentation/vfio.txt > > index f1a4d3c3ba0b..fa37d65363f9 100644 > > --- a/Documentation/vfio.txt > > +++ b/Documentation/vfio.txt > > @@ -308,7 +308,7 @@ This implementation has some specifics: > > currently there is no way to reduce the number of calls. In order to make > > things faster, the map/unmap handling has been implemented in real mode > > which provides an excellent performance which has limitations such as > > - inability to do locked pages accounting in real time. > > + inability to do pinned pages accounting in real time. > > > > 4) According to sPAPR specification, A Partitionable Endpoint (PE) is an I/O > > subtree that can be treated as a unit for the purposes of partitioning and > > @@ -324,7 +324,7 @@ This implementation has some specifics: > > returns the size and the start of the DMA window on the PCI bus. > > > > VFIO_IOMMU_ENABLE > > - enables the container. The locked pages accounting > > + enables the container. The pinned pages accounting > > is done at this point. This lets user first to know what > > the DMA window is and adjust rlimit before doing any real job. I don't know of a ulimit only covering pinned pages, so for documentation it seems more correct to continue referring to this as locked page accounting. > > @@ -454,7 +454,7 @@ This implementation has some specifics: > > > > PPC64 paravirtualized guests generate a lot of map/unmap requests, > > and the handling of those includes pinning/unpinning pages and updating > > - mm::locked_vm counter to make sure we do not exceed the rlimit. > > + mm::pinned_vm counter to make sure we do not exceed the rlimit. > > The v2 IOMMU splits accounting and pinning into separate operations: > > > > - VFIO_IOMMU_SPAPR_REGISTER_MEMORY/VFIO_IOMMU_SPAPR_UNREGISTER_MEMORY ioctls > > diff --git a/drivers/vfio/vfio_iommu_spapr_tce.c b/drivers/vfio/vfio_iommu_spapr_tce.c > > index c424913324e3..f47e020dc5e4 100644 > > --- a/drivers/vfio/vfio_iommu_spapr_tce.c > > +++ b/drivers/vfio/vfio_iommu_spapr_tce.c > > @@ -34,9 +34,11 @@ > > static void tce_iommu_detach_group(void *iommu_data, > > struct iommu_group *iommu_group); > > > > -static long try_increment_locked_vm(struct mm_struct *mm, long npages) > > +static long try_increment_pinned_vm(struct mm_struct *mm, long npages) > > { > > - long ret = 0, locked, lock_limit; > > + long ret = 0; > > + s64 pinned; > > + unsigned long lock_limit; > > > > if (WARN_ON_ONCE(!mm)) > > return -EPERM; > > @@ -44,39 +46,33 @@ static long try_increment_locked_vm(struct mm_struct *mm, long npages) > > if (!npages) > > return 0; > > > > - down_write(&mm->mmap_sem); > > - locked = mm->locked_vm + npages; > > + pinned = atomic64_add_return(npages, &mm->pinned_vm); > > lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; > > - if (locked > lock_limit && !capable(CAP_IPC_LOCK)) > > + if (pinned > lock_limit && !capable(CAP_IPC_LOCK)) { > > ret = -ENOMEM; > > - else > > - mm->locked_vm += npages; > > + atomic64_sub(npages, &mm->pinned_vm); > > + } > > > > - pr_debug("[%d] RLIMIT_MEMLOCK +%ld %ld/%ld%s\n", current->pid, > > + pr_debug("[%d] RLIMIT_MEMLOCK +%ld %ld/%lu%s\n", current->pid, > > npages << PAGE_SHIFT, > > - mm->locked_vm << PAGE_SHIFT, > > - rlimit(RLIMIT_MEMLOCK), > > - ret ? " - exceeded" : ""); > > - > > - up_write(&mm->mmap_sem); > > + atomic64_read(&mm->pinned_vm) << PAGE_SHIFT, > > + rlimit(RLIMIT_MEMLOCK), ret ? " - exceeded" : ""); > > > > return ret; > > } > > > > -static void decrement_locked_vm(struct mm_struct *mm, long npages) > > +static void decrement_pinned_vm(struct mm_struct *mm, long npages) > > { > > if (!mm || !npages) > > return; > > > > - down_write(&mm->mmap_sem); > > - if (WARN_ON_ONCE(npages > mm->locked_vm)) > > - npages = mm->locked_vm; > > - mm->locked_vm -= npages; > > - pr_debug("[%d] RLIMIT_MEMLOCK -%ld %ld/%ld\n", current->pid, > > + if (WARN_ON_ONCE(npages > atomic64_read(&mm->pinned_vm))) > > + npages = atomic64_read(&mm->pinned_vm); > > + atomic64_sub(npages, &mm->pinned_vm); > > + pr_debug("[%d] RLIMIT_MEMLOCK -%ld %ld/%lu\n", current->pid, > > npages << PAGE_SHIFT, > > - mm->locked_vm << PAGE_SHIFT, > > + atomic64_read(&mm->pinned_vm) << PAGE_SHIFT, > > rlimit(RLIMIT_MEMLOCK)); > > - up_write(&mm->mmap_sem); > > > So it used to be down_write+up_write and stuff in between. > > Now it is 3 independent accesses (actually 4 but the last one is > diagnostic) with no locking around them. Why do not we need a lock > anymore precisely? Thanks, The first 2 look pretty sketchy to me, is there a case where you don't know how many pages you've pinned to unpin them? And can it ever really be correct to just unpin whatever remains? The last access is diagnostic, which leaves 1. Daniel's rework to warn on a negative result looks more sane. Thanks, Alex