From patchwork Mon Jul 24 21:14:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Micha=C5=82_Winiarski?= X-Patchwork-Id: 13325330 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 32997C001B0 for ; Mon, 24 Jul 2023 21:15:25 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id EA64A10E35B; Mon, 24 Jul 2023 21:15:22 +0000 (UTC) Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by gabe.freedesktop.org (Postfix) with ESMTPS id 1F1B810E35B; Mon, 24 Jul 2023 21:15:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1690233321; x=1721769321; h=from:to:cc:subject:date:message-id:in-reply-to: references:content-transfer-encoding:mime-version; bh=rhtNiYYVteziVFqrrQ6PoJGiyXUfob6EsfrQkEYBa3E=; b=T8DJ9B/fxGw3I3Xo79SGcJCcd4FKqv+N5Ef4hlncm56r+2VXX5PUkW1e RjOTwO+v7xuoCw5waHQINBdQNQokOqShnC2aHp0bmss2eId1TrCWCYu33 SGvC4QBmjbSRFfJsOFp+ZOJqwzDqmu3IUqg55afSKBQSZ3Utdi62+jt1T vr6MQaRs7EQ7Ni2FlJ1gQYriXlKNEw7BInBlaMjYRMHEdqoj0TW9MKEQi Gyl3ZJx+EhveCv6Q5Ywx6BPLjvSIb+3+GtwrOdmMrIS6klQ3qPiR0qvAE P51zFnzDLdEtSSlJYPqIv1MehseyTDtz8VChrlb1c+P0qU339QkHsqGJ8 g==; X-IronPort-AV: E=McAfee;i="6600,9927,10781"; a="453931075" X-IronPort-AV: E=Sophos;i="6.01,228,1684825200"; d="scan'208";a="453931075" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Jul 2023 14:15:20 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10781"; a="1056569339" X-IronPort-AV: E=Sophos;i="6.01,228,1684825200"; d="scan'208";a="1056569339" Received: from orsmsx602.amr.corp.intel.com ([10.22.229.15]) by fmsmga005.fm.intel.com with ESMTP; 24 Jul 2023 14:15:19 -0700 Received: from orsmsx610.amr.corp.intel.com (10.22.229.23) by ORSMSX602.amr.corp.intel.com (10.22.229.15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.27; Mon, 24 Jul 2023 14:15:19 -0700 Received: from ORSEDG601.ED.cps.intel.com (10.7.248.6) by orsmsx610.amr.corp.intel.com (10.22.229.23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.27 via Frontend Transport; Mon, 24 Jul 2023 14:15:19 -0700 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (104.47.70.108) by edgegateway.intel.com (134.134.137.102) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.27; Mon, 24 Jul 2023 14:15:19 -0700 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=dBUlf3hSNxW3ay3fAuKJMln/8y3mrAYst5TdgkurJCpom7Su0xBCcCC7KZCAw/SDjr+IpqW0NBV9AbBH98wcBtb2qo/ry2oT8/Djty+hY0hSGsy6GRBsqKT5U8CVBZb5g1Co6K+zXOz4ZxEFKpMFWNbScYL0O5XOaOhJiYUpPF5cD/J7KNL/JUkY6/LxlGvu4X0XjmFRy0BT2pv3NLtOWadYXMiONT4/d+XgDhLkQ41cMqXY4Tqso+XLsQ28mBv28/1e3jaCmukh8fff4yp3lQ2PxfLNg24WV+96tFbkGbST9XyOvCeBY/TCXQlu2ubz2SZ+DZRbNZvdhT2Vf3VwPA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=SAQjAU/oQDuqdibu8w6peKc1dtCBsSy6GPLMGiJD/a0=; b=QKGSoKo6Zb5+5BVuJU6Ch4wUcwf4e6T+jYDnpM2boWfbei2IXRR6JZpXyWblIKtofPzxombIncuQ/bFP4CpaPxDeGEm5r5kFvZ6S62p4/RgsczuBGIm7UpR7tcAL3SvaWoVc+vXuZB/LPOBk7+aiDYld0fnvSKUi/XX/4SX1KYERGofMqiZU07A3Q4hYgX3UcvgdQTTmjLhvGOt05AUhOosgySQtfmzO+CxtsvVyJc7112VY0/PLWLhtswscOzjkrBCWln0XJvUCWCjQSGvfnGSWodRQ99larSrjoe825MVQunE7+XMLgTGJ/ZKEZ6xlbNJSOErQywgy70bIIgZFVg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=intel.com; dmarc=pass action=none header.from=intel.com; dkim=pass header.d=intel.com; arc=none Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=intel.com; Received: from DM4PR11MB5373.namprd11.prod.outlook.com (2603:10b6:5:394::7) by MW5PR11MB5809.namprd11.prod.outlook.com (2603:10b6:303:197::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6609.32; Mon, 24 Jul 2023 21:15:16 +0000 Received: from DM4PR11MB5373.namprd11.prod.outlook.com ([fe80::1127:c109:d888:f6d1]) by DM4PR11MB5373.namprd11.prod.outlook.com ([fe80::1127:c109:d888:f6d1%5]) with mapi id 15.20.6609.031; Mon, 24 Jul 2023 21:15:16 +0000 From: =?utf-8?q?Micha=C5=82_Winiarski?= To: , , Subject: [PATCH v6 1/4] drm: Use XArray instead of IDR for minors Date: Mon, 24 Jul 2023 23:14:25 +0200 Message-ID: <20230724211428.3831636-2-michal.winiarski@intel.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230724211428.3831636-1-michal.winiarski@intel.com> References: <20230724211428.3831636-1-michal.winiarski@intel.com> X-ClientProxiedBy: FR0P281CA0147.DEUP281.PROD.OUTLOOK.COM (2603:10a6:d10:96::11) To DM4PR11MB5373.namprd11.prod.outlook.com (2603:10b6:5:394::7) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM4PR11MB5373:EE_|MW5PR11MB5809:EE_ X-MS-Office365-Filtering-Correlation-Id: ef3ff2a9-ff21-4954-cbdc-08db8c8b13b7 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: ulmgBOlYXeBgg4WDtpKRrg69uU5vqhsZXE4SvzpBdnnamJPxtgdCwTzC/nd8v6a5fCC1ABCrYPBZcWkQs/g99tTNYWoxhfItvRNz1Up4Lq9t1d1JdhIrpjUQ4f//enjacbZmEKcWikuKSEaeMwtu77kPyhdZ9FqNGrimHiea/LGblJdxFQmD6V0gmLlT9WGnjh2Tp8i+Myrg8h0m++jqzOZu7N/QAuMFzD31gKko/aYThC8vLpWwgOj/XEd28Nnnr74UrBqvA+i+Ji8Hh3Y1Z5yoYoCBJDg4ykHyqwGKVSKMg1sCWAWdvlO5OPuUPlE3MhYXji/trL1gpcYrYdfV/NnI2GES/WYomDy/E9reHlF2InzyzN3DOUmwESUbo5HZopAa32H0gYi4oFEGAyLQsr3Q5PjaNEsmb/h23LnYBJAa5GJ73eSFH496kcvCDovFwMnf+wACntyqFs5roCNPuU8VQ3HsWRn+/x5Kf/uBQ6FKNS6G9BD19fh+KbOgkjreZLatf9E4l/Otl/efWY46fu4pMWbh/6HSuNP4DPd1vUO7H8AUGu87flmB5j3hPiP4 X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:DM4PR11MB5373.namprd11.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230028)(136003)(346002)(376002)(39860400002)(396003)(366004)(451199021)(83380400001)(38100700002)(36756003)(82960400001)(86362001)(54906003)(2906002)(186003)(6506007)(1076003)(26005)(6666004)(6512007)(6486002)(8936002)(5660300002)(8676002)(7416002)(478600001)(316002)(41300700001)(66476007)(66946007)(66556008)(4326008)(2616005); DIR:OUT; SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?qSJKUetv0SWghLaPVzHAwwMouwo2?= =?utf-8?q?Ds/0Hfj+YTrES8JOfETcA7IEKBCmFqdbrqHtObtKPYael1KE+Wh8OcsmgrhekbsZS?= =?utf-8?q?TZIjWuT74wGuaS3Vb/17c/dT1NDIY84Ux290iSEMafAdW/j6fz5DLZCaOcPTWU17g?= =?utf-8?q?/2Zon0ZXfDJlTvaJxW0QjrFB52O6MW2FnIyOHeGCyyfsoz722zoNqB3yX9+WTtgSJ?= =?utf-8?q?CLmMIjh0JinGMp8TUNPa8IwG/G1WijeuvuYukbvmfybNckDA0MnFQ18DPawyc4g2Z?= =?utf-8?q?BskhShtYeScWPLRBNKP3ZY9q0knPVMiXaLKXesGd7I5nWNb5JjZxZOHQL3pKyMnvF?= =?utf-8?q?N3J5bullt7PCvQE2Sh+vDCZfJxrQ7xtPYnVZAi60frTcgMXjmdc2L/V4V0wSffkzE?= =?utf-8?q?12DPKHquTr4H0LqBf1OsMkKDPR+em0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qShU1NBP5UViuww2D4ij7b0f?= =?utf-8?q?cmfw1ypOTnGuq/eqmhbm40P9NJkklgWQyHK+Y/VVrkRs/xze+YdhpJmYG1swB0fjg?= =?utf-8?q?ihnWuAg+jkRFpIbDII8oiWNxKQbujFkfJ//ADTXHEiI1REuNp6K6h0bx0vXS2zHT3?= =?utf-8?q?+zWr+vZ0awOqCkeBePO2n4dPRA2ybANE5RI/8hZBHoO0xp3m9d4Nrp0n31DnWVW5u?= =?utf-8?q?BddGxJqqVu/4kPMJ21V1BhCg8hoHfD4tDA=3D=3D?= X-MS-Exchange-CrossTenant-Network-Message-Id: ef3ff2a9-ff21-4954-cbdc-08db8c8b13b7 X-MS-Exchange-CrossTenant-AuthSource: DM4PR11MB5373.namprd11.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Jul 2023 21:15:16.3583 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 46c98d88-e344-4ed4-8496-4ed7712e255d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: KgjCtM807tkgDF2zkSAgfGuONWUFBOmXosXi3Zb2gtPPDhFCO6hvUwIW5SxnIxHwPG2HCLryySPIxt+ntjT0pi3yPWgeasljxW89pEWGP2w= X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW5PR11MB5809 X-OriginatorOrg: intel.com X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Pekka Paalanen , David Airlie , Oded Gabbay , Maxime Ripard , Emil Velikov , Matthew Wilcox , Thomas Zimmermann , =?utf-8?q?Micha=C5=82_Winiarski?= , James Zhu , =?utf-8?q?Christian_K=C3=B6nig?= Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" IDR is deprecated, and since XArray manages its own state with internal locking, it simplifies the locking on DRM side. Additionally, don't use the IRQ-safe variant, since operating on drm minor is not done in IRQ context. Signed-off-by: Michał Winiarski Suggested-by: Matthew Wilcox --- drivers/gpu/drm/drm_drv.c | 63 ++++++++++++++++----------------------- 1 file changed, 25 insertions(+), 38 deletions(-) diff --git a/drivers/gpu/drm/drm_drv.c b/drivers/gpu/drm/drm_drv.c index 3eda026ffac6..3faecb01186f 100644 --- a/drivers/gpu/drm/drm_drv.c +++ b/drivers/gpu/drm/drm_drv.c @@ -34,6 +34,7 @@ #include #include #include +#include #include #include @@ -54,8 +55,7 @@ MODULE_AUTHOR("Gareth Hughes, Leif Delgass, José Fonseca, Jon Smirl"); MODULE_DESCRIPTION("DRM shared core routines"); MODULE_LICENSE("GPL and additional rights"); -static DEFINE_SPINLOCK(drm_minor_lock); -static struct idr drm_minors_idr; +static DEFINE_XARRAY_ALLOC(drm_minors_xa); /* * If the drm core fails to init for whatever reason, @@ -101,26 +101,23 @@ static struct drm_minor **drm_minor_get_slot(struct drm_device *dev, static void drm_minor_alloc_release(struct drm_device *dev, void *data) { struct drm_minor *minor = data; - unsigned long flags; WARN_ON(dev != minor->dev); put_device(minor->kdev); - if (minor->type == DRM_MINOR_ACCEL) { + if (minor->type == DRM_MINOR_ACCEL) accel_minor_remove(minor->index); - } else { - spin_lock_irqsave(&drm_minor_lock, flags); - idr_remove(&drm_minors_idr, minor->index); - spin_unlock_irqrestore(&drm_minor_lock, flags); - } + else + xa_erase(&drm_minors_xa, minor->index); } +#define DRM_MINOR_LIMIT(t) ({ typeof(t) _t = (t); XA_LIMIT(64 * _t, 64 * _t + 63); }) + static int drm_minor_alloc(struct drm_device *dev, enum drm_minor_type type) { struct drm_minor *minor; - unsigned long flags; - int r; + int index, r; minor = drmm_kzalloc(dev, sizeof(*minor), GFP_KERNEL); if (!minor) @@ -129,24 +126,17 @@ static int drm_minor_alloc(struct drm_device *dev, enum drm_minor_type type) minor->type = type; minor->dev = dev; - idr_preload(GFP_KERNEL); if (type == DRM_MINOR_ACCEL) { r = accel_minor_alloc(); + index = r; } else { - spin_lock_irqsave(&drm_minor_lock, flags); - r = idr_alloc(&drm_minors_idr, - NULL, - 64 * type, - 64 * (type + 1), - GFP_NOWAIT); - spin_unlock_irqrestore(&drm_minor_lock, flags); + r = xa_alloc(&drm_minors_xa, &index, NULL, DRM_MINOR_LIMIT(type), GFP_KERNEL); } - idr_preload_end(); if (r < 0) return r; - minor->index = r; + minor->index = index; r = drmm_add_action_or_reset(dev, drm_minor_alloc_release, minor); if (r) @@ -163,7 +153,7 @@ static int drm_minor_alloc(struct drm_device *dev, enum drm_minor_type type) static int drm_minor_register(struct drm_device *dev, enum drm_minor_type type) { struct drm_minor *minor; - unsigned long flags; + void *entry; int ret; DRM_DEBUG("\n"); @@ -190,9 +180,12 @@ static int drm_minor_register(struct drm_device *dev, enum drm_minor_type type) if (minor->type == DRM_MINOR_ACCEL) { accel_minor_replace(minor, minor->index); } else { - spin_lock_irqsave(&drm_minor_lock, flags); - idr_replace(&drm_minors_idr, minor, minor->index); - spin_unlock_irqrestore(&drm_minor_lock, flags); + entry = xa_store(&drm_minors_xa, minor->index, minor, GFP_KERNEL); + if (xa_is_err(entry)) { + ret = xa_err(entry); + goto err_debugfs; + } + WARN_ON(entry); } DRM_DEBUG("new minor registered %d\n", minor->index); @@ -206,20 +199,16 @@ static int drm_minor_register(struct drm_device *dev, enum drm_minor_type type) static void drm_minor_unregister(struct drm_device *dev, enum drm_minor_type type) { struct drm_minor *minor; - unsigned long flags; minor = *drm_minor_get_slot(dev, type); if (!minor || !device_is_registered(minor->kdev)) return; /* replace @minor with NULL so lookups will fail from now on */ - if (minor->type == DRM_MINOR_ACCEL) { + if (minor->type == DRM_MINOR_ACCEL) accel_minor_replace(NULL, minor->index); - } else { - spin_lock_irqsave(&drm_minor_lock, flags); - idr_replace(&drm_minors_idr, NULL, minor->index); - spin_unlock_irqrestore(&drm_minor_lock, flags); - } + else + xa_store(&drm_minors_xa, minor->index, NULL, GFP_KERNEL); device_del(minor->kdev); dev_set_drvdata(minor->kdev, NULL); /* safety belt */ @@ -238,13 +227,12 @@ static void drm_minor_unregister(struct drm_device *dev, enum drm_minor_type typ struct drm_minor *drm_minor_acquire(unsigned int minor_id) { struct drm_minor *minor; - unsigned long flags; - spin_lock_irqsave(&drm_minor_lock, flags); - minor = idr_find(&drm_minors_idr, minor_id); + xa_lock(&drm_minors_xa); + minor = xa_load(&drm_minors_xa, minor_id); if (minor) drm_dev_get(minor->dev); - spin_unlock_irqrestore(&drm_minor_lock, flags); + xa_unlock(&drm_minors_xa); if (!minor) { return ERR_PTR(-ENODEV); @@ -1067,7 +1055,7 @@ static void drm_core_exit(void) unregister_chrdev(DRM_MAJOR, "drm"); debugfs_remove(drm_debugfs_root); drm_sysfs_destroy(); - idr_destroy(&drm_minors_idr); + WARN_ON(!xa_empty(&drm_minors_xa)); drm_connector_ida_destroy(); } @@ -1076,7 +1064,6 @@ static int __init drm_core_init(void) int ret; drm_connector_ida_init(); - idr_init(&drm_minors_idr); drm_memcpy_init_early(); ret = drm_sysfs_init();