From nobody Fri Dec 09 12:17:42 2022 X-Original-To: dev-commits-src-main@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 4NT95L2vPdz4kPyL; Fri, 9 Dec 2022 12:17:42 +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 4NT95L2MB0z3rN3; Fri, 9 Dec 2022 12:17:42 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1670588262; 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=GCBojStFZPg3oDCo7hIPbRrWMwCxgmi1zMj+QoL7mEI=; b=r6QI/VPl2z+9FBBXuCxihWFkOg3ribkLofd4TqhSFpUWULYZMwcXAiW2X/oQPHLXVzfisS AncDRlqjiV+UVS4n09M/CknxLkPEKB6sWyi/kxvw/1Nq7miGsZDCeqY5vQmDc9QjYEOuWT qBjL7dhDS051nIlTDOL77EM3j/+GVVbexwzesyUCZPpINnBto2ldhANt1Ja79ovHzsxPxs +XZLlVOO2XfuAvF0CJ+6BSRP1guyiDa2Gg1ue/tEBLLMpm+gnGyYgdFZe31wb2+MPdSnpr n15mDt56uAKL46eptzFbRP91sSlDbbAwp80yEqa6Ro9zezsurDQPhdioyM5VCA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1670588262; 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=GCBojStFZPg3oDCo7hIPbRrWMwCxgmi1zMj+QoL7mEI=; b=HfHFfjm8jylRLhLQWqzEjis7YGPabDRTt8g/qSc/pya3NMK6YIpiRbDb63auty9N/eYP+H zg7Gzuh5CyU4scYGPAwtIyOdSrthy0qnMSNZPhmihMZ71827+wLg6x+cpn5Bju4/hdaAYN Ce6auf1YRxw+rv0PFv28/i1dEdSjs8q23wxbLtLGyiikob0jWSUcAW3FOch/RKk03c0QMT VxdeyFgftRVvpq1ndAg4BHfKYoheQiEO/CATbomtwYu+vXOn8qxol6Ib+gwKA+MvGCZ4BT CQNzyydIREIuEv8HM7XgVCMGCoLAXwgpDWDsJkBCTkwyXfO9d/HqttM3OpEuKA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1670588262; a=rsa-sha256; cv=none; b=tApw2kFo25KvplAYndeZzplMFRhSpDcQAiFHaZzBvR5bvtLUHp6Yf0eNo8LjXSnddGnSpv 1akNYkKc8YLB8SZOVVfbGI+r+62dY04EjCkYIuYS4cESManacYueK6TXZ+JMF/9Mu4Rw5U 9B9OUOpEpiXE3hIp85P5Wfwm0DcY+4pwtRK75L+/blUx7Rxtn0O9rE1Xul8iEkHj7qGmx9 YbulAIl6QDIcv/t79lcHZkcgIgyr889wp2cv/tahMcN37R++QcrQk+vSG+03thwZ7EQv/w KR720gZxkh65uz1URTx65t/2vqDO6MTqsKyCGDLVTFfxJX72qawY7f/UEXF4Bw== 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 4NT95L1RBFzbTL; Fri, 9 Dec 2022 12:17:42 +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 2B9CHg3Y086515; Fri, 9 Dec 2022 12:17:42 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 2B9CHgbB086514; Fri, 9 Dec 2022 12:17:42 GMT (envelope-from git) Date: Fri, 9 Dec 2022 12:17:42 GMT Message-Id: <202212091217.2B9CHgbB086514@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Konstantin Belousov Subject: git: 0919f29d9103 - main - shmfd: account for the actually allocated pages List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@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/main X-Git-Reftype: branch X-Git-Commit: 0919f29d91031cf42eca99266bffe7b0faef8f1b Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=0919f29d91031cf42eca99266bffe7b0faef8f1b commit 0919f29d91031cf42eca99266bffe7b0faef8f1b Author: Konstantin Belousov AuthorDate: 2022-11-23 21:26:38 +0000 Commit: Konstantin Belousov CommitDate: 2022-12-09 12:17:12 +0000 shmfd: account for the actually allocated pages Return the value as stat(2) st_blocks. Suggested and reviewed by: markj (previous version) Tested by: pho Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D37097 --- 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 923930a708a3..355e76cd41fa 100644 --- a/sys/kern/uipc_shm.c +++ b/sys/kern/uipc_shm.c @@ -347,6 +347,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) { @@ -563,7 +618,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; @@ -576,8 +630,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); } @@ -876,6 +934,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; @@ -888,8 +947,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); @@ -918,6 +981,7 @@ shm_hold(struct shmfd *shmfd) void shm_drop(struct shmfd *shmfd) { + vm_object_t obj; if (refcount_release(&shmfd->shm_refs)) { #ifdef MAC @@ -925,7 +989,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); } } @@ -964,6 +1034,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;