From patchwork Mon Feb 5 19:01:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Will Deacon X-Patchwork-Id: 196992 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:168b:b0:106:860b:bbdd with SMTP id ma11csp1079963dyb; Mon, 5 Feb 2024 11:03:07 -0800 (PST) X-Google-Smtp-Source: AGHT+IEfAHiwRHIykY48o8vbwgGtkrESw8TTJUUN9sqo9CjV7y81Ky/o5Uiu0kHyhWoqojvdS3Sv X-Received: by 2002:a05:6214:2424:b0:68c:a6e7:5dfc with SMTP id gy4-20020a056214242400b0068ca6e75dfcmr5688164qvb.31.1707159787643; Mon, 05 Feb 2024 11:03:07 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707159787; cv=pass; d=google.com; s=arc-20160816; b=KkUXx/Jp4s0QJzfVP26Fl0TtMZ9ecWUZ6+t7e6frsY5sdmItNY2dT1pkhghukjI4tx jin59VqiOUShW47bexuU7onqO1x+phxXnQn+mJMnc6P91JbIR09FtjhuxwYLIsbFczfD 4aYe7aTrju8AgDHHgxxphdGjkFfkd2rcEBr/78mitZ5ZkxNIUopEBcVkaCS7YXACLzQU DJsM16XllwusWbob+DlGaiYmBBj2iaeT4wUt9WdxNQmZcrwppTyPhZiGNs09w99SlgPn eOLjvG9PEQRxY0YnkkD2H/f5m3yYredWoJqPjuqYurZ5gVzkXaLmQUOXR7hKxUuwPXOG tWvw== 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=dbzfMS+PxbVaT4LlHheKXW8CsHPbfGvifjaxiKEZrUg=; b=dWfTOwWO+5O4SdCARxPZVszWPnCJehppAcKuHoK+5qFfyNRVYzCJzLR/zp6bHT/xlr /y9Fq/ZuBX6I/LLyOBF9VnYdX0QeMjjbZDZiFypyAyoNNA+/OwzrXrqKQex/NlatM7pv 2Z0DSFtuPATO55y26sKK3nebd6YVqmAsT4tlakR+00l1E0O8fR1g/LyDgFeSJVWp3CfN bxr87arPzDqtH0fWHR82frZ8uNM3wXrsr8TjyTT8Apqz+aW/em6hobpS7m6fEkkleyRM SEEQKV8RNgCmbo7WpcqGyF93i2uu94W+KQC6RCdlZgaH5DxU56humFpSc8pIaL/gwUvm 7uBg==; 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-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-53275-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Forwarded-Encrypted: i=1; AJvYcCU0zGcwW/gIAGvbjhayiEtP2Wf/6kHPaK0CTLIo1DcabklbPrFsEdy0i/9lgJtacCgV+SDhfNoYldi+r0SXz12nx3o3DA== Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id el18-20020ad459d2000000b0068c83271adcsi531475qvb.228.2024.02.05.11.03.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 11:03:07 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-53275-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; 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-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-53275-ouuuleilei=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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 3CA041C24340 for ; Mon, 5 Feb 2024 19:03:07 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6B9954D5BF; Mon, 5 Feb 2024 19:01:44 +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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790086781686975610 X-GMAIL-MSGID: 1790086781686975610 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 Reviewed-by: Petr Tesarik --- 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)); }