From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9277BC433F5 for ; Mon, 24 Jan 2022 23:16:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1BC5A6B0083; Mon, 24 Jan 2022 18:16:51 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 16A2B6B0085; Mon, 24 Jan 2022 18:16:51 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0323E6B0087; Mon, 24 Jan 2022 18:16:50 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0225.hostedemail.com [216.40.44.225]) by kanga.kvack.org (Postfix) with ESMTP id EAB9A6B0083 for ; Mon, 24 Jan 2022 18:16:50 -0500 (EST) Received: from smtpin25.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id AEC68181DF751 for ; Mon, 24 Jan 2022 23:16:50 +0000 (UTC) X-FDA: 79066742580.25.0F6E419 Received: from mail-ej1-f41.google.com (mail-ej1-f41.google.com [209.85.218.41]) by imf21.hostedemail.com (Postfix) with ESMTP id 34DAF1C000D for ; Mon, 24 Jan 2022 23:16:50 +0000 (UTC) Received: by mail-ej1-f41.google.com with SMTP id o12so25758667eju.13 for ; Mon, 24 Jan 2022 15:16:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amacapital-net.20210112.gappssmtp.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=QxPfP8pNdiwhYtTaz9f4lBwxupGy0qqIDwxrF8YmvL4=; b=0lvwqQR2eMER7dNpywxJaMiUqn32TykDwHsFXQbk9QtjkddJz2JxMGMZ7vUIn8qNts b4NnCXXnk++9wRjgnjBoj/H8wwHQbeMEeOeFiXKEz1Ghgw9sUe15oV7MQf5g0moLeSDr HHQwzSZxyKgDcTjLe+gQyBxqKouzz3a6ZOqPPGvmFR0kxoZSQmhWErOkToPMsq+H6Ece e8LzxFG2GXvbEYwqdSuTihLXeFvh6lNrlYwiULzPCsV35seif51vYRYesGdEkjf9RRvZ 2xcpPXV1xP8ARGPHNtdSNmtDF9vk4RSO4pz23LKFXsjvYns3rjQCRe8CcKkLqLnQCHtZ h1MQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=QxPfP8pNdiwhYtTaz9f4lBwxupGy0qqIDwxrF8YmvL4=; b=j2mttAJDFj9CfXRxYypx4+lLoIYZDaAZMVLDttk1MJR9nCie2KOdw9ChGSZkkbfafa U/70dexuzNJ9IIqooRm7Kavh0rJgRoU3TSRJtDecRCmcYffln0TEyFvV7F0SnXrg4TaG Lz4+FkMyi1ZyWT+OZFJ0pH21561UIaXjjXop4aup3rvEpPbzHdndaExfj2VRnXLxCmZH oqlTwXiiAYl0kcS7/BaobG2GXIRTWxEeG/+CRXraKsYx1Ww21Yt5yEeuy3GQBKba+RCl 7yRVmiGHF0nME+/raP3ggPt+P5lbsnspRl03quNoDv3Q4oEwnr9XWV6Iaz+rQwuEf3ge vLng== X-Gm-Message-State: AOAM533tLqCIdhN6LXttgW3qYjKrOj4DXOptxm6xAEFcVwIxp9I8Ixqr PAG1YOZLFzN3Px7KgGhq5llhiL4oli1RwAKJrIuxGg== X-Google-Smtp-Source: ABdhPJw5ZrUO3qCzeWxUtnf+ow/Ga2rQet7PkkHrJWNYwOkFrn2Bn5jHHu8lm5LUBgrkablnldXJANswKhLcg+kcvBE= X-Received: by 2002:a17:906:e15:: with SMTP id l21mr13841455eji.675.1643066208803; Mon, 24 Jan 2022 15:16:48 -0800 (PST) MIME-Version: 1.0 References: <4d333527-391f-fe6b-eb2d-123d67242d2c@oracle.com> In-Reply-To: From: Andy Lutomirski Date: Mon, 24 Jan 2022 15:16:26 -0800 Message-ID: Subject: Re: [RFC PATCH 0/6] Add support for shared PTEs across processes To: Khalid Aziz Cc: Mike Rapoport , akpm@linux-foundation.org, willy@infradead.org, longpeng2@huawei.com, arnd@arndb.de, dave.hansen@linux.intel.com, david@redhat.com, surenb@google.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-api@vger.kernel.org Content-Type: text/plain; charset="UTF-8" X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 34DAF1C000D X-Stat-Signature: 544rbn93mbwpd4dcixogfz8umhamd4tg Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=amacapital-net.20210112.gappssmtp.com header.s=20210112 header.b=0lvwqQR2; dmarc=none; spf=pass (imf21.hostedemail.com: domain of luto@amacapital.net designates 209.85.218.41 as permitted sender) smtp.mailfrom=luto@amacapital.net X-Rspam-User: nil X-HE-Tag: 1643066210-62703 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: On Mon, Jan 24, 2022 at 2:34 PM Khalid Aziz wrote: > > On 1/24/22 12:45, Andy Lutomirski wrote: > > On Mon, Jan 24, 2022 at 10:54 AM Khalid Aziz wrote: > >> > >> On 1/22/22 04:31, Mike Rapoport wrote: > >>> (added linux-api) > >>> > >>> On Tue, Jan 18, 2022 at 02:19:12PM -0700, Khalid Aziz wrote: > >>>> Page tables in kernel consume some of the memory and as long as > >>>> number of mappings being maintained is small enough, this space > >>>> consumed by page tables is not objectionable. When very few memory > >>>> pages are shared between processes, the number of page table entries > >>>> (PTEs) to maintain is mostly constrained by the number of pages of > >>>> memory on the system. As the number of shared pages and the number > >>>> of times pages are shared goes up, amount of memory consumed by page > >>>> tables starts to become significant. > >>>> > >>>> Some of the field deployments commonly see memory pages shared > >>>> across 1000s of processes. On x86_64, each page requires a PTE that > >>>> is only 8 bytes long which is very small compared to the 4K page > >>>> size. When 2000 processes map the same page in their address space, > >>>> each one of them requires 8 bytes for its PTE and together that adds > >>>> up to 8K of memory just to hold the PTEs for one 4K page. On a > >>>> database server with 300GB SGA, a system carsh was seen with > >>>> out-of-memory condition when 1500+ clients tried to share this SGA > >>>> even though the system had 512GB of memory. On this server, in the > >>>> worst case scenario of all 1500 processes mapping every page from > >>>> SGA would have required 878GB+ for just the PTEs. If these PTEs > >>>> could be shared, amount of memory saved is very significant. > >>>> > >>>> This is a proposal to implement a mechanism in kernel to allow > >>>> userspace processes to opt into sharing PTEs. The proposal is to add > >>>> a new system call - mshare(), which can be used by a process to > >>>> create a region (we will call it mshare'd region) which can be used > >>>> by other processes to map same pages using shared PTEs. Other > >>>> process(es), assuming they have the right permissions, can then make > >>>> the mashare() system call to map the shared pages into their address > >>>> space using the shared PTEs. When a process is done using this > >>>> mshare'd region, it makes a mshare_unlink() system call to end its > >>>> access. When the last process accessing mshare'd region calls > >>>> mshare_unlink(), the mshare'd region is torn down and memory used by > >>>> it is freed. > >>>> > >>>> > >>>> API Proposal > >>>> ============ > >>>> > >>>> The mshare API consists of two system calls - mshare() and mshare_unlink() > >>>> > >>>> -- > >>>> int mshare(char *name, void *addr, size_t length, int oflags, mode_t mode) > >>>> > >>>> mshare() creates and opens a new, or opens an existing mshare'd > >>>> region that will be shared at PTE level. "name" refers to shared object > >>>> name that exists under /sys/fs/mshare. "addr" is the starting address > >>>> of this shared memory area and length is the size of this area. > >>>> oflags can be one of: > >>>> > >>>> - O_RDONLY opens shared memory area for read only access by everyone > >>>> - O_RDWR opens shared memory area for read and write access > >>>> - O_CREAT creates the named shared memory area if it does not exist > >>>> - O_EXCL If O_CREAT was also specified, and a shared memory area > >>>> exists with that name, return an error. > >>>> > >>>> mode represents the creation mode for the shared object under > >>>> /sys/fs/mshare. > >>>> > >>>> mshare() returns an error code if it fails, otherwise it returns 0. > >>> > >>> Did you consider returning a file descriptor from mshare() system call? > >>> Then there would be no need in mshare_unlink() as close(fd) would work. > >> > >> That is an interesting idea. It could work and eliminates the need for a new system call. It could be confusing though > >> for application writers. A close() call with a side-effect of deleting shared mapping would be odd. One of the use cases > >> for having files for mshare'd regions is to allow for orphaned mshare'd regions to be cleaned up by calling > >> mshare_unlink() with region name. This can require calling mshare_unlink() multiple times in current implementation to > >> bring the refcount for mshare'd region to 0 when mshare_unlink() finally cleans up the region. This would be problematic > >> with a close() semantics though unless there was another way to force refcount to 0. Right? > >> > > > > I'm not sure I understand the problem. If you're sharing a portion of > > an mm and the mm goes away, then all that should be left are some > > struct files that are no longer useful. They'll go away when their > > refcount goes to zero. > > > > --Andy > > > > The mm that holds shared PTEs is a separate mm not tied to a task. I started out by sharing portion of the donor process > initially but that necessitated keeping the donor process alive. If the donor process dies, its mm and the mshare'd > portion go away. > > One of the requirements I have is the process that creates mshare'd region can terminate, possibly involuntarily, but > the mshare'd region persists and rest of the consumer processes continue without hiccup. So I create a separate mm to > hold shared PTEs and that mm is cleaned up when all references to mshare'd region go away. Each call to mshare() > increments the refcount and each call to mshare_unlink() decrements the refcount. In general, objects which are kept alive by name tend to be quite awkward. Things like network namespaces essentially have to work that way and end up with awkward APIs. Things like shared memory don't actually have to be kept alive by name, and the cases that do keep them alive by name (tmpfs, shmget) can end up being so awkward that people invent nameless variants like memfd. So I would strongly suggest you see how the design works out with no names and no external keep-alive mechanism. Either have the continued existence of *any* fd keep the whole thing alive or make it be a pair of struct files, one that controls the region (and can map things into it, etc) and one that can map it. SCM_RIGHTS is pretty good for passing objects like this around. --Andy