Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp2772450pxb; Tue, 9 Mar 2021 10:25:09 -0800 (PST) X-Google-Smtp-Source: ABdhPJzdXma4u4W8g1e79BFc3g3bju5gqkYjrDBskaEfQewSXyJ1/eg5K+FlH3XEAQJvvAI8VCxV X-Received: by 2002:a17:907:a06b:: with SMTP id ia11mr21454089ejc.294.1615314309723; Tue, 09 Mar 2021 10:25:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1615314309; cv=none; d=google.com; s=arc-20160816; b=g7OP0/3Gf3ibIonIelpYNDi1DBl7ijCnyutI0/9rwqQ5bVhlzCKAun+XURGxzgmwLy JoAbZKEQi6K74nUxFYIa2EplIEVKJaqj9XN2uTiLLWgrDLMnu1kSn+YVUobZnsBvcnon fTYLtxBhLomj2Rnj4cuWG0AMMTONM8//IFrOrmPbj/a1X2RyC2PUlgVjhovqnH5k9Hvz lsaOQU9yKhrjcfZr5eervWLy0pRGn0NBvOYFp0/QQ1WdFeZ3v5wzn9UJ2vYc2bobcYg+ pzcsPgTuiw+I+m+6+JvrM/pbzXFaHmiI40IPkHU9a5+n2ag8HZd8wroiFahmFm2z2iEa DlDw== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=KRM6QdMdDJ7qMhWqPbinSiplIY0IhkhW4oPmPHJvLz8=; b=I1TD76lk2SnhCezxTn5DDtcUYNmGjFkgXOEUyAr9/XLVRWweIOFk1J0QAk1lvMWxHe +rYZrnmcbpOx8ruih5NeQu0uktNLOIqPctG1Jo9K8wLSeMMwbz+/6FxOn1cup68JU/F2 ZG7hyPNmiN0/rd1XIInmJ63K8s4HKAm/eiTimbI74PknL2FhLwuwcUKoEAsx+wqTvz5q 9dTYRNiXIKUPTzmdt7Iaum903FWpRTKCWnGCYLh/a0KIyprqlo4E5ycJxW8SUhFSwXoi A/9Lq+iHZXe1QlKnWvbgv3yR6IMsTyKe94CUkUXl46oOLIJVfPnHi/fcVgd99QW9yI/4 y9pw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@yandex-team.com header.s=default header.b=qr680Q7r; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=yandex-team.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id e22si10336851edu.45.2021.03.09.10.24.47; Tue, 09 Mar 2021 10:25:09 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@yandex-team.com header.s=default header.b=qr680Q7r; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=yandex-team.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231181AbhCISXd (ORCPT + 99 others); Tue, 9 Mar 2021 13:23:33 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53230 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230431AbhCISXV (ORCPT ); Tue, 9 Mar 2021 13:23:21 -0500 Received: from forwardcorp1j.mail.yandex.net (forwardcorp1j.mail.yandex.net [IPv6:2a02:6b8:0:1619::183]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A562CC06174A; Tue, 9 Mar 2021 10:23:20 -0800 (PST) Received: from iva8-d077482f1536.qloud-c.yandex.net (iva8-d077482f1536.qloud-c.yandex.net [IPv6:2a02:6b8:c0c:2f26:0:640:d077:482f]) by forwardcorp1j.mail.yandex.net (Yandex) with ESMTP id 26CB52E155E; Tue, 9 Mar 2021 21:23:15 +0300 (MSK) Received: from iva4-f06c35e68a0a.qloud-c.yandex.net (iva4-f06c35e68a0a.qloud-c.yandex.net [2a02:6b8:c0c:152e:0:640:f06c:35e6]) by iva8-d077482f1536.qloud-c.yandex.net (mxbackcorp/Yandex) with ESMTP id KBzV4JWaNe-NEJ8xCNk; Tue, 09 Mar 2021 21:23:15 +0300 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex-team.com; s=default; t=1615314195; bh=KRM6QdMdDJ7qMhWqPbinSiplIY0IhkhW4oPmPHJvLz8=; h=In-Reply-To:Message-Id:References:Date:Subject:To:From:Cc; b=qr680Q7rv5UvtkzV2He8omyRONVauShVadrXemuEWAkv54ZgfcCHNrqOAx2QOe66C eFTG3HmZ/swRUjzfDAVuaVwVgrvQzDCGtp3bmRD8Z6YI7WhTx8HyzI9z0mL8Qm6EHo vgGxiwcu8Kz99JE3MtKt69HxPpXRxaNosAL6fGVQ= Authentication-Results: iva8-d077482f1536.qloud-c.yandex.net; dkim=pass header.i=@yandex-team.com Received: from dynamic-vpn.dhcp.yndx.net (dynamic-vpn.dhcp.yndx.net [2a02:6b8:b081:203::1:15]) by iva4-f06c35e68a0a.qloud-c.yandex.net (smtpcorp/Yandex) with ESMTPSA id T5bJwzP5HK-NEnW1CdL; Tue, 09 Mar 2021 21:23:14 +0300 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (Client certificate not present) From: Andrey Ryabinin To: gregkh@linuxfoundation.org, stable@vger.kernel.org Cc: jroedel@suse.de, will@kernel.org, linux-kernel@vger.kernel.org, Andrey Ryabinin Subject: [PATCH stable 5.10-5.11] iommu/amd: Fix sleeping in atomic in increase_address_space() Date: Tue, 9 Mar 2021 21:24:29 +0300 Message-Id: <20210309182430.18849-3-arbn@yandex-team.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210309182430.18849-1-arbn@yandex-team.com> References: <161512522533161@kroah.com> <20210309182430.18849-1-arbn@yandex-team.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org commit 140456f994195b568ecd7fc2287a34eadffef3ca upstream. increase_address_space() calls get_zeroed_page(gfp) under spin_lock with disabled interrupts. gfp flags passed to increase_address_space() may allow sleeping, so it comes to this: BUG: sleeping function called from invalid context at mm/page_alloc.c:4342 in_atomic(): 1, irqs_disabled(): 1, pid: 21555, name: epdcbbf1qnhbsd8 Call Trace: dump_stack+0x66/0x8b ___might_sleep+0xec/0x110 __alloc_pages_nodemask+0x104/0x300 get_zeroed_page+0x15/0x40 iommu_map_page+0xdd/0x3e0 amd_iommu_map+0x50/0x70 iommu_map+0x106/0x220 vfio_iommu_type1_ioctl+0x76e/0x950 [vfio_iommu_type1] do_vfs_ioctl+0xa3/0x6f0 ksys_ioctl+0x66/0x70 __x64_sys_ioctl+0x16/0x20 do_syscall_64+0x4e/0x100 entry_SYSCALL_64_after_hwframe+0x44/0xa9 Fix this by moving get_zeroed_page() out of spin_lock/unlock section. Fixes: 754265bcab ("iommu/amd: Fix race in increase_address_space()") Signed-off-by: Andrey Ryabinin Acked-by: Will Deacon Link: https://lore.kernel.org/r/20210217143004.19165-1-arbn@yandex-team.com Signed-off-by: Joerg Roedel Signed-off-by: Andrey Ryabinin --- drivers/iommu/amd/iommu.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c index f0adbc48fd179..9256f84f5ebf1 100644 --- a/drivers/iommu/amd/iommu.c +++ b/drivers/iommu/amd/iommu.c @@ -1502,6 +1502,10 @@ static bool increase_address_space(struct protection_domain *domain, bool ret = true; u64 *pte; + pte = (void *)get_zeroed_page(gfp); + if (!pte) + return false; + spin_lock_irqsave(&domain->lock, flags); amd_iommu_domain_get_pgtable(domain, &pgtable); @@ -1513,10 +1517,6 @@ static bool increase_address_space(struct protection_domain *domain, if (WARN_ON_ONCE(pgtable.mode == PAGE_MODE_6_LEVEL)) goto out; - pte = (void *)get_zeroed_page(gfp); - if (!pte) - goto out; - *pte = PM_LEVEL_PDE(pgtable.mode, iommu_virt_to_phys(pgtable.root)); pgtable.root = pte; @@ -1530,10 +1530,12 @@ static bool increase_address_space(struct protection_domain *domain, */ amd_iommu_domain_set_pgtable(domain, pte, pgtable.mode); + pte = NULL; ret = true; out: spin_unlock_irqrestore(&domain->lock, flags); + free_page((unsigned long)pte); return ret; } -- 2.26.2