From nobody Mon Feb 9 10:33:23 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1664992143; cv=none; d=zohomail.com; s=zohoarc; b=UG4xlON896TCUTnt6lFu5dfx/rqt42LCjUkA3KDjVmfG7FBwOMf1fKkmwTMoi512zyMEf0DxTiEC/xEsu5DEjRf/++rsHDz47v2OVI/NPrhhfGqLExsGMsx/t2KkOclzxaATvBji8VoxVL07y7pe8aNC4P5HtJ/fnvIt6gDL4AU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1664992143; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=lUqPpYv5HbJS3RJZgdTtH5zBJM8cF/Q/yQo70/LIPGg=; b=VWUlgAbeH/p1zcGlaO6YVKdjV1YqTC8sJ2DzbQgEyDWwIRutVWtMo5NHoPa5c6z/J0ULbLH8PN4RhUkyFcNZ7TrVLeSDmtr6Y/bNszfnN9bzvoiye8YsoKxlyHgil6cEDlNJsURisTIDhy3lF68LA+/zLiXv87NfZXNka6zmk0I= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1664992143576866.9687809839107; Wed, 5 Oct 2022 10:49:03 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.416379.661040 (Exim 4.92) (envelope-from ) id 1og8VY-0003v0-Eh; Wed, 05 Oct 2022 17:48:36 +0000 Received: by outflank-mailman (output) from mailman id 416379.661040; Wed, 05 Oct 2022 17:48:36 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1og8VY-0003ut-Be; Wed, 05 Oct 2022 17:48:36 +0000 Received: by outflank-mailman (input) for mailman id 416379; Wed, 05 Oct 2022 17:48:35 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1og8VX-0003fH-6y for xen-devel@lists.xenproject.org; Wed, 05 Oct 2022 17:48:35 +0000 Received: from mail-ej1-x62e.google.com (mail-ej1-x62e.google.com [2a00:1450:4864:20::62e]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id eed704e3-44d5-11ed-964a-05401a9f4f97; Wed, 05 Oct 2022 19:48:34 +0200 (CEST) Received: by mail-ej1-x62e.google.com with SMTP id k2so7121038ejr.2 for ; Wed, 05 Oct 2022 10:48:34 -0700 (PDT) Received: from otyshchenko.router ([212.22.223.21]) by smtp.gmail.com with ESMTPSA id ee32-20020a056402292000b0044e7862ab3fsm4320697edb.7.2022.10.05.10.48.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Oct 2022 10:48:32 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: eed704e3-44d5-11ed-964a-05401a9f4f97 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=lUqPpYv5HbJS3RJZgdTtH5zBJM8cF/Q/yQo70/LIPGg=; b=q2NAXUerg5Qq4svf9ZBclqSa7jhIsyyWtisVfdHoR09jVE9TUdLqPM4efflTILBJ2E 1QzruERT96PZYju0r7C2ZaHvg30mr5WuMueOXGd/ZwvoZmmuOUN13WUjo0E1wd+IbDqs BHtddK4HNWj9DlWucI/LF6hcSCBL5vdIlYwIr7rf7LHwpwKw1u0JzBn48GS4yFmoiv+C BXfd99AdwgNOMIo8PqFDtYXynE8wj8nj18ErlAFmAjYKe4avYXwKc2MTtpfsUUJ0Hr+d nyJcbqw+4Yt20d6Zxo8CDJDZfh2dhLrWeY1cgcrGP5JOqbkMqzLlOTlOQlgFouD46h6r MMng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=lUqPpYv5HbJS3RJZgdTtH5zBJM8cF/Q/yQo70/LIPGg=; b=sNyYJOSRJzkOojEVEp8LqCP/WX03axbkZHiFGjvLuGQBm1DbOR7V9EQNCeybUnAGOT b+5OQRDOKjkxaXZ1f+Fy1LM8/Zxqb8fh7esFvTbFHgT/C18yjcQexk+nnFZEUSyavu86 Wyb/Mi/tSIV0K5Kjf7Qa7aGFAivcn3QclcPhhe6OObJuKzYUUWPkEJTSXudpdRP9N14X 29PuNYGC/blThAIDcDXCFJeB8VVU9iaMR+VlPMCsfy+I+Es/m36fl/Puadh8jKLfJUHm Mxw5ZGeDgx7KjdYrHTXHyszxUIDfOuYg8ubW6hPzbbUMANXbVqZ/JKVEI1kw5M63ja2C 7vAg== X-Gm-Message-State: ACrzQf1hjKoauVlUwcFmJRRIxVbkj7VbWOn1LoYMSXBWW35rg9+STCSu Ttic1Kf8dTGmESHOsHbCbQpN149dm7A= X-Google-Smtp-Source: AMsMyM7qFxTtDClHub9HwGnUCHbj0K2B+uD3aLcbrl4tDcKrhmWQ/TdQGfyyThVB1O4Bfc/Y82Eqaw== X-Received: by 2002:a17:906:4ac1:b0:780:3448:ff06 with SMTP id u1-20020a1709064ac100b007803448ff06mr587283ejt.403.1664992113581; Wed, 05 Oct 2022 10:48:33 -0700 (PDT) From: Oleksandr Tyshchenko To: xen-devel@lists.xenproject.org, linux-kernel@vger.kernel.org Cc: Oleksandr Tyshchenko , Stefano Stabellini , Juergen Gross Subject: [PATCH 1/2] xen/virtio: Fix n_pages calculation in xen_grant_dma_map(unmap)_page() Date: Wed, 5 Oct 2022 20:48:22 +0300 Message-Id: <20221005174823.1800761-2-olekstysh@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221005174823.1800761-1-olekstysh@gmail.com> References: <20221005174823.1800761-1-olekstysh@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1664992144242100001 Content-Type: text/plain; charset="utf-8" From: Oleksandr Tyshchenko Take page offset into the account when calculating the number of pages to be granted. Signed-off-by: Oleksandr Tyshchenko Fixes: d6aca3504c7d ("xen/grant-dma-ops: Add option to restrict memory acce= ss under Xen") Reviewed-by: Juergen Gross Reviewed-by: Stefano Stabellini --- drivers/xen/grant-dma-ops.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/drivers/xen/grant-dma-ops.c b/drivers/xen/grant-dma-ops.c index 8973fc1e9ccc..1998d0e8ce82 100644 --- a/drivers/xen/grant-dma-ops.c +++ b/drivers/xen/grant-dma-ops.c @@ -153,7 +153,7 @@ static dma_addr_t xen_grant_dma_map_page(struct device = *dev, struct page *page, unsigned long attrs) { struct xen_grant_dma_data *data; - unsigned int i, n_pages =3D PFN_UP(size); + unsigned int i, n_pages =3D PFN_UP(offset + size); grant_ref_t grant; dma_addr_t dma_handle; =20 @@ -185,7 +185,8 @@ static void xen_grant_dma_unmap_page(struct device *dev= , dma_addr_t dma_handle, unsigned long attrs) { struct xen_grant_dma_data *data; - unsigned int i, n_pages =3D PFN_UP(size); + unsigned long offset =3D dma_handle & (PAGE_SIZE - 1); + unsigned int i, n_pages =3D PFN_UP(offset + size); grant_ref_t grant; =20 if (WARN_ON(dir =3D=3D DMA_NONE)) --=20 2.25.1 From nobody Mon Feb 9 10:33:23 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1664992151; cv=none; d=zohomail.com; s=zohoarc; b=E80tOYd4A2bGOE22eKiuEHtrVABoJ3eRY4rHV+D2QulPeFwEaYAd+13/pZBBS5phWi+EB9YXdG2H9j3A4X4aqf6UbSQw9L+vWfLwZLgbldw24z3lkVnyhuPJiwnznOQc88ZsDEhos2EdpjkkzE7u+tcw7Bh3tdDtnBhy7fyWpMA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1664992151; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=2ygXgrMvwI83oTWciRbWlrh/kQps7FfPG+yghSVi9/U=; b=XXy+SNh/IdrlQLYqsfUtwYdrTtYzC+wa34g8LzcG99SFr+NaHAoEtuGIkQeXvMWyXZ2Sj1WfbN5Wwqynhhl2/HeHXxtSKt8rMYUVrx1ciYRFBBiZ5dVEYDQilH3HHD4SAgj3P3/Mn3QQwlMPp349IUhGDUsBG7F38xIGMKgz1co= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1664992151259821.7288979382996; Wed, 5 Oct 2022 10:49:11 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.416381.661052 (Exim 4.92) (envelope-from ) id 1og8VZ-0004BN-RJ; Wed, 05 Oct 2022 17:48:37 +0000 Received: by outflank-mailman (output) from mailman id 416381.661052; Wed, 05 Oct 2022 17:48:37 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1og8VZ-0004B7-OK; Wed, 05 Oct 2022 17:48:37 +0000 Received: by outflank-mailman (input) for mailman id 416381; Wed, 05 Oct 2022 17:48:36 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1og8VY-0003fH-Af for xen-devel@lists.xenproject.org; Wed, 05 Oct 2022 17:48:36 +0000 Received: from mail-ej1-x636.google.com (mail-ej1-x636.google.com [2a00:1450:4864:20::636]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id ef7b0aa5-44d5-11ed-964a-05401a9f4f97; Wed, 05 Oct 2022 19:48:35 +0200 (CEST) Received: by mail-ej1-x636.google.com with SMTP id o21so744309ejm.11 for ; Wed, 05 Oct 2022 10:48:35 -0700 (PDT) Received: from otyshchenko.router ([212.22.223.21]) by smtp.gmail.com with ESMTPSA id ee32-20020a056402292000b0044e7862ab3fsm4320697edb.7.2022.10.05.10.48.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Oct 2022 10:48:34 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: ef7b0aa5-44d5-11ed-964a-05401a9f4f97 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=2ygXgrMvwI83oTWciRbWlrh/kQps7FfPG+yghSVi9/U=; b=NDy/TvK3L7DC7SkJDhT99xIPSRmwqUPUnB9VOPISmgU4v7mI2YvcoR1ddz/qLrnw+0 JtBz1FcFB8Hh4CpZJIL5Q+9dR1Kvk7FSPZ0QY57RKem+vG3zMyPQvs2nEtW3eP9DlSte Q4Q00ZX8taSGQ9FGTZwSmDEE/CU7q3Waw2GQgwV/QleXYiFIkD1QBokMpLj5yTgyFGg3 H2LNVp4TR3NuYpy0KYQENae1x6fej8VH6Je2Fa2B8fJCHhTADIKgj4isSF/rvP0nhV5E 3bMD3Rl83GS3q/aaSd70GK9gqKLw7jMxWByorqPO22D1SN2zgBz4kAvKbkZBYcwxBZDh KA0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=2ygXgrMvwI83oTWciRbWlrh/kQps7FfPG+yghSVi9/U=; b=C+zR+qAQUNDA/CDhPkDrXXZDFlTPIYt7TSB7J4ImKmKcs3C4nrW3EUkmtfyoRVmNjR c6XZk4h7BmO/dQujdUHFHrUXnlIcfdB/jCTn/GLZfxe8dO+GnU1plmMvcXReJoPgk4Ph D5lyxxq14z1jyfyV4HQC3o2P+3a7C96lzfYNuxP7mXYOKP0g77wq9iST2EVyW/HcQVGM RLqYlRgRPJskYxy8HlWpPs30Q8lCIULDHszmvoJyGyh9zbpL6e5pQ4oNXYkSk0zOUrob 5rW0/YPvLs8SOwjkiodv3eCUwSthMAAB+BSK7yeMrdTsDW4K3csOCstDbEmWm9OihaMB zbgg== X-Gm-Message-State: ACrzQf2N0d0k7r4rqb22vO0LKsqQMyHeoyYBfC4EBbR5Fx4w0ewkob5S a6EzfYUMMKHB9obDrhOifVLSTtbhCDM= X-Google-Smtp-Source: AMsMyM4gLayulwBKZ3oV/w4C4GfrZsOmtMMi5W8Of0+00e76+GDcK9bgiaaGE+JcXnb5caWk4vJrHg== X-Received: by 2002:a17:906:cc14:b0:78d:2267:3a57 with SMTP id ml20-20020a170906cc1400b0078d22673a57mr596908ejb.241.1664992114759; Wed, 05 Oct 2022 10:48:34 -0700 (PDT) From: Oleksandr Tyshchenko To: xen-devel@lists.xenproject.org, linux-kernel@vger.kernel.org Cc: Oleksandr Tyshchenko , Stefano Stabellini , Juergen Gross Subject: [PATCH 2/2] xen/virtio: Fix potential deadlock when accessing xen_grant_dma_devices Date: Wed, 5 Oct 2022 20:48:23 +0300 Message-Id: <20221005174823.1800761-3-olekstysh@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221005174823.1800761-1-olekstysh@gmail.com> References: <20221005174823.1800761-1-olekstysh@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1664992152197100001 Content-Type: text/plain; charset="utf-8" From: Oleksandr Tyshchenko As find_xen_grant_dma_data() is called from both interrupt and process contexts, the access to xen_grant_dma_devices XArray must be protected by xa_lock_irqsave to avoid deadlock scenario. As XArray API doesn't provide xa_store_irqsave helper, call lockless __xa_store directly and guard it externally. Also move the storage of the XArray's entry to a separate helper. Signed-off-by: Oleksandr Tyshchenko Fixes: d6aca3504c7d ("xen/grant-dma-ops: Add option to restrict memory acce= ss under Xen") Reviewed-by: Juergen Gross Reviewed-by: Stefano Stabellini --- drivers/xen/grant-dma-ops.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/drivers/xen/grant-dma-ops.c b/drivers/xen/grant-dma-ops.c index 1998d0e8ce82..c66f56d24013 100644 --- a/drivers/xen/grant-dma-ops.c +++ b/drivers/xen/grant-dma-ops.c @@ -25,7 +25,7 @@ struct xen_grant_dma_data { bool broken; }; =20 -static DEFINE_XARRAY(xen_grant_dma_devices); +static DEFINE_XARRAY_FLAGS(xen_grant_dma_devices, XA_FLAGS_LOCK_IRQ); =20 #define XEN_GRANT_DMA_ADDR_OFF (1ULL << 63) =20 @@ -42,14 +42,29 @@ static inline grant_ref_t dma_to_grant(dma_addr_t dma) static struct xen_grant_dma_data *find_xen_grant_dma_data(struct device *d= ev) { struct xen_grant_dma_data *data; + unsigned long flags; =20 - xa_lock(&xen_grant_dma_devices); + xa_lock_irqsave(&xen_grant_dma_devices, flags); data =3D xa_load(&xen_grant_dma_devices, (unsigned long)dev); - xa_unlock(&xen_grant_dma_devices); + xa_unlock_irqrestore(&xen_grant_dma_devices, flags); =20 return data; } =20 +static int store_xen_grant_dma_data(struct device *dev, + struct xen_grant_dma_data *data) +{ + unsigned long flags; + int ret; + + xa_lock_irqsave(&xen_grant_dma_devices, flags); + ret =3D xa_err(__xa_store(&xen_grant_dma_devices, (unsigned long)dev, dat= a, + GFP_ATOMIC)); + xa_unlock_irqrestore(&xen_grant_dma_devices, flags); + + return ret; +} + /* * DMA ops for Xen frontends (e.g. virtio). * @@ -338,8 +353,7 @@ void xen_grant_setup_dma_ops(struct device *dev) */ data->backend_domid =3D iommu_spec.args[0]; =20 - if (xa_err(xa_store(&xen_grant_dma_devices, (unsigned long)dev, data, - GFP_KERNEL))) { + if (store_xen_grant_dma_data(dev, data)) { dev_err(dev, "Cannot store Xen grant DMA data\n"); goto err; } --=20 2.25.1