From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f48.google.com (mail-pj1-f48.google.com [209.85.216.48]) (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 4D68B3A8755 for ; Thu, 4 Jun 2026 05:18:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.48 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780550306; cv=none; b=vDmOR0ulklay5UBdcrv7GSXmnMs0CoLh4oWjJMMhkkhdA4y4IwD7+skH8ISlTErNUQDevp63mhGlYCs3zYRvoLSsy+2fBF+40b6wxd/hOalzwtVZe2lJJBgLH2xyHeWEvXVzg7IlGD3Jrhx8KWioqVz7ubYmEBXSMDHnAB0DRt4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780550306; c=relaxed/simple; bh=swCN0pyL9on2TRI3+1vBUIXQTmxVF54W3TG1qGC77p4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ICE0yIrhEsVTCeEwsY455S70xz8rgCvr1LbeU3RVYZObNUkuwWV24vpwK37wmpB6hSqzNc+L3tUZqCAiIyUOKzY8ASWvac1e5NWgQavqt/AQgh/K1eNejg+brXYenzD6dPJE0BszCLzdXjZrakWb4KrzLuE89lQmmOM20++/9Cs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=dYTEpg25; arc=none smtp.client-ip=209.85.216.48 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="dYTEpg25" Received: by mail-pj1-f48.google.com with SMTP id 98e67ed59e1d1-36dac5d5da0so135235a91.2 for ; Wed, 03 Jun 2026 22:18:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1780550304; x=1781155104; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=VzXAoL8G5yh9nalpFe2jIG9YHdbmlWG6e7G4yOR+whk=; b=dYTEpg254osk7Yhe/e3/3U4E+sClV8WXpTyh4+CRga2ner7048RxWyqOdpy6X869dG 2ktY/tjQaQ2VnEHgYoOuAe2fzG+UgyhqQf+aF3xtKdf+FFaPJvyfWnMVfL/g7MMHXVGb eK1wDsu1pqZf7zS+1/GMvkcqZ8GlGzec3P8Or4NFe8KAG11hLEvdFiiCR7rl8NrS/Nvl 2u5mVbNx3syzGF5GgH7HyaaSo2yQHm3GyVdnKK5awigc+Iz2KlrfIN4PLCLvu/Pr7pHT m9EOwcx3nbhWpAfpyj2YtmBMBjTY6bRiuXGa+4gkfIPJEBX2KIFCNdaDkNRzdh/oXSyw JJlg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780550304; x=1781155104; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=VzXAoL8G5yh9nalpFe2jIG9YHdbmlWG6e7G4yOR+whk=; b=NrCrBTGGjJXSF5j6nPL35rtPSg7m8VqVjQpKeds/xeH27Banyuk0l+derHZWZr6gGE b4du8yTpj8focVsZgZttU8fUuoAhWMQKtKxgIpsp/MfMFjX9L4JKRPvuUi+ekLsZq3c3 s/C0AvOa/f4L+jk2OPc1IIMD/AYxX/mD6ErQbXyF3Fh8+XV5pBYiCgz3CEV/Ma47bUR0 km8FwA1xNi8RZjCJGEExWIW0QivXgckTx1JEQxQvDEX47h1Pm48CEra3NOCCfSORNnTd 3RiE6vqWT9XbOpwBInT/riiYQ4JFnv/+9o0txqhfAI7eU/LHB1EEhy+XADOIDS7xi283 8KYQ== X-Forwarded-Encrypted: i=1; AFNElJ9ESTLvXRJA6X9MAj38P1VwbVIc801ygh+HUYrvso/3jxbBttEd8tnnrjDKgjm7bYzXB3TTb74P/xfC7Ms=@vger.kernel.org X-Gm-Message-State: AOJu0Yxgw/EbvoiQuMxfL9azQNG8XRT15u0kYPIq0HlPdD3FP2gz+gpn IFT9OXoP23IUocxRjuBi2yn0mYU82SMPhauaCgJtjtcKYyiOONqvmQ+SVMndtg== X-Gm-Gg: Acq92OELvWYIbQyxanwQuHOLg6M7d5qYep1bN/s31YbRDDcng3zmL2kzAIVUA+aM6Dt UBtosHEAv2Mx1cO7Z0Awzwvk+3SywoLs/HFMllzrs66ZBYAYQafzJbjNcOvJIoahbRFZG9+PYAT xPKY5y1wg3msQi4Por92E80y8X7qYD4niBWZYzCCwqHIY1pX0+PvPtOGTZuy8FJKdYL20YgEbeO 0lIHcmDjst0bGC26qv52kDC7l7bfkhHVzECHB7aJqla3yPMkhFZ533OYnFPQ/MxgfesEd/CZTq3 xpdlCMILCG5jVvnWZuYGVjzDrLENykHGLfvL67Xd3UiUS2ICMcvWFgSv1sdxFcddmcw5s7tgAQ8 pbMiAGpmfTPnSLQOvpbL9HyoAmYglq8yMkJ3bCQJ2akFDIcbbqRoZzfyx30vlpU2c+DmC0mjIB/ gwBQoookhG7MsPFgSt/gdNIon7PcCR6bow9lp4P1Qy83TLyf1DfAxF2TyaVQo= X-Received: by 2002:a17:90a:e710:b0:368:83e6:ca95 with SMTP id 98e67ed59e1d1-36e2dd4c595mr6569489a91.0.1780550304582; Wed, 03 Jun 2026 22:18:24 -0700 (PDT) Received: from csl-conti-dell7858.ntu.edu.sg ([155.69.195.57]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-36f6bf827e6sm2009262a91.1.2026.06.03.22.18.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Jun 2026 22:18:24 -0700 (PDT) From: Maoyi Xie To: joro@8bytes.org, will@kernel.org, robin.murphy@arm.com, jpb@kernel.org Cc: iommu@lists.linux.dev, linux-kernel@vger.kernel.org, virtualization@lists.linux.dev, Maoyi Xie Subject: [PATCH 1/2] iommu: Avoid using the list iterator past the loop in iommu_insert_resv_region() Date: Thu, 4 Jun 2026 13:18:15 +0800 Message-Id: <20260604051816.2976221-2-maoyixie.tju@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260604051816.2976221-1-maoyixie.tju@gmail.com> References: <20260604051816.2976221-1-maoyixie.tju@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit iommu_insert_resv_region() walks @regions to find the insertion point. When no element compares greater, the list_for_each_entry() iterator ends up one past the last entry, so &iter->list aliases the list head through container_of() offset cancellation and the following list_add_tail() still targets the tail. The result is correct, but using the iterator after the loop is undefined per the list_for_each_entry() contract. The loop only needs a list_head to use as the insertion point, not the entry itself, so iterate with list_for_each() and keep the typed list_entry() dereference inside the loop body. No functional change. Suggested-by: Robin Murphy Signed-off-by: Maoyi Xie --- drivers/iommu/iommu.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 61c12ba78206..f9f53db9696f 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -862,6 +862,7 @@ static int iommu_insert_resv_region(struct iommu_resv_region *new, struct list_head *regions) { struct iommu_resv_region *iter, *tmp, *nr, *top; + struct list_head *pos; LIST_HEAD(stack); nr = iommu_alloc_resv_region(new->start, new->length, @@ -870,12 +871,14 @@ static int iommu_insert_resv_region(struct iommu_resv_region *new, return -ENOMEM; /* First add the new element based on start address sorting */ - list_for_each_entry(iter, regions, list) { + list_for_each(pos, regions) { + iter = list_entry(pos, struct iommu_resv_region, list); + if (nr->start < iter->start || (nr->start == iter->start && nr->type <= iter->type)) break; } - list_add_tail(&nr->list, &iter->list); + list_add_tail(&nr->list, pos); /* Merge overlapping segments of type nr->type in @regions, if any */ list_for_each_entry_safe(iter, tmp, regions, list) { -- 2.34.1