From owner-svn-src-head@freebsd.org Tue Oct 22 15:59:16 2019 Return-Path: Delivered-To: svn-src-head@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id B55EF15E48A; Tue, 22 Oct 2019 15:59:16 +0000 (UTC) (envelope-from kib@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) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 46yJ904KlKz3xXW; Tue, 22 Oct 2019 15:59:16 +0000 (UTC) (envelope-from kib@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 780F2EAA9; Tue, 22 Oct 2019 15:59:16 +0000 (UTC) (envelope-from kib@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id x9MFxGiP047534; Tue, 22 Oct 2019 15:59:16 GMT (envelope-from kib@FreeBSD.org) Received: (from kib@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id x9MFxG2R047533; Tue, 22 Oct 2019 15:59:16 GMT (envelope-from kib@FreeBSD.org) Message-Id: <201910221559.x9MFxG2R047533@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: kib set sender to kib@FreeBSD.org using -f From: Konstantin Belousov Date: Tue, 22 Oct 2019 15:59:16 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r353888 - head/sys/vm X-SVN-Group: head X-SVN-Commit-Author: kib X-SVN-Commit-Paths: head/sys/vm X-SVN-Commit-Revision: 353888 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 22 Oct 2019 15:59:16 -0000 Author: kib Date: Tue Oct 22 15:59:16 2019 New Revision: 353888 URL: https://svnweb.freebsd.org/changeset/base/353888 Log: vm_fault(): extract code to lock the vnode into a helper vn_fault_lock_vnode(). Tested by: pho Reviewed by: markj Sponsored by: The FreeBSD Foundation MFC after: 2 weeks Differential revision: https://reviews.freebsd.org/D21883 Modified: head/sys/vm/vm_fault.c Modified: head/sys/vm/vm_fault.c ============================================================================== --- head/sys/vm/vm_fault.c Tue Oct 22 14:20:35 2019 (r353887) +++ head/sys/vm/vm_fault.c Tue Oct 22 15:59:16 2019 (r353888) @@ -628,19 +628,62 @@ vm_fault_trap(vm_map_t map, vm_offset_t vaddr, vm_prot return (result); } +static int +vm_fault_lock_vnode(struct faultstate *fs) +{ + struct vnode *vp; + int error, locked; + + if (fs->object->type != OBJT_VNODE) + return (KERN_SUCCESS); + vp = fs->object->handle; + if (vp == fs->vp) + return (KERN_SUCCESS); + + /* + * Perform an unlock in case the desired vnode changed while + * the map was unlocked during a retry. + */ + unlock_vp(fs); + + locked = VOP_ISLOCKED(vp); + if (locked != LK_EXCLUSIVE) + locked = LK_SHARED; + + /* + * We must not sleep acquiring the vnode lock while we have + * the page exclusive busied or the object's + * paging-in-progress count incremented. Otherwise, we could + * deadlock. + */ + error = vget(vp, locked | LK_CANRECURSE | LK_NOWAIT, curthread); + if (error == 0) { + fs->vp = vp; + return (KERN_SUCCESS); + } + + vhold(vp); + release_page(fs); + unlock_and_deallocate(fs); + error = vget(vp, locked | LK_RETRY | LK_CANRECURSE, curthread); + vdrop(vp); + fs->vp = vp; + KASSERT(error == 0, ("vm_fault: vget failed %d", error)); + return (KERN_RESOURCE_SHORTAGE); +} + int vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type, int fault_flags, vm_page_t *m_hold) { struct faultstate fs; - struct vnode *vp; struct domainset *dset; vm_object_t next_object, retry_object; vm_offset_t e_end, e_start; vm_pindex_t retry_pindex; vm_prot_t prot, retry_prot; - int ahead, alloc_req, behind, cluster_offset, error, era, faultcount; - int locked, nera, oom, result, rv; + int ahead, alloc_req, behind, cluster_offset, era, faultcount; + int nera, oom, result, rv; u_char behavior; boolean_t wired; /* Passed by reference. */ bool dead, hardfault, is_first_object_locked; @@ -1001,41 +1044,11 @@ readrest: */ unlock_map(&fs); - if (fs.object->type == OBJT_VNODE && - (vp = fs.object->handle) != fs.vp) { - /* - * Perform an unlock in case the desired vnode - * changed while the map was unlocked during a - * retry. - */ - unlock_vp(&fs); - - locked = VOP_ISLOCKED(vp); - if (locked != LK_EXCLUSIVE) - locked = LK_SHARED; - - /* - * We must not sleep acquiring the vnode lock - * while we have the page exclusive busied or - * the object's paging-in-progress count - * incremented. Otherwise, we could deadlock. - */ - error = vget(vp, locked | LK_CANRECURSE | - LK_NOWAIT, curthread); - if (error != 0) { - vhold(vp); - release_page(&fs); - unlock_and_deallocate(&fs); - error = vget(vp, locked | LK_RETRY | - LK_CANRECURSE, curthread); - vdrop(vp); - fs.vp = vp; - KASSERT(error == 0, - ("vm_fault: vget failed")); - goto RetryFault; - } - fs.vp = vp; - } + rv = vm_fault_lock_vnode(&fs); + MPASS(rv == KERN_SUCCESS || + rv == KERN_RESOURCE_SHORTAGE); + if (rv == KERN_RESOURCE_SHORTAGE) + goto RetryFault; KASSERT(fs.vp == NULL || !fs.map->system_map, ("vm_fault: vnode-backed object mapped by system map"));