From: Daniel Jordan <daniel.m.jordan@oracle.com>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: Daniel Jordan <daniel.m.jordan@oracle.com>,
Alan Tull <atull@kernel.org>,
Alexey Kardashevskiy <aik@ozlabs.ru>,
Alex Williamson <alex.williamson@redhat.com>,
Benjamin Herrenschmidt <benh@kernel.crashing.org>,
Christoph Lameter <cl@linux.com>,
Davidlohr Bueso <dave@stgolabs.net>,
Michael Ellerman <mpe@ellerman.id.au>,
Moritz Fischer <mdf@kernel.org>,
Paul Mackerras <paulus@ozlabs.org>, Wu Hao <hao.wu@intel.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
Subject: Re: [PATCH 1/6] mm: change locked_vm's type from unsigned long to atomic64_t
Date: Wed, 3 Apr 2019 11:58:39 -0400 [thread overview]
Message-ID: <20190403155839.m447czluxd74n5ad@ca-dmjordan1.us.oracle.com> (raw)
In-Reply-To: <20190402150424.5cf64e19deeafa58fc6c1a9f@linux-foundation.org>
On Tue, Apr 02, 2019 at 03:04:24PM -0700, Andrew Morton wrote:
> On Tue, 2 Apr 2019 16:41:53 -0400 Daniel Jordan <daniel.m.jordan@oracle.com> wrote:
> > static long kvmppc_account_memlimit(unsigned long stt_pages, bool inc)
> > {
> > long ret = 0;
> > + s64 locked_vm;
> >
> > if (!current || !current->mm)
> > return ret; /* process exited */
> >
> > down_write(¤t->mm->mmap_sem);
> >
> > + locked_vm = atomic64_read(¤t->mm->locked_vm);
> > if (inc) {
> > unsigned long locked, lock_limit;
> >
> > - locked = current->mm->locked_vm + stt_pages;
> > + locked = locked_vm + stt_pages;
> > lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
> > if (locked > lock_limit && !capable(CAP_IPC_LOCK))
> > ret = -ENOMEM;
> > else
> > - current->mm->locked_vm += stt_pages;
> > + atomic64_add(stt_pages, ¤t->mm->locked_vm);
> > } else {
> > - if (WARN_ON_ONCE(stt_pages > current->mm->locked_vm))
> > - stt_pages = current->mm->locked_vm;
> > + if (WARN_ON_ONCE(stt_pages > locked_vm))
> > + stt_pages = locked_vm;
> >
> > - current->mm->locked_vm -= stt_pages;
> > + atomic64_sub(stt_pages, ¤t->mm->locked_vm);
> > }
>
> With the current code, current->mm->locked_vm cannot go negative.
> After the patch, it can go negative. If someone else decreased
> current->mm->locked_vm between this function's atomic64_read() and
> atomic64_sub().
>
> I guess this is a can't-happen in this case because the racing code
> which performed the modification would have taken it negative anyway.
>
> But this all makes me rather queazy.
mmap_sem is still held in this patch, so updates to locked_vm are still
serialized and I don't think what you describe can happen. A later patch
removes mmap_sem, of course, but it also rewrites the code to do something
different. This first patch is just a mechanical type change from unsigned
long to atomic64_t.
So...does this alleviate your symptoms?
> Also, we didn't remove any down_write(mmap_sem)s from core code so I'm
> thinking that the benefit of removing a few mmap_sem-takings from a few
> obscure drivers (sorry ;)) is pretty small.
Not sure about the other drivers, but vfio type1 isn't obscure. We use it
extensively in our cloud, and from Andrea's __GFP_THISNODE thread a few months
back it seems Red Hat also uses it:
https://lore.kernel.org/linux-mm/20180820032204.9591-3-aarcange@redhat.com/
> Also, the argument for switching 32-bit arches to a 64-bit counter was
> suspiciously vague. What overflow issues? Or are we just being lazy?
If user-controlled values are used to increase locked_vm, multiple threads
doing it at once on a 32-bit system could theoretically cause overflow, so in
the absence of atomic overflow checking, the 64-bit counter on 32b is defensive
programming.
I wouldn't have thought to do it, but Jason Gunthorpe raised the same issue in
the pinned_vm series:
https://lore.kernel.org/linux-mm/20190115205311.GD22031@mellanox.com/
I'm fine with changing it to atomic_long_t if the scenario is too theoretical
for people.
Anyway, thanks for looking at this.
next prev parent reply other threads:[~2019-04-03 16:00 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-04-02 20:41 [PATCH 0/6] convert locked_vm " Daniel Jordan
2019-04-02 20:41 ` [PATCH 1/6] mm: change locked_vm's type " Daniel Jordan
2019-04-02 22:04 ` Andrew Morton
2019-04-02 23:43 ` Davidlohr Bueso
2019-04-03 16:07 ` Daniel Jordan
2019-04-03 15:58 ` Daniel Jordan [this message]
2019-04-03 4:46 ` Christophe Leroy
2019-04-03 16:09 ` Daniel Jordan
2019-04-11 4:22 ` Alexey Kardashevskiy
2019-04-11 9:55 ` Mark Rutland
2019-04-11 20:28 ` Daniel Jordan
2019-04-16 23:33 ` Andrew Morton
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 ` [PATCH 4/6] fpga/dlf/afu: " Daniel Jordan
2019-04-02 20:41 ` [PATCH 5/6] powerpc/mmu: " Daniel Jordan
2019-04-03 4:58 ` Christophe Leroy
2019-04-03 16:40 ` Daniel Jordan
2019-04-24 2:15 ` Davidlohr Bueso
2019-04-24 2:31 ` Davidlohr Bueso
2019-04-24 11:10 ` Jason Gunthorpe
2019-04-25 1:47 ` Daniel Jordan
2019-04-02 20:41 ` [PATCH 6/6] kvm/book3s: " Daniel Jordan
2019-04-03 12:51 ` [PATCH 0/6] convert locked_vm from unsigned long to atomic64_t Steven Sistare
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=20190403155839.m447czluxd74n5ad@ca-dmjordan1.us.oracle.com \
--to=daniel.m.jordan@oracle.com \
--cc=aik@ozlabs.ru \
--cc=akpm@linux-foundation.org \
--cc=alex.williamson@redhat.com \
--cc=atull@kernel.org \
--cc=benh@kernel.crashing.org \
--cc=cl@linux.com \
--cc=dave@stgolabs.net \
--cc=hao.wu@intel.com \
--cc=kvm-ppc@vger.kernel.org \
--cc=kvm@vger.kernel.org \
--cc=linux-fpga@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linuxppc-dev@lists.ozlabs.org \
--cc=mdf@kernel.org \
--cc=mpe@ellerman.id.au \
--cc=paulus@ozlabs.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox