From owner-freebsd-amd64@FreeBSD.ORG Sun Jul 7 17:03:50 2013 Return-Path: Delivered-To: freebsd-amd64@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) by hub.freebsd.org (Postfix) with ESMTP id AEE4BAA1; Sun, 7 Jul 2013 17:03:50 +0000 (UTC) (envelope-from davide.italiano@gmail.com) Received: from mail-ve0-x235.google.com (mail-ve0-x235.google.com [IPv6:2607:f8b0:400c:c01::235]) by mx1.freebsd.org (Postfix) with ESMTP id 5002E1C24; Sun, 7 Jul 2013 17:03:50 +0000 (UTC) Received: by mail-ve0-f181.google.com with SMTP id db10so2869779veb.40 for ; Sun, 07 Jul 2013 10:03:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type; bh=lUiWnj24mPctHnyA2HS04n0pXIdnKdzYztlAwIb21mA=; b=XfiDy9DYC8yOpBOlDqf8YWZxFmuyYuNA9odyM8UIkgnExkyEf5+LAv4Fd+yLPUqnif sHRxYXzTS8rpnjBlpIQuSJQs3/nRCkPLA5VwIRuCNlwowJTsujHSrXEWhb5B94moLGdA MHegK6qFhhWvlKMAkqc3sJZ34fOD6EF1bsSkx2svn/JHczxcVc/gHLxyHGIJ+9GdVvvx pVvbHKIr/6mtB/qgbuUuAT/18t12QvOMFo5sKSZlQ2GnPQRy/IMUgNmSxmvL1nx3GZtj NWvxfc78PWgI4+DsW1j/XbivaFO9EuCt96dkkgHwmL+i/4u89hav/Mo8q5rLOuutZVcv ShRA== MIME-Version: 1.0 X-Received: by 10.58.197.5 with SMTP id iq5mr12233635vec.30.1373216629907; Sun, 07 Jul 2013 10:03:49 -0700 (PDT) Sender: davide.italiano@gmail.com Received: by 10.220.206.202 with HTTP; Sun, 7 Jul 2013 10:03:49 -0700 (PDT) In-Reply-To: <51D99BB7.1020101@freebsd.org> References: <51D99BB7.1020101@freebsd.org> Date: Sun, 7 Jul 2013 10:03:49 -0700 X-Google-Sender-Auth: q2jx5irjvUuVaWQxw88FWamPo84 Message-ID: Subject: Re: Accessing struct pthread from kernel From: Davide Italiano To: Julian Elischer Content-Type: text/plain; charset=ISO-8859-1 X-Mailman-Approved-At: Sun, 07 Jul 2013 23:19:35 +0000 Cc: freebsd-amd64@freebsd.org, freebsd-threads@freebsd.org X-BeenThere: freebsd-amd64@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Porting FreeBSD to the AMD64 platform List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 07 Jul 2013 17:03:50 -0000 On Sun, Jul 7, 2013 at 9:47 AM, Julian Elischer wrote: > On 7/6/13 7:22 AM, Davide Italiano wrote: >> >> Hi, >> as a preliminary step in the implementation of adaptive spinning for >> umtx, I'm switching the pthread/umtx code so that a thread that >> acquires a pthread_mutex writes the address of struct pthread in the >> owner field of the lock instead of the thread id (tid). This is >> because having struct pthread pointer allows easily to access >> informations of the thread, and makes easy to get the state of the >> thread exported from the kernel (once this will be implemented). >> >> For what concerns the libthr side, the internal function >> _get_curthread() goes into the TLS to obtain the struct field of >> curthread, so I'm done. >> OTOH, I'm quite unsure instead about how to get address of struct >> pthread for curthread from the kernel side (for example, in >> do_lock_umutex() sys/kern/kern_umtx.c). >> >> I guess I need to write some MD code because the TLS is different on >> the various architecture supported in FreeBSD, and as a first step I >> focused on amd64. >> It looks like from the SDM that %%fs register points to the base of >> the TLS, so I think that accessing using curthread->td_pcb->pcb_fsbase >> (and then adding the proper offset to acces the right field) is a >> viable solution to do this. Am I correct? >> In particular what worries me is if the read of 'struct pthread' for >> curthread from the TLS register is atomic with respect to preemptions. >> >> Alternatively, is there an (easier way) to accomplish this task? > > there is a struct pthread for each tid (and visa versa) > you could notify the kernel of it when you make a new thread. > (new arg/call) > Yes, I previously tried implementing something like what you propose, exploiting the fact that thr_create() pass the pointer to struct pthread to the kernel as argument. The problem I see here is that the main thread is not created by thr_create() but by fork(). So, another way to pass the struct to the kernel is needed. One might think about adding another system call for the purpose, but I guess this is overkill so if you have other suggestions I'll be happpy to hear. > for what it's worth the M:N threading system used to do something like this, > with a user callback straucture supplied to the kernel.. > > >> >> Thanks, >> > Thanks for your answer, -- Davide "There are no solved problems; there are only problems that are more or less solved" -- Henri Poincare