From patchwork Thu Feb 27 08:56:35 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Beulich X-Patchwork-Id: 324735 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 426D12C009B for ; Thu, 27 Feb 2014 19:56:25 +1100 (EST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1750985AbaB0I4X (ORCPT ); Thu, 27 Feb 2014 03:56:23 -0500 Received: from nat28.tlf.novell.com ([130.57.49.28]:50960 "EHLO nat28.tlf.novell.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750844AbaB0I4X convert rfc822-to-8bit (ORCPT ); Thu, 27 Feb 2014 03:56:23 -0500 Received: from EMEA1-MTA by nat28.tlf.novell.com with Novell_GroupWise; Thu, 27 Feb 2014 08:56:21 +0000 Message-Id: <530F0BD3020000780011FC25@nat28.tlf.novell.com> X-Mailer: Novell GroupWise Internet Agent 12.0.2 Date: Thu, 27 Feb 2014 08:56:35 +0000 From: "Jan Beulich" To: "Bjorn Helgaas" Cc: , Subject: [PATCH] PCI/MSI: simplify populate_msi_sysfs() Mime-Version: 1.0 Content-Disposition: inline Sender: linux-pci-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org While originally this was a patch to fix the two memory leaks (which got preempted by the two commits that went in the day before I wanted to submit this fix), I think this can (and should) be done more efficiently: - swapping the order of the two allocations and storing the msi_dev_attr-derived pointer right after allocation, allowing the cleanup code to pick things up without extra effort - using kasprintf() instead of the kmalloc()/sprintf() pair Signed-off-by: Jan Beulich Cc: Greg Kroah-Hartman Acked-by: Greg Kroah-Hartman --- drivers/pci/msi.c | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) -- To unsubscribe from this list: send the line "unsubscribe linux-pci" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html --- 3.14-rc4/drivers/pci/msi.c +++ 3.14-rc4-pci-msi-sysfs-cleanup/drivers/pci/msi.c @@ -544,22 +544,18 @@ static int populate_msi_sysfs(struct pci if (!msi_attrs) return -ENOMEM; list_for_each_entry(entry, &pdev->msi_list, list) { - char *name = kmalloc(20, GFP_KERNEL); - if (!name) - goto error_attrs; - msi_dev_attr = kzalloc(sizeof(*msi_dev_attr), GFP_KERNEL); - if (!msi_dev_attr) { - kfree(name); + if (!msi_dev_attr) goto error_attrs; - } + msi_attrs[count] = &msi_dev_attr->attr; - sprintf(name, "%d", entry->irq); sysfs_attr_init(&msi_dev_attr->attr); - msi_dev_attr->attr.name = name; + msi_dev_attr->attr.name = kasprintf(GFP_KERNEL, "%d", + entry->irq); + if (!msi_dev_attr->attr.name) + goto error_attrs; msi_dev_attr->attr.mode = S_IRUGO; msi_dev_attr->show = msi_mode_show; - msi_attrs[count] = &msi_dev_attr->attr; ++count; }