Received: by 2002:ac0:946b:0:0:0:0:0 with SMTP id j40csp4099459imj; Tue, 12 Feb 2019 09:45:40 -0800 (PST) X-Google-Smtp-Source: AHgI3IaxQ6AcPsB0PvAcMBXZPP7cVthAg98HGoSEUsoIgu/h51pSt4E/32A8KAvkuPTrszIL7vUn X-Received: by 2002:a62:6491:: with SMTP id y139mr2997290pfb.9.1549993539977; Tue, 12 Feb 2019 09:45:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1549993539; cv=none; d=google.com; s=arc-20160816; b=r5pLQt21cEYKLkofRVNhOEK7OoxY+iHqzLUv+XK7EJ9OhSYmY6y8M/satIm2uMjHj1 lz4xtYY5Uf45r8ygRb/5fsWM/p+Q6pktl3CH1xykQeH6yLlHZ739bRzESQgNvyQbBksX JbJlKkqX278vvEjKNrvhXmY3Ur+BmHeC86RJ0e76dlMHRSWm5+nXcEhHQKPB51Mf6RP8 7EaIkPnN56uv5ZCxDijWLkwkAkLD5/I3urLOQzwnnJbUYWlMsf6a9gjkZwzypd8CNim7 pRb6317jcF5FLyxrf7JGJL/rbnRonutkTXHP0aw4sN0AU324vKlpfoVyzYclJX5FV3S8 DrVA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=VmxsxuHb/nNuCt+dhukfv0Psv77KAOzs+jgh6bmnEkg=; b=Vg0YMRlNn5QlkmwyaHZaobOU+PkEzpnaVZZGsQpDL8xvEyiQnU3ciFQagb4FrvQ5DO vS0mIFt9NlqCRIWzMNVOOb7+EpiD1qMLMiw7bQ7HydCz0DCknBSrfBDL8n2M/bG2Meyq z0RAcLzTPghXUcVF1jQRz81PB/4ZY18MQDF8avC2EZs91OluGbUu32dif2Ogr6rAnCns PiAYHetSVww4zBpwWFz2uXpgpWRRbZe9MPAjvoFJVfQxBIVHM5haU25yoJqLnnv1tmA0 8tdjpYEsASWCTO1lvkhGZzNOUaLyR1uh4Goj+ipPJTbRHjsGeaX/XExd+KY/HAP3iB36 QmDw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2018-07-02 header.b=cAWqpD7p; 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=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id u5si12167758pgr.33.2019.02.12.09.45.22; Tue, 12 Feb 2019 09:45:39 -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; dkim=pass header.i=@oracle.com header.s=corp-2018-07-02 header.b=cAWqpD7p; 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=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729007AbfBLRS4 (ORCPT + 99 others); Tue, 12 Feb 2019 12:18:56 -0500 Received: from aserp2130.oracle.com ([141.146.126.79]:49100 "EHLO aserp2130.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726550AbfBLRSz (ORCPT ); Tue, 12 Feb 2019 12:18:55 -0500 Received: from pps.filterd (aserp2130.oracle.com [127.0.0.1]) by aserp2130.oracle.com (8.16.0.27/8.16.0.27) with SMTP id x1CH9Ntn177072; Tue, 12 Feb 2019 17:18:25 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=date : from : to : cc : subject : message-id : references : mime-version : content-type : in-reply-to; s=corp-2018-07-02; bh=VmxsxuHb/nNuCt+dhukfv0Psv77KAOzs+jgh6bmnEkg=; b=cAWqpD7pHuas7OAJKc8a16t6RRPPDvFeCUPvPLZSU1DV0m2niD9UyXU3JG8AYTXb3UxD cosODj0IWQ1vAiERykYUlPqGoibt95GRbbgdi4RD4ABybKDbmwmFElonnj8b4ddSz/QK 6nleplkWrOjhJNgKzwYLwOHDzLMxbXqaBJoPzKcoD/svMYyNKKM/JoQzurdj0tdkZKl8 HDDbK5VE+Z/Zj+rEul6f03Zbw6ns6ncUl17Yj3kLDMrDh4Fh7x3jWua+z1d3wCExktWc EJfg2BAMbjN87Ls8vB3umLoTeN1KX8AHFvGt7O1pcQtaYUJIMPmAjNOUFJUjqd0m/RDL ww== Received: from userv0022.oracle.com (userv0022.oracle.com [156.151.31.74]) by aserp2130.oracle.com with ESMTP id 2qhre5da28-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 12 Feb 2019 17:18:25 +0000 Received: from userv0121.oracle.com (userv0121.oracle.com [156.151.31.72]) by userv0022.oracle.com (8.14.4/8.14.4) with ESMTP id x1CHIOKL026587 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 12 Feb 2019 17:18:24 GMT Received: from abhmp0018.oracle.com (abhmp0018.oracle.com [141.146.116.24]) by userv0121.oracle.com (8.14.4/8.13.8) with ESMTP id x1CHIKOJ029838; Tue, 12 Feb 2019 17:18:20 GMT Received: from ca-dmjordan1.us.oracle.com (/10.211.9.48) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Tue, 12 Feb 2019 09:18:20 -0800 Date: Tue, 12 Feb 2019 12:18:40 -0500 From: Daniel Jordan To: Christopher Lameter Cc: Alexey Kardashevskiy , Daniel Jordan , jgg@ziepe.ca, akpm@linux-foundation.org, dave@stgolabs.net, jack@suse.cz, 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, alex.williamson@redhat.com, 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: <20190212171839.env4rnjwdjyips6z@ca-dmjordan1.us.oracle.com> References: <20190211224437.25267-1-daniel.m.jordan@oracle.com> <20190211224437.25267-3-daniel.m.jordan@oracle.com> <01000168e29daf0a-cb3a9394-e3dd-4d88-ad3c-31df1f9ec052-000000@email.amazonses.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <01000168e29daf0a-cb3a9394-e3dd-4d88-ad3c-31df1f9ec052-000000@email.amazonses.com> User-Agent: NeoMutt/20180323-268-5a959c X-Proofpoint-Virus-Version: vendor=nai engine=5900 definitions=9165 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 priorityscore=1501 malwarescore=0 suspectscore=18 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1810050000 definitions=main-1902120122 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Feb 12, 2019 at 04:50:11PM +0000, Christopher Lameter wrote: > On Tue, 12 Feb 2019, Alexey Kardashevskiy wrote: > > > 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, > > Updating a regular counter is racy and requires a lock. It was converted > to be an atomic which can be incremented without a race. Yes, though Alexey may have meant that the multiple reads of the atomic in decrement_pinned_vm are racy. It only matters when there's a bug that would make the counter go negative, but it's there. And FWIW the debug print in try_increment_pinned_vm is also racy. This fixes all that. It doesn't try to correct the negative pinned_vm as the old code did because it's already a bug and adjusting the value by the negative amount seems to do nothing but make debugging harder. If it's ok, I'll respin the whole series this way (another point for common helper) diff --git a/drivers/vfio/vfio_iommu_spapr_tce.c b/drivers/vfio/vfio_iommu_spapr_tce.c index f47e020dc5e4..b79257304de6 100644 --- a/drivers/vfio/vfio_iommu_spapr_tce.c +++ b/drivers/vfio/vfio_iommu_spapr_tce.c @@ -53,25 +53,24 @@ static long try_increment_pinned_vm(struct mm_struct *mm, long npages) atomic64_sub(npages, &mm->pinned_vm); } - pr_debug("[%d] RLIMIT_MEMLOCK +%ld %ld/%lu%s\n", current->pid, - npages << PAGE_SHIFT, - atomic64_read(&mm->pinned_vm) << PAGE_SHIFT, - rlimit(RLIMIT_MEMLOCK), ret ? " - exceeded" : ""); + pr_debug("[%d] RLIMIT_MEMLOCK +%ld %lld/%lu%s\n", current->pid, + npages << PAGE_SHIFT, pinned << PAGE_SHIFT, + lock_limit, ret ? " - exceeded" : ""); return ret; } static void decrement_pinned_vm(struct mm_struct *mm, long npages) { + s64 pinned; + if (!mm || !npages) return; - 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, - atomic64_read(&mm->pinned_vm) << PAGE_SHIFT, + pinned = atomic64_sub_return(npages, &mm->pinned_vm); + WARN_ON_ONCE(pinned < 0); + pr_debug("[%d] RLIMIT_MEMLOCK -%ld %lld/%lu\n", current->pid, + npages << PAGE_SHIFT, pinned << PAGE_SHIFT, rlimit(RLIMIT_MEMLOCK)); }