From mboxrd@z Thu Jan 1 00:00:00 1970 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 33A34C61DA4 for ; Mon, 13 Mar 2023 05:13:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 842E06B0071; Mon, 13 Mar 2023 01:13:16 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7F2C18E0002; Mon, 13 Mar 2023 01:13:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6E2088E0001; Mon, 13 Mar 2023 01:13:16 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 5F07D6B0071 for ; Mon, 13 Mar 2023 01:13:16 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 27318C026B for ; Mon, 13 Mar 2023 05:13:16 +0000 (UTC) X-FDA: 80562706392.01.17EA154 Received: from out30-118.freemail.mail.aliyun.com (out30-118.freemail.mail.aliyun.com [115.124.30.118]) by imf14.hostedemail.com (Postfix) with ESMTP id A4846100004 for ; Mon, 13 Mar 2023 05:13:12 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=none; spf=pass (imf14.hostedemail.com: domain of GuoRui.Yu@linux.alibaba.com designates 115.124.30.118 as permitted sender) smtp.mailfrom=GuoRui.Yu@linux.alibaba.com; dmarc=pass (policy=none) header.from=alibaba.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1678684394; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Za+NGdCFX2nTLFeFj8r6BA2U1VjCoF7ekktBC1kZUVo=; b=pW5spLs9ytehsB6r/xWIBlWLAsLEZ/9aS1DQV6EIeP6MV0OV7Ffda1b7ENQ8cwD0lYUVil /BtkK+irGhMoV0fuhlxx8iGNwXQyAT5ZKcXcyDEpmeMkd89fKZL3sh3bzpKW6v8q1VS4xu 2z5vLz5kyc51Eob3D/8pGe3ugKk0CdI= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=none; spf=pass (imf14.hostedemail.com: domain of GuoRui.Yu@linux.alibaba.com designates 115.124.30.118 as permitted sender) smtp.mailfrom=GuoRui.Yu@linux.alibaba.com; dmarc=pass (policy=none) header.from=alibaba.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1678684394; a=rsa-sha256; cv=none; b=qb3cOH2ZLv7iLLOBqS5EKPqUhH0HRNY65ssn9OuXs7v+gh0o6T0MsZpWTRGNK+8XbhD4nU NGDMFQVGugeAVFnLhrPfjj6j9UkrAD38OotmFgxHeAM3KMzpz5KBNh8OJOkzsG82vpqtdj dvR9J9ArB1Yf7uKBnc0fP6kEfwQjUSg= X-Alimail-AntiSpam:AC=PASS;BC=-1|-1;BR=01201311R551e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018046060;MF=guorui.yu@linux.alibaba.com;NM=1;PH=DS;RN=6;SR=0;TI=SMTPD_---0Vdg9z0c_1678684384; Received: from 127.0.0.1(mailfrom:GuoRui.Yu@linux.alibaba.com fp:SMTPD_---0Vdg9z0c_1678684384) by smtp.aliyun-inc.com; Mon, 13 Mar 2023 13:13:08 +0800 Message-ID: <59d74495-fb00-e316-6198-12ab2b2c2c64@linux.alibaba.com> Date: Mon, 13 Mar 2023 13:13:03 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.8.0 Subject: Re: [PATCH v2] swiotlb: fix the deadlock in swiotlb_do_find_slots To: hch@lst.de, m.szyprowski@samsung.com Cc: robin.murphy@arm.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, linux-mm@kvack.org References: <20230222165315.89135-1-GuoRui.Yu@linux.alibaba.com> From: Guorui Yu In-Reply-To: <20230222165315.89135-1-GuoRui.Yu@linux.alibaba.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Stat-Signature: wpsbdipbddbbhr3os6nmqse1jyqeztnb X-Rspam-User: X-Rspamd-Queue-Id: A4846100004 X-Rspamd-Server: rspam06 X-HE-Tag: 1678684392-654724 X-HE-Meta: U2FsdGVkX19Ta/lFnpsOLKS9A2EFe0NxC296xFB9LDHESihZYwGe9ZPW7O8spxFWitHsQKzArClZm2SLoxwwHeIekz+Pd+UUKvzccsNRpRuMAzPkTaqrFLXbcR2WbHfeTPF4aXV/1PxDXWzkCMZ+kLfaviVXpRiIy7vBydwcv8ZLs7c4c1GveCTcB5KENNPXzEmyZDy0TxOZ+eBh8xZgkvE8kD3CmdZuBqKTpP40CwjhvT2+ZCCkFJDPLNvs8F2gz+29ml7hJAp2/ifpwQB5QN/Hi7un44wlZK05ObRUZARdhQpdlOCNxwGudUwjaFujfpkbqrGq9hMBm+9MMZGb4cIo5okoAyDfPlpUVjOButzZdoRT1L32z+U0BE9XlQ1HrU+4Pb16LZAgjV9Xtqu26+DZR3pNf5misjK5UM+ScI+fkMi0Uk5OENLg/AY7v35jFVbywK1zkTnHpc9AvE2ysxEc4OGLaUe/GT/FhhiMJD4FKIvWLMsudU3VzzsemBXh63p+MwD4czbtG9FG2/zESA7PFZFOjZXM6yiulnQo/NpvXlDGp3Ca+oDwgyO7fDrGxVSRsq+fv32t/TJvMZ1wpM2HHuLHx1YC31pgj+GMQ4hpuo4e9d+Gex242bp0b3be7QAFh59wQWWe8tg58ukjMZTLOyWG6IhetoBFnA9qqaIKmpdxqPCrmJynrXiHowQ0MK37gn10r49MVvCKXC/EFakBEXwk5JSP7j0J+TPaGiC+cWH8S6xj3EqFwpVz3PN/uYTj3jziYdhKqtJraoAUofMMf/8PPXwzOGOnXY5A8MyV5qk3DGZdC35kkJmX4oXbJPjYOtiRMhgpZsRADYZt/6tgvJnhOwd7mnkPsQSNPd/+bUyvewAvmqUIScojDoJayo9rS3UFo3+rFQL6hewFoXnKLasPhhqq2fa/sQSfhD5oYgVi4PS7QE2I7dEZRQLOWpty5ueihkceqEcNTB/ eHiXpZy7 8PmSn0CMpoPF0BZFgU9P/YrvKkbWYZssqkF+e011nhayFFyd7jtJUIENdiw7S69j/vqJPGcwuW0Vrsm2wgkjF5oTP5yWgyxpkYwfIDXUmKCT+v8GwmvmWDLun0aGxyHWk5Uu+gF+8U1C22dsDy0J9NvoVAFxwfDZYjHUMvCU9B+lHs9oqzDRnLZ4fUc5axC29VM1pg5AdvyN37HjPtA0NAKYgSCs2CD7Vi/T+ 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: Hi Christoph, kindly ping for any comments here? Thanks, Guorui 在 2023/2/23 00:53, GuoRui.Yu 写道: > 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);