From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-io0-f174.google.com (mail-io0-f174.google.com [209.85.223.174]) by kanga.kvack.org (Postfix) with ESMTP id 8FDB36B0038 for ; Tue, 24 Nov 2015 15:49:01 -0500 (EST) Received: by iouu10 with SMTP id u10so33772056iou.0 for ; Tue, 24 Nov 2015 12:49:01 -0800 (PST) Received: from mail-io0-x22a.google.com (mail-io0-x22a.google.com. [2607:f8b0:4001:c06::22a]) by mx.google.com with ESMTPS id ej8si319154igc.1.2015.11.24.12.49.00 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 24 Nov 2015 12:49:00 -0800 (PST) Received: by ioc74 with SMTP id 74so33256846ioc.2 for ; Tue, 24 Nov 2015 12:49:00 -0800 (PST) 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 Message-ID: Subject: Re: [PATCH v3 09/22] kthread: Allow to cancel kthread work From: Linus Torvalds Content-Type: text/plain; charset=UTF-8 Sender: owner-linux-mm@kvack.org List-ID: 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 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, send a message with 'unsubscribe linux-mm' in the body to majordomo@kvack.org. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: email@kvack.org