Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp504890pxu; Wed, 25 Nov 2020 08:28:42 -0800 (PST) X-Google-Smtp-Source: ABdhPJwAuSeWC1tzdpa2XftHJe4FUgSKvKxjjkMC9YIY2oR6/5pWtNoeZUrAmIp2fPRLn4PPIEQk X-Received: by 2002:a17:906:fc4:: with SMTP id c4mr3860425ejk.490.1606321722220; Wed, 25 Nov 2020 08:28:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606321722; cv=none; d=google.com; s=arc-20160816; b=bRVnxn8rohb6Y9IBHxwsf4YJ+3GVNbqbMPlIK/bB4BAyJk0zb7VIYAG3P/fRdlCH2l cIZcUTyu8kRoxd2M0URCBWj7N4cJppmRceXromSA6KAfXOT3zKCaMisc8QBU8uh7a4tv xKvgMergImWfIHplUy875NXvptVIudyZvc8CNmLeUtHhsevTpfpqkI6aV6r0yuatde/q gRJSUESzd//pwcTbJKj8tpJBE6pilmja82GCI0bRDQA/oem49C3lkpusDW5G55+F3u1i H85xaNVKVAybGxxVPL2yp/iBGBAf/mZVPteHX/TwVvWQd1J/Apvwge8VpYhx9bwTyAns TKqg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=UNSb6Wp1AZE4VV1tRwOeD55taeEGpBqVtqBoZlJ/7FM=; b=dpwtXd0KqaiXHOvgq1cy90zwbRjF8mWdj1+jhYivRbJ2VRKL1yHkrSEO0GenMDBnir RnzpUqn62bUAR2WKuE5F2bVbqa4E0eFxhA9OeC7qa0uEbVe8kihP2RqVRVVls8I7RoEd +wE8UMCGUMTIxXmKjA5+DQDZ/xLy7piCGeEup7KFX9eo6Dm0E3o5j8PwHt6wPcGXrM7j TqkaBKyehiISkQVqP1Fw+wbz4vRp54fZlSU7V1TEVDFXvZ0Cyybo/SZdXkUF49sRk6b5 R75OK0Gr7e//issfmQEeOlG9IOV33M8NsuRYwEBb7kbwZjGnFkTg2MbeNB6qCbm/isjk Q2kg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ffwll.ch header.s=google header.b=QMYNGEeA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a4si1509953ejf.460.2020.11.25.08.28.19; Wed, 25 Nov 2020 08:28:42 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@ffwll.ch header.s=google header.b=QMYNGEeA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731607AbgKYQZp (ORCPT + 99 others); Wed, 25 Nov 2020 11:25:45 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58086 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731547AbgKYQZn (ORCPT ); Wed, 25 Nov 2020 11:25:43 -0500 Received: from mail-wm1-x344.google.com (mail-wm1-x344.google.com [IPv6:2a00:1450:4864:20::344]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A9D9BC061A51 for ; Wed, 25 Nov 2020 08:25:42 -0800 (PST) Received: by mail-wm1-x344.google.com with SMTP id h21so2701409wmb.2 for ; Wed, 25 Nov 2020 08:25:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ffwll.ch; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=UNSb6Wp1AZE4VV1tRwOeD55taeEGpBqVtqBoZlJ/7FM=; b=QMYNGEeA4YfhfQORAYeqBZnZcsSUcenw0mAE059wCrKDDwvxV6pkAgR4/U23dLWec0 RevBzZUbIN+mA+zBM07VEiG7Y25QELLdsH60N6ottEwiWDuwJQpLTRsg7/QipBmILxir xUhl44+qhKqBTkmVewpxVXpAPVi4yZQtwpPgw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=UNSb6Wp1AZE4VV1tRwOeD55taeEGpBqVtqBoZlJ/7FM=; b=APJTtgKbYju7huHEfDpiyDCNZTKiVVIJ0+77qXp5L8erasZxZfOK8i/KSoR4f38amf L4Ydb9s/qCkz3G5A98lvG27Mh2tNzltsyA0fMfpHCPOASI+p2brNIjv+RVqHiCF2Xz8a 99ryPlIVN20ok7/nbVxG5GSSTYC6HyNIMWbs13mI8K/nUmPMOXWCLUIkmh6i1KljDTva QOD87uyL7XCsWUucVVEOAXngPW1pPrCMhbePE7Ljqe9RpWwAynfLYO6pCUQFpAgf4Pc6 nm08SF46FqgI08TezN5b7Jj+UbxMPiiVC9hdVID2OxIwwKaoF7vWCVWwcSUpK7sPYYMH Yk+g== X-Gm-Message-State: AOAM532B/qza0ZAIHcOlKc3DCuAhbBDyrdYTt8FQy4lXpseqw/XcVtIh z682PsopxCSptFPGgw8bT15MkA== X-Received: by 2002:a1c:6209:: with SMTP id w9mr4886901wmb.120.1606321541410; Wed, 25 Nov 2020 08:25:41 -0800 (PST) Received: from phenom.ffwll.local ([2a02:168:57f4:0:efd0:b9e5:5ae6:c2fa]) by smtp.gmail.com with ESMTPSA id a21sm4855187wmb.38.2020.11.25.08.25.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Nov 2020 08:25:40 -0800 (PST) From: Daniel Vetter To: DRI Development Cc: Intel Graphics Development , linux-mm@kvack.org, linux-xfs@vger.kernel.org, linux-fsdevel@vger.kernel.org, LKML , Daniel Vetter , Vlastimil Babka , "Paul E . McKenney" , Jason Gunthorpe , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , Peter Zijlstra , Ingo Molnar , Mathieu Desnoyers , Sebastian Andrzej Siewior , Michel Lespinasse , Waiman Long , Thomas Gleixner , Randy Dunlap , Dave Chinner , Qian Cai , "Matthew Wilcox (Oracle)" , Daniel Vetter Subject: [PATCH v4 2/3] mm: Extract might_alloc() debug check Date: Wed, 25 Nov 2020 17:25:30 +0100 Message-Id: <20201125162532.1299794-3-daniel.vetter@ffwll.ch> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201125162532.1299794-1-daniel.vetter@ffwll.ch> References: <20201125162532.1299794-1-daniel.vetter@ffwll.ch> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Extracted from slab.h, which seems to have the most complete version including the correct might_sleep() check. Roll it out to slob.c. Motivated by a discussion with Paul about possibly changing call_rcu behaviour to allocate memory, but only roughly every 500th call. There are a lot fewer places in the kernel that care about whether allocating memory is allowed or not (due to deadlocks with reclaim code) than places that care whether sleeping is allowed. But debugging these also tends to be a lot harder, so nice descriptive checks could come in handy. I might have some use eventually for annotations in drivers/gpu. Note that unlike fs_reclaim_acquire/release gfpflags_allow_blocking does not consult the PF_MEMALLOC flags. But there is no flag equivalent for GFP_NOWAIT, hence this check can't go wrong due to memalloc_no*_save/restore contexts. Willy is working on a patch series which might change this: https://lore.kernel.org/linux-mm/20200625113122.7540-7-willy@infradead.org/ I think best would be if that updates gfpflags_allow_blocking(), since there's a ton of callers all over the place for that already. v2: Fix typos in kerneldoc (Randy) Acked-by: Vlastimil Babka Acked-by: Paul E. McKenney Reviewed-by: Jason Gunthorpe Cc. Randy Dunlap Cc: Paul E. McKenney Cc: Christoph Lameter Cc: Pekka Enberg Cc: David Rientjes Cc: Joonsoo Kim Cc: Andrew Morton Cc: Peter Zijlstra Cc: Ingo Molnar Cc: Vlastimil Babka Cc: Mathieu Desnoyers Cc: Sebastian Andrzej Siewior Cc: Michel Lespinasse Cc: Daniel Vetter Cc: Waiman Long Cc: Thomas Gleixner Cc: Randy Dunlap Cc: linux-mm@kvack.org Cc: linux-fsdevel@vger.kernel.org Cc: Dave Chinner Cc: Qian Cai Cc: linux-xfs@vger.kernel.org Cc: "Matthew Wilcox (Oracle)" Signed-off-by: Daniel Vetter --- include/linux/sched/mm.h | 16 ++++++++++++++++ mm/slab.h | 5 +---- mm/slob.c | 6 ++---- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/include/linux/sched/mm.h b/include/linux/sched/mm.h index d5ece7a9a403..a11a61b5226f 100644 --- a/include/linux/sched/mm.h +++ b/include/linux/sched/mm.h @@ -180,6 +180,22 @@ static inline void fs_reclaim_acquire(gfp_t gfp_mask) { } static inline void fs_reclaim_release(gfp_t gfp_mask) { } #endif +/** + * might_alloc - Mark possible allocation sites + * @gfp_mask: gfp_t flags that would be used to allocate + * + * Similar to might_sleep() and other annotations, this can be used in functions + * that might allocate, but often don't. Compiles to nothing without + * CONFIG_LOCKDEP. Includes a conditional might_sleep() if @gfp allows blocking. + */ +static inline void might_alloc(gfp_t gfp_mask) +{ + fs_reclaim_acquire(gfp_mask); + fs_reclaim_release(gfp_mask); + + might_sleep_if(gfpflags_allow_blocking(gfp_mask)); +} + /** * memalloc_noio_save - Marks implicit GFP_NOIO allocation scope. * diff --git a/mm/slab.h b/mm/slab.h index 6d7c6a5056ba..37b981247e5d 100644 --- a/mm/slab.h +++ b/mm/slab.h @@ -500,10 +500,7 @@ static inline struct kmem_cache *slab_pre_alloc_hook(struct kmem_cache *s, { flags &= gfp_allowed_mask; - fs_reclaim_acquire(flags); - fs_reclaim_release(flags); - - might_sleep_if(gfpflags_allow_blocking(flags)); + might_alloc(flags); if (should_failslab(s, flags)) return NULL; diff --git a/mm/slob.c b/mm/slob.c index 7cc9805c8091..8d4bfa46247f 100644 --- a/mm/slob.c +++ b/mm/slob.c @@ -474,8 +474,7 @@ __do_kmalloc_node(size_t size, gfp_t gfp, int node, unsigned long caller) gfp &= gfp_allowed_mask; - fs_reclaim_acquire(gfp); - fs_reclaim_release(gfp); + might_alloc(gfp); if (size < PAGE_SIZE - minalign) { int align = minalign; @@ -597,8 +596,7 @@ static void *slob_alloc_node(struct kmem_cache *c, gfp_t flags, int node) flags &= gfp_allowed_mask; - fs_reclaim_acquire(flags); - fs_reclaim_release(flags); + might_alloc(flags); if (c->size < PAGE_SIZE) { b = slob_alloc(c->size, flags, c->align, node, 0); -- 2.29.2