From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f169.google.com (mail-pl1-f169.google.com [209.85.214.169]) (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 EB544372B58 for ; Wed, 20 May 2026 17:24:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.169 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779297895; cv=none; b=kluHVRSAvKpLyPVDCVDblM2IaylPtDpiy5VtS1ebsP8PzBkYXQ/8YdOCQOorO7JALM/RHo0oSSH1hK2HI5K1u5CEXjV8MeKZ132KoKC9+O14snxx8O9iwpkUFm3JrMGFrH4l/feWmWEN/jTqzy3vgPHAcStpYN32iaxAt6bLtUE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779297895; c=relaxed/simple; bh=y3tjD6rMTxegPZ3G74BAk72u6S11iQNTtJ2D9z/46Es=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=Lby9Fe7/20VoD8Qb9zcXnrRrWuROQxgZdysKlNPCfbTWivQnW0axmjoau1vQ0XZTArTilDOiBTngEHy9ji2s7u/Ku4gx7j6wyxRIj75kIqy9/+t4Sf646C17yevCKvYzuKCDMOxNFy4KYkQssETg5pBIHakFjNov/4xxg2T0J4c= 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=nx1Tz9D2; arc=none smtp.client-ip=209.85.214.169 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="nx1Tz9D2" Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-2b2e8b95bdbso1485ad.0 for ; Wed, 20 May 2026 10:24:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1779297892; x=1779902692; 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=wJhl7WJ2lQSroNNhwKFoiQuIjJsM08m4ektSy1+OM9s=; b=nx1Tz9D2emutkwuM+OM6P5f5s278hQIjrdF6pldisKVJVUD+C8P9Vex6RAT4GqcXBR vUSopWzgy8Umib49eJGTCmdaZ/x932haLx2gJ9iwzSQi7GQJJvcSuIyN1sm4STsoYsem 0YJIR60KjJOvYO1Wc5ygE9oNUZrQXWdGk5TUmMsGMif1KebGuEChXuoR4RMsjNXxSr0a rMu4dFnsKR+gj/dfG2jbsEYdfY84GZ1GG6B2q4hpUUbHgd4AqCNdSB5MqtHqRa//8dqD YXGc//vnO4gi5id3yRmeH/jJvlnr6juZWPtdf4LBuXdOgPaeDSphS17XF6bCa5K+XJ6G 5yPg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779297892; x=1779902692; 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=wJhl7WJ2lQSroNNhwKFoiQuIjJsM08m4ektSy1+OM9s=; b=Ps3hQm+I3+7BJrVa7fgmBIoXu50ytPlq4KZ/QAbuZ7FdMEHl4j+Z/Dmx2X0VNWvTGx RB4XsavyRxcmjkRsg7Fa8LSS9HoKUvIRwiFobouZwP6BzcBax1anMWJu83nuhnLxu5vk 2zaXuW3uI/p1HsN04FY8BaJ6rvi7CZEMbq1lXzF+DZSac8yd6GIDF0bH+KwE4TQNcDJa WAh3JObxyAAD2NEBUk90H1tjnWyNb9qqepJ7kV9IT/LmPQSSDnHp87Le1k9SwePTZiIU LTbw2gW4257u5aOTYUNxn5a4QwvRIf8x2AKc9sCCgCASGW8DbGXlsRrTQm/e1OeuN3+8 52RA== X-Forwarded-Encrypted: i=1; AFNElJ+nf+pb76vf3bfdZODIWReHGKejVBxgsTdU7YYEEu926/3/IrhT3fWTDSnqFo34ebcRWxKe7GVd6fYHtKQ=@vger.kernel.org X-Gm-Message-State: AOJu0YwdLQYNdM8RrMCDke5/O/9VEVfnd0VdhxGKu2vPnvyJDdJFNADL LKXUdfoN7TNc3xGPRlgKXKWJHjDCBEHgiThZRrAKY+GD4rAmAA963Ei45l1OGOmAMQ== X-Gm-Gg: Acq92OHIcTn2ElP+e6+iF3laYaMiAwWKSMPNlwQhkVdhoFE2JqijQYwDnIZA8N9Tqgi yxA6dFLpLYFLh0Rz3CfOaf/J1N02LqWIXc8rLO1WAHjWY5Kf4E19mrTXQZbDwtPMls7YgXUzod4 Giz2xb2n4DSKKonON6YsAmlWbV92D8zAURzJ4uca5QHlU0sls7P//9/FDZp26QUd2+IS3GnVcu4 x4RkYuf+QVA3OLL4V6DVpePJ02mwidDNniCcJBFidrr5psH3HGqLsgwXtCcvgkl6uYjOXyqkpQs M5PSOBmyUGT3nYF0q82+KIxzwzoJ6ztcqCvvezrEbQjvemx33lrw9N6SuBIwt4HCH6YLL0nD948 z49Y1THdSKe8I8P4DIgtaOZLeecQTp/L45DCVyeib6uzxiQ+HMfg9lDSEFq4r+4axq5eun8QFfQ yekI72ddEn1mgSxLdEBtvG0HXiWQw7S1mXGZvY2vAwabYr3uU0ypEXi0bT5j56klvox7MC1A== X-Received: by 2002:a17:903:3545:b0:2bd:6dad:7cce with SMTP id d9443c01a7336-2be9f448422mr86475ad.26.1779297891648; Wed, 20 May 2026 10:24:51 -0700 (PDT) Received: from google.com (153.46.83.34.bc.googleusercontent.com. [34.83.46.153]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2bd5c05ffbesm232289015ad.27.2026.05.20.10.24.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 May 2026 10:24:51 -0700 (PDT) Date: Wed, 20 May 2026 17:24:47 +0000 From: Samiullah Khawaja To: Pranjal Shrivastava Cc: 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 , Vipin Sharma , YiFei Zhu Subject: Re: [PATCH v2 08/16] iommu: Add APIs to get iommu and device preserved state Message-ID: References: <20260427175633.1978233-1-skhawaja@google.com> <20260427175633.1978233-9-skhawaja@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 Tue, May 19, 2026 at 03:52:43PM +0000, Pranjal Shrivastava wrote: >On Mon, Apr 27, 2026 at 05:56:25PM +0000, Samiullah Khawaja wrote: >> The preserved state of the device and IOMMU needs to be fetched during >> shutdown and boot in the next kernel. Add APIs that can be used to fetch >> the preserved state of a device and IOMMU. The APIs will only be used >> during shutdown and after liveupdate so no locking needed. >> >> Signed-off-by: Samiullah Khawaja >> --- >> drivers/iommu/liveupdate.c | 57 ++++++++++++++++++++++++++++++++ >> include/linux/iommu-liveupdate.h | 31 +++++++++++++++++ >> 2 files changed, 88 insertions(+) >> >> diff --git a/drivers/iommu/liveupdate.c b/drivers/iommu/liveupdate.c >> index 765d042e22e3..60ee29b0c6bd 100644 >> --- a/drivers/iommu/liveupdate.c >> +++ b/drivers/iommu/liveupdate.c >> @@ -17,6 +17,14 @@ >> #define iommu_max_objs_per_page(_array) \ >> ((PAGE_SIZE - sizeof(struct iommu_array_hdr_ser)) / sizeof((_array)->objects[0])) >> >> +#define iommu_liveupdate_for_each_obj(_arr, _obj, _idx) \ >> + for (; (_arr); \ >> + (_arr) = (_arr)->hdr.next_array_phys ? \ >> + phys_to_virt((_arr)->hdr.next_array_phys) : NULL) \ >> + for ((_idx) = 0, (_obj) = (_arr)->objects; \ >> + (_idx) < (_arr)->hdr.nr_objects; (_idx)++, (_obj)++) \ >> + if (!(_obj)->hdr.deleted) >> + > >Nit: While the usage of this macro is safe right now because we use >goto out; to short-circuit the scan. Such naked nested loops inside an >iterator macro can be a bit of a trap for the future. > >If anyone tries to reuse this macro down the road and uses a break; >statement, it will only break the innermost object loop and continue >scanning subsequent array blocks. > >We could nest two macros or it as a helper fn to prevent such bugs. Agreed on all points. I will change this to two nested macros. > > >> static void *iommu_liveupdate_restore_array(u64 array_phys) >> { >> struct iommu_array_hdr_ser *array_hdr; >> @@ -201,6 +209,55 @@ void iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) >> } >> EXPORT_SYMBOL(iommu_liveupdate_unregister_flb); >> >> +int iommu_for_each_preserved_device(iommu_preserved_device_iter_fn fn, >> + void *arg) >> +{ >> + struct iommu_flb_obj *flb_obj; >> + struct iommu_device_array_ser *array; >> + struct iommu_device_ser *device_ser; >> + int ret, idx; >> + >> + ret = liveupdate_flb_get_incoming(&iommu_flb, (void **)&flb_obj); >> + if (ret) >> + return -ENOENT; >> + >> + array = phys_to_virt(flb_obj->ser->device_array_phys); > >Nit: phys_to_virt doesn't return an error for NULL. While we shouldn't >be in a case where serialized ptrs are NULL. I'm wondering how painful >catching such bugs would be.. should we check for NULL phys_addr here? >(and at other places) Agreed. Will update. > >> + iommu_liveupdate_for_each_obj(array, device_ser, idx) { >> + ret = fn(device_ser, arg); >> + if (ret) >> + goto out; >> + } >> + >> +out: >> + liveupdate_flb_put_incoming(&iommu_flb); >> + return ret; >> +} >> +EXPORT_SYMBOL(iommu_for_each_preserved_device); >> + > >With those two nits, > >Reviewed-by: Pranjal Shrivastava i Thanks, Sami