From nobody Fri Jan 20 03:23:35 2023 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4NylFh2PZGz30vfh; Fri, 20 Jan 2023 03:23:36 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4NylFh0XxRz3nR6; Fri, 20 Jan 2023 03:23:36 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1674185016; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=frLRGfloMeFF5Apc/ESOAnLRd0G05nrj7pxMdthRDcA=; b=gIBsOcdlxfqAJIRiFExzBODUjGjQEX0uuLvc8OQ8DK4o9rCqn8D8ed7FLI57vduTqOxEKE 9ceCamur9aKUNyxUE1veq1mPw5Le31n+98/YuDQ6JVMAzfCWY0XLXKSWsANYpRrh0SE40I WfvEcc8vJCsnchAd8o9FmLBNaAvLzvvkt0vPZVGjBxTTxNsBEHU3jt6CVodJTZ1r3d3foX nLx8mf+NrGnFfq9wAZWbM8o/9M/3xg7DsLbIqgFoK5qupjAFsLdhlpRFZup9AnRHtUmtKH KQCC5+szDiDnWblJ6NSXfDnv2mPeId5ThHvtUcOmJiIvP/0qvdUBf/FPuSeJ9A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1674185016; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=frLRGfloMeFF5Apc/ESOAnLRd0G05nrj7pxMdthRDcA=; b=xYV96IhAYGB2u/atIg7gQJlt2/2eAcujxkBCWD/TGdLLQfgLUteX+I7SvEGX0N4aVDYYU7 Y5okaI7tylTNGwG8K3prcOhQz9tXnMWOCLfWlOqm9YEbEseZJtcZyNZSD5PpROwhE/N13k z1+42Y4rw0jDmSOB6sXJnCW3XGZY+XKGOihmU4Tg5OFFXY1BRjw/k+vJeC0qRtQmmmSSQv v6Y9q59UqCGRIMF34qfkMW9UM21B52RJDpuMahnrRZAImZ4CYCYULMkOICW4D9nA++5wkN RgAj6wMwtXNTElyYZ0rF95PkbHtq+nmN6RfGK7EhMDz2Rnhgp7DWLNjwjBEeqA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1674185016; a=rsa-sha256; cv=none; b=Db8MU938xpNGCOCUazgV7a0MSnVaG2zVJKqPUT7RcBlZjd19M0oRkso4WYaMHrGavIk5Xa 774/2EoLNmnqy7fnF450ZniZzgRp/zWM15ceO3nmG1ThKRPSbyC4s9TozHgcQ5d4XYHsP5 rZ+vUZxWiYUp2HsXNMjkqzaBkwbhgqwgZ61b+tGcCxx4Mu+Y1BywnksiOtaXJd6UBHFzcG QsvzaLp/6WEJkRCviHxEGsoerD3hqiCiP2MzVTEOi8SYD+wLRGlO4TQGyvv5QQoem3vph6 hTl2/ET4v5bOjexBALGV565a0LXRf80CTkGKVXGenPTg0HjbwfcPUYZWCekj9g== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4NylFg6mQVzGDN; Fri, 20 Jan 2023 03:23:35 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 30K3NZha042376; Fri, 20 Jan 2023 03:23:35 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 30K3NZis042375; Fri, 20 Jan 2023 03:23:35 GMT (envelope-from git) Date: Fri, 20 Jan 2023 03:23:35 GMT Message-Id: <202301200323.30K3NZis042375@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Konstantin Belousov Subject: git: d689fda0703b - stable/13 - shmfd: account for the actually allocated pages List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kib X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: d689fda0703bd7bf7320fe6e34f2abf260650f8e Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=d689fda0703bd7bf7320fe6e34f2abf260650f8e commit d689fda0703bd7bf7320fe6e34f2abf260650f8e Author: Konstantin Belousov AuthorDate: 2022-11-23 21:26:38 +0000 Commit: Konstantin Belousov CommitDate: 2023-01-20 03:20:02 +0000 shmfd: account for the actually allocated pages Tested by: pho (cherry picked from commit 0919f29d91031cf42eca99266bffe7b0faef8f1b) --- sys/kern/uipc_shm.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++++---- sys/sys/mman.h | 1 + 2 files changed, 79 insertions(+), 5 deletions(-) diff --git a/sys/kern/uipc_shm.c b/sys/kern/uipc_shm.c index a1f26b40f343..f1dc0f828b23 100644 --- a/sys/kern/uipc_shm.c +++ b/sys/kern/uipc_shm.c @@ -343,6 +343,61 @@ shm_largepage(struct shmfd *shmfd) return (shmfd->shm_object->type == OBJT_PHYS); } +static void +shm_pager_freespace(vm_object_t obj, vm_pindex_t start, vm_size_t size) +{ + struct shmfd *shm; + vm_size_t c; + + swap_pager_freespace(obj, start, size, &c); + if (c == 0) + return; + + shm = obj->un_pager.swp.swp_priv; + if (shm == NULL) + return; + KASSERT(shm->shm_pages >= c, + ("shm %p pages %jd free %jd", shm, + (uintmax_t)shm->shm_pages, (uintmax_t)c)); + shm->shm_pages -= c; +} + +static void +shm_page_inserted(vm_object_t obj, vm_page_t m) +{ + struct shmfd *shm; + + shm = obj->un_pager.swp.swp_priv; + if (shm == NULL) + return; + if (!vm_pager_has_page(obj, m->pindex, NULL, NULL)) + shm->shm_pages += 1; +} + +static void +shm_page_removed(vm_object_t obj, vm_page_t m) +{ + struct shmfd *shm; + + shm = obj->un_pager.swp.swp_priv; + if (shm == NULL) + return; + if (!vm_pager_has_page(obj, m->pindex, NULL, NULL)) { + KASSERT(shm->shm_pages >= 1, + ("shm %p pages %jd free 1", shm, + (uintmax_t)shm->shm_pages)); + shm->shm_pages -= 1; + } +} + +static struct pagerops shm_swap_pager_ops = { + .pgo_kvme_type = KVME_TYPE_SWAP, + .pgo_freespace = shm_pager_freespace, + .pgo_page_inserted = shm_page_inserted, + .pgo_page_removed = shm_page_removed, +}; +static int shmfd_pager_type = -1; + static int shm_seek(struct file *fp, off_t offset, int whence, struct thread *td) { @@ -560,7 +615,6 @@ shm_stat(struct file *fp, struct stat *sb, struct ucred *active_cred, bzero(sb, sizeof(*sb)); sb->st_blksize = PAGE_SIZE; sb->st_size = shmfd->shm_size; - sb->st_blocks = howmany(sb->st_size, sb->st_blksize); mtx_lock(&shm_timestamp_lock); sb->st_atim = shmfd->shm_atime; sb->st_ctim = shmfd->shm_ctime; @@ -573,8 +627,12 @@ shm_stat(struct file *fp, struct stat *sb, struct ucred *active_cred, sb->st_dev = shm_dev_ino; sb->st_ino = shmfd->shm_ino; sb->st_nlink = shmfd->shm_object->ref_count; - sb->st_blocks = shmfd->shm_object->size / - (pagesizes[shmfd->shm_lp_psind] >> PAGE_SHIFT); + if (shm_largepage(shmfd)) { + sb->st_blocks = shmfd->shm_object->size / + (pagesizes[shmfd->shm_lp_psind] >> PAGE_SHIFT); + } else { + sb->st_blocks = shmfd->shm_pages; + } return (0); } @@ -858,6 +916,7 @@ struct shmfd * shm_alloc(struct ucred *ucred, mode_t mode, bool largepage) { struct shmfd *shmfd; + vm_object_t obj; shmfd = malloc(sizeof(*shmfd), M_SHMFD, M_WAITOK | M_ZERO); shmfd->shm_size = 0; @@ -870,8 +929,12 @@ shm_alloc(struct ucred *ucred, mode_t mode, bool largepage) VM_PROT_DEFAULT, 0, ucred); shmfd->shm_lp_alloc_policy = SHM_LARGEPAGE_ALLOC_DEFAULT; } else { - shmfd->shm_object = vm_pager_allocate(OBJT_SWAP, NULL, + obj = vm_pager_allocate(shmfd_pager_type, NULL, shmfd->shm_size, VM_PROT_DEFAULT, 0, ucred); + VM_OBJECT_WLOCK(obj); + obj->un_pager.swp.swp_priv = shmfd; + VM_OBJECT_WUNLOCK(obj); + shmfd->shm_object = obj; } KASSERT(shmfd->shm_object != NULL, ("shm_create: vm_pager_allocate")); vfs_timestamp(&shmfd->shm_birthtime); @@ -900,6 +963,7 @@ shm_hold(struct shmfd *shmfd) void shm_drop(struct shmfd *shmfd) { + vm_object_t obj; if (refcount_release(&shmfd->shm_refs)) { #ifdef MAC @@ -907,7 +971,13 @@ shm_drop(struct shmfd *shmfd) #endif rangelock_destroy(&shmfd->shm_rl); mtx_destroy(&shmfd->shm_mtx); - vm_object_deallocate(shmfd->shm_object); + obj = shmfd->shm_object; + if (!shm_largepage(shmfd)) { + VM_OBJECT_WLOCK(obj); + obj->un_pager.swp.swp_priv = NULL; + VM_OBJECT_WUNLOCK(obj); + } + vm_object_deallocate(obj); free(shmfd, M_SHMFD); } } @@ -946,6 +1016,9 @@ shm_init(void *arg) new_unrhdr64(&shm_ino_unr, 1); shm_dev_ino = devfs_alloc_cdp_inode(); KASSERT(shm_dev_ino > 0, ("shm dev inode not initialized")); + shmfd_pager_type = vm_pager_alloc_dyn_type(&shm_swap_pager_ops, + OBJT_SWAP); + MPASS(shmfd_pager_type != -1); for (i = 1; i < MAXPAGESIZES; i++) { if (pagesizes[i] == 0) diff --git a/sys/sys/mman.h b/sys/sys/mman.h index 71172bfd49a3..0413249dee2c 100644 --- a/sys/sys/mman.h +++ b/sys/sys/mman.h @@ -268,6 +268,7 @@ struct file; struct shmfd { vm_ooffset_t shm_size; vm_object_t shm_object; + vm_pindex_t shm_pages; /* allocated pages */ int shm_refs; uid_t shm_uid; gid_t shm_gid;