From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) (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 3BF7B29346F for ; Fri, 20 Mar 2026 18:27:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.177 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774031266; cv=none; b=ViA8tcsKKXZw0vznLZmUtF1C/nM94f9gT6W7A9i3nv5x6eY1KBsaTDHSuqfD7pVRIuy+BiESgvR99nHhAGDMYAWTCkG4rFyvokw5RFHp8kljNCEvsZSZT4zt3vO6k2jywYnpyaSPHKUDuXl15xon4uqPvnI4DmDcIxaHik/IUpw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774031266; c=relaxed/simple; bh=rUJDpu/Tbsmr3Z96uyeTBTqVQmswY1ls9ES7BCAQFPQ=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=Wk5FEjhed51Hq1kf/5pQkUMxWmP+TqvTlSRVbXUvEB86ODsc9zQV/vcTnax81sWXq8KfumsvWF16kLErXQdua8UDa7eiLYSYJWIj1o7rpvqZoItYQ7h+xbGlwSpiIBc1TvykM2yw0y3fbxCUkjfPHctFJv/+ys4AmtUGHo2bQ+Q= 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=JzUZxAhq; arc=none smtp.client-ip=209.85.214.177 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="JzUZxAhq" Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-2b052ec7176so10095ad.1 for ; Fri, 20 Mar 2026 11:27:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1774031264; x=1774636064; 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=f8W5cvFUR4VXf+WaiJdKnvQRdmelG4HkMeFFekxFTBY=; b=JzUZxAhqDI3fkZtkuQtZfFs+pNKCrLLueoi4rGb1/yvavp4w73zp9BShpg2KQPE7uZ DMlcbsAMtDHQwZSFP52LlHJdPW2yy4lEKn/tdXKjk9hUq/PZhCMjo/EvLwee4l/1SC9+ J+HoieW2lXF5pacpWawNWJ3XSHYAEBkUX/obqbO5oC5OPBVndKMPBI8WSDU8bDUVBIX2 x9IfsPJDHGi/iAVyUxxF4gqAtwUJ/LrFi8wB3mHa2r5fV1+vvl1eCA+LSoTbyLHgpAQm Nvio0V2Pt6Ka5nW6N50ohd0yKWLBjFWLoIHzr8v+V+wXmIT2n9G7i4kIez3F9jzZwTcQ Pd0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774031264; x=1774636064; 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=f8W5cvFUR4VXf+WaiJdKnvQRdmelG4HkMeFFekxFTBY=; b=D6hwn/o+Y1XxFYvsGlCp7yct3t5+Bs+PjUqm24k4Sd6rz3M0ISari97Emau0mYE3Lh 1RJMlGHYKGiOWy8FmFCKaopdR/6Iape4pbNYJj93s3MCmfjRQwTj3gdle2NoHyFEL9rd BmesuBU5snvmi9PsKOuTeVktK9+SYfB1o/PR8M96HajhvyjOXR8vQdwdfMGirhxtPOMd MvLBSIEV6BUC570FM5RTrMoaRD3Z0UVgGm3fyBC5iutj3wxOPyJk6IJTAKiaC7dZMdPZ zcosB1yqcnCeck5Pr0h3mw+PfGOoPD1pRDLb/0do++q26zLsbpwMrbkaIMS+DK60c2TS IbgA== X-Forwarded-Encrypted: i=1; AJvYcCWosQ9NpPyI7DEz0Zw1PjaHpoQPVoPZBqwX30dTdsAu7JS4kgDf1bu9XFMBu0Mp94mkMBKY3/Ho0v8csmY=@vger.kernel.org X-Gm-Message-State: AOJu0Yz4/EW8B7RDQEv4iQD/ZWHJrE2U4+L9ONtDm5NXOwQN6GCmFrmV xpHNzJFVHf8nIR7tOUErPqqcg18ydHtg14/6QxKh11haimhk2VoErDG1SkrWhMZIHg== X-Gm-Gg: ATEYQzx9cqWKuZjlKbpgm8kON/YzTBcZ6m3h1aA6E8WTOODFLsXSRTZL4ukERbFgnDU 7foosYtQaHPR+NcexDhjNGOzT9WysiA5XJ1JBrBnChDv49WFvcEYooLs0J5sjAad3uTpi0VCybH hvEhFW20ZAMumyEAVURxeI51eJojhkIwRHpjUw2HVIsI89qRQ212lWl/SsLzBXuvUal3N8/nsVv 5EoRpIUITf1ICLat3eOt5iVN2yTR9LY7HllgeVbl159KmA1Zxj4uVByPy1gIcCbPGR/ol+MhBK2 7VIVcZeH9NJ65W5G9eS2t1yxiaThjqXFeBNs5xrOTo0trAOqihNcgMvM5AafwhkcjosXk72vxTz Yyokxpc5nJGQtqXaCtadusyh9qfKkVTQufPyThBCelE00J8gzz6FDPP5I2ph+3RoB3l2VwEhjQf ymtmLEzTSmYRYa3XXjJOsWV6ShqLR5a1ZhpqL03TEMzpHrZO4xIfCP5+ASlDcVtA== X-Received: by 2002:a17:902:f541:b0:2ae:45cc:aeb6 with SMTP id d9443c01a7336-2b08b40ab79mr410815ad.6.1774031264003; Fri, 20 Mar 2026 11:27:44 -0700 (PDT) Received: from google.com (168.136.83.34.bc.googleusercontent.com. [34.83.136.168]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-c743a8176e7sm2212848a12.8.2026.03.20.11.27.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Mar 2026 11:27:43 -0700 (PDT) Date: Fri, 20 Mar 2026 18:27:39 +0000 From: Samiullah Khawaja To: Pranjal Shrivastava Cc: Vipin Sharma , 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; format=flowed Content-Disposition: inline In-Reply-To: On Fri, Mar 20, 2026 at 09:28:28AM +0000, Pranjal Shrivastava wrote: >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: Agreed. I will use this approach in next revision. Thanks, Sami > >+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