From patchwork Wed Feb 22 16:52:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guorui Yu X-Patchwork-Id: 60616 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp691678wrd; Wed, 22 Feb 2023 09:06:56 -0800 (PST) X-Google-Smtp-Source: AK7set9Z3rkE6Z/exhLZBewnm0CRYOxc1B/tMGrmDepbNfbwuMpIJabEQ4+oKH1AgdAtsPE4MBS6 X-Received: by 2002:a17:903:283:b0:199:2e77:fe16 with SMTP id j3-20020a170903028300b001992e77fe16mr11800287plr.58.1677085616215; Wed, 22 Feb 2023 09:06:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677085616; cv=none; d=google.com; s=arc-20160816; b=O16CZisAd3IeHNNOGYRkGEz2e56AYTuj5KyYfWpTt3KYyZrm4WerxyI83oBd9pHa3o v1R7ks0KszDG2ZDO3p3K6ptRoCGcZllVet8qOqy67Pf2nAvUowE+K6JBErSIlk7i3Blm HoAkWQHwRDNNUsFEKY8jHHdz7kBLWkt4Lw1XeiRws5n72aTYEPjJCauhpsciP4d1XSWH exPFNqU4G19dxmkXo68lKDuRGVsDp4T0tZIOkbF8gaZoPS0moMHHEzD0WpS+sLoZkbUt R2GcGGSQbHq/cOS/1xsGCJrn9WdvlvoTuauNB0Z6a4SMItSpZVLiflFB5CuK05s3C5IV tvug== 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 :message-id:date:subject:cc:to:from; bh=frbUtv/s55VoWycBaYAQd46ZWTVXEFmJiVlXYetHK6g=; b=H9mNCL/GcKS0iWu8vJTNWYGNJh4wm4BuCbawikPfL/qku4l3eu0HTwTYIe2UWNC7Tl ud8wj9ihow++X4GvHG78JP1b8ML3G0UARpjW0MO5dGGVkR0gHzRQ/Iikk3u5JzF1Qh3i otB1Ld4GVt2VosJyYI1fVRIYKUSBQTQYnlQGaCTbfvfp3CQikYusaa8/ZHh+bI/yzcpu XEAjA8N0XJoBIT/L2vCHB9P2G9YSp+Tb8KQD8LwPPWa8a2FbErqKuracAuwBMHh/O1XZ EGIaSs8zB9EWUny7p8FWRE8kQ6cYTijrYaPflbl2f8qzZaBOeNKaGqJlNYCUEuAC39Ia 6Vjg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b15-20020a1709027e0f00b00198e1b0680csi14514364plm.578.2023.02.22.09.06.42; Wed, 22 Feb 2023 09:06:56 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231854AbjBVQxF (ORCPT + 99 others); Wed, 22 Feb 2023 11:53:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41416 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232136AbjBVQxC (ORCPT ); Wed, 22 Feb 2023 11:53:02 -0500 Received: from out30-99.freemail.mail.aliyun.com (out30-99.freemail.mail.aliyun.com [115.124.30.99]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F0001BDC6 for ; Wed, 22 Feb 2023 08:52:56 -0800 (PST) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R381e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018046059;MF=guorui.yu@linux.alibaba.com;NM=1;PH=DS;RN=7;SR=0;TI=SMTPD_---0VcHP-D1_1677084772; Received: from localhost(mailfrom:GuoRui.Yu@linux.alibaba.com fp:SMTPD_---0VcHP-D1_1677084772) by smtp.aliyun-inc.com; Thu, 23 Feb 2023 00:52:52 +0800 From: "GuoRui.Yu" To: hch@lst.de, m.szyprowski@samsung.com Cc: robin.murphy@arm.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, GuoRui.Yu@linux.alibaba.com, linux-mm@kvack.org Subject: [PATCH] swiotlb: fix the deadlock in swiotlb_do_find_slots Date: Thu, 23 Feb 2023 00:52:51 +0800 Message-Id: <20230222165251.88700-1-GuoRui.Yu@linux.alibaba.com> X-Mailer: git-send-email 2.29.2.540.g3cf59784d4 MIME-Version: 1.0 X-Spam-Status: No, score=-9.9 required=5.0 tests=BAYES_00, ENV_AND_HDR_SPF_MATCH,SPF_HELO_NONE,SPF_PASS,UNPARSEABLE_RELAY, USER_IN_DEF_SPF_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1758551727018089470?= X-GMAIL-MSGID: =?utf-8?q?1758551727018089470?= In general, if swiotlb is sufficient, the logic of index = wrap_area_index(mem, index + 1) is fine, it will quickly take a slot and release the area->lock; But if swiotlb is insufficient and the device has min_align_mask requirements, such as NVME, we may not be able to satisfy index == wrap and exit the loop properly. In this case, other kernel threads will not be able to acquire the area->lock and release the slot, resulting in a deadlock. The current implementation of wrap_area_index does not involve a modulo operation, so adjusting the wrap to ensure the loop ends is not trivial. Introduce the index_nowrap variable to record the number of loops and exit the loop after completing the traversal. Backtraces: Other CPUs are waiting this core to exit the swiotlb_do_find_slots loop. [10199.924391] RIP: 0010:swiotlb_do_find_slots+0x1fe/0x3e0 [10199.924403] Call Trace: [10199.924404] [10199.924405] swiotlb_tbl_map_single+0xec/0x1f0 [10199.924407] swiotlb_map+0x5c/0x260 [10199.924409] ? nvme_pci_setup_prps+0x1ed/0x340 [10199.924411] dma_direct_map_page+0x12e/0x1c0 [10199.924413] nvme_map_data+0x304/0x370 [10199.924415] nvme_prep_rq.part.0+0x31/0x120 [10199.924417] nvme_queue_rq+0x77/0x1f0 ... [ 9639.596311] NMI backtrace for cpu 48 [ 9639.596336] Call Trace: [ 9639.596337] [ 9639.596338] _raw_spin_lock_irqsave+0x37/0x40 [ 9639.596341] swiotlb_do_find_slots+0xef/0x3e0 [ 9639.596344] swiotlb_tbl_map_single+0xec/0x1f0 [ 9639.596347] swiotlb_map+0x5c/0x260 [ 9639.596349] dma_direct_map_sg+0x7a/0x280 [ 9639.596352] __dma_map_sg_attrs+0x30/0x70 [ 9639.596355] dma_map_sgtable+0x1d/0x30 [ 9639.596356] nvme_map_data+0xce/0x370 ... [ 9639.595665] NMI backtrace for cpu 50 [ 9639.595682] Call Trace: [ 9639.595682] [ 9639.595683] _raw_spin_lock_irqsave+0x37/0x40 [ 9639.595686] swiotlb_release_slots.isra.0+0x86/0x180 [ 9639.595688] dma_direct_unmap_sg+0xcf/0x1a0 [ 9639.595690] nvme_unmap_data.part.0+0x43/0xc0 Fixes: 1f221a0d0dbf ("swiotlb: respect min_align_mask") Signed-off-by: GuoRui.Yu Signed-off-by: Xiaokang Hu --- kernel/dma/swiotlb.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c index a34c38bbe28f..638ba3ea94f4 100644 --- a/kernel/dma/swiotlb.c +++ b/kernel/dma/swiotlb.c @@ -632,7 +632,7 @@ static int swiotlb_do_find_slots(struct device *dev, int area_index, unsigned int iotlb_align_mask = dma_get_min_align_mask(dev) & ~(IO_TLB_SIZE - 1); unsigned int nslots = nr_slots(alloc_size), stride; - unsigned int index, wrap, count = 0, i; + unsigned int index, index_nowrap = 0, wrap, count = 0, i; unsigned int offset = swiotlb_align_offset(dev, orig_addr); unsigned long flags; unsigned int slot_base; @@ -665,6 +665,7 @@ static int swiotlb_do_find_slots(struct device *dev, int area_index, (slot_addr(tbl_dma_addr, slot_index) & iotlb_align_mask) != (orig_addr & iotlb_align_mask)) { index = wrap_area_index(mem, index + 1); + index_nowrap++; continue; } @@ -680,7 +681,8 @@ static int swiotlb_do_find_slots(struct device *dev, int area_index, goto found; } index = wrap_area_index(mem, index + stride); - } while (index != wrap); + index_nowrap += stride; + } while (index_nowrap < mem->area_nslabs); not_found: spin_unlock_irqrestore(&area->lock, flags);