From patchwork Tue Sep 7 14:13:01 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marco Elver X-Patchwork-Id: 12478815 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-21.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT, USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 13390C433F5 for ; Tue, 7 Sep 2021 14:14:04 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 98DC461101 for ; Tue, 7 Sep 2021 14:14:03 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 98DC461101 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id AE9DC900002; Tue, 7 Sep 2021 10:14:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A9A2F6B0072; Tue, 7 Sep 2021 10:14:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9B184900002; Tue, 7 Sep 2021 10:14:02 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0247.hostedemail.com [216.40.44.247]) by kanga.kvack.org (Postfix) with ESMTP id 891FF6B0071 for ; Tue, 7 Sep 2021 10:14:02 -0400 (EDT) Received: from smtpin30.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 40F002D00B for ; Tue, 7 Sep 2021 14:14:02 +0000 (UTC) X-FDA: 78560971524.30.1EF7BAF Received: from mail-qt1-f201.google.com (mail-qt1-f201.google.com [209.85.160.201]) by imf07.hostedemail.com (Postfix) with ESMTP id EB91310000BA for ; Tue, 7 Sep 2021 14:14:01 +0000 (UTC) Received: by mail-qt1-f201.google.com with SMTP id p21-20020ac846150000b02902982d999bfbso12839618qtn.7 for ; Tue, 07 Sep 2021 07:14:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:message-id:mime-version:subject:from:to:cc; bh=G9h0FcdPN7QTl0SnF2a3SazYP80XeLyqs+enOhlxDM0=; b=oOD572sLnXhVe7QgHu+KQer3JbHzc+LF3jzbIc6wUu3Sx3mScoDJUYuplStwzHfZkh 6XzOfJA0dLRGgZtzm1dUj72473EonzHRboLP08B3iJdeRMwVP3k8Wj5ax5koBQakFdyX 7pBm3mnVhEP6JdcIs3ypz72iIJD9gHNGwFPo7VPUoKBsIwM5hjKNQ57Oo1OpLFj5o+iV nv3Fc8dtJOw+UtyJiDmvrbcXstZGyJAMfFnXg+ZFUjplM7mdnu7kNoByGu4cY/aLTda1 ajIhMfTn+7bSnZ+h6twkxVagx2H4M2bq1i3qVjrpOOLdAbJ1/0kaUKs+xkbfb/XDp3VS Vt2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=G9h0FcdPN7QTl0SnF2a3SazYP80XeLyqs+enOhlxDM0=; b=VwDKm2iWr48dNjGuE3Ht4R7Rf5tqWL0CnOrkgs0/taS1Hb+e7P3S9CN7RdrTlHESjA tD5pkrspKIUrRRpt3jp6gbtZithNUPVEawYmvQcgXU8Ie4IDyYgoxow980t0URJTZ2+K QeDHgPhFUoZpxV3hP6fsMxOi4fjD1QIMmSpO/JBMwXMsbNidsPcoKCTgLHB+cgBEp09j mLMpEn9p4PIQCLBoQSxd0UaC1Pc5MwcV5In9nkeb8q5E5gEqP/S7eVTYVlROgK8kU7Gu Hh95aIITsS3uMt2lLcl183QDOrD8lT/FLhZF9QWB8asejpqU72lPwwMCpvw8aHUIvy2X 8cyw== X-Gm-Message-State: AOAM530sbA6ADRkupTxTDNiy9qeLde1iCSekclCw8MEInEZ3piRJuKG1 hSHpW6ACfiStrBdc/RXKyOA3WH40bw== X-Google-Smtp-Source: ABdhPJxc7oYPLHdGXrX4znzakcQKYmQM5QuhChGNkqrwNZoyRG5J0ql1yMnSwvUZSSFWUDlULeUgjiJRPg== X-Received: from elver.muc.corp.google.com ([2a00:79e0:15:13:6800:c1ea:4271:5898]) (user=elver job=sendgmr) by 2002:ad4:4e86:: with SMTP id dy6mr17251813qvb.30.1631024041249; Tue, 07 Sep 2021 07:14:01 -0700 (PDT) Date: Tue, 7 Sep 2021 16:13:01 +0200 Message-Id: <20210907141307.1437816-1-elver@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.33.0.153.gba50c8fa24-goog Subject: [PATCH 0/6] stackdepot, kasan, workqueue: Avoid expanding stackdepot slabs when holding raw_spin_lock From: Marco Elver To: elver@google.com, Andrew Morton Cc: Shuah Khan , Tejun Heo , Lai Jiangshan , Andrey Konovalov , Walter Wu , Andrey Ryabinin , Alexander Potapenko , Dmitry Vyukov , Vijayanand Jitta , Vinayak Menon , "Gustavo A. R. Silva" , kasan-dev@googlegroups.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Aleksandr Nogikh , Taras Madan Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=oOD572sL; spf=pass (imf07.hostedemail.com: domain of 3qXM3YQUKCH4gnxgtiqqing.eqonkpwz-oomxcem.qti@flex--elver.bounces.google.com designates 209.85.160.201 as permitted sender) smtp.mailfrom=3qXM3YQUKCH4gnxgtiqqing.eqonkpwz-oomxcem.qti@flex--elver.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: EB91310000BA X-Stat-Signature: t8aiobea87fjxe7dxi93zbytr5168wfi X-HE-Tag: 1631024041-857612 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: Shuah Khan reported [1]: | When CONFIG_PROVE_RAW_LOCK_NESTING=y and CONFIG_KASAN are enabled, | kasan_record_aux_stack() runs into "BUG: Invalid wait context" when | it tries to allocate memory attempting to acquire spinlock in page | allocation code while holding workqueue pool raw_spinlock. | | There are several instances of this problem when block layer tries | to __queue_work(). Call trace from one of these instances is below: | | kblockd_mod_delayed_work_on() | mod_delayed_work_on() | __queue_delayed_work() | __queue_work() (rcu_read_lock, raw_spin_lock pool->lock held) | insert_work() | kasan_record_aux_stack() | kasan_save_stack() | stack_depot_save() | alloc_pages() | __alloc_pages() | get_page_from_freelist() | rm_queue() | rm_queue_pcplist() | local_lock_irqsave(&pagesets.lock, flags); | [ BUG: Invalid wait context triggered ] [1] https://lkml.kernel.org/r/20210902200134.25603-1-skhan@linuxfoundation.org PROVE_RAW_LOCK_NESTING is pointing out that (on RT kernels) the locking rules are being violated. More generally, memory is being allocated from a non-preemptive context (raw_spin_lock'd c-s) where it is not allowed. To properly fix this, we must prevent stackdepot from replenishing its "stack slab" pool if memory allocations cannot be done in the current context: it's a bug to use either GFP_ATOMIC nor GFP_NOWAIT in certain non-preemptive contexts, including raw_spin_locks (see gfp.h and ab00db216c9c7). The only downside is that saving a stack trace may fail if: stackdepot runs out of space AND the same stack trace has not been recorded before. I expect this to be unlikely, and a simple experiment (boot the kernel) didn't result in any failure to record stack trace from insert_work(). The series includes a few minor fixes to stackdepot that I noticed in preparing the series. It then introduces __stack_depot_save(), which exposes the option to force stackdepot to not allocate any memory. Finally, KASAN is changed to use the new stackdepot interface and provide kasan_record_aux_stack_noalloc(), which is then used by workqueue code. Marco Elver (6): lib/stackdepot: include gfp.h lib/stackdepot: remove unused function argument lib/stackdepot: introduce __stack_depot_save() kasan: common: provide can_alloc in kasan_save_stack() kasan: generic: introduce kasan_record_aux_stack_noalloc() workqueue, kasan: avoid alloc_pages() when recording stack include/linux/kasan.h | 2 ++ include/linux/stackdepot.h | 6 +++++ kernel/workqueue.c | 2 +- lib/stackdepot.c | 51 ++++++++++++++++++++++++++++++-------- mm/kasan/common.c | 6 ++--- mm/kasan/generic.c | 14 +++++++++-- mm/kasan/kasan.h | 2 +- 7 files changed, 65 insertions(+), 18 deletions(-) Tested-by: Shuah Khan Acked-by: Alexander Potapenko