From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752288AbcGSUqC (ORCPT ); Tue, 19 Jul 2016 16:46:02 -0400 Received: from mail-pa0-f47.google.com ([209.85.220.47]:34859 "EHLO mail-pa0-f47.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751741AbcGSUqA (ORCPT ); Tue, 19 Jul 2016 16:46:00 -0400 Date: Tue, 19 Jul 2016 13:45:52 -0700 (PDT) From: David Rientjes X-X-Sender: rientjes@chino.kir.corp.google.com To: Johannes Weiner cc: Michal Hocko , linux-mm@kvack.org, Mikulas Patocka , Ondrej Kozina , Tetsuo Handa , Mel Gorman , Neil Brown , Andrew Morton , LKML , dm-devel@redhat.com, Michal Hocko Subject: Re: [RFC PATCH 1/2] mempool: do not consume memory reserves from the reclaim path In-Reply-To: <20160719135426.GA31229@cmpxchg.org> Message-ID: References: <1468831164-26621-1-git-send-email-mhocko@kernel.org> <1468831285-27242-1-git-send-email-mhocko@kernel.org> <20160719135426.GA31229@cmpxchg.org> User-Agent: Alpine 2.10 (DEB 1266 2009-07-14) MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, 19 Jul 2016, Johannes Weiner wrote: > Mempool guarantees forward progress by having all necessary memory > objects for the guaranteed operation in reserve. Think about it this > way: you should be able to delete the pool->alloc() call entirely and > still make reliable forward progress. It would kill concurrency and be > super slow, but how could it be affected by a system OOM situation? > > If our mempool_alloc() is waiting for an object that an OOM victim is > holding, where could that OOM victim get stuck before giving it back? > As I asked in the previous thread, surely you wouldn't do a mempool > allocation first and then rely on an unguarded page allocation to make > forward progress, right? It would defeat the purpose of using mempools > in the first place. And surely the OOM victim wouldn't be waiting for > a lock that somebody doing mempool_alloc() *against the same mempool* > is holding. That'd be an obvious ABBA deadlock. > > So maybe I'm just dense, but could somebody please outline the exact > deadlock diagram? Who is doing what, and how are they getting stuck? > > cpu0: cpu1: > mempool_alloc(pool0) > mempool_alloc(pool0) > wait for cpu1 > not allocating memory - would defeat mempool > not taking locks held by cpu0* - would ABBA > ??? > mempool_free(pool0) > > Thanks > > * or any other task that does mempool_alloc(pool0) before unlock > I'm approaching this from a perspective of any possible mempool usage, not with any single current user in mind. Any mempool_alloc() user that then takes a contended mutex can do this. An example: taskA taskB taskC ----- ----- ----- mempool_alloc(a) mutex_lock(b) mutex_lock(b) mempool_alloc(a) Imagine the mempool_alloc() done by taskA depleting all free elements so we rely on it to do mempool_free() before any other mempool allocator can be guaranteed. If taskC is oom killed, or has PF_MEMALLOC set, it cannot access memory reserves from the page allocator if __GFP_NOMEMALLOC is automatic in mempool_alloc(). This livelocks the page allocator for all processes. taskB in this case need only stall after taking mutex_lock() successfully; that could be because of the oom livelock, it is contended on another mutex held by an allocator, etc. Obviously taskB stalling while holding a mutex that is contended by a mempool user holding an element is not preferred, but it's possible. (A simplified version is also possible with 0-size mempools, which are also allowed.) My point is that I don't think we should be forcing any behavior wrt memory reserves as part of the mempool implementation. In the above, taskC mempool_alloc() would succeed and not livelock unless __GFP_NOMEMALLOC is forced. The mempool_alloc() user may construct their set of gfp flags as appropriate just like any other memory allocator in the kernel. The alternative would be to ensure no mempool users ever take a lock that another thread can hold while contending another mutex or allocating memory itself.