From owner-freebsd-hackers@FreeBSD.ORG Fri Mar 26 21:07:38 2010 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 08A371065670 for ; Fri, 26 Mar 2010 21:07:38 +0000 (UTC) (envelope-from pluknet@gmail.com) Received: from mail-bw0-f216.google.com (mail-bw0-f216.google.com [209.85.218.216]) by mx1.freebsd.org (Postfix) with ESMTP id 7FF318FC0A for ; Fri, 26 Mar 2010 21:07:37 +0000 (UTC) Received: by bwz8 with SMTP id 8so3349735bwz.3 for ; Fri, 26 Mar 2010 14:07:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:received:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=zcm8MOn9kpoxvzup07fqB/R7gJi1itYsySfFgvvaryQ=; b=tDYFElxOyWt8QzcH5+WJSoz6s5uXZzczWT6ZjmrfMQJJcG3H5slzINADWvzUFWP8R9 coDWR0g/mInJINfW08b6Jzetu+wj4phPYrqnmGZa/0OO7L/taBNHPnm+ElBbtdwo/7hg STmFZNFJ8xs6qETCIw6nWOs3FUh992ay33E60= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; b=Gqq3Prtr5jnlAxwfUsqDRAaeR94pr+zyoglrqfq7Igi5YomBuZUoWmOKUK6pcdEEC1 c+bPagcXSKtY0GqbssehSlbuNBKaXKpPb3AEWRegFD+sCSNiU3/IhkLugX66KWCm0a5s i+PlBaOenO/kDta4Y4DXZre8ZcTgYHtpLkYB8= MIME-Version: 1.0 Received: by 10.204.60.204 with HTTP; Fri, 26 Mar 2010 14:07:36 -0700 (PDT) In-Reply-To: <4BAD1498.5040402@tomjudge.com> References: <4BACF92E.60600@tomjudge.com> <20100326195659.GU2415@deviant.kiev.zoral.com.ua> <4BAD1498.5040402@tomjudge.com> Date: Sat, 27 Mar 2010 00:07:36 +0300 Received: by 10.204.81.164 with SMTP id x36mr1875735bkk.162.1269637656292; Fri, 26 Mar 2010 14:07:36 -0700 (PDT) Message-ID: From: pluknet To: Tom Judge Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: Kostik Belousov , freebsd-hackers@freebsd.org Subject: Re: Panic in vm_map_stack X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 26 Mar 2010 21:07:38 -0000 On 26 March 2010 23:10, Tom Judge wrote: > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > This is the function, I am guessing that I need to unlock the proc > before calling vmspace_free ? > > As far as I know you cannot lock a process around locking vmspace and/or vm_map (at least on 6.x). I used process reference counting for that purpose. Sort of the following mumble.. sx_slock(&allproc_lock); FOREACH_PROC_IN_SYSTEM(p) { struct vmspace *vm; PROC_LOCK(p); /* Keep this process around until we finish this request. *= / _PHOLD(p); PROC_UNLOCK(p); vm =3D vmspace_acquire_ref(p); if (vm =3D=3D NULL) { PRELE(p); continue; } if (!vm_map_trylock_read(&vm->vm_map)) { vmspace_free(vm); PRELE(p); continue; } vm_map_unlock_read(&vm->vm_map); vmspace_free(vm); } /* * Drop our hold on this process now * that the request has completed. */ PRELE(p); } sx_sunlock(&allproc_lock); > > > =A0673 /* Given credential, return memory usage in bytes. */ > =A0674 void > =A0675 prison_memory(struct prison *pr) > =A0676 { > =A0677 =A0 =A0 struct proc *p; > =A0678 =A0 =A0 struct thread *td; > =A0679 =A0 =A0 struct vmspace *vm; > =A0680 =A0 =A0 long mem_used =3D 0; > =A0681 =A0 =A0 long full_mem_used =3D 0; > =A0682 =A0 =A0 long proc_res =3D 0; > =A0683 > =A0684 =A0 =A0 /* > =A0685 =A0 =A0 =A0* TODO: this is a really bad way of doing the > =A0686 =A0 =A0 =A0* search, as we end up going across all processes > =A0687 =A0 =A0 =A0* for each jail. =A0It'd be more efficient to just do > =A0688 =A0 =A0 =A0* this once in a period and update the relevant jail. > =A0689 =A0 =A0 =A0* > =A0690 =A0 =A0 =A0*/ > =A0691 =A0 =A0 sx_slock(&allproc_lock); > =A0692 > =A0693 =A0 =A0 FOREACH_PROC_IN_SYSTEM(p) { > =A0694 =A0 =A0 int breakout; > =A0695 =A0 =A0 =A0 =A0 proc_res=3D0; > =A0696 =A0 =A0 vm =3D NULL; > =A0697 =A0 =A0 =A0 =A0 if (PROC_TRYLOCK(p) =3D=3D 0) > =A0698 =A0 =A0 =A0 =A0 continue; > =A0699 =A0 =A0 /* > =A0700 =A0 =A0 =A0* If this is a system or protected process, skip it. > =A0701 =A0 =A0 =A0*/ > =A0702 =A0 =A0 if ((p->p_flag & P_SYSTEM) || (p->p_pid =3D=3D 1) || > =A0703 =A0 =A0 =A0 =A0 (p->p_flag & P_PROTECTED) || > =A0704 =A0 =A0 =A0 =A0 (p->p_pid < 48)) { > =A0705 =A0 =A0 =A0 =A0 PROC_UNLOCK(p); > =A0706 =A0 =A0 =A0 =A0 continue; > =A0707 =A0 =A0 } > =A0708 =A0 =A0 /* > =A0709 =A0 =A0 =A0* If the process is in a non-running type state, > =A0710 =A0 =A0 =A0* don't touch it. =A0Check all the threads individually= . > =A0711 =A0 =A0 =A0*/ > =A0712 =A0 =A0 breakout =3D 0; > =A0713 =A0 =A0 FOREACH_THREAD_IN_PROC(p, td) { > =A0714 =A0 =A0 =A0 =A0 thread_lock(td); > =A0715 =A0 =A0 =A0 =A0 if (!TD_ON_RUNQ(td) && > =A0716 =A0 =A0 =A0 =A0 =A0 =A0 !TD_IS_RUNNING(td) && > =A0717 =A0 =A0 =A0 =A0 =A0 =A0 !TD_IS_SLEEPING(td)) { > =A0718 =A0 =A0 =A0 =A0 =A0 =A0 thread_unlock(td); > =A0719 =A0 =A0 =A0 =A0 =A0 =A0 breakout =3D 1; > =A0720 =A0 =A0 =A0 =A0 =A0 =A0 break; > =A0721 =A0 =A0 =A0 =A0 } > =A0722 =A0 =A0 =A0 =A0 thread_unlock(td); > =A0723 =A0 =A0 } > =A0724 =A0 =A0 if (breakout) { > =A0725 =A0 =A0 =A0 =A0 PROC_UNLOCK(p); > =A0726 =A0 =A0 =A0 =A0 continue; > =A0727 =A0 =A0 } > =A0728 > =A0729 =A0 =A0 =A0 =A0 if (p->p_state =3D=3D PRS_NEW || > =A0730 =A0 =A0 =A0 =A0 p->p_state =3D=3D PRS_ZOMBIE || > =A0731 =A0 =A0 =A0 =A0 =A0 =A0 !jailed(p->p_ucred) || > =A0732 =A0 =A0 =A0 =A0 =A0 =A0 (pr !=3D p->p_ucred->cr_prison) || > =A0733 =A0 =A0 =A0 =A0 =A0 =A0 !p->p_vmspace) { > =A0734 =A0 =A0 =A0 =A0 =A0 =A0 PROC_UNLOCK(p); > =A0735 =A0 =A0 =A0 =A0 =A0 =A0 continue; > =A0736 =A0 =A0 =A0 =A0 } > =A0737 =A0 =A0 /* > =A0738 =A0 =A0 =A0* get the process size > =A0739 =A0 =A0 =A0*/ > =A0740 =A0 =A0 vm =3D vmspace_acquire_ref(p); > =A0741 =A0 =A0 if (vm =3D=3D NULL) { > =A0742 =A0 =A0 =A0 =A0 PROC_UNLOCK(p); > =A0743 =A0 =A0 =A0 =A0 continue; > =A0744 =A0 =A0 } > =A0745 > =A0746 =A0 =A0 =A0 =A0 if (!vm_map_trylock_read(&vm->vm_map)) { > =A0747 =A0 =A0 =A0 =A0 vmspace_free(vm); > =A0748 =A0 =A0 =A0 =A0 =A0 =A0 PROC_UNLOCK(p); > =A0749 =A0 =A0 =A0 =A0 =A0 =A0 continue; > =A0750 =A0 =A0 =A0 =A0 } > =A0751 =A0 =A0 =A0 =A0 full_mem_used +=3D vmspace_swap_count(vm); > =A0752 =A0 =A0 =A0 =A0 vm_map_unlock_read(&vm->vm_map); > =A0753 =A0 =A0 =A0 =A0 proc_res =3D vmspace_resident_count(vm); > =A0754 =A0 =A0 =A0 =A0 full_mem_used +=3D proc_res; > =A0755 =A0 =A0 =A0 =A0 mem_used +=3D proc_res; > =A0756 =A0 =A0 =A0 =A0 vmspace_free(vm); > =A0757 =A0 =A0 =A0 =A0 PROC_UNLOCK(p); > =A0758 =A0 =A0 } > =A0759 =A0 =A0 sx_sunlock(&allproc_lock); > =A0760 > =A0761 =A0 =A0 mem_used *=3D PAGE_SIZE; > =A0762 =A0 =A0 full_mem_used *=3D PAGE_SIZE; > =A0763 =A0 =A0 /* Copy the current memory usage to the prison struct */ > =A0764 =A0 =A0 mtx_lock(&pr->pr_mtx); > =A0765 =A0 =A0 pr->pr_mem_usage =3D mem_used; > =A0766 =A0 =A0 pr->pr_full_mem_usage =3D full_mem_used; > =A0767 =A0 =A0 mtx_unlock(&pr->pr_mtx); > =A0768 } > =A0769 > > > > Tom > > --=20 wbr, pluknet