Received: by 2002:a05:7412:3b8b:b0:fc:a2b0:25d7 with SMTP id nd11csp932822rdb; Fri, 9 Feb 2024 05:44:50 -0800 (PST) X-Google-Smtp-Source: AGHT+IHqcKVmYPvpPM/nFEA93t6MLEdgQmN6spjqZJpQ0Thggk+EMidYEQbHopavAnPo6bXSHJPB X-Received: by 2002:a05:622a:1893:b0:42b:eba5:ddfa with SMTP id v19-20020a05622a189300b0042beba5ddfamr2006580qtc.45.1707486290563; Fri, 09 Feb 2024 05:44:50 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707486290; cv=pass; d=google.com; s=arc-20160816; b=rB2jnRcJF3YLXFdR+esdXiAii+qnirvjOw+mYJq0XT3P9t/K4AD8zEIEp52CMZjCbe ZeKm7RPYTHUKS8wADr5r/IigW7YalW52XR8hk1nmbPWP5fxQguStay7kMlSwe+mbLQzO 5gotK8czbTRXNwOeisx9jDrjIQ+FkJkpzfLcHu1SfzykftzsH5lcJtCL8R+sPqm/nmRR 9kw5OQgqL8z2QlYQttSA/URmxvgMk03UT72ZZ+OOy/r+atGYsBOJJ5LCkDF7KMWfguVA WhGMOpMPVXPMHw6HKdY71h9o8QCUJSYRx3mUz2zH8tEAD95afQ2oR2haz+xiyamNB/GH lTLQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id; bh=D7vY47pTXnGid8jsuYq3/+j1OEV/uxlcoMSDDlpxr24=; fh=WUn9es0LGrUwRfiyYO31d7NLeYaFi3l6kvTgj02t7VM=; b=VQfyQUNCH2kvg0PK7IDY1QBbr//fR3n9KxqLZw2RCeEUdI09HEcouG+v7LqKwAIoO5 gjl/K0cgF/QyzCd24+rfMl4eCMJUvjpWyqzFaXd8vLVzTqrnOsQ7Wkurbao4BLwshqwj w3zbURtJqL4mYNoJWsh/DApeDY/C7JKpbCQZSEIGecJE8in6iV0JVwlFmMkqg5RVFTsp 8ZzslwufEjTfcQiNz8kF5+7XL6xMTaa5IRE350QSWwNyg0rD9mD1YYRyioq3Sc0Q898s YWT16YNZXSVT9bWv60jRGcMInMjgNkoB5wc8CSowlAaMXwRuoufBRDDxFl/nvBTfHpgi BUxg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-59380-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-59380-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com X-Forwarded-Encrypted: i=2; AJvYcCWFxdWlYyWDxDbNOaR7bNClBGeuVbnwL4IULTmTz3S0OXLHnUzWjnpdR2B7cQwlIww2leVZjDuthHt7ti3Vrf2NGUh8Q6CRirIErUKWYw== Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id g3-20020ac85803000000b0042c56d2f0a3si1942381qtg.140.2024.02.09.05.44.50 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Feb 2024 05:44:50 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-59380-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-59380-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-59380-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com 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 448411C227AA for ; Fri, 9 Feb 2024 13:44:50 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CF9B33C48A; Fri, 9 Feb 2024 13:44:33 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9163C1E487; Fri, 9 Feb 2024 13:44:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707486273; cv=none; b=rOzSdRK74HWJATeBOOsmLIRQibFEhUQqG5AWZLhcfy6x0AvhSM1R8f07LUnNGoXQOWhIB1u+9+iogqcPn9o9rxbt0JzOjEHlY7iRsqzqrE+gnDnWHeydw+4GnnY3JLlVsyfgHnJsbR3Z3pUMlLrdghFMFdaY1Nid6mFqnCSKnMI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707486273; c=relaxed/simple; bh=IgoMaZM9kw0Wpb/KvMD5/2YbdK7ysD6H6KO4z/h6t3c=; h=Message-ID:Date:MIME-Version:Subject:To:References:From: In-Reply-To:Content-Type; b=GRwhBcmNdbztbLvfU4ZBAvyrnWm9cP5u4UEQPreprYqaBW+cgY+zOqdsjzj9AcFQAyCiQoj3omwYDhuxhpdOS3P8/lCGKFogKNLE4K4pfeNY7pGrzVapY1BLHIMgAZfYrMdEa8cms1ab+/3Yn7L2VOFOCPpqNBKac98e442OQCw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id B9D9BDA7; Fri, 9 Feb 2024 05:45:09 -0800 (PST) Received: from [10.57.47.119] (unknown [10.57.47.119]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 6B0283F762; Fri, 9 Feb 2024 05:44:21 -0800 (PST) Message-ID: <8ce2cd7b-7702-45aa-b4c8-25a01c27ed83@arm.com> Date: Fri, 9 Feb 2024 13:44:20 +0000 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v4 01/10] iommu/vt-d: add wrapper functions for page allocations Content-Language: en-GB To: Pasha Tatashin , akpm@linux-foundation.org, alim.akhtar@samsung.com, alyssa@rosenzweig.io, asahi@lists.linux.dev, baolu.lu@linux.intel.com, bhelgaas@google.com, cgroups@vger.kernel.org, corbet@lwn.net, david@redhat.com, dwmw2@infradead.org, hannes@cmpxchg.org, heiko@sntech.de, iommu@lists.linux.dev, jernej.skrabec@gmail.com, jonathanh@nvidia.com, joro@8bytes.org, krzysztof.kozlowski@linaro.org, linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-rockchip@lists.infradead.org, linux-samsung-soc@vger.kernel.org, linux-sunxi@lists.linux.dev, linux-tegra@vger.kernel.org, lizefan.x@bytedance.com, marcan@marcan.st, mhiramat@kernel.org, m.szyprowski@samsung.com, paulmck@kernel.org, rdunlap@infradead.org, samuel@sholland.org, suravee.suthikulpanit@amd.com, sven@svenpeter.dev, thierry.reding@gmail.com, tj@kernel.org, tomas.mudrunka@gmail.com, vdumpa@nvidia.com, wens@csie.org, will@kernel.org, yu-cheng.yu@intel.com, rientjes@google.com, bagasdotme@gmail.com, mkoutny@suse.com References: <20240207174102.1486130-1-pasha.tatashin@soleen.com> <20240207174102.1486130-2-pasha.tatashin@soleen.com> From: Robin Murphy In-Reply-To: <20240207174102.1486130-2-pasha.tatashin@soleen.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 2024-02-07 5:40 pm, Pasha Tatashin wrote: [...]> diff --git a/drivers/iommu/iommu-pages.h b/drivers/iommu/iommu-pages.h > new file mode 100644 > index 000000000000..c412d0aaa399 > --- /dev/null > +++ b/drivers/iommu/iommu-pages.h > @@ -0,0 +1,204 @@ > +/* SPDX-License-Identifier: GPL-2.0-only */ > +/* > + * Copyright (c) 2024, Google LLC. > + * Pasha Tatashin > + */ > + > +#ifndef __IOMMU_PAGES_H > +#define __IOMMU_PAGES_H > + > +#include > +#include > +#include > + > +/* > + * All page allocation that are performed in the IOMMU subsystem must use one of "All page allocations" is too broad; As before, this is only about pagetable allocations, or I guess for the full nuance, allocations of pagetables and other per-iommu_domain configuration structures which are reasonable to report as "pagetables" to userspace. > + * the functions below. This is necessary for the proper accounting as IOMMU > + * state can be rather large, i.e. multiple gigabytes in size. > + */ > + > +/** > + * __iommu_alloc_pages_node - allocate a zeroed page of a given order from > + * specific NUMA node. > + * @nid: memory NUMA node id > + * @gfp: buddy allocator flags > + * @order: page order > + * > + * returns the head struct page of the allocated page. > + */ > +static inline struct page *__iommu_alloc_pages_node(int nid, gfp_t gfp, > + int order) > +{ > + struct page *page; > + > + page = alloc_pages_node(nid, gfp | __GFP_ZERO, order); > + if (unlikely(!page)) > + return NULL; > + > + return page; > +} All 3 invocations of this only use the returned struct page to trivially derive page_address(), so we really don't need it; just clean up these callsites a bit more. > + > +/** > + * __iommu_alloc_pages - allocate a zeroed page of a given order. > + * @gfp: buddy allocator flags > + * @order: page order > + * > + * returns the head struct page of the allocated page. > + */ > +static inline struct page *__iommu_alloc_pages(gfp_t gfp, int order) > +{ > + struct page *page; > + > + page = alloc_pages(gfp | __GFP_ZERO, order); > + if (unlikely(!page)) > + return NULL; > + > + return page; > +} Same for the single invocation of this one. > + > +/** > + * __iommu_alloc_page_node - allocate a zeroed page at specific NUMA node. > + * @nid: memory NUMA node id > + * @gfp: buddy allocator flags > + * > + * returns the struct page of the allocated page. > + */ > +static inline struct page *__iommu_alloc_page_node(int nid, gfp_t gfp) > +{ > + return __iommu_alloc_pages_node(nid, gfp, 0); > +} There are no users of this at all. > + > +/** > + * __iommu_alloc_page - allocate a zeroed page > + * @gfp: buddy allocator flags > + * > + * returns the struct page of the allocated page. > + */ > +static inline struct page *__iommu_alloc_page(gfp_t gfp) > +{ > + return __iommu_alloc_pages(gfp, 0); > +} > + > +/** > + * __iommu_free_pages - free page of a given order > + * @page: head struct page of the page > + * @order: page order > + */ > +static inline void __iommu_free_pages(struct page *page, int order) > +{ > + if (!page) > + return; > + > + __free_pages(page, order); > +} > + > +/** > + * __iommu_free_page - free page > + * @page: struct page of the page > + */ > +static inline void __iommu_free_page(struct page *page) > +{ > + __iommu_free_pages(page, 0); > +} Beyond one more trivial Intel cleanup for __iommu_alloc_pages(), these 3 are then only used by tegra-smmu, so honestly I'd be inclined to just open-code there page_address()/virt_to_page() conversions as appropriate there (once again I think the whole thing could in fact be refactored to not use struct pages at all because all it's ever ultimately doing with them is page_address(), but that would be a bigger job so definitely out-of-scope for this series). > + > +/** > + * iommu_alloc_pages_node - allocate a zeroed page of a given order from > + * specific NUMA node. > + * @nid: memory NUMA node id > + * @gfp: buddy allocator flags > + * @order: page order > + * > + * returns the virtual address of the allocated page > + */ > +static inline void *iommu_alloc_pages_node(int nid, gfp_t gfp, int order) > +{ > + struct page *page = __iommu_alloc_pages_node(nid, gfp, order); > + > + if (unlikely(!page)) > + return NULL; As a general point I'd prefer to fold these checks into the accounting function itself rather than repeat them all over. > + > + return page_address(page); > +} > + > +/** > + * iommu_alloc_pages - allocate a zeroed page of a given order > + * @gfp: buddy allocator flags > + * @order: page order > + * > + * returns the virtual address of the allocated page > + */ > +static inline void *iommu_alloc_pages(gfp_t gfp, int order) > +{ > + struct page *page = __iommu_alloc_pages(gfp, order); > + > + if (unlikely(!page)) > + return NULL; > + > + return page_address(page); > +} > + > +/** > + * iommu_alloc_page_node - allocate a zeroed page at specific NUMA node. > + * @nid: memory NUMA node id > + * @gfp: buddy allocator flags > + * > + * returns the virtual address of the allocated page > + */ > +static inline void *iommu_alloc_page_node(int nid, gfp_t gfp) > +{ > + return iommu_alloc_pages_node(nid, gfp, 0); > +} TBH I'm not entirely convinced that saving 4 characters per invocation times 11 invocations makes this wrapper worthwhile :/ > + > +/** > + * iommu_alloc_page - allocate a zeroed page > + * @gfp: buddy allocator flags > + * > + * returns the virtual address of the allocated page > + */ > +static inline void *iommu_alloc_page(gfp_t gfp) > +{ > + return iommu_alloc_pages(gfp, 0); > +} > + > +/** > + * iommu_free_pages - free page of a given order > + * @virt: virtual address of the page to be freed. > + * @order: page order > + */ > +static inline void iommu_free_pages(void *virt, int order) > +{ > + if (!virt) > + return; > + > + __iommu_free_pages(virt_to_page(virt), order); > +} > + > +/** > + * iommu_free_page - free page > + * @virt: virtual address of the page to be freed. > + */ > +static inline void iommu_free_page(void *virt) > +{ > + iommu_free_pages(virt, 0); > +} > + > +/** > + * iommu_free_pages_list - free a list of pages. > + * @page: the head of the lru list to be freed. > + * > + * There are no locking requirement for these pages, as they are going to be > + * put on a free list as soon as refcount reaches 0. Pages are put on this LRU > + * list once they are removed from the IOMMU page tables. However, they can > + * still be access through debugfs. > + */ > +static inline void iommu_free_pages_list(struct list_head *page) Nit: I'd be inclined to call this iommu_put_pages_list for consistency. > +{ > + while (!list_empty(page)) { > + struct page *p = list_entry(page->prev, struct page, lru); > + > + list_del(&p->lru); > + put_page(p); > + } > +} I realise now you've also missed the common freelist freeing sites in iommu-dma. Thanks, Robin. > + > +#endif /* __IOMMU_PAGES_H */