From patchwork Wed Jul 7 11:12:45 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Desmond Cheong Zhi Xi X-Patchwork-Id: 12362421 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=-13.5 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable 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 31BB5C07E9C for ; Wed, 7 Jul 2021 11:13:39 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id A808861C88 for ; Wed, 7 Jul 2021 11:13:38 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org A808861C88 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 4E53B6B0036; Wed, 7 Jul 2021 07:13:38 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4950E6B005D; Wed, 7 Jul 2021 07:13:38 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 335BE6B006C; Wed, 7 Jul 2021 07:13:38 -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 113F56B0036 for ; Wed, 7 Jul 2021 07:13:38 -0400 (EDT) Received: from smtpin36.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 499531AEC5 for ; Wed, 7 Jul 2021 11:13:37 +0000 (UTC) X-FDA: 78335531274.36.6F51A93 Received: from mail-pg1-f181.google.com (mail-pg1-f181.google.com [209.85.215.181]) by imf29.hostedemail.com (Postfix) with ESMTP id E8A959000094 for ; Wed, 7 Jul 2021 11:13:36 +0000 (UTC) Received: by mail-pg1-f181.google.com with SMTP id u14so1835549pga.11 for ; Wed, 07 Jul 2021 04:13:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=ZdzfnR4j5eQS/lxbgzCM8QfZTWzUyhCC6sdEedhtEac=; b=l5qvT9pK4sLGvXuFFcV6Xw0M5gy0owqlmoKq5yUKAd6+LmwxFt8cfQYuxsKzbXGTdf f836Fm5gfNv1ml0LuQ1yH3ewcDtCD2w2tqm1QNX1iUGsjXm7KC5dK/wHq0c1mX4AmXXM TldtoS2fNCFNgY+rVrVTB3Z74ZZUnKHAb2q2465brs3iENz8716C5cBrNYN7VJUQNirK aBJdEcaTydHg5fqSeVLCfQSLy1hEztU2idlcRwzTfieccA/qk/Lqr3ZGLJgfdsc9L7zQ d63pu8Ae5moDd04el4WrVyrMa8v70EdVKsLR26jLvOFSCJ2lRuGeO8iyMNkj2QxJinz7 yIeg== 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:mime-version :content-transfer-encoding; bh=ZdzfnR4j5eQS/lxbgzCM8QfZTWzUyhCC6sdEedhtEac=; b=m2z76YB25nucg2t6y4KCyap/Wz1IQ0YKwKePvtj65BoDw+1/x7qj1dN4oFbKTnNgPH ZYXkZ6d+dt0DUg3dagHgK4pFcweHFY3ljT4Rbe6UNOH/GbDMLjvmCRMfJLoLalLlIKBc 9vF3eru1NEVWM+MbvrGf2+LnwpKX13RZrxIw9HDU6SZg2T9U9+rtw1u2NiIFhtBh1cmQ xA1VLfQ5Y/4fpQZCM+EGHmnL41TX7idM/ZNqVfFpfJp2T+5+wDto9KQekB0Yv4/iOKBL JGJN1c5ynwzuThrRlA2zKkCVfYiPd5pr44n75d+GIzHXHGeiQfv27MSOzKwjq2LVJkng 8tYw== X-Gm-Message-State: AOAM533jcV05jwVMBC9TU/BNhHV/3xlqz50ReTfB/flyuxK+Nkaqzopx yzHwRG9bVCbcignyrFE/ovY= X-Google-Smtp-Source: ABdhPJxBfBQ8Wo2ahM2ctn4u10fcGSESS6ZBw/A8VV8R4TJQJBRDdFLtmUkeJFchBFHaUWGBBSd6Gg== X-Received: by 2002:a63:4201:: with SMTP id p1mr25888381pga.386.1625656415995; Wed, 07 Jul 2021 04:13:35 -0700 (PDT) Received: from localhost.localdomain ([118.200.190.93]) by smtp.gmail.com with ESMTPSA id k189sm18896577pgk.14.2021.07.07.04.13.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Jul 2021 04:13:35 -0700 (PDT) From: Desmond Cheong Zhi Xi To: akpm@linux-foundation.org, mgorman@techsingularity.net Cc: Desmond Cheong Zhi Xi , linux-mm@kvack.org, linux-kernel@vger.kernel.org, skhan@linuxfoundation.org, gregkh@linuxfoundation.org, linux-kernel-mentees@lists.linuxfoundation.org, syzbot+127fd7828d6eeb611703@syzkaller.appspotmail.com Subject: [PATCH] mm/page_alloc: avoid deadlocks for &pagesets.lock Date: Wed, 7 Jul 2021 19:12:45 +0800 Message-Id: <20210707111245.625374-1-desmondcheongzx@gmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=gmail.com header.s=20161025 header.b=l5qvT9pK; spf=pass (imf29.hostedemail.com: domain of desmondcheongzx@gmail.com designates 209.85.215.181 as permitted sender) smtp.mailfrom=desmondcheongzx@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-Stat-Signature: f8mfdbp5fs8eifzmniix7dy5qyideab6 X-Rspamd-Queue-Id: E8A959000094 X-Rspamd-Server: rspam06 X-HE-Tag: 1625656416-333839 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: Syzbot reports a number of potential deadlocks for &pagesets.lock. It seems that this new lock is being used as both an inner and outer lock, which makes it prone to creating circular dependencies. For example, one such call trace goes as follows: __alloc_pages_bulk() local_lock_irqsave(&pagesets.lock, flags) <---- outer lock here prep_new_page(): post_alloc_hook(): set_page_owner(): __set_page_owner(): save_stack(): stack_depot_save(): alloc_pages(): alloc_page_interleave(): __alloc_pages(): get_page_from_freelist(): rm_queue(): rm_queue_pcplist(): local_lock_irqsave(&pagesets.lock, flags); *** DEADLOCK *** The common culprit for the lockdep splats seems to be the call to local_lock_irqsave(&pagesets.lock, flags) inside __alloc_pages_bulk(). &pagesets.lock becomes an outer lock if it's held during the call to prep_new_page(). As the local lock is used to protect the PCP structure, we adjust the locking in __alloc_pages_bulk so that only the necessary structures are protected. Fixes: dbbee9d5cd83 ("mm/page_alloc: convert per-cpu list protection to local_lock") Reported-and-tested-by: syzbot+127fd7828d6eeb611703@syzkaller.appspotmail.com Signed-off-by: Desmond Cheong Zhi Xi Reported-by: Desmond Cheong Zhi Xi Reported-by: "Zhang, Qiang" Reported-by: syzbot+127fd7828d6eeb611703@syzkaller.appspotmail.com Signed-off-by: Mel Gorman --- mm/page_alloc.c | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 0817d88383d5..0e005b1a60e3 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -5288,10 +5288,6 @@ unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid, goto failed; /* Attempt the batch allocation */ - local_lock_irqsave(&pagesets.lock, flags); - pcp = this_cpu_ptr(zone->per_cpu_pageset); - pcp_list = &pcp->lists[order_to_pindex(ac.migratetype, 0)]; - while (nr_populated < nr_pages) { /* Skip existing pages */ @@ -5300,12 +5296,16 @@ unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid, continue; } + local_lock_irqsave(&pagesets.lock, flags); + pcp = this_cpu_ptr(zone->per_cpu_pageset); + pcp_list = &pcp->lists[order_to_pindex(ac.migratetype, 0)]; page = __rmqueue_pcplist(zone, 0, ac.migratetype, alloc_flags, pcp, pcp_list); + local_unlock_irqrestore(&pagesets.lock, flags); if (unlikely(!page)) { /* Try and get at least one page */ if (!nr_populated) - goto failed_irq; + goto failed; break; } nr_account++; @@ -5318,16 +5318,11 @@ unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid, nr_populated++; } - local_unlock_irqrestore(&pagesets.lock, flags); - __count_zid_vm_events(PGALLOC, zone_idx(zone), nr_account); zone_statistics(ac.preferred_zoneref->zone, zone, nr_account); return nr_populated; -failed_irq: - local_unlock_irqrestore(&pagesets.lock, flags); - failed: page = __alloc_pages(gfp, 0, preferred_nid, nodemask); if (page) {