From patchwork Wed Oct 5 17:48:22 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oleksandr Tyshchenko X-Patchwork-Id: 12999530 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 lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 8E854C433F5 for ; Wed, 5 Oct 2022 17:48:50 +0000 (UTC) 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 X-Outflank-Mailman: Message body and most headers restored to incoming version 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-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 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 access under Xen") Reviewed-by: Stefano Stabellini Reviewed-by: Juergen Gross --- 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 = PFN_UP(size); + unsigned int i, n_pages = PFN_UP(offset + size); grant_ref_t grant; dma_addr_t dma_handle; @@ -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 = PFN_UP(size); + unsigned long offset = dma_handle & (PAGE_SIZE - 1); + unsigned int i, n_pages = PFN_UP(offset + size); grant_ref_t grant; if (WARN_ON(dir == DMA_NONE)) From patchwork Wed Oct 5 17:48:23 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oleksandr Tyshchenko X-Patchwork-Id: 12999529 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 lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 6183DC433FE for ; Wed, 5 Oct 2022 17:48:50 +0000 (UTC) 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 X-Outflank-Mailman: Message body and most headers restored to incoming version 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-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 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 access under Xen") Reviewed-by: Stefano Stabellini Reviewed-by: Juergen Gross --- 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; }; -static DEFINE_XARRAY(xen_grant_dma_devices); +static DEFINE_XARRAY_FLAGS(xen_grant_dma_devices, XA_FLAGS_LOCK_IRQ); #define XEN_GRANT_DMA_ADDR_OFF (1ULL << 63) @@ -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 *dev) { struct xen_grant_dma_data *data; + unsigned long flags; - xa_lock(&xen_grant_dma_devices); + xa_lock_irqsave(&xen_grant_dma_devices, flags); data = xa_load(&xen_grant_dma_devices, (unsigned long)dev); - xa_unlock(&xen_grant_dma_devices); + xa_unlock_irqrestore(&xen_grant_dma_devices, flags); return data; } +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 = xa_err(__xa_store(&xen_grant_dma_devices, (unsigned long)dev, data, + 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 = iommu_spec.args[0]; - 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; }