From owner-freebsd-current@FreeBSD.ORG Mon Feb 18 22:18:18 2013 Return-Path: Delivered-To: freebsd-current@freebsd.org Received: from mx1.freebsd.org (mx1.FreeBSD.org [8.8.178.115]) by hub.freebsd.org (Postfix) with ESMTP id 0AEC5E7B for ; Mon, 18 Feb 2013 22:18:18 +0000 (UTC) (envelope-from onwahe@gmail.com) Received: from mail-qe0-f51.google.com (mail-qe0-f51.google.com [209.85.128.51]) by mx1.freebsd.org (Postfix) with ESMTP id BD1D2A6D for ; Mon, 18 Feb 2013 22:18:17 +0000 (UTC) Received: by mail-qe0-f51.google.com with SMTP id 6so2718822qea.38 for ; Mon, 18 Feb 2013 14:18:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:cc:content-type; bh=xX7AsK3mUfI7CTwtmICG+mpF5L+e/M2qnzA20YVMM1o=; b=FRkalGzVuSICutRmp/DhWMSuNn8lDY9/ZW4LA4L7FpIejV+siwU0FtF6JQ5Do9KIwR jFxt8BQ0cgek/yJyNB5DTZAczP9Fa1bJvLfX/Uosk1bhc7GGFUqQmHg/7ZPAxLCZbtyq nXcM5UzSvmCO3EdTMa5UyHYE0qlaEAFdBD/TxH7pKTtUW2fx8EN7jSyUI8ruUMQZe8tE UvQoJzyNlY2CyD1V169HDiWPF/VHBq+XDXxA0vl4O8kTiTM1zgb1O1dkIZPAQ1b5IFNa /sHAbZJMJB+ZCkNf6ptauNRd+KgX8nsPTPpgmy6peopZuBblFZCP8WaWaJg4PBV20ZRQ iQOA== MIME-Version: 1.0 X-Received: by 10.49.105.73 with SMTP id gk9mr6078723qeb.40.1361225896853; Mon, 18 Feb 2013 14:18:16 -0800 (PST) Received: by 10.49.121.198 with HTTP; Mon, 18 Feb 2013 14:18:16 -0800 (PST) In-Reply-To: <20130218203630.GO2598@kib.kiev.ua> References: <20130218150809.GG2598@kib.kiev.ua> <20130218170957.GJ2598@kib.kiev.ua> <20130218203630.GO2598@kib.kiev.ua> Date: Mon, 18 Feb 2013 23:18:16 +0100 Message-ID: Subject: Re: [patch] i386 pmap sysmaps_pcpu[] atomic access From: Svatopluk Kraus To: Konstantin Belousov Content-Type: text/plain; charset=ISO-8859-1 Cc: freebsd-current@freebsd.org X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 18 Feb 2013 22:18:18 -0000 On Mon, Feb 18, 2013 at 9:36 PM, Konstantin Belousov wrote: > On Mon, Feb 18, 2013 at 09:27:40PM +0100, Svatopluk Kraus wrote: >> On Mon, Feb 18, 2013 at 6:09 PM, Konstantin Belousov >> wrote: >> > On Mon, Feb 18, 2013 at 06:06:42PM +0100, Svatopluk Kraus wrote: >> >> On Mon, Feb 18, 2013 at 4:08 PM, Konstantin Belousov >> >> wrote: >> >> > On Mon, Feb 18, 2013 at 01:44:35PM +0100, Svatopluk Kraus wrote: >> >> >> Hi, >> >> >> >> >> >> the access to sysmaps_pcpu[] should be atomic with respect to >> >> >> thread migration. Otherwise, a sysmaps for one CPU can be stolen by >> >> >> another CPU and the purpose of per CPU sysmaps is broken. A patch is >> >> >> enclosed. >> >> > And, what are the problem caused by the 'otherwise' ? >> >> > I do not see any. >> >> >> >> The 'otherwise' issue is the following: >> >> >> >> 1. A thread is running on CPU0. >> >> >> >> sysmaps = &sysmaps_pcpu[PCPU_GET(cpuid)]; >> >> >> >> 2. A sysmaps variable contains a pointer to 'CPU0' sysmaps. >> >> 3. Now, the thread migrates into CPU1. >> >> 4. However, the sysmaps variable still contains a pointers to 'CPU0' sysmaps. >> >> >> >> mtx_lock(&sysmaps->lock); >> >> >> >> 4. The thread running on CPU1 locked 'CPU0' sysmaps mutex, so the >> >> thread uselessly can block another thread running on CPU0. Maybe, it's >> >> not a problem. However, it definitely goes against the reason why the >> >> submaps (one for each CPU) exist. >> > So what ? >> >> It depends. You don't understand it or you think it's ok? Tell me. >> > Both. I do not understand your concern, and I think that the code is fine. Well, I'm taking a part on porting FreeBSD to ARM11mpcore. UP case was simple. SMP case is more complex and rather new for me. Recently, I was solving a problem with PCPU stuff. For example, PCPU_GET is implemented by one instruction on i386 arch. So, a need of atomicity with respect to interrupts can be overlooked. On load-store archs, the implementation which works in SMP case is not so simple. And what works in UP case (single PCPU), not works in SMP case. Believe me, mysterious and sporadic 'mutex not owned' assertions and others ones caused by curthreads mess, it takes a while ... After this, I took a look at how PCPU stuff is used in whole kernel and found out mentioned here i386 pmap issue. So, my concern is following: 1. to verify my newly gained ideas how per CPU data should be used, 2. to decide how to change my implementation of pmap on ARM11mpcore, as it's based on i386 pmap, 3. to make FreeBSD code better. In the meanwhile, it looks that using data dedicated to one CPU on another one is OK. However, I can't agree. At least, without comments, it is misleading for anyone new in FreeBSD and makes code misty. > Both threads in your description make useful progress, and computation > proceeds correctly. I thought, there is only one thread in my example. One thread running on CPU1, but holding sysmaps dedicated to CPU0 instead of holding sysmaps dedicated to CPU1. So, any thread running on CPU0 must wait because the thread running on CPU1 is a thief. Futhermore, the idea that a thread on CPU1 should hold data for CPU1 is not valid. So, either some comment is missing in the code that it's OK or the using of PCPU_GET(cpuid) is unneeded and some kind of free sysmaps list can be used and it will serve better. >> >> >> >> >> >> >> > Really, taking the mutex while bind to a CPU could be deadlock-prone >> >> > under some situations. >> >> > >> >> > This was discussed at least one more time. Might be, a comment saying that >> >> > there is no issue should be added. >> >> >> >> I missed the discussion. Can you point me to it, please? A deadlock is >> >> not problem here, however, I can be wrong, as I can't imagine now how >> >> a simple pinning could lead into a deadlock at all. >> > Because some other load on the bind cpu might prevent the thread from >> > being scheduled. >> >> I'm afraid I still have no idea. On single CPU, a binding has no >> meaning. Thus, if any deadlock exists then exists without binding too. >> Hmm, you are talking about a deadlock caused by heavy CPU load? Is it >> a deadlock at all? Anyhow, mutex is a lock with priority propagation, >> isn't it? >> > > When executing on single cpu, kernel sometimes make different decisions. > Yes, the deadlock can be more precisely described as livelock. > > It might not make any matter for exactly this case, but still is useful > to remember.