From patchwork Wed Sep 7 03:26:39 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Huang Rui X-Patchwork-Id: 9318155 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 691F460752 for ; Wed, 7 Sep 2016 03:27:31 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4EC6B28FB1 for ; Wed, 7 Sep 2016 03:27:31 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 42D4128FC2; Wed, 7 Sep 2016 03:27:31 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.1 required=2.0 tests=BAD_ENC_HEADER,BAYES_00, DKIM_SIGNED, RCVD_IN_DNSWL_MED, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id E59BA28FB1 for ; Wed, 7 Sep 2016 03:27:28 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 1CE3C6E836; Wed, 7 Sep 2016 03:27:27 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from NAM03-CO1-obe.outbound.protection.outlook.com (mail-co1nam03on0052.outbound.protection.outlook.com [104.47.40.52]) by gabe.freedesktop.org (Postfix) with ESMTPS id 2DA876E3DF; Wed, 7 Sep 2016 03:27:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector1-amd-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=TBmrONa35tHjr/pYFFwQFGaQFdeNJy/pIT7GoRknUE8=; b=NMnlkckqdEbvsIKBTdID9ymSp4SYNO5agUkt44zVqyLBaC5H9o0abWDK/Nxm2LQLEtnOKt3s3WnYGcuMh2Ext1c1Eqi8RdfBVm7Fw7WIy/hyEKETacK8Aus838kyh9XrrsgGn/avPmIYKFOyvt2iMHAmlvC0AwvQ1uFD2CrGyek= Received: from CY1PR1201CA0032.namprd12.prod.outlook.com (10.169.17.170) by CY1PR12MB0534.namprd12.prod.outlook.com (10.161.175.26) with Microsoft SMTP Server (version=TLS1_0, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA_P384) id 15.1.557.21; Wed, 7 Sep 2016 03:27:23 +0000 Received: from BY2NAM03FT003.eop-NAM03.prod.protection.outlook.com (2a01:111:f400:7e4a::201) by CY1PR1201CA0032.outlook.office365.com (2a01:111:e400:5b9a::42) with Microsoft SMTP Server (version=TLS1_0, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA_P384) id 15.1.599.9 via Frontend Transport; Wed, 7 Sep 2016 03:27:23 +0000 Authentication-Results: spf=none (sender IP is 165.204.84.22) smtp.mailfrom=amd.com; lists.freedesktop.org; dkim=none (message not signed) header.d=none;lists.freedesktop.org; dmarc=permerror action=none header.from=amd.com; Received-SPF: None (protection.outlook.com: amd.com does not designate permitted sender hosts) Received: from SATLEXCHOV02.amd.com (165.204.84.22) by BY2NAM03FT003.mail.protection.outlook.com (10.152.84.105) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.587.6 via Frontend Transport; Wed, 7 Sep 2016 03:27:23 +0000 X-IncomingTopHeaderMarker: TopHeader Received: from SCYBEXDAG03.amd.com (10.34.11.13) by SATLEXCHOV02.amd.com (10.181.40.72) with Microsoft SMTP Server (TLS) id 14.3.266.1; Tue, 6 Sep 2016 22:26:44 -0500 Received: from hr-intel.amd.com (10.237.70.71) by SCYBEXDAG03.amd.com (10.34.11.13) with Microsoft SMTP Server id 14.3.266.1; Wed, 7 Sep 2016 11:26:41 +0800 From: Huang Rui To: , Alex Deucher , Dave Airlie , =?UTF-8?q?Christian=20K=C3=B6nig?= , Daniel Vetter , Sean Paul Subject: [PATCH v2] drm: modify drm_global_item_ref to avoid two times of writing ref->object Date: Wed, 7 Sep 2016 11:26:39 +0800 Message-ID: <1473218799-6890-1-git-send-email-ray.huang@amd.com> X-Mailer: git-send-email 2.7.4 MIME-Version: 1.0 X-IncomingHeaderCount: 12 X-EOPAttributedMessage: 0 X-MS-Office365-Filtering-HT: Tenant X-Forefront-Antispam-Report: CIP:165.204.84.22; IPV:CAL; CTRY:US; EFV:NLI; SFV:NSPM; SFS:(10009020)(6009001)(7916002)(2980300002)(428002)(189002)(199003)(87936001)(626004)(305945005)(11100500001)(50466002)(50986999)(106466001)(92566002)(2906002)(26826002)(47776003)(356003)(7696003)(246002)(33646002)(19580395003)(189998001)(5001770100001)(77096005)(86362001)(8936002)(5003940100001)(16796002)(19580405001)(7846002)(50226002)(105586002)(48376002)(101416001)(229853001)(36756003)(8676002)(53416004)(5660300001)(586003)(4326007)(5001870100001); DIR:OUT; SFP:1101; SCL:1; SRVR:CY1PR12MB0534; H:SATLEXCHOV02.amd.com; FPR:; SPF:None; PTR:hybridsmtp02.amd.com,hybridsmtp.amd.com; MX:1; A:1; LANG:en; X-Microsoft-Exchange-Diagnostics: 1; BY2NAM03FT003; 1:Xed9W65Q1VEbFVMdrTHzp9I7tCciQ45089bycmtCkWUy+9z9TPK49KbSA9d3yugCyWAhdu0ucSYZLlCAbTvkRlEdZW5gQ9VxfcOVfOYriiJte1m6ddivt3XzIDLZIy7ws4jI+1SGAag7GKwQuWhNtYi6an+Q0ksF5DYbnmpfcLFv0lXz7wDc3ZWFM3CzQuYy9PZEMQDSVd6ymS9YtKJQPJK27ZS5F6woZiu4ZXQINl99eBYj26G6c1D8wkv4hOVTcBOtkSc8Kgj01dZCoHVs7mbO1MikxghPHJzKn55p33ASkSTeKooCgeORrd1yIIuT28/YnUKbSySaBsqgjqzKkY+CJUAq/dc3rlOi54mbAi9maiSRk04L7zHmZIY50LtA1RbmQOg7T8A3QQBpjTBwv0T+haGjUzsyODXkSa1uNmNMBoASu0gNVp4sLtjJ26tXRyzgOjE+KB14LR9R9g1oWnfquW1xaeFlgCgCWIHIMi8qf4+LzHPJFA3HQtSLaA5QdGnBWJEZ7N13/umaONaY4w== X-MS-Office365-Filtering-Correlation-Id: d0d187a1-86ff-476e-85ce-08d3d6cee1fe X-Microsoft-Exchange-Diagnostics: 1; CY1PR12MB0534; 2:mjg0XLcRZdnr0ZKjBdJQqhlvz6VSn6mqOdEEa4uCRYFsqLI56WEBn4JFMdD9ZJCV2vgQ03Dn6eQe4o2WaM3FB+YC8z8sWT91uH3AQzKC3gEoiOv/GQUDRtV2cMiYoSIGJrsfMigqO5WR8Q59Kj/sxYqMrr7HbE5r8DE+Y71+mMdtyQXqgL7cE4qPaYzFoN88; 3:hvm7z8kThWFpp7nadkzIzhwz3nEyakMBgaJdyas1+LXPGFCqIvl3zGfGsT9jgPlUHA249E+mz/6ucy8AweT8ZX10rdlVfpxOGe9cjf8R8XuVN9OjMEeLsYMDmHnWPjLFUTgPzCOOWIssZcobkLoeRRJit6SvlLIi3EVnl9YUz50hT+WzEst7OtnE67pvKRvLKt1cgCB5Df/166uqF2xUgWosAMmVie2/0kfDl3/vreo=; 25:NhEC6eEOEfK0u5+43ucS4De1uC+3ad/YYG9dHbOY3DhdDPR3y4LcJCo5WaZJUq4nVCc24CzxwexAZE8EsSyHlrX1huaOggvW0yvU2AiZcWf99fCK4IRicW0hOHr44AsNUzmBNJutWCImjYd1lGmQqlvTmBlOwwBsdoipWmkHCQpcWg7Y9XYf38LAD+AaZiZKBOevAOqo4rTu861lcucb6FNIrq9LDOuYI2kymnKUTPXqyTE3kpIJR+zQfVyyINLsslCzYcg6b94qx45k4aaR9q9mHVwTtWiKpHpiSdFfdnGSlW3444BXt57/ZHrRgRTohhtVhIAQ5msdCY2YZhxgUq4Y+mN7EbJgu4LlXk9RNXkya7Cjwd6FzEQiGwv57UNLN4ppE+K7nNuTFbNxyHnXiIHm6vMluKEpWbN66yO8U0Q= X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:CY1PR12MB0534; X-Microsoft-Exchange-Diagnostics: 1; CY1PR12MB0534; 31:fVHrg4/stEbwhEU99TP3KglrejXdQEEywNvnfJ9u8vat6CajHr0pl96nw9zl5yfQA9r4n44jE1VzPg8j92cTQJhFO6WFq8b7TjAHmPET0wEINu8C0UbRE3dqR96ifYf0S218Uof+PU560NsJfkKDGv1aaBFdaLbdQtgwFM4ZTcsAR9tHAXcnm7QnROakbUVc1KVnzvnlv+1sd9ar1FpXDlu0sHAp6K8puuaLNDclPOA=; 20:uJrlbjV13GG1YQ6snMQlMbf2+gp14A+Axehupc1/QPksrxuydEz6Add/mfDvbLEAt3EG4L71cqT18e/S+03nvH31nzE89Wu56FbwVNIeVz07R2HCWPNvFxb6AywUByTmPLHAVWDptxvwLQuM756y9B/PHztziOoF4rS7G1wgx1CPIOee7rLIJPPyQoO9KmgGkLfLgdVBm+phfYtrrZlZTgBjMoqBZAP2aLHBhKloe1QvLCJg+ZtFlYiOAN29hmE4yj4NF6kHDHjlaXrJfryhNtaAVmq+efWTkuv+nx+V1tHHyxIVidsqTMF7HzikU5E5cXMuvXOPJjkovBhsR8iXWa6tQdW2TdGhp8hYh1yv6ZFnjPzVxLW+FmSxVQqwDy6ZN1n5nl04ilaiQVC4KSzdqC5dne4aYp2BMWusb8OwmKCLF8GGjxRBhw0toZtJL/bqfh//kbWA1AoZtBvg/3a4mtnDjQ/FBeUIOqvQzaa8LyW0jf2JKS9Vlfp7taOLALm8 X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(767451399110); X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(6040176)(601004)(2401047)(13013025)(8121501046)(5005006)(13023025)(13021025)(10201501046)(3002001)(6055026); SRVR:CY1PR12MB0534; BCL:0; PCL:0; RULEID:; SRVR:CY1PR12MB0534; X-Microsoft-Exchange-Diagnostics: 1; CY1PR12MB0534; 4:EWWFaHbeXbRTXzGJ6+UyscMNbEdG6GnZNF/vEDLKV4VoVPXX+G7xkM0DXSgZemmWVdvWVwUtVCerivedSf2DmzdeXEFrCKoqVV8gSdagL/osn4NZxOpKl3IaEABD0/hIkIAVJrUehPVOe3CQ45k/mR1wBQIGvz9nnblUREIG+vOIvuGuCSM9JLft07+bacZUniUnfgyatRXvtMFBQ1Gd3CwN+qMhOWMK1laI+AmsucHMaZ4vJ2JQ3//M2QKSAMZwvxIZ5MrKdDkYhlsZCBJ1KNYBnnAntBiKHpoRQSexitfZ6PZQZ9Al9H8XdsIWkFxjpQpw/5oyWpqmHNiBvcD3X8Aeq6z3FduqwBdu7F4ok76GqjCa+R3KZ0xwNs7MykHumnV8RNmT1r8ngwjMgNtNVO1xupus30/i9b0gAAKCj49GtbQPRrLNAN4s5oLypjwt4bvXkxFeE6k1/EXHzObrpHC76BvaQELKrQzYlmTWoyt/aYxmwncoIXgdlMGKbIsa X-Forefront-PRVS: 0058ABBBC7 X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; CY1PR12MB0534; 23:mRwJXoO/Z+3LMGqtb7MobAKOqIlOf3IYXXPBQxSye?= =?us-ascii?Q?RYHCSif4FSDwQ5PRE4kiiSNvb74udX9/Jc3Cdxh2OXsmALurOsooE2yebD1T?= =?us-ascii?Q?x0S22WiXFqk0xZRvtwNpcCyPsIx3ICoiW2kVLIObEEMbhQQZKQxMpW7p/lzw?= =?us-ascii?Q?nm9OkHD0oHi3WurZFdPgrkl5IpkNx23AMwqvuAB0ryJkSy5TDUUWbjvuhzp3?= =?us-ascii?Q?wlwxff+roaPkivcF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fKA/GHkQAJX9zATHSbJAOeGKdXFNHgg2EvHAA4R5ugGCAvG4R3ZeAT9AxK8afCBLZ6UII4HcE3lthsk9hA6pGOf4dQk8tIPlg/S8qB0IvBQiZ7O0VaudA6dgIdUfmgS+cDsJlfJQg/xDiux4KlbDftDsxwKLyeoDkvaEeFZ95gU+KlAhijZZ4IT6LwlxQ X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Sep 2016 03:27:23.0970 (UTC) X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d; Ip=[165.204.84.22]; Helo=[SATLEXCHOV02.amd.com] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY1PR12MB0534 Cc: Ken Wang , Huang Rui , amd-gfx@lists.freedesktop.org X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Virus-Scanned: ClamAV using ClamSMTP In previous drm_global_item_ref, there are two times of writing ref->object if item->refcount is 0. So this patch does a minor update to put alloc and init ref firstly, and then to modify the item of glob array. Use "else" to avoid two times of writing ref->object. It can make the code logic more clearly. Signed-off-by: Huang Rui --- Changes from V1 -> V2: - Add kfree exceptional handle to avoid memory leak. - Improve code style. --- drivers/gpu/drm/drm_global.c | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/drivers/gpu/drm/drm_global.c b/drivers/gpu/drm/drm_global.c index 3d2e91c..6a63808 100644 --- a/drivers/gpu/drm/drm_global.c +++ b/drivers/gpu/drm/drm_global.c @@ -65,30 +65,31 @@ void drm_global_release(void) int drm_global_item_ref(struct drm_global_reference *ref) { - int ret; + int ret = -ENOMEM; struct drm_global_item *item = &glob[ref->global_type]; mutex_lock(&item->mutex); if (item->refcount == 0) { - item->object = kzalloc(ref->size, GFP_KERNEL); - if (unlikely(item->object == NULL)) { - ret = -ENOMEM; + ref->object = kzalloc(ref->size, GFP_KERNEL); + if (unlikely(ref->object == NULL)) goto out_err; - } - - ref->object = item->object; ret = ref->init(ref); if (unlikely(ret != 0)) - goto out_err; + goto out; + item->object = ref->object; + } else { + ref->object = item->object; } + ++item->refcount; - ref->object = item->object; - mutex_unlock(&item->mutex); - return 0; + goto out; + out_err: + kfree(ref->object); + ref->object = NULL; +out: mutex_unlock(&item->mutex); - item->object = NULL; return ret; } EXPORT_SYMBOL(drm_global_item_ref);