From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AA562364EB8 for ; Fri, 20 Mar 2026 09:28:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.171 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773998920; cv=none; b=RE1CNl79oVcANPttISAAtiMO4+fhWKDLNvxm85df8aiWG+dj+Ou0R1vKK9p77eGX8N5n6eBdO8xMufH78Repl5WjKKkf3z7VtWHntLwXJ5aW5+ndIbNA0aymj53GwjmZNaEqFHLNEELcFIhIIxz7x8nHnNNCcuP0qHuQVsA/2vo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773998920; c=relaxed/simple; bh=DpDCTJXegbg2cBFxSiSI814EG9RKRIOvAWoZV93niFU=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=WBCpPOEV7fxLZZKJ8xAoSsqk7GqOTdeN8pAU/vK8awJWHyCHAFBhqg86RPSNH36OG/DQUR+Iqrl6dvLFxHw93DwkNXK1FL2KSkO6EiuiUed+GZq1WCS38tdLb07Hwwat21rOC0pSr5oD14LPWVKlhHzHabLXFCySPxBins1Bd5Y= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=stKCl6rt; arc=none smtp.client-ip=209.85.214.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="stKCl6rt" Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-2b052ec7176so50805ad.1 for ; Fri, 20 Mar 2026 02:28:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1773998918; x=1774603718; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=lfFJWDO2bwhUd92Uo4pePPcCXizTvYm07yIGqhjNLlw=; b=stKCl6rtLlvskQXpExVM++DwkyN1PvWuVL+1kxBaAtGR5yJYxUTaAYUbYQJWgRRGVa IskKI+w7AP3bNzrTHp+k0sYX8Uou+/XoW/mB/tL+tjA4MPVJJ7ItexeeBWekFqvNWS8p QGv+t6OwWzLkkwPelu7RHnCGdrG1LCJpEHTr2kHHttOtwkbTm0GlFv9z1t/bBwNapp+Z yCveOvSFCHHJ83/+NtMtl9F2MgFJM81uKn8iREx7200iTFzXfYFxAw/VaLDwDtLE8l+k xg6c0Yx0U5Q4E9OFRcFZasrGkccowkJ949FCFVTA7AYB6TfOY9e/JZpjxrYZs4XEaOMM YWZg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773998918; x=1774603718; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=lfFJWDO2bwhUd92Uo4pePPcCXizTvYm07yIGqhjNLlw=; b=ZECP10m42vJKCLPScLQswoFdhJ3QYCftupzZVjDeFSxSD0j8QHO/G1XAqkiwqgohpK Cc884JPjhbpJgol81VcdSZs7AOLL/vn8oiNr+5hwneFn9bJmYjYjlC6TcFzYbjHElb2A OFxEEOosgfR2/7AucO+k7CziooMPvKRQ5NfCvux7LDvTTNZm/xDYL0rGhaMjsqs8TxiV Mh7dUMEEN/qoXFPK6GoqmYQ3goHdrEzB6HBlLL1XXBNKjMypxMjblIJI9cgTgTMDnor5 EH1Rk9++77ImvvagDLfJlV9Y0Sqg7Ea/BmRTTT4wyFE0ISf0HQrXg42vq3dVhyxV+aiQ 6jWQ== X-Forwarded-Encrypted: i=1; AJvYcCU+UFZCopHVC4NUh/2GZFdXkAX73DYZ3xhDACfRlNMW7Oi+YuIxUkrqTBtWOZzc/AHlQgXffGxXFPnWAWo=@vger.kernel.org X-Gm-Message-State: AOJu0YyfMvg2VK4jw7GPB8IggcfaPSW0GEkEQUdQcs/oVacHxBumA+zb JaHaG7NqJhLx5StIvaWx69OGmTN+lzsdAys4D/XMrVu3CVuMpF6gMWzJzEaJHWqKEA== X-Gm-Gg: ATEYQzyB5b41y6EV8AfYohXnB4XYUQLcOWg6g0EzpBO8pGgJBMqAf84cLbeO2R/+U/7 CEHvTkkTjBIXk3qwS+iheDidQit5oZBtCKDZmYqhhbGASJMsSWRWGSDEi6/ZzBWroXK62h7wwtb hlGPwy7f//UF3ihTUuFqiTBjVIOiDUG/eEKWEOSi+4LhezNedOVCejr//qveDmsWeu9SUxTf8K0 UPRi/XeTHmJSQAefKxmw+cRB4G1mETxwFYPQIuWbDOI6Q4B/s2GtUv9PUQo1RT5COt7tZ3VCJQP 31AXbMqWYRVtRjJG58fAG7NtRdPFut9DPU1DGJTvgImi0sSHYrNq7Cscpif4+ly+fKy5XW0sx1p 3/1DLrd9pJfv1NqviGlges8GISlQs+qBrN5rB8CI4S5UIxd6NOJ5/cuX5dmysKzADGSiCWiMh2z D6h/scUiPk2Lq67Ywf1rpRZMHJzGv6BtxlshqGc+jQzWqxa69rTxTZCkB8Bw== X-Received: by 2002:a17:902:e750:b0:2ae:44db:570c with SMTP id d9443c01a7336-2b0828a93e1mr2454385ad.12.1773998917324; Fri, 20 Mar 2026 02:28:37 -0700 (PDT) Received: from google.com (10.129.124.34.bc.googleusercontent.com. [34.124.129.10]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-82b0409c0f1sm1652552b3a.38.2026.03.20.02.28.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Mar 2026 02:28:36 -0700 (PDT) Date: Fri, 20 Mar 2026 09:28:28 +0000 From: Pranjal Shrivastava To: Vipin Sharma Cc: Samiullah Khawaja , David Woodhouse , Lu Baolu , Joerg Roedel , Will Deacon , Jason Gunthorpe , Robin Murphy , Kevin Tian , Alex Williamson , Shuah Khan , iommu@lists.linux.dev, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Saeed Mahameed , Adithya Jayachandran , Parav Pandit , Leon Romanovsky , William Tu , Pratyush Yadav , Pasha Tatashin , David Matlack , Andrew Morton , Chris Li , YiFei Zhu Subject: Re: [PATCH 04/14] iommu/pages: Add APIs to preserve/unpreserve/restore iommu pages Message-ID: References: <20260203220948.2176157-1-skhawaja@google.com> <20260203220948.2176157-5-skhawaja@google.com> <20260317202925.GA2582517.vipinsh@google.com> 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: <20260317202925.GA2582517.vipinsh@google.com> On Tue, Mar 17, 2026 at 01:59:03PM -0700, Vipin Sharma wrote: > On Tue, Feb 03, 2026 at 10:09:38PM +0000, Samiullah Khawaja wrote: > > + > > +void iommu_unpreserve_pages(struct iommu_pages_list *list, int count) > > +{ > > + struct ioptdesc *iopt; > > + > > + if (!count) > > + return; > > + > > + /* If less than zero then unpreserve all pages. */ > > + if (count < 0) > > + count = 0; > > + > > + list_for_each_entry(iopt, &list->pages, iopt_freelist_elm) { > > + kho_unpreserve_folio(ioptdesc_folio(iopt)); > > + if (count > 0 && --count == 0) > > + break; > > + } > > +} > > I see you are trying to have common function for error handling in > iommu_preserve_pages() down and unpreserving in the next patch with > overloaded meaning of count. > > < 0 means unpreserve all of the pages > = 0 means do nothing > > 0 unpreserve these many pages. > > It seems non-intuitive to me and code also end up having multiple ifs. I > will recommend to just have this function go through all of the pages > without passing count argument. In iommu_preserve_pages() function down, > handle the error case with a "goto err" statement. If this is the way > things happen in iommu codebase feel free to ignore this suggestion. > +1 to this. The current version of overloaded count is counter-intuitive as it expects a back-handed API contract. Since we're using lists here, I'd recommend using the list_for_each_entry_continue_reverse() helper: +void iommu_unpreserve_pages(struct iommu_pages_list *list) +{ + struct ioptdesc *iopt; + + list_for_each_entry(iopt, &list->pages, iopt_freelist_elm) + kho_unpreserve_folio(ioptdesc_folio(iopt)); +} +EXPORT_SYMBOL_GPL(iommu_unpreserve_pages); + +int iommu_preserve_pages(struct iommu_pages_list *list) +{ + struct ioptdesc *iopt; + int ret; + + list_for_each_entry(iopt, &list->pages, iopt_freelist_elm) { + ret = kho_preserve_folio(ioptdesc_folio(iopt)); + if (ret) + goto err_rollback; + } + return 0; + +err_rollback: + /* Rollback only the successfully preserved pages */ + list_for_each_entry_continue_reverse(iopt, &list->pages, iopt_freelist_elm) + kho_unpreserve_folio(ioptdesc_folio(iopt)); + return ret; +} [ ---- snip >8---- ] Thanks, Praan