From nobody Sun May 12 01:13:49 2024 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 4VcPlL1CyYz5JhyS; Sun, 12 May 2024 01:13:50 +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 4VcPlK6dxdz45Sc; Sun, 12 May 2024 01:13:49 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1715476429; 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=Rd+H0KzxfvHm6PonVHKypEUxkKXZOXImkhsgquayvY8=; b=sdEUTOGP3+X1diYXabncBRo6tBimWvLmJsIaUyu0+BAaXzcW18zJ9zkOMibteDtZfW9qpg QOaJU+82k5keDv2XrslPoNUvtgHQ26vIUH6w6MosgMlgv/BZKHsVHc7hqzQ7bPxiEVLSbT iRsC38bF+L7549dPyBmiJlFBErtIBIp72mzGi1eZQ2H2uUV2RNnTQbu/yHVkRnADkOst9v 7dLjEf9eMOVbbEeXkAQxgpoGpfJ22lKfqZp62Ty9bm5qIDk7Yhfc1rWpukVbDQIkoj75FZ kcR7Tl0YUcx+GfssEVQKDAkYKdgOmL9yR9L8h5jIPmUTPWJ0maAnxF7zcp0qzg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1715476429; a=rsa-sha256; cv=none; b=QsxHl3ufieDUd4UFn5+yzggFqfrQnZFE4rnJL7iaiveoydqK/SMH2rSlCniJFZyxc2CJoF qOqBL4P5M8O4n+8smYA5dOV7KoyJMDxSxOTPxAYZVbGX6YRlGvP+tRpxVdbDGW5tLACMK0 46iH+Li6qrllO+1oSZo312YmmlXa2htuRdSxod4r5n/t+I3B1AFAQ1FVtZY48u8TRZWypp q1dvqeuLqRc3fJQFrV7kGzmh7Z2mKbgD0eI0R+gFMU+EWRjA2KaYVHz4ZjY6uS0gI7leW7 P7Q0OmYYwNKnaiiEbQTaZqYc5dWWpvjgjOHm0uNYC2he4fra2+ngUjA8rkrYvA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1715476429; 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=Rd+H0KzxfvHm6PonVHKypEUxkKXZOXImkhsgquayvY8=; b=jvcakGgLr+zv20ioynvClDIhvknXBXz3SgefLQxamOzqiy70upxQRvrb8NGFLOTAZNelld osV9IbfZQ0A+mE7efeSNKCMOgELA8RYll8HsAwmPsQfqxLtugmlRWSrVXlclMZmP7fQl2U GSqdcEc9zVnIMmcaYRJi8KOi2ZcAo0h7fb85Hz45T8NvaBId7s8O3mNXhvATZeBkq4eUYI fAJ4HzCuO9DA2cYGVuiQMv64JODzIsJ60WjyohWLyuI2NzVp/3ri4aOA1lTy3pY0DdhFy2 k5QDSKbA/3n3FpMdewr9kxki/pO0BksgpHKX10kOtkwBb28Dcm4jRhvFQKupMw== 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 4VcPlK6Fx5zk1j; Sun, 12 May 2024 01:13:49 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 44C1Dn7D019347; Sun, 12 May 2024 01:13:49 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 44C1DnjQ019344; Sun, 12 May 2024 01:13:49 GMT (envelope-from git) Date: Sun, 12 May 2024 01:13:49 GMT Message-Id: <202405120113.44C1DnjQ019344@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: e93404065177 - main - cdev_pager_allocate(): ensure that the cdev_pager_ops ctr is called only once 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: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-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: e93404065177d6c909cd64bf7d74fe0d8df35edf Auto-Submitted: auto-generated The branch main has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=e93404065177d6c909cd64bf7d74fe0d8df35edf commit e93404065177d6c909cd64bf7d74fe0d8df35edf Author: Konstantin Belousov AuthorDate: 2024-05-07 13:23:28 +0000 Commit: Konstantin Belousov CommitDate: 2024-05-12 01:13:00 +0000 cdev_pager_allocate(): ensure that the cdev_pager_ops ctr is called only once per allocated vm_object. Otherwise, since constructors are not idempotent, we e.g. leak device reference in case of non-managed pager. PR: 278826 Reported by: Austin Zhang Reviewed by: alc, markj Tested by: pho Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D45113 --- sys/vm/device_pager.c | 70 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 51 insertions(+), 19 deletions(-) diff --git a/sys/vm/device_pager.c b/sys/vm/device_pager.c index 0b1c5287b884..4f8651411851 100644 --- a/sys/vm/device_pager.c +++ b/sys/vm/device_pager.c @@ -115,8 +115,15 @@ cdev_pager_lookup(void *handle) { vm_object_t object; +again: mtx_lock(&dev_pager_mtx); object = vm_pager_object_lookup(&dev_pager_object_list, handle); + if (object != NULL && object->un_pager.devp.dev == NULL) { + msleep(&object->un_pager.devp.dev, &dev_pager_mtx, + PVM | PDROP, "cdplkp", 0); + vm_object_deallocate(object); + goto again; + } mtx_unlock(&dev_pager_mtx); return (object); } @@ -126,9 +133,8 @@ cdev_pager_allocate(void *handle, enum obj_type tp, const struct cdev_pager_ops *ops, vm_ooffset_t size, vm_prot_t prot, vm_ooffset_t foff, struct ucred *cred) { - vm_object_t object, object1; + vm_object_t object; vm_pindex_t pindex; - u_short color; if (tp != OBJT_DEVICE && tp != OBJT_MGTDEVICE) return (NULL); @@ -154,16 +160,16 @@ cdev_pager_allocate(void *handle, enum obj_type tp, pindex < OFF_TO_IDX(size)) return (NULL); - if (ops->cdev_pg_ctor(handle, size, prot, foff, cred, &color) != 0) - return (NULL); +again: mtx_lock(&dev_pager_mtx); /* * Look up pager, creating as necessary. */ - object1 = NULL; object = vm_pager_object_lookup(&dev_pager_object_list, handle); if (object == NULL) { + vm_object_t object1; + /* * Allocate object and associate it with the pager. Initialize * the object's pg_color based upon the physical address of the @@ -171,15 +177,19 @@ cdev_pager_allocate(void *handle, enum obj_type tp, */ mtx_unlock(&dev_pager_mtx); object1 = vm_object_allocate(tp, pindex); - object1->flags |= OBJ_COLORED; - object1->pg_color = color; - object1->handle = handle; - object1->un_pager.devp.ops = ops; - object1->un_pager.devp.dev = handle; - TAILQ_INIT(&object1->un_pager.devp.devp_pglist); mtx_lock(&dev_pager_mtx); object = vm_pager_object_lookup(&dev_pager_object_list, handle); if (object != NULL) { + object1->type = OBJT_DEAD; + vm_object_deallocate(object1); + object1 = NULL; + if (object->un_pager.devp.dev == NULL) { + msleep(&object->un_pager.devp.dev, + &dev_pager_mtx, PVM | PDROP, "cdplkp", 0); + vm_object_deallocate(object); + goto again; + } + /* * We raced with other thread while allocating object. */ @@ -191,29 +201,51 @@ cdev_pager_allocate(void *handle, enum obj_type tp, KASSERT(object->un_pager.devp.ops == ops, ("Inconsistent devops %p %p", object, ops)); } else { + u_short color; + object = object1; object1 = NULL; object->handle = handle; + object->un_pager.devp.ops = ops; + TAILQ_INIT(&object->un_pager.devp.devp_pglist); TAILQ_INSERT_TAIL(&dev_pager_object_list, object, pager_object_list); + mtx_unlock(&dev_pager_mtx); if (ops->cdev_pg_populate != NULL) vm_object_set_flag(object, OBJ_POPULATE); + if (ops->cdev_pg_ctor(handle, size, prot, foff, + cred, &color) != 0) { + mtx_lock(&dev_pager_mtx); + TAILQ_REMOVE(&dev_pager_object_list, object, + pager_object_list); + wakeup(&object->un_pager.devp.dev); + mtx_unlock(&dev_pager_mtx); + object->type = OBJT_DEAD; + vm_object_deallocate(object); + object = NULL; + mtx_lock(&dev_pager_mtx); + } else { + mtx_lock(&dev_pager_mtx); + object->flags |= OBJ_COLORED; + object->pg_color = color; + object->un_pager.devp.dev = handle; + wakeup(&object->un_pager.devp.dev); + } } + MPASS(object1 == NULL); } else { + if (object->un_pager.devp.dev == NULL) { + msleep(&object->un_pager.devp.dev, + &dev_pager_mtx, PVM | PDROP, "cdplkp", 0); + vm_object_deallocate(object); + goto again; + } if (pindex > object->size) object->size = pindex; KASSERT(object->type == tp, ("Inconsistent device pager type %p %d", object, tp)); } mtx_unlock(&dev_pager_mtx); - if (object1 != NULL) { - object1->handle = object1; - mtx_lock(&dev_pager_mtx); - TAILQ_INSERT_TAIL(&dev_pager_object_list, object1, - pager_object_list); - mtx_unlock(&dev_pager_mtx); - vm_object_deallocate(object1); - } return (object); }