From patchwork Tue Jun 2 07:43:22 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: You-Sheng Yang X-Patchwork-Id: 1302132 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.ubuntu.com (client-ip=91.189.94.19; helo=huckleberry.canonical.com; envelope-from=kernel-team-bounces@lists.ubuntu.com; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=canonical.com Received: from huckleberry.canonical.com (huckleberry.canonical.com [91.189.94.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 49bkZp4Zkpz9sSn; Tue, 2 Jun 2020 17:44:34 +1000 (AEST) Received: from localhost ([127.0.0.1] helo=huckleberry.canonical.com) by huckleberry.canonical.com with esmtp (Exim 4.86_2) (envelope-from ) id 1jg1b4-0002Ad-Mu; Tue, 02 Jun 2020 07:44:30 +0000 Received: from mail-pj1-f67.google.com ([209.85.216.67]) by huckleberry.canonical.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1jg1b2-00029e-FV for kernel-team@lists.ubuntu.com; Tue, 02 Jun 2020 07:44:28 +0000 Received: by mail-pj1-f67.google.com with SMTP id h95so1047054pje.4 for ; Tue, 02 Jun 2020 00:44:28 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=wKahWwHKucGlsPaI25DGaC+57dbiBq+bTTFtFgGPih8=; b=L7qe0f7rJrp62vGrs0NaxWocTJIE3JS+PKPGjuH0iohjmp393G89vgA13YE4PycPyW +AxIqVRbgpCEBXsa9PH8XUTUqnbM+Ef2mXDtA93mV3I7XYvtfeG/54iZGX2fER8dHgLg Jy2ov8of+CZ9i5LJBIibLalfojBpU7uytoahUB7scmaCwE2OlP0SsWllAUFPk/krc5mW alAi7s7D/M57iVCfvqAlUpacD84NWOnrsA6OLAD+E7hDDAbU2s64ok6uu31b3KlIGh0O PyYLG7sOQ3D/kbt999RFhbUwr8U2lNSN/Cx0mnpJrJzPkH1H+8Uy4CTzKlhjlUlaNtW0 l2fw== X-Gm-Message-State: AOAM531hpImdMSlUJX/nHzhZqMoJJwl7Z7gx7nXqJAGyq6YdqpECFQ9/ HRwfxoK4i4Xi+yNhdrKBJT/Cy0MiaSI= X-Google-Smtp-Source: ABdhPJwxHzVhN4dzuCYplt8No4gOum4RBEkvlFZBQuu/o0RnfM31A/HTrz+ly9tI3Z6rCcyjGhWqWQ== X-Received: by 2002:a17:90a:fe83:: with SMTP id co3mr3848370pjb.204.1591083866598; Tue, 02 Jun 2020 00:44:26 -0700 (PDT) Received: from localhost (61-220-137-37.HINET-IP.hinet.net. [61.220.137.37]) by smtp.gmail.com with ESMTPSA id u35sm1421531pgm.48.2020.06.02.00.44.25 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Jun 2020 00:44:25 -0700 (PDT) From: You-Sheng Yang To: kernel-team@lists.ubuntu.com Subject: [PATCH v2 01/60][SRU][OEM-5.6] iommu: Use C99 flexible array in fwspec Date: Tue, 2 Jun 2020 15:43:22 +0800 Message-Id: <20200602074421.1742802-2-vicamo.yang@canonical.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200602074421.1742802-1-vicamo.yang@canonical.com> References: <20200602074421.1742802-1-vicamo.yang@canonical.com> MIME-Version: 1.0 X-BeenThere: kernel-team@lists.ubuntu.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Kernel team discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: kernel-team-bounces@lists.ubuntu.com Sender: "kernel-team" From: Robin Murphy BugLink: https://bugs.launchpad.net/bugs/1876707 Although the 1-element array was a typical pre-C99 way to implement variable-length structures, and indeed is a fundamental construct in the APIs of certain other popular platforms, there's no good reason for it here (and in particular the sizeof() trick is far too "clever" for its own good). We can just as easily implement iommu_fwspec's preallocation behaviour using a standard flexible array member, so let's make it look the way most readers would expect. Signed-off-by: Robin Murphy Signed-off-by: Joerg Roedel (cherry picked from commit 098accf2da940189f4d62d3514d17f8bb05dc6e1) Signed-off-by: You-Sheng Yang --- drivers/iommu/iommu.c | 15 ++++++++------- include/linux/iommu.h | 2 +- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 22b28076d48e..273f35dddaa9 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -2410,7 +2410,8 @@ int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode, if (fwspec) return ops == fwspec->ops ? 0 : -EINVAL; - fwspec = kzalloc(sizeof(*fwspec), GFP_KERNEL); + /* Preallocate for the overwhelmingly common case of 1 ID */ + fwspec = kzalloc(struct_size(fwspec, ids, 1), GFP_KERNEL); if (!fwspec) return -ENOMEM; @@ -2437,15 +2438,15 @@ EXPORT_SYMBOL_GPL(iommu_fwspec_free); int iommu_fwspec_add_ids(struct device *dev, u32 *ids, int num_ids) { struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); - size_t size; - int i; + int i, new_num; if (!fwspec) return -EINVAL; - size = offsetof(struct iommu_fwspec, ids[fwspec->num_ids + num_ids]); - if (size > sizeof(*fwspec)) { - fwspec = krealloc(fwspec, size, GFP_KERNEL); + new_num = fwspec->num_ids + num_ids; + if (new_num > 1) { + fwspec = krealloc(fwspec, struct_size(fwspec, ids, new_num), + GFP_KERNEL); if (!fwspec) return -ENOMEM; @@ -2455,7 +2456,7 @@ int iommu_fwspec_add_ids(struct device *dev, u32 *ids, int num_ids) for (i = 0; i < num_ids; i++) fwspec->ids[fwspec->num_ids + i] = ids[i]; - fwspec->num_ids += num_ids; + fwspec->num_ids = new_num; return 0; } EXPORT_SYMBOL_GPL(iommu_fwspec_add_ids); diff --git a/include/linux/iommu.h b/include/linux/iommu.h index d1b5f4d98569..4d1ba76c9a64 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -592,7 +592,7 @@ struct iommu_fwspec { u32 flags; u32 num_pasid_bits; unsigned int num_ids; - u32 ids[1]; + u32 ids[]; }; /* ATS is supported */