Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754903AbbKXUtE (ORCPT ); Tue, 24 Nov 2015 15:49:04 -0500 Received: from mail-io0-f181.google.com ([209.85.223.181]:35975 "EHLO mail-io0-f181.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753861AbbKXUtB (ORCPT ); Tue, 24 Nov 2015 15:49:01 -0500 MIME-Version: 1.0 In-Reply-To: <20151124202855.GV17033@mtj.duckdns.org> References: <1447853127-3461-1-git-send-email-pmladek@suse.com> <1447853127-3461-10-git-send-email-pmladek@suse.com> <20151123225823.GI19072@mtj.duckdns.org> <20151124202855.GV17033@mtj.duckdns.org> Date: Tue, 24 Nov 2015 12:49:00 -0800 X-Google-Sender-Auth: bjVqr971LUEeSOBfPh-OSiRCA8E Message-ID: Subject: Re: [PATCH v3 09/22] kthread: Allow to cancel kthread work From: Linus Torvalds To: Tejun Heo Cc: Petr Mladek , Andrew Morton , Oleg Nesterov , Ingo Molnar , Peter Zijlstra , Steven Rostedt , "Paul E. McKenney" , Josh Triplett , Thomas Gleixner , Jiri Kosina , Borislav Petkov , Michal Hocko , linux-mm , Vlastimil Babka , Linux API , Linux Kernel Mailing List Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 1949 Lines: 44 On Tue, Nov 24, 2015 at 12:28 PM, Tejun Heo wrote: >> >> In general, it's very dangerous to try to cook up your own locking >> rules. People *always* get it wrong. > > It's either trylock on timer side or timer active spinning trick on > canceling side, so this seems the lesser of the two evils. I'm not saying the approach is wrong. I'm saying that people need to realize that locking is harder than they think, and not cook up their own lock primitives using things like trylock without really thinking about it a *lot*. Basically, "trylock()" on its own should never be used in a loop. The main use for trylock should be one of: - thing that you can just not do at all if you can't get the lock - avoiding ABBA deadlocks: if you have a A->B locking order, but you already hold B, instead of "drop B, then take A and B in the right order", you may decide to first "trylock(A)" - and if that fails you then fall back on the "drop and relock in the right order". but if what you want to create is a "get lock using trylock", you need to be very aware of the cache coherency traffic issue at least. It is possible that we should think about trying to introduce a new primitive for that "loop_try_lock()" thing. But it's probably not common enough to be worth it - we've had this issue before, but I think it's a "once every couple of years" kind of thing rather than anything that we need to worry about. The "locking is hard" issue is very real, though. We've traditionally had a *lot* of code that tried to do its own locking, and not getting the memory ordering right etc. Things that happen to work on x86 but don't on other architectures etc. Linus -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/