From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-qk1-f180.google.com (mail-qk1-f180.google.com [209.85.222.180]) (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 A290C498911 for ; Tue, 8 Sep 2026 21:35:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.180 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788903308; cv=none; b=el80XIV9I/FvgYCF5L7n4rCw2BHTBnYzipdMY4QEDAIj63gIdAPR4cEnCMGhSV2B3a5DjjR7W6c1d4j2h/3FzsLmZIu8sN73MGoxOE5KF31KYb8gnJwMHu6uwovQNhx3xKdTLITHeaSDxZirBUa1O46PAUTZJb8YXSW9+qBDcFg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788903308; c=relaxed/simple; bh=LRZ8vmf7yVnDP8AHhwc+k+6VHYjhFocJjHx3TQ/DOkc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=SfVZFd8eh7lAY3OHmSxq1lit58s/NcOMJaD86wJje4VmC3aKqNZ13SfdaFN8VEUnl+c252yYPVOGS72lRKXjMbwcCjCHS7+ZeErmLYRveZBQk+kcDKsaOHI09ysFUJMyc6pJPjras2nbASz3RiTpA0Qhv9ZC71Hl98CgnkDOQGE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=riscstar.com; spf=pass smtp.mailfrom=riscstar.com; dkim=pass (2048-bit key) header.d=riscstar-com.20251104.gappssmtp.com header.i=@riscstar-com.20251104.gappssmtp.com header.b=WBw/Q616; arc=none smtp.client-ip=209.85.222.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=riscstar.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=riscstar.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=riscstar-com.20251104.gappssmtp.com header.i=@riscstar-com.20251104.gappssmtp.com header.b="WBw/Q616" Received: by mail-qk1-f180.google.com with SMTP id af79cd13be357-9399a1c21c1so262852985a.3 for ; Tue, 08 Sep 2026 14:35:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=riscstar-com.20251104.gappssmtp.com; s=20251104; t=1788903304; x=1789508104; 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:content-type; bh=OlXU1fYfQVNOpzWxYYTTBNzAHTPsq9zYyGkBJYPHark=; b=WBw/Q616nSnQpo3D+YhR6IwmFP6QLF2C721MeiAH4C/BgqiGBVMsjUIqMiMr9DYu4m rzEoCvldkH6dkV15IswfMJIXfbkcEX0TE+R2vtClZIKoiewx79xLemr+9oeLamQrqy7y jzLrt2+zONY8tRhoroIA4pnO1x7gy4zmr1QJ/0Tea8z4GroRvwDnDoYJa+ta44Oyy2hi rsffKmE7v1ClR1gLvMyl7TDVwE5ryNB4c0wX9aJX7/obtUyrqfWuOB73+FEOePoKDtFU RJVBGJMgyylmzDcg7rzLjmR6H1xaRxGbVgq49uBRlyCtrBJN0xdU5OlmwC9vC8rAfDp4 2Giw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1788903304; x=1789508104; 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:content-type; bh=OlXU1fYfQVNOpzWxYYTTBNzAHTPsq9zYyGkBJYPHark=; b=a5M1esUiRyAK7BTUpNZkfIr65TLCBVFSwrG+aL8umcF3hJV7L1mifpoydmJX5wSJO5 0G4j5Ic+REV4c+0Y+6BwMCZ0tEE/cdcLr8UweToK4dDvEV84yCcDJ7dV+hYNzzShKc/b NvbwPXjFRvn17ssCLghBRPWNH7JLOjOZVAPyjRFWCQXLdWLGCKQveLtUJevIZPQZAAIA 6zDbu7enc/vE4Bcp+eCo67kZWQjIMWXlCU9yFvEbHuVzsVKe8TwnUHHn/gnqu5UjofcX cOzE2lKM70OjaBudjfQneNn6F32Ieo85ZPvqJIduWlBEJU6jpVf+3BPaTHwns83FAP5b NM8Q== X-Forwarded-Encrypted: i=1; AKwUvBzafZGwOShyaSthW6pZfaNOfWCA8B1dUQzhbVwnP0Te+gvYuTR7Mj68UX2i9e5Ph4e6IR8qL2mt6s4w@vger.kernel.org X-Gm-Message-State: AFuF++m7uWsM/y+EExywYl87xLUyAVSoUN1CjIVQaHolD0NlqR6fTqyd 1/s85oYunglVUO18VB0O0moOJnYJUiOEhQFH3C9k9V3/3qpY/4cnp89Tw50rVqAu3A4= X-Gm-Gg: AYBFou2DNVx1xsJDKNX7YvIf20x40p1vOYcTwGliGwxQZwpeDUj4llWGBdBshwO8Bl7 0zd9HqF6JfXqltWeUPzTUWp4u5i8sjEZRJzHHwsD8n51PQq+9P9XwuYl12484fSf73YAHfiM0OK lSSNT55Dd49Tr0zto4OYaMsA6KjfUMQkRNplbBDuY57ElZRr/LgHTqzjVck4O9pxRu3F5dMl/2Q 6eVlB5J0oTCaT5TOwlfev0uzvkDGq0+g+lgNrYop+YHwPfmnsMZKm0HeookQIW4DAaCwKnDgjyI 6yGCV8Uc5ZEaxEPsCs80BLHaUoklhMOX1pvbKMODqYRCs66uWfE9lPFRHBMdmbhtQZXbHk5U4w+ PPx0lO5jH0XB3R8WBCgxWnt1QrJDzC8GUZqqdIs6EnNxRycO5/2I9PXX/yLxGqHq9t5ZEor653A TeVdx+RnoB634jps6yj/yad/3KLs2Bw84KansZ2xVcQtndR51maccpMPvxeXVYhylY X-Received: by 2002:a05:620a:6989:b0:936:d833:6a35 with SMTP id af79cd13be357-93980133923mr3591761285a.0.1788903304149; Tue, 08 Sep 2026 14:35:04 -0700 (PDT) Received: from zippy.localdomain ([73.62.185.64]) by smtp.gmail.com with ESMTPSA id af79cd13be357-9397f9f0734sm1250497885a.4.2026.09.08.14.35.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Sep 2026 14:35:03 -0700 (PDT) From: Alex Elder To: bhelgaas@google.com, robh@kernel.org, saravanak@kernel.org Cc: herve.codina@bootlin.com, daniel@riscstar.com, mohd.anwar@oss.qualcomm.com, lorenzo.bianconi@oss.qualcomm.com, linux-pci@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Sashiko Subject: [PATCH v5 1/4] PCI: of: avoid allocations in of_pci_prop_compatible() Date: Tue, 8 Sep 2026 16:34:55 -0500 Message-ID: <20260908213459.2519059-2-elder@riscstar.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260908213459.2519059-1-elder@riscstar.com> References: <20260908213459.2519059-1-elder@riscstar.com> Precedence: bulk X-Mailing-List: devicetree@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Three compatible strings are formatted in of_pci_prop_compatible(). Their sizes are known in advance, and the largest is 16 bytes. Rather than dynamically allocating the space for those strings, just set aside a buffer on the stack large enough to hold all three. This avoids a problem that Sashiko pointed out, where an allocation failure would cause subsequent crash because strlen() is called unconditionally in of_changeset_add_prop_string_array(). Reported-by: Sashiko Link: https://lore.kernel.org/sashiko-reviews/a647bd56-7dc8-4fec-9d96-834622054cdf@riscstar.com Signed-off-by: Alex Elder --- v5: - Made "bufp" have pointer type - Eliminated the unused local variable "i" drivers/pci/of_property.c | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/drivers/pci/of_property.c b/drivers/pci/of_property.c index 75a358f73e694..d66c702218081 100644 --- a/drivers/pci/of_property.c +++ b/drivers/pci/of_property.c @@ -324,27 +324,36 @@ static int of_pci_prop_intr_map(struct pci_dev *pdev, struct of_changeset *ocs, return ret; } +/* The three compatible property strings have max sizes 12+1, 15+1, and 13+1 */ +#define PROP_SIZE 16 /* Max size of each compatible string */ static int of_pci_prop_compatible(struct pci_dev *pdev, struct of_changeset *ocs, struct device_node *np) { const char *compat_strs[PROP_COMPAT_NUM] = { 0 }; - int i, ret; + char buf[PROP_COMPAT_NUM * PROP_SIZE] = { }; + char *bufp = buf; + int ret; - compat_strs[PROP_COMPAT_PCI_VVVV_DDDD] = - kasprintf(GFP_KERNEL, "pci%x,%x", pdev->vendor, pdev->device); - compat_strs[PROP_COMPAT_PCICLASS_CCSSPP] = - kasprintf(GFP_KERNEL, "pciclass,%06x", pdev->class); - compat_strs[PROP_COMPAT_PCICLASS_CCSS] = - kasprintf(GFP_KERNEL, "pciclass,%04x", pdev->class >> 8); + ret = snprintf(bufp, PROP_SIZE, "pci%x,%x", pdev->vendor, pdev->device); + if (ret >= PROP_SIZE) + return -EINVAL; + compat_strs[PROP_COMPAT_PCI_VVVV_DDDD] = bufp; + bufp += ret + 1; - ret = of_changeset_add_prop_string_array(ocs, np, "compatible", - compat_strs, PROP_COMPAT_NUM); - for (i = 0; i < PROP_COMPAT_NUM; i++) - kfree(compat_strs[i]); + ret = snprintf(bufp, PROP_SIZE, "pciclass,%06x", pdev->class); + if (ret >= PROP_SIZE) + return -EINVAL; + compat_strs[PROP_COMPAT_PCICLASS_CCSSPP] = bufp; + bufp += ret + 1; - return ret; + ret = snprintf(bufp, PROP_SIZE, "pciclass,%04x", pdev->class >> 8); + compat_strs[PROP_COMPAT_PCICLASS_CCSS] = bufp; + + return of_changeset_add_prop_string_array(ocs, np, "compatible", + compat_strs, PROP_COMPAT_NUM); } +#undef PROP_SIZE int of_pci_add_properties(struct pci_dev *pdev, struct of_changeset *ocs, struct device_node *np) -- 2.53.0