Received: by 2002:a05:7412:bbc7:b0:fc:a2b0:25d7 with SMTP id kh7csp2599652rdb; Mon, 5 Feb 2024 11:21:51 -0800 (PST) X-Google-Smtp-Source: AGHT+IFLvJOltyQkvbIwzHqJJ4vbVjFMmRhfz6lPvPvRhHNARm/a2kAK5OI+44pw2hlX9k83o4P/ X-Received: by 2002:a05:6a00:4b13:b0:6dd:c23c:ea4f with SMTP id kq19-20020a056a004b1300b006ddc23cea4fmr9230886pfb.5.1707160910799; Mon, 05 Feb 2024 11:21:50 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707160910; cv=pass; d=google.com; s=arc-20160816; b=X6R7ZGlveEMnsukIFAdzH7EL8cGgjfruwcTNKNxO6xUNr4JhlCTf0RR28trjLEFTe/ amGO1anv4FQSnT167EzNjfPFPMVy2xs9BfkIhuz5XDCqRkxj6VwIRqgGtKdWmNfbZYyg H5sK9+mKZo50f3oiipfMRDA1t5g5Fy5Wp1rqyiGTeHfXNsFliv/bS0IpRrxpFGQDzGlQ 3MDL4Hw02vCxLQTHD9ysxNPx01V/43KjHTPpRSrYhRzgs+Ngcw1QifGSMgVblyJMOlSx l3JPckEYz7wRpPf7M4XZhg6JJ0uA/2OrMej2tR/C2nypGWcZnSd+4R65u4foxDXusx+d fhUA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=GRPv3lA+N9l1B9AL3Rf0jB62dNvadY7vTk/wi4j1b3Q=; fh=OGDGRmfIGq/VyJ62AKfsEj7uHgsuCTpjw2lXUJ47L68=; b=RKO7vL1gps5LaKD6igAnE7LHK7A/H6siFYQbKDMfeXZOI/ti+g0dJy3VbKoAFix2Q8 jP5FdKXrCD7b4gWFwbaI6KFRAOye3fsl4UWoYziGtZMSY4iK7bmUqn0INY0mfmZpOx8R 2w4SDHgJ+yHgfBtg9LfmuJf1wfGMBzLyShGW9yS0kh9sfEgr6UqhXY0cV4mVU8a8MTK2 OuOAPbGFvvdCJsiYRv5+K4L2VZ2IFhKVHFpMjCkXxw4N4gmhbXRdAsjtuO0BV3mQWfbl dw58jwt3EAhAOupetGyqW25EFdike2RIRsnTBDlkh/vOW/+c6lAxD1rLEE2MFkU5QbKn 85FQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=GkLGdqbt; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-53275-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-53275-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Forwarded-Encrypted: i=1; AJvYcCViWGnJ6vRr1MgTCVC2dNqg6jqwDRYC/y64HD9Pxeb0y97YocA8NWo4KGyg107g4OXuejngECXLk7OW55wughs4oO2oPyFje0IhegzMrA== Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id lp12-20020a056a003d4c00b006e04030ef0dsi231784pfb.195.2024.02.05.11.21.50 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 11:21:50 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-53275-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=GkLGdqbt; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-53275-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-53275-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id C5CABB26D87 for ; Mon, 5 Feb 2024 19:02:53 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 987EB4CE17; Mon, 5 Feb 2024 19:01:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="GkLGdqbt" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A3B074BA96; Mon, 5 Feb 2024 19:01:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707159701; cv=none; b=kx56FlHZKSe4uRgzTJ988OG0o4roD/27K4Q5AWgJxMLtnY0Yc9OarIuP5MUw7o++ISqQxq+UQRy6JtvEK4yIK+w+ZUtPerU6wQ4DBpcAQCNpLG8GFjn5unF9e4idV88UV/6UX65V952t3mbf/6hLmht0l74L3FDrTITIjK8HTNc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707159701; c=relaxed/simple; bh=Xm+zNHaI1L6yDroz8J/2SUT0P9yQzO240Q33Rj/NF8c=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Xwt8at4/6Rz9UNBXi2kixRqCskCGhp/X1zWjqo2MPgZZhK1ne1dckibKeCKqu0GWmcMqlTC/akhSD160ctkXQ7qoHE5jrnT2AAYDCf9gorT8hSK4AthGsMeykiCY480p8c16l4HzAsy9qU0TBR/NfS9Z3QaDNUeRKt9jiVFXgho= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=GkLGdqbt; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 50CA9C43390; Mon, 5 Feb 2024 19:01:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1707159701; bh=Xm+zNHaI1L6yDroz8J/2SUT0P9yQzO240Q33Rj/NF8c=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GkLGdqbtVBK2Cn0h+xSZ/PXnBH9ZzuzbPxDZyJevoa4KTz2c2JKWo1J2L05OMfoS4 yE2WSQWMmIr+86D8xLTHJrok/gyrBAGAuVSDbJfev3T4Mcxq4ey++Ii6xYQfBWJgJ7 ppbmSp9LecvqGB8aOHD4Iz2LQZOpnoPbcFjjgh6dZfFO0sOan7u5OnFvMFCnrgLcKj L20sMxRTnQakMyFSO7MkjdfUERnK3w63JicuVJcWO3zHLWTmH1pK4/uc6K2KZJGaLJ 7XHeeFFe0RO8KFvhdwCa3HHZWuL8rJMcM8DBSsgLZqmZ4dYC9A5VTsYUJzHrJfd78Y x7YJ6seoe6whw== From: Will Deacon To: linux-kernel@vger.kernel.org Cc: kernel-team@android.com, Will Deacon , iommu@lists.linux.dev, Christoph Hellwig , Marek Szyprowski , Robin Murphy , Petr Tesarik , Dexuan Cui Subject: [PATCH v3 2/3] swiotlb: Enforce page alignment in swiotlb_alloc() Date: Mon, 5 Feb 2024 19:01:26 +0000 Message-Id: <20240205190127.20685-3-will@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20240205190127.20685-1-will@kernel.org> References: <20240205190127.20685-1-will@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit When allocating pages from a restricted DMA pool in swiotlb_alloc(), the buffer address is blindly converted to a 'struct page *' that is returned to the caller. In the unlikely event of an allocation bug, page-unaligned addresses are not detected and slots can silently be double-allocated. Add a simple check of the buffer alignment in swiotlb_alloc() to make debugging a little easier if something has gone wonky. Cc: Christoph Hellwig Cc: Marek Szyprowski Cc: Robin Murphy Cc: Petr Tesarik Cc: Dexuan Cui Signed-off-by: Will Deacon --- kernel/dma/swiotlb.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c index 9ff909a0039a..adbb3143238b 100644 --- a/kernel/dma/swiotlb.c +++ b/kernel/dma/swiotlb.c @@ -1643,6 +1643,12 @@ struct page *swiotlb_alloc(struct device *dev, size_t size) return NULL; tlb_addr = slot_addr(pool->start, index); + if (unlikely(!PAGE_ALIGNED(tlb_addr))) { + dev_WARN_ONCE(dev, 1, "Cannot allocate pages from non page-aligned swiotlb addr 0x%pa.\n", + &tlb_addr); + swiotlb_release_slots(dev, tlb_addr); + return NULL; + } return pfn_to_page(PFN_DOWN(tlb_addr)); } -- 2.43.0.594.gd9cf4e227d-goog