Received: by 2002:a05:7412:d1aa:b0:fc:a2b0:25d7 with SMTP id ba42csp600536rdb; Mon, 29 Jan 2024 11:49:54 -0800 (PST) X-Google-Smtp-Source: AGHT+IHl8Hypn7tka87S+YGQdr2LTGcaqsCxPelsSY5fw3SlrFLVXWYG+5mwoi8wnSlOPSRCgXY+ X-Received: by 2002:a05:6359:411b:b0:175:6fff:d6a0 with SMTP id kh27-20020a056359411b00b001756fffd6a0mr2630168rwc.1.1706557794024; Mon, 29 Jan 2024 11:49:54 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706557793; cv=pass; d=google.com; s=arc-20160816; b=wRqx0RTjQvzINzA/he0K3KMvti9Kk5Q0Hk0ylL1tv6Q364iNw0lXfwfR0UilaNGemj PN8NJSJJZTja7APlrh6B1/JtxBJoU9wxatzpDsCJCfdkOy9SPFQuVJUavcr8TNmuFOe5 P8z1KeCSAbSb1WPl7W3G4SArg8vL2Rc4tDOh0eMmztdbImSPxar6dDy57gJf3eY8f/tg raMOoYqT3VTSnWk+/oCe3C7UzUEPGk8LEEyHZPa3i/jj+tLdLafqE2byRRvO12STZ6l4 UOxI40y6HTrPzWoabM9IeT8MGO41mVhizdxXxerqKqlT1kDfKrlqD08Dv9OgfdTViksV 4vCw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=user-agent:in-reply-to:content-disposition:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:references :message-id:subject:cc:to:from:date:dkim-signature; bh=74Fmti8btLDrXYNkdVdXt397otFu9uFbX1ElM8CUN4Q=; fh=0cfoGa3/aGv4cIp2xhZ6EsEF9g2Qw5hwrL/jIqwrhCc=; b=yxDsWt5bN/IqKohzWCd2bGIcEQRxcIm9TtMJCNYm9wfIl7QFT6szkgOeV8FqxCI/e3 jBBtx6wiiSjPh2tce/QPAJk433309DmTQcMz2UV3x94npGRLRdwXbHtCcOS+/0JmXSWz vXx+VUcBNH/nKfsTPgRjMn4BQ1rAz8Aszg/YNjsMHD6R8/MxUZ9r9I+9UNZ2VSlIdjE3 045pAknoYXrQJ/0ZSNcn5oBmW5D8xHoLzGCEfbsBBn8h7j0bz4757H3QuUTO9eHwCgMh nLZrm4y1QJWxXqHjJ9hw9l8U+HT19PJVk7FgqQ2dzl+VTOrc217WQcwEPHK64Gp9eFQ0 NJ3w== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=MdoABHmG; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-43427-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-43427-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id r29-20020a638f5d000000b005d8bb95ce75si3826334pgn.197.2024.01.29.11.49.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Jan 2024 11:49:53 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-43427-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=MdoABHmG; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-43427-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-43427-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id A71DD286C32 for ; Mon, 29 Jan 2024 19:49:53 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 91E5B6F083; Mon, 29 Jan 2024 19:49:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="MdoABHmG" 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 B7D394C619; Mon, 29 Jan 2024 19:49:45 +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=1706557785; cv=none; b=fmxmwQfoaSHcei2/lptMUXC+7VVMlEOZ35eCkcK3/CzoQA4ZevOf+hsatyH/OGqCSfv/MN50iyhigAiv1xYlpjUj9JNH9qnZoab+RAc0LwCxEOTQHShf9Vf4ii2p2xB1NdrOoZTB/ZcuzsxHfV/zO/VvLYAHURKP5oQhatndE9k= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706557785; c=relaxed/simple; bh=AFzIbjcOWSZos8y3SLuij19ce0bzY428EYdt2/6+Cpk=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=kr2tuD0PxyqqFZsueDqmsoF4tKwX3yfwZIHLLpC/cdTB7Q/nP3LsdJAsQk9n/GSi35ZU8RGm/r8fO5My3eImdthRrXfs7AH879ns5Lo97mviH2W6SoIjfCuhoicwWToGQMU9TpLx7miVB18tz0Cy/SM5O1NbwqhBoiX4avoNUf0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=MdoABHmG; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8AB93C43390; Mon, 29 Jan 2024 19:49:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1706557785; bh=AFzIbjcOWSZos8y3SLuij19ce0bzY428EYdt2/6+Cpk=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=MdoABHmGOXwnshWW4D5hELpR9s7yY69FTCmInEgq8suc3c2l3EGmjnVZhTLznkrFx DEvb8GBmww1NXRcZN7+xt/O0ajuY7Rwe4BxhBGrR9CP2+zOhOQ7lZ2bSldtEj/nH0O 9orUw5tnHHJ3cL1UaPLATYfmH61xKusG207ogj3PmR7mMhYyavCqTr41oA0pHT9LiT SsDkQs9YGBEbooHTLRlaLjOpjKvQlzqejAhx8Om4NCXo2oJ7FUf4dUg/TJ9MMRCCTe RXorT9zg3rtejrUbaKWZvqgS4vzb0VjkKpfD1/2FN/G7XAN2b/5Vmzg/9inW7o5H3H YOh3i2Z5ic7gA== Date: Mon, 29 Jan 2024 19:49:40 +0000 From: Will Deacon To: Christoph Hellwig Cc: linux-kernel@vger.kernel.org, kernel-team@android.com, iommu@lists.linux.dev, Marek Szyprowski , Robin Murphy , Petr Tesarik , Dexuan Cui Subject: Re: [PATCH 2/2] swiotlb: Enforce page alignment in swiotlb_alloc() Message-ID: <20240129194939.GE12631@willie-the-truck> References: <20240126151956.10014-1-will@kernel.org> <20240126151956.10014-3-will@kernel.org> <20240129060853.GB19258@lst.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240129060853.GB19258@lst.de> User-Agent: Mutt/1.10.1 (2018-07-13) On Mon, Jan 29, 2024 at 07:08:53AM +0100, Christoph Hellwig wrote: > On Fri, Jan 26, 2024 at 03:19:56PM +0000, Will Deacon wrote: > > diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c > > index 25febb9e670c..92433ea9f2d2 100644 > > --- a/kernel/dma/swiotlb.c > > +++ b/kernel/dma/swiotlb.c > > @@ -1647,6 +1647,12 @@ struct page *swiotlb_alloc(struct device *dev, size_t size) > > return NULL; > > > > tlb_addr = slot_addr(pool->start, index); > > + if (!PAGE_ALIGNED(tlb_addr)) { > > + dev_WARN_ONCE(dev, 1, "Cannot return 'struct page *' for 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)); > > So PFN_DOWN aligns the address and thus per se converting the unaligned > address isn't a problem. Hmm, I'm not sure I follow why it isn't a problem. If the first 2KiB slot of the 4KiB page has already been allocated to somebody else, isn't it a big problem to align down like that? Maybe I should word the warning message a bit better -- how about: "Cannot allocate pages from non page-aligned swiotlb addr 0x%pa.\n" ? > That being said swiotlb obviously should never > allocate unaligned addresses, but the placement of this check feels > odd to me. Also because it only catches swiotlb_alloc and not the > map side. > > Maybe just throw a WARN_ON_ONCE into slot_addr() ? Everything is slot-aligned, so I don't think slot_addr() can detect this. I put the check in swiotlb_alloc() because I think that's the only place where we assume that a slot address is page-aligned. I don't think the map path particularly cares, but if you prefer to have the warning there too then I think we'd have to stick it at the end of swiotlb_search_pool_area() (effectively just checking that the returned slot is consistent with the 'alloc_align_mask' parameter). Will