From patchwork Fri Nov 22 15:39:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marco Elver X-Patchwork-Id: 13883300 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4686BE6916D for ; Fri, 22 Nov 2024 15:41:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 92B1E8D0008; Fri, 22 Nov 2024 10:41:02 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8D72B8D0007; Fri, 22 Nov 2024 10:41:02 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 777C28D0008; Fri, 22 Nov 2024 10:41:02 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 51F248D0007 for ; Fri, 22 Nov 2024 10:41:02 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id F2F02AF00B for ; Fri, 22 Nov 2024 15:41:01 +0000 (UTC) X-FDA: 82814141886.08.2CEFD44 Received: from mail-ej1-f74.google.com (mail-ej1-f74.google.com [209.85.218.74]) by imf17.hostedemail.com (Postfix) with ESMTP id A781F40023 for ; Fri, 22 Nov 2024 15:40:18 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=mh81zBeV; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf17.hostedemail.com: domain of 3CqZAZwUKCJk7EO7K9HH9E7.5HFEBGNQ-FFDO35D.HK9@flex--elver.bounces.google.com designates 209.85.218.74 as permitted sender) smtp.mailfrom=3CqZAZwUKCJk7EO7K9HH9E7.5HFEBGNQ-FFDO35D.HK9@flex--elver.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1732289923; a=rsa-sha256; cv=none; b=ffHZzUzNqgCmwQlur6At4fPou+5Pv/o3eUGd26E30Q2bP/TVHtGdghhjnn3WrZOV5bXovh Xmon15y1QGXszdjWmIAt+iaZH2DQqpbB9hoHo7vHMWb7eFeL1Udoua3OZKYelCD8OTLmds Tud7LzUJ7oZPFhSDKk912eNg7ZrHUy0= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=mh81zBeV; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf17.hostedemail.com: domain of 3CqZAZwUKCJk7EO7K9HH9E7.5HFEBGNQ-FFDO35D.HK9@flex--elver.bounces.google.com designates 209.85.218.74 as permitted sender) smtp.mailfrom=3CqZAZwUKCJk7EO7K9HH9E7.5HFEBGNQ-FFDO35D.HK9@flex--elver.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1732289923; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding:in-reply-to: references:dkim-signature; bh=xTxOguFgwy9tOkyUrOPXOUVNTctOk0wNzBQXya5aVos=; b=SjmjrflMkTcQfBvVf+GtGqgmbgEFaw+njFjTIkSwc4fTeDt1MsFFCeHOmr2PcjMS1OKN6O pgsMY2BnXn4kR/SQ2lo4qbGm8+W0eg7zIbFG/dqxdqs4mjWwesSB20OXLm6MYF71eHEfDC vcxCU+j7GmbjUTZtezlsAK6aGKiS3NY= Received: by mail-ej1-f74.google.com with SMTP id a640c23a62f3a-a99fff1ad9cso160300966b.2 for ; Fri, 22 Nov 2024 07:40:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732290058; x=1732894858; darn=kvack.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=xTxOguFgwy9tOkyUrOPXOUVNTctOk0wNzBQXya5aVos=; b=mh81zBeVzr6Gi4cDCIxCQ5yRx4S4ytWuG7wZGOAUHd5EWKavywEe/IW+4DjQqU7mAW B5+eR1OLRh+IE3r7utESYiQwADSRKa9TP60B4oS6kJ+ZrhPu5wwCXg+7pMTpv+XFrsZT BWb4NJIfptf0iqQ56LXgGmy8cRlZ82u1JjBeM9y+gTEFVo97GdEFbOEpUQ5Gc9tE9sCt UfF2YIwoeycaJj3AfdTPnGpbCeM8xZRtypk6F+GQqluko9cGJOfNkXH9xzvXuuLlxE0x r+/HgDp4WGN3rT0qwaZEFyOAhV4XN6b+mVNcnsOMAixBCGKPNsAZXwMR0/eM3XhxkPdU KQIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732290058; x=1732894858; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=xTxOguFgwy9tOkyUrOPXOUVNTctOk0wNzBQXya5aVos=; b=PRrrCkONNcy5UyHh9S54iATPB8k1abZCoY3WwKXwlbzZ0iXxTCvgM2y9+GZzZf91P5 DMyNgrqm3Ii++jUDAbnIMzXLOc/+ZGBdReG2x0jnqK+dKJPAnNJn+0rNqwRwwRhmTO69 rIciYPfNpnn++9MTHSlSqq4xz1JKEdH14ih8KEhixIeipGjfPnnanFAIX4fYPyDhSWAI AfDaZAGQPLp1kfCcLadHZQ1NZY4k9RsHbvOKR83BlkzYmqLtAUydQ0Z+p62c4mG01Gb6 FNVcORK/3GdycPns3zrrQs5KhuGcmAIETBqHUg+I5FaEqnKg1+IoJZ9sNKitqOQvnUNZ PF4Q== X-Forwarded-Encrypted: i=1; AJvYcCXYDjfb9oeVtP5syJutZe0LqeO7wKrYdKspS6Z5ayh5VwpR3HLF/TdpmBC3t4JSCVuTJZWigNujzg==@kvack.org X-Gm-Message-State: AOJu0YyNpKJvmGDg2rTldoE/UzFFDaOvEuCxyPLpz9DU4qofhRuTG6sy 8DJ0DtlPPRXs35qru+UicuCfCEhUhRJJmSupEvuLLJvP9suBRnpchs/EoQsNH6aoTOkk8HePwQ= = X-Google-Smtp-Source: AGHT+IHC6kfIvPj3vG1i+LAY/CGzJaSxv8wDpDJqxPFx2Qz/GZuoQ+cNZBfybl6y2Mvx/LGv/9WCbm1jIg== X-Received: from elver.muc.corp.google.com ([2a00:79e0:9c:201:e369:a6f7:a3ea:97bb]) (user=elver job=sendgmr) by 2002:a17:906:f194:b0:a9a:11e5:c8d3 with SMTP id a640c23a62f3a-aa509bc55eamr69466b.6.1732290058216; Fri, 22 Nov 2024 07:40:58 -0800 (PST) Date: Fri, 22 Nov 2024 16:39:47 +0100 Mime-Version: 1.0 X-Mailer: git-send-email 2.47.0.371.ga323438b13-goog Message-ID: <20241122154051.3914732-1-elver@google.com> Subject: [PATCH] stackdepot: fix stack_depot_save_flags() in NMI context From: Marco Elver To: elver@google.com, Andrew Morton Cc: Andrey Konovalov , Alexander Potapenko , Dmitry Vyukov , Vlastimil Babka , Oscar Salvador , linux-kernel@vger.kernel.org, linux-mm@kvack.org, kasan-dev@googlegroups.com, Sebastian Andrzej Siewior X-Rspamd-Queue-Id: A781F40023 X-Stat-Signature: 85u39zqcdpkf5xn3der686z6upiw734w X-Rspam-User: X-Rspamd-Server: rspam05 X-HE-Tag: 1732290018-174170 X-HE-Meta: U2FsdGVkX1+56cS0bVo1xGjcgrB94q0ensVpuI05rhIHZ6H6lKxK8KkKTx9PnrWF+sDj/EmfWYLvuYn00eyX2J6kHcOWCVFALslzNT7/XSKmhsfqk/AiBjGqw0tXtFM5N2sI+TJYzGvt9f+5pCRxNxO9ojTM86mFeZndLpYvTcJ+bsNAB6ig1g/iO4j7q/UbXqVtTNDJ0EI+3JiZw3mMRS8lDgcI0bQPz5hyp1mswhnp443ci1B5ImuLg52yROTvtLTqxx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gz8Qi/x1/+u1z5Qcey/9mnoWurvb3LJCw3r0xNWN/WTMXCqsjdRdlXfznANUeD1Q9ex/wYDjKFj3XDjTRuDgQHl4WsHipNAG/ounRdxFRpDvhSsGlapN5ytf1kXBo6qwKqokX0x3t2pFKWUaiXEilCCN1IbPQaSEu10HkVrMJRrQ/yMdN0sAT4WtJIX/NDmDMx2aAUt/I13SWoYp2m5GAHBRjooDJFK/KBFBUmQTN9iZsDVMRfIEXjuBEXikCeMDE X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Per documentation, stack_depot_save_flags() was meant to be usable from NMI context if STACK_DEPOT_FLAG_CAN_ALLOC is unset. However, it still would try to take the pool_lock in an attempt to save a stack trace in the current pool (if space is available). This could result in deadlock if an NMI is handled while pool_lock is already held. To avoid deadlock, only try to take the lock in NMI context and give up if unsuccessful. The documentation is fixed to clearly convey this. Link: https://lkml.kernel.org/r/Z0CcyfbPqmxJ9uJH@elver.google.com Fixes: 4434a56ec209 ("stackdepot: make fast paths lock-less again") Reported-by: Sebastian Andrzej Siewior Signed-off-by: Marco Elver Reviewed-by: Sebastian Andrzej Siewior --- include/linux/stackdepot.h | 6 +++--- lib/stackdepot.c | 10 +++++++++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/include/linux/stackdepot.h b/include/linux/stackdepot.h index e9ec32fb97d4..2cc21ffcdaf9 100644 --- a/include/linux/stackdepot.h +++ b/include/linux/stackdepot.h @@ -147,7 +147,7 @@ static inline int stack_depot_early_init(void) { return 0; } * If the provided stack trace comes from the interrupt context, only the part * up to the interrupt entry is saved. * - * Context: Any context, but setting STACK_DEPOT_FLAG_CAN_ALLOC is required if + * Context: Any context, but unsetting STACK_DEPOT_FLAG_CAN_ALLOC is required if * alloc_pages() cannot be used from the current context. Currently * this is the case for contexts where neither %GFP_ATOMIC nor * %GFP_NOWAIT can be used (NMI, raw_spin_lock). @@ -156,7 +156,7 @@ static inline int stack_depot_early_init(void) { return 0; } */ depot_stack_handle_t stack_depot_save_flags(unsigned long *entries, unsigned int nr_entries, - gfp_t gfp_flags, + gfp_t alloc_flags, depot_flags_t depot_flags); /** @@ -175,7 +175,7 @@ depot_stack_handle_t stack_depot_save_flags(unsigned long *entries, * Return: Handle of the stack trace stored in depot, 0 on failure */ depot_stack_handle_t stack_depot_save(unsigned long *entries, - unsigned int nr_entries, gfp_t gfp_flags); + unsigned int nr_entries, gfp_t alloc_flags); /** * __stack_depot_get_stack_record - Get a pointer to a stack_record struct diff --git a/lib/stackdepot.c b/lib/stackdepot.c index 5ed34cc963fc..245d5b416699 100644 --- a/lib/stackdepot.c +++ b/lib/stackdepot.c @@ -630,7 +630,15 @@ depot_stack_handle_t stack_depot_save_flags(unsigned long *entries, prealloc = page_address(page); } - raw_spin_lock_irqsave(&pool_lock, flags); + if (in_nmi()) { + /* We can never allocate in NMI context. */ + WARN_ON_ONCE(can_alloc); + /* Best effort; bail if we fail to take the lock. */ + if (!raw_spin_trylock_irqsave(&pool_lock, flags)) + goto exit; + } else { + raw_spin_lock_irqsave(&pool_lock, flags); + } printk_deferred_enter(); /* Try to find again, to avoid concurrently inserting duplicates. */