From nobody Sat May 4 02:52:39 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; arc=pass (i=1 dmarc=pass fromdomain=epam.com); dmarc=pass(p=quarantine dis=none) header.from=epam.com ARC-Seal: i=2; a=rsa-sha256; t=1672666965; cv=pass; d=zohomail.com; s=zohoarc; b=cXFZkMQ+gTsyKjRkjU76sBq3AzJq6pJy6e/VFRt5y2cCiBWp6eiPB4IT1k5T87ifjHJJfH5o5sYOEprXfobCA9C1Qn7B2M66qQqiCtMrHNjh2QnGa54k8SwtYFPJwpFlvz799X28ZUbcuCD61ReH/DdUatnSKgvpDqFEAmlvNWo= ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1672666965; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=upLENppnbaOJqNZ7wAgRPaS772ApiRNNpeCnQADukok=; b=lAur5gkXoi0vljYhEmMws9lhyHB5DX0jR7FesRWm1HB7ZZbHZGZpNk/+vl8mnW2rUV0Q6oN1U9n+edtKTUEV7Op7Z3uROCepv2+4dPTRi1ClHWIssgrbCX+HrG42UAFSRHtqV8ssZNroTREqKm9qBYCqpm6YvbSM+qF7Sqs6TLQ= ARC-Authentication-Results: i=2; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; arc=pass (i=1 dmarc=pass fromdomain=epam.com); dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1672666965911680.5348015153725; Mon, 2 Jan 2023 05:42:45 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.470288.729746 (Exim 4.92) (envelope-from ) id 1pCL4u-00028V-Ac; Mon, 02 Jan 2023 13:42:12 +0000 Received: by outflank-mailman (output) from mailman id 470288.729746; Mon, 02 Jan 2023 13:42:12 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1pCL4u-00026Y-3h; Mon, 02 Jan 2023 13:42:12 +0000 Received: by outflank-mailman (input) for mailman id 470288; Mon, 02 Jan 2023 13:42:11 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1pCL4s-00020m-W3 for xen-devel@lists.xenproject.org; Mon, 02 Jan 2023 13:42:10 +0000 Received: from mx0b-0039f301.pphosted.com (mx0b-0039f301.pphosted.com [148.163.137.242]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 3fe4c446-8aa3-11ed-91b6-6bf2151ebd3b; Mon, 02 Jan 2023 14:42:08 +0100 (CET) Received: from pps.filterd (m0174682.ppops.net [127.0.0.1]) by mx0b-0039f301.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 302Bx46D011991; Mon, 2 Jan 2023 13:41:52 GMT Received: from eur04-he1-obe.outbound.protection.outlook.com (mail-he1eur04lp2054.outbound.protection.outlook.com [104.47.13.54]) by mx0b-0039f301.pphosted.com (PPS) with ESMTPS id 3mtd0uv746-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 02 Jan 2023 13:41:51 +0000 Received: from PA4PR03MB7136.eurprd03.prod.outlook.com (2603:10a6:102:ea::23) by DB9PR03MB9709.eurprd03.prod.outlook.com (2603:10a6:10:459::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5944.19; Mon, 2 Jan 2023 13:41:48 +0000 Received: from PA4PR03MB7136.eurprd03.prod.outlook.com ([fe80::2da6:6d63:389b:3552]) by PA4PR03MB7136.eurprd03.prod.outlook.com ([fe80::2da6:6d63:389b:3552%9]) with mapi id 15.20.5944.019; Mon, 2 Jan 2023 13:41:48 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 3fe4c446-8aa3-11ed-91b6-6bf2151ebd3b ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=EH7Q1ygdq8hb6QLZQg+FO3oVGM0vm/dgC8rYDMaCGbqIzXkBuWvCcCjEkT9BLFqskHwaumIyq3O3nt6S5Lv7I20UGLq5EbJEuzTyi4uAGvxCefF2eJ+MLBNKzcGQCMf99/O6b+D/cvmd1SOTEBLp7S5aSdrAICK71R/mpn+6dPMsNBX9SvNqhNas0XK59rwP+6JqZz6b8q8+ito9T2Q97MSKzUYHfnpWR1KMBwizuOo6HjMPZZL6snO3eWsUlI+d3zRu+9cGTFheT0qcGXU+/YLlh1I79DJOB+VbOub05RYYlQ3TvBxRSNAp4DZQG2EoyCpsMwrK/QjvIfQcEebgkg== 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=upLENppnbaOJqNZ7wAgRPaS772ApiRNNpeCnQADukok=; b=h+zfyrOORIykAmzlkpyogetoAix8Ex/PKUn+FKR/CFwgSrN2riv/nmkccGuDkFF3iiK9iWQe4JH40PFdW3gVJQEqnW3kaaGslapFEmRK/TraLd/VkjPYvKzhHc9aCfG7l/LljrHRLbsIZN4YNplomA3lqaUPLyMRuTyv39tjSkBm4Ko4k2vNmtjtlXIBF4EDigFUpgo5qiSl30DEz2m9ro3GF2XAHRxccHCCeMtd+eeMWzpe7xFrvksY2hejLBl8G0Lkv2341IL6xZ1hUE+aloTtaisvD3JqTfwBOK8rQ+DZVOtP83gFWo1r5OFzyV+B5k7px/Hh7RzcV7YOA9L8ww== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=epam.com; dmarc=pass action=none header.from=epam.com; dkim=pass header.d=epam.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=epam.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=upLENppnbaOJqNZ7wAgRPaS772ApiRNNpeCnQADukok=; b=T3oL8E80Zu6y5uljNdl6ASKZKYjjEA7AvCxRY+i21xtxiBfvjMG6/xT7U1cxnRV8lE86tjF/e6m7K0WSOpKufW2F26xxojhuABWsRb9Rdktjhc9cFPEfgcVpTiLOWy350xpe2P9Tb2eXTIs7kvUmoOuOUXre2jiercXn7OwfUsXdDUa48eBYSKuWR4trfV7gAhRp6gSvM8JJhEtgWUpo/9Y71yLuG4xGOvkI1CvXsSA2dU0+dGAeyRGFM6zAuqb440w9PlqSo0KFmegGnGw7qbB41zxXhotEha0P45ur/8uCbWEGqGTmJVe3xyippsarZ1xnEtawF+otzRYHSeUnlg== From: Oleksii Moisieiev To: "jgross@suse.com" CC: Oleksii Moisieiev , Stefano Stabellini , Oleksandr Tyshchenko , "xen-devel@lists.xenproject.org" , "linux-kernel@vger.kernel.org" , Sumit Semwal , =?iso-8859-1?Q?Christian_K=F6nig?= , "linux-media@vger.kernel.org" , "dri-devel@lists.freedesktop.org" , "linaro-mm-sig@lists.linaro.org" Subject: [PATCH v1 1/3] xen/grant-table: save page_count on map and use if during async unmapping Thread-Topic: [PATCH v1 1/3] xen/grant-table: save page_count on map and use if during async unmapping Thread-Index: AQHZHq/2VrVTMTTaekaw3MeMlNz4/w== Date: Mon, 2 Jan 2023 13:41:48 +0000 Message-ID: References: In-Reply-To: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-publictraffictype: Email x-ms-traffictypediagnostic: PA4PR03MB7136:EE_|DB9PR03MB9709:EE_ x-ms-office365-filtering-correlation-id: 01aafa7a-9d3f-4d4e-013e-08daecc718ea x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: qufCfaYw3n7K5GBDOfPY8lJ0s2EbpdNaaVh1FB2Pw+YoK5QwbfcpPET4AXM5Toz2dzO9dIr+DdNC4joci4/RzuhGLqi21535Yz3P3srG6Lpcnh7CUF2hJv/HHJIo0FYK7JNW0GwdhZT8nsBTZUNHceKZjm5bn/VOJRDshf278kimt9ZzSTXviP06ZWob69c/WnQGL5BMf34w91h10zbmqzZwUdRBsgn44uTXvUfabiQWwUdSUvqiZfxXh7MADOPECKVxUZpOM+ev0TJxcSh9SFuc2DE9zKdog0Ikyu9z+yKmTFvPtmmAb5B+PqNXAtO8c/7NqDZLxOjEnXUq1yJO00YW3fodzAxmUGKLwvsS0s1kIr3BsboABlbV5GsHXpJlano+8KhyfZYdjFzYqPFo21E+sg5CTewTHHOL7kLIJkwQEpAJaC4XCTxhA/SLDVpj6ZCCnAzR89BcHAbZeXMQyB2Hjwi+q+/YDnZix3Vsht6ar5afyQ0WGDdtD08a3BCnkOAZ3g+WgyqselwMRSFAVdlRLIVolZKEsWGR8iwB3eEXrak1oySjbeKAjKnaVScUQDaILO8fZ/9v6KZQz/ic05R5cFDigD6yCyMTnU6xaQWRkCEakZ/7/gyGCyVciq/zys1pLiMmM3pGFA4XlGHpy9WESyonSwVWHzF22oyBXuxZC+T/5ValxPLViewW81d699ESQS/SFiQXon855al4XyCzxm+4AUow5YOHP5/f9ao= x-forefront-antispam-report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:PA4PR03MB7136.eurprd03.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(4636009)(396003)(346002)(136003)(376002)(39860400002)(366004)(451199015)(38100700002)(122000001)(86362001)(8676002)(38070700005)(66476007)(76116006)(66946007)(64756008)(66446008)(66556008)(6916009)(91956017)(4326008)(41300700001)(54906003)(316002)(2906002)(5660300002)(8936002)(2616005)(83380400001)(6512007)(478600001)(71200400001)(6486002)(6506007)(26005)(55236004)(186003)(36756003)(22166006);DIR:OUT;SFP:1101; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?iso-8859-1?Q?dANsysGQdFemPfZPLN3R5/iueQyffU7Aefc222DGZ8engqUXxDYFqDhhWs?= =?iso-8859-1?Q?klXBmUqO8vdTsG9eOYChTyQ3+PL/+hhuseX0TkGSJKfjWL5yL1FxZI3DLL?= =?iso-8859-1?Q?LNWx8q2ucibwxBygufAWID7zKm+cikKSS2rG1pejj+wR1TIuu+K4jplG65?= =?iso-8859-1?Q?gFh/+hDBrmYEOHMFr+uTX/Lh8QwwaB/6qHCUjgRwIdlHc3a3UYM96ErdWU?= =?iso-8859-1?Q?QOvxwx+ofVp73MmjqxruCfkoSXCYr6T3sXHXGJRNK6L5zKsa8qmeIqt7Zb?= =?iso-8859-1?Q?ZwrLGQMJPVB0+P6bg4yghqhyj69aQHwy0x6O94ezPzNMmQYcIMdO+ekY3u?= =?iso-8859-1?Q?TKEWrbhJMy+Smzzu52UFjuCq5NTQdEoX1mhiEvnomzVaryP9ZqHooMRPTT?= =?iso-8859-1?Q?2p9XEorxFULahLRpfxbZUE28+dnNoLxy3DwrzLfP2YecjeBmwxewRXbxMl?= =?iso-8859-1?Q?Q3g+1N68v6lKIRYINDvORC2v7aSOodhMMxcd1Fto72kkDvuQgDrnY27u55?= =?iso-8859-1?Q?8OmEd4flK0bQ2jx2sFB5LzGQFN6jUcJIKVIoUyM0lSuHQY+/6PKUKfp/IT?= =?iso-8859-1?Q?hCgXV/xWkEELFj5S8nu+/znOyxSwEWeXHshjI5VUt45VhiCvHaHNFzkoQM?= =?iso-8859-1?Q?VCcKqsJpv+w8qy4aC+O/zHSn88lQWNbMgd8YYWfRgM75keJMuZbWA1lHQr?= =?iso-8859-1?Q?DOnSVB7uyVzulo/R3rWzsTR6oXTE7k3smf49EChQUJRzono5yFwXGDKCfZ?= =?iso-8859-1?Q?Sw4/+83oz2cTfxyPYnd/AFfj+M5himSq/t282B5M00TU4Xz3b8YgeDs3Qg?= =?iso-8859-1?Q?J3ZBI/2oaAh5nWD9Y+7758VlM/ANYR9Aj46qDx+FOZzJN0cE/xwggKnVDT?= =?iso-8859-1?Q?0lX616JttIsRpZ0MYz0cDn+Z388IrOdy9tqgiGe3wvEZFujLz3XRxnyVlz?= =?iso-8859-1?Q?xRHIhUGyj6ii4r+jvD2KA8nvMV1mcxtbHkcKlzG9O5dn7+w1/pvVF8f8tI?= =?iso-8859-1?Q?nCJ/1SG18OlFiPoUQkKNXYi0gcjEdnxlIQ9Jp+Bt+NKwn9zr1VtjovfjPh?= =?iso-8859-1?Q?Rzun/CuzGk/fh2uuaVEadwIcntMwSGXrroTPLOiB17mDlUqd86F+lC/615?= =?iso-8859-1?Q?wEzM4vFGJo1gN5F/PmMfolS1kGUcXjVYdYC0wnTFtFlfW1BEZE9EW8MWmT?= =?iso-8859-1?Q?dQ/4u6JIBk/Kn8b9cXEOqmg2mcP6yGcIfARxp9CgDU9v9Zuwqe4Ut9v0iF?= =?iso-8859-1?Q?Q8QR4DIedxMgyTZPc6wfygq0+Prcu3CzYyAahoY7BAAROmXxKXvIuHxlx+?= =?iso-8859-1?Q?tWyMROlWn5GapUZEH34JKeVSD0H9TdnrT7yvsTncF5KCNdKVCtpOuoM+L1?= =?iso-8859-1?Q?PgL62+5zHNaB7PqzKmXwB0nwr9yOPIgblcWs1df9/tVt1syoCdf0YNcayZ?= =?iso-8859-1?Q?6706N9LqTL7QejzPhha14Lqym3usffPv8FSd80TdfyRJ5wv0ICxWXp8D/Y?= =?iso-8859-1?Q?gIqGHet/mIPyvNR10HTpJVUT1rXskt0g3Dc5EPWVM637dWaMp0SfhwOfT0?= =?iso-8859-1?Q?zyufy/nezBU2LaSsW2RaKNfl2Gp4tzoSSO3uoJzsHiIDy0pwY1tGcqeB1H?= =?iso-8859-1?Q?f/0OgvjdnGNOcLr6n/E1H018gdiz50V/9uefCUfa6ecGjGARXGxbNAOw?= =?iso-8859-1?Q?=3D=3D?= Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: epam.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: PA4PR03MB7136.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 01aafa7a-9d3f-4d4e-013e-08daecc718ea X-MS-Exchange-CrossTenant-originalarrivaltime: 02 Jan 2023 13:41:48.6825 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: b41b72d0-4e9f-4c26-8a69-f949f367c91d X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: kI1NPR1PtH+s/DzX1W50xaAwDmqfN4R/xQrpV0d+2vI3yUPaymI20bn90qI3J/4UWEJxnW+SL0clikRBIlzGek+KvB6SWXedvjDmoLuBIUU= X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB9PR03MB9709 X-Proofpoint-GUID: mk6-_2x5bNgRKHNXlG71jmOCVvk0mvbE X-Proofpoint-ORIG-GUID: mk6-_2x5bNgRKHNXlG71jmOCVvk0mvbE X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.923,Hydra:6.0.545,FMLib:17.11.122.1 definitions=2023-01-02_08,2022-12-30_01,2022-06-22_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxlogscore=902 phishscore=0 lowpriorityscore=0 bulkscore=0 impostorscore=0 spamscore=0 priorityscore=1501 adultscore=0 suspectscore=0 mlxscore=0 malwarescore=0 clxscore=1015 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2212070000 definitions=main-2301020124 X-ZohoMail-DKIM: pass (identity @epam.com) X-ZM-MESSAGEID: 1672666967388100001 Content-Type: text/plain; charset="utf-8" Save the reference count of the page before mapping and use this value in gntdev_unmap_refs_async() call. This is the enhancement of the commit 3f9f1c67572f5e5e6dc84216d48d1480f3c4fcf6 ("xen/grant-table: add a mechanism to safely unmap pages that are in use"). Safe unmapping mechanism defers page that may being use (ref count > 1). This is needed to allow to map/unmap pages, which have more than 1 reference. For example, DRM_IOCTL_MODE_CREATE_DUMB creates dma buffer with page_count =3D 2, which unmap call would be deferred while buffer exists because ref count will never equals 1. This means the buffer remains mapped during DRM_IOCTL_MODE_DESTROY_DUMB call which causes an error: Unable to handle kernel paging request at virtual address .... Call trace: check_move_unevictable_folios+0xb8/0x4d0 check_move_unevictable_pages+0x8c/0x110 drm_gem_put_pages+0x118/0x198 drm_gem_shmem_put_pages_locked+0x4c/0x70 drm_gem_shmem_unpin+0x30/0x50 virtio_gpu_cleanup_object+0x84/0x130 virtio_gpu_cmd_unref_cb+0x18/0x2c virtio_gpu_dequeue_ctrl_func+0x124/0x290 process_one_work+0x1d0/0x320 worker_thread+0x14c/0x444 kthread+0x10c/0x110 This enhancement allows to provide the expected page_count during map call so refs could be unmapped properly without unneeded defers. Signed-off-by: Oleksii Moisieiev --- drivers/xen/grant-table.c | 16 +++++++++++++++- include/xen/grant_table.h | 3 +++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/drivers/xen/grant-table.c b/drivers/xen/grant-table.c index e1ec725c2819..d6576c8b6f0f 100644 --- a/drivers/xen/grant-table.c +++ b/drivers/xen/grant-table.c @@ -1241,11 +1241,23 @@ int gnttab_map_refs(struct gnttab_map_grant_ref *ma= p_ops, case GNTST_okay: { struct xen_page_foreign *foreign; + int page_cnt; =20 SetPageForeign(pages[i]); foreign =3D xen_page_foreign(pages[i]); foreign->domid =3D map_ops[i].dom; foreign->gref =3D map_ops[i].ref; + page_cnt =3D page_count(pages[i]); + if (page_cnt > FOREIGN_MAX_PAGE_COUNT) { + /* foreign structure can't hold more than FOREIGN_MAX_PAGE_COUNT. + * That's why we save page_count =3D 1 so safe unmap mechanism will + * defer unmapping until all users stops using this page and let + * caller handle page users. + */ + pr_warn_ratelimited("page have too many users. Will wait for 0 on umap= \n"); + foreign->private =3D 1; + } else + foreign->private =3D page_cnt; break; } =20 @@ -1308,9 +1320,11 @@ static void __gnttab_unmap_refs_async(struct gntab_u= nmap_queue_data* item) { int ret; int pc; + struct xen_page_foreign *foreign; =20 for (pc =3D 0; pc < item->count; pc++) { - if (page_count(item->pages[pc]) > 1) { + foreign =3D xen_page_foreign(item->pages[pc]); + if (page_count(item->pages[pc]) > foreign->private) { unsigned long delay =3D GNTTAB_UNMAP_REFS_DELAY * (item->age + 1); schedule_delayed_work(&item->gnttab_work, msecs_to_jiffies(delay)); diff --git a/include/xen/grant_table.h b/include/xen/grant_table.h index e279be353e3f..8e220edf44ab 100644 --- a/include/xen/grant_table.h +++ b/include/xen/grant_table.h @@ -49,6 +49,7 @@ #include #include #include +#include =20 /* * Technically there's no reliably invalid grant reference or grant handle, @@ -274,9 +275,11 @@ int gnttab_unmap_refs_sync(struct gntab_unmap_queue_da= ta *item); void gnttab_batch_map(struct gnttab_map_grant_ref *batch, unsigned count); void gnttab_batch_copy(struct gnttab_copy *batch, unsigned count); =20 +#define FOREIGN_MAX_PAGE_COUNT U16_MAX =20 struct xen_page_foreign { domid_t domid; + uint16_t private; grant_ref_t gref; }; =20 --=20 2.25.1 From nobody Sat May 4 02:52:39 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; arc=pass (i=1 dmarc=pass fromdomain=epam.com); dmarc=pass(p=quarantine dis=none) header.from=epam.com ARC-Seal: i=2; a=rsa-sha256; t=1672666962; cv=pass; d=zohomail.com; s=zohoarc; b=h2eitizHzCVe9SXig0mo9c99jWwzBmqVLZ9ANA6vQjXfX/Hu4lR1Y/T6eeAI507JQnYsPv4CLqpOvD7KygN3G72jDXWzsk8MvloRt+dz+G7uTdnAgoTZ+9DV3f91+h9XHzjEMsuJ7qLme2htqu3MT0QdpCF2/Z758LUay7Yz1LU= ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1672666962; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=cIV9qnXRfvVgBfrc0HVcsMdFAbZxe0DZ7oyvFfhKedY=; b=e2VExXOhTUDacohV4GOA9kTPFfGUg3pBclQmpnfG0P6ISWDwGDFnOmE0Ch1rm8ZI0pJWIahU5aPA4IQb2p9Oo1J4VhhLj58m5NLELP0KAzo3aMG1P85o2uu2Z1u7AAkuWl/7mGkqsPC82re+ZZ/QXfkhHvGKQOoRsQfvdC/gZk8= ARC-Authentication-Results: i=2; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; arc=pass (i=1 dmarc=pass fromdomain=epam.com); dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1672666962197851.2037973766365; Mon, 2 Jan 2023 05:42:42 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.470286.729735 (Exim 4.92) (envelope-from ) id 1pCL4t-000219-PW; Mon, 02 Jan 2023 13:42:11 +0000 Received: by outflank-mailman (output) from mailman id 470286.729735; Mon, 02 Jan 2023 13:42:11 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1pCL4t-000212-M0; Mon, 02 Jan 2023 13:42:11 +0000 Received: by outflank-mailman (input) for mailman id 470286; Mon, 02 Jan 2023 13:42:10 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1pCL4r-00020m-Oq for xen-devel@lists.xenproject.org; Mon, 02 Jan 2023 13:42:10 +0000 Received: from mx0b-0039f301.pphosted.com (mx0b-0039f301.pphosted.com [148.163.137.242]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 3f199e71-8aa3-11ed-91b6-6bf2151ebd3b; Mon, 02 Jan 2023 14:42:07 +0100 (CET) Received: from pps.filterd (m0174682.ppops.net [127.0.0.1]) by mx0b-0039f301.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 302Bx46E011991; Mon, 2 Jan 2023 13:41:52 GMT Received: from eur04-he1-obe.outbound.protection.outlook.com (mail-he1eur04lp2054.outbound.protection.outlook.com [104.47.13.54]) by mx0b-0039f301.pphosted.com (PPS) with ESMTPS id 3mtd0uv746-3 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 02 Jan 2023 13:41:52 +0000 Received: from PA4PR03MB7136.eurprd03.prod.outlook.com (2603:10a6:102:ea::23) by DB9PR03MB9709.eurprd03.prod.outlook.com (2603:10a6:10:459::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5944.19; Mon, 2 Jan 2023 13:41:49 +0000 Received: from PA4PR03MB7136.eurprd03.prod.outlook.com ([fe80::2da6:6d63:389b:3552]) by PA4PR03MB7136.eurprd03.prod.outlook.com ([fe80::2da6:6d63:389b:3552%9]) with mapi id 15.20.5944.019; Mon, 2 Jan 2023 13:41:49 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 3f199e71-8aa3-11ed-91b6-6bf2151ebd3b ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=QxO5o76k7CjUEU6rKKrq5RwfzDgbpnnwrNT6z5k537oR14SrE8C61Oycz18oAseHvZ5mfJVPJWAiqGbzvAf6WtNKcqLto4f8HZ9QWw0Aa1Xyay7WuO5n2azLMPYE5n+up7JuVXh61z1ntQL7NpDN1dRN8uX8z99LefuygMdoyhVSSkrOuEFl8PPx58VfIdoQD/PgyTze/YMO4G1TqX9pumEtbErIdRFjYc9tln8xP92nSfCi7ygEVvm44G/zF30Go8W5eS1hLo8dNblHsLBCd3ihFUxweQyzGpTB2r75WciMOaoqm2oe0ET/vo7c5bJ2Rdr3fCIhTrx+IBjaHdtmiQ== 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=cIV9qnXRfvVgBfrc0HVcsMdFAbZxe0DZ7oyvFfhKedY=; b=OyKgCuMQiWAlZ80MzuCxTb5BV5m7N6DxFVfVmJcnrVuzV7yPFAR2DNZg98UNuLIu6II7/bqX78wXnGwAdkqwI3sLPDi8wyrrhLfcpXhKIBjIvZnvlAyqw1E70WsOPMyjP8qFB15hIf/AvEi3SSydbz0Q0qJ08rtBu26mMHaJ42lrFPyuCmJMOlmoc73cDiD7KEoyWm/ZkNXpS0bctlQpUFIqkFsA+dcKbUTeruuz2M3quMg5wH0UHkm422fc9mcSMl7eRRiiXj6j6wpj73+tgciZDtakctFbVGXLkpsGuJcdIlTm6jH5w/LKE/F55yfuY4axoZIQurkYogZ8YG8PTw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=epam.com; dmarc=pass action=none header.from=epam.com; dkim=pass header.d=epam.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=epam.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=cIV9qnXRfvVgBfrc0HVcsMdFAbZxe0DZ7oyvFfhKedY=; b=lm6SXYlSebGIE4OJ9EN9GySexcKaGRBtdfEQ/x13J2lS+m1haBrn9HM+JRVBdHcFmMi9NgfCnCYkwS4oRsmfjgM/Pp/BhL9+bl9/+2xRhKX8NbkBQ2JpNT8rEK52Dh9zCo1scOxpA5kSjNzXzOdZbhQmADgvRTNTYZ+c9g9JIwT5De9jwft0GVJmeX+ond7HcQVe++B1jVYDyUQOm3PwBEl8I0rfXIatq82OzrqiGt+1MEV41xJ8vDc/6Yuc7rWpHCYI7z5k2uGGTWFYpbyazqDQCyefpdTl4NBcT1QpxfL44hZyX/0Gzl1wYVxwh4tiIloMJ/CyhIMDW0pDD9Xccg== From: Oleksii Moisieiev To: "jgross@suse.com" CC: Oleksii Moisieiev , Stefano Stabellini , Oleksandr Tyshchenko , "xen-devel@lists.xenproject.org" , "linux-kernel@vger.kernel.org" , Sumit Semwal , =?iso-8859-1?Q?Christian_K=F6nig?= , "linux-media@vger.kernel.org" , "dri-devel@lists.freedesktop.org" , "linaro-mm-sig@lists.linaro.org" Subject: [PATCH v1 2/3] dma-buf: add dma buffer release notifier callback Thread-Topic: [PATCH v1 2/3] dma-buf: add dma buffer release notifier callback Thread-Index: AQHZHq/2iAqVu94bUUyzCrbXi66OTA== Date: Mon, 2 Jan 2023 13:41:48 +0000 Message-ID: <835ecf35d2d2d1ea763fe25837f52297c83c511f.1672666311.git.oleksii_moisieiev@epam.com> References: In-Reply-To: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-publictraffictype: Email x-ms-traffictypediagnostic: PA4PR03MB7136:EE_|DB9PR03MB9709:EE_ x-ms-office365-filtering-correlation-id: 6a772b46-7aa1-4f3f-dd01-08daecc71919 x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: I6xZItbt6oR7HVPauOPDvu99ZgGdQHMCdkGOIK7Kf9Q67OhE2zJOil5CfaljTx7dGNpXpOL7iVBxiNA6cDipi83qifFGwuURzg0ogrKgNTtcaPFHeFUrFc308gr41XBMdaR6o5qI3hFXm0QxX6MMOsXkeg3gdozFIGVtyQ21cL9HCBlItZUcTbzFk5xzmK4xTrpy3evhOWQ6u/pjRS9jRApRLS3wPXK5VWRQuFeoRYB66LUvwCwsoWPwttdan2jw7GFgsJSwe5C2FLyPsXJVEChGO8Fby4TJFfND2Hos1MYb49wG8v5SRWwlm7Ye1qaW8HyvmG6o1+WdZrNm+u8K/X/wDWoLtrlCgQZTYO3dJ7w9HNUMHSfSQA76KMjqiKzjwVa75kDB0339uWOrRQH9uqhKdkm17k3p0lElizw/IeD6j8ZcJ6TtW9Ip1Bqki0aE1DgMHnf/K/xvdAVIwCnrhbeSubHqL1I+n/CtdxYAW6zIo4k4vSeGkqubyzuqKMVlxPBGTVYIv3PCDwDmXnNvPCEs4vgXXG2dEevKtpgdq9Hx8jBAUR1u1hN1wkW3HeUGnL47AhPZVSjbh2Z82WJ0vIydhVjQQo0HDzzbKCG9wuNr6TLWNkY/fSlr33cstWE2qjLGIfR/fPP6+PIyH35CP/ZHcLlHkol+qxAnqJzRHO0R5QUiu0hwzWP5zpsw3FZq6FBDj/GLll8rrIWm8aHqp2jRryQgsZ8t4zXmPuWzv9o= x-forefront-antispam-report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:PA4PR03MB7136.eurprd03.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(4636009)(396003)(346002)(136003)(376002)(39860400002)(366004)(451199015)(38100700002)(122000001)(86362001)(8676002)(38070700005)(66476007)(76116006)(66946007)(64756008)(66446008)(66556008)(6916009)(91956017)(4326008)(41300700001)(54906003)(316002)(2906002)(5660300002)(8936002)(2616005)(83380400001)(6512007)(478600001)(71200400001)(6486002)(6506007)(26005)(55236004)(186003)(36756003)(22166006);DIR:OUT;SFP:1101; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?iso-8859-1?Q?ZSgpoXOX/ZZA1Vgo0MZN1aN//8+zLiN43kF69YgxoeR8frlg8vwJu7x+fX?= =?iso-8859-1?Q?xSVVGWVO+q9rOqDQ9N4W9nEqQNi2I9DBQGEKkGFY8lAvbuko1m+1Gx5nOz?= =?iso-8859-1?Q?3crIq0sE206KoIUQn0Gcbiw227Jw/43zylM2WeSxpdk6zwcRcJnD0STJav?= =?iso-8859-1?Q?d/srqcdvx+np25XIsevDxDjp870UXFPTrf3IRdhamWSbZwn9pgYQ0oT+1M?= =?iso-8859-1?Q?LET6oOcFfHtashYQ5Ng8LTGXAvTg/w/wIg7iYFJ1valtosgTq75RFPu+92?= =?iso-8859-1?Q?IIszRPLSm4D91jgr7DjKgKv/9HG6Lg9ybkjaHxqhb3gLBMpdkw0dd0XqHT?= =?iso-8859-1?Q?NTQKTXDjW3FlDAtpd5+4XcJZN5wduuVwzxyoGekNeWG2OO9/l4kZGi98JD?= =?iso-8859-1?Q?vhr3wvyfwqQzsRM9uZaMd/uxb6J5zNpUoL1xB+ZhaoZ3k1NElO7VKihYXy?= =?iso-8859-1?Q?fWDVlte/YoQvR8qFxGBDpemv1eDSLI4Re20GqnMbZdxSlLWmi74SuUnRKe?= =?iso-8859-1?Q?v5xBmPRUYMWPoCuV90hoUTsUI8mcxYsbkXd4DD9slIZCXeeSqHCjl15nZV?= =?iso-8859-1?Q?GB3bo4PcmzpxcilJ+XCBDaxaRXge5YFVfEPqaYRq1bjDAZn+r6cJNuORoe?= =?iso-8859-1?Q?bjHEWBPyYuKKjqtiuGQIggyAJFzcImJgGATOGwTGAk8WE1z2CACo1yzYm0?= =?iso-8859-1?Q?TyC9xvxMFRFUApNvf8KzQEb9h++X1yaF2QgM+KNdzTq2UtW0WeiuOx8b9g?= =?iso-8859-1?Q?iW26H4jd29TMsxLttDkCPzvAkLfth484McHNtMx/+KDocsuFCzGMM6osmW?= =?iso-8859-1?Q?rUnNVTgAww+E+CLXCezs+SXKFtpGPib4fV9kOIQBVN0QKmspbCo3+wiy1p?= =?iso-8859-1?Q?CuYE/FmvVd65LYXWy46+nHpljHzWQ1cc6T09tMUM1+OT0W/dyuvbUkveRR?= =?iso-8859-1?Q?SsFMhvX1Od9GbwHyYkoH7f53dOQNk9xlNsa9rXM8OxCRsuB/fAGnJUcvky?= =?iso-8859-1?Q?sU2DkExSpeJ8pYZl1lJtpZT2Qd4jdHxy2aSgjxME/UEMH9krvlW10q7a0q?= =?iso-8859-1?Q?5x+y3o0nZQUysNTjISbrddrugIr8UWRiPI+ftxgTG/zc5g3ch69oq43157?= =?iso-8859-1?Q?kvQGca/mbT46Lc1zuM8vLC/B5HwRv0Hwx6l6a707EGl6chP7GbF0wHULp/?= =?iso-8859-1?Q?wbwK+THDjqzspYRZWtguxMKTZGLCvj7uP0vJ2rJQl6Oq1quyMsT4Baf0ik?= =?iso-8859-1?Q?PtZkubNMOn7EXCY8t+v3DzH/Y3bBoBOQsQQwpsx3Wt/hxUx2u9oj2aOoSn?= =?iso-8859-1?Q?nUoVeI+uEXZ6ifqg73aVy7Fs4lkz1VtJ2h3TqukUoS3zJnOGGcKX2Q/FYE?= =?iso-8859-1?Q?Ry0RQA83pZWnjqGrODWrhSPtD15MVFqEmiaFC0pQnWQ0710f5gxDNITEQk?= =?iso-8859-1?Q?GK71JA9yeagTmfH2B47Anu1Pigjzi0q9+Nxc+thr52VW2ZuFlFNDUNT4DW?= =?iso-8859-1?Q?70JOX5LB9vjGFeuefKeoxBKBMe8okVWvG/S58DURQwul6XywvvH7nIPbEe?= =?iso-8859-1?Q?d3ynZ5XCGF/MuTDMJxaE4OSoFgQ9CxfJhEe3jHmzo8JGW/y+L/EGeEj77W?= =?iso-8859-1?Q?DzrgnbHaz9y2LKMm61n4Q3bHmF5Pe19SL0jC32gwVMOlRULlCDYWk3vA?= =?iso-8859-1?Q?=3D=3D?= Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: epam.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: PA4PR03MB7136.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 6a772b46-7aa1-4f3f-dd01-08daecc71919 X-MS-Exchange-CrossTenant-originalarrivaltime: 02 Jan 2023 13:41:48.9962 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: b41b72d0-4e9f-4c26-8a69-f949f367c91d X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: TQbH5ZtPMRkf5Lq6Jdo1NpaaN0V7CSgdcxJfYDDOXQM4wc1apBGOrkf7D/qZ/5DSjk5C2bFAufoxpe7okefYoO8K7PdqQLH7QRo332Az9W8= X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB9PR03MB9709 X-Proofpoint-GUID: OvaobUQFU99AdHxtz2tas7SLtdW6NrxG X-Proofpoint-ORIG-GUID: OvaobUQFU99AdHxtz2tas7SLtdW6NrxG X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.923,Hydra:6.0.545,FMLib:17.11.122.1 definitions=2023-01-02_08,2022-12-30_01,2022-06-22_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxlogscore=999 phishscore=0 lowpriorityscore=0 bulkscore=0 impostorscore=0 spamscore=0 priorityscore=1501 adultscore=0 suspectscore=0 mlxscore=0 malwarescore=0 clxscore=1015 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2212070000 definitions=main-2301020124 X-ZohoMail-DKIM: pass (identity @epam.com) X-ZM-MESSAGEID: 1672666963439100003 Content-Type: text/plain; charset="utf-8" Add posibility to register callback on dma-buffer which is called before dma_buf->ops->release call. This helps when external user of the dma buffer should be notified before buffer releases without changing dma-buf ops. This is needed when external dma buffer is used as backing storage for gntdev refs export and grant refs should be unmapped before dma buffer release. Signed-off-by: Oleksii Moisieiev --- drivers/dma-buf/dma-buf.c | 44 +++++++++++++++++++++++++++++++++++++++ include/linux/dma-buf.h | 15 +++++++++++++ 2 files changed, 59 insertions(+) diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c index efb4990b29e1..3e663ef92e1f 100644 --- a/drivers/dma-buf/dma-buf.c +++ b/drivers/dma-buf/dma-buf.c @@ -25,6 +25,7 @@ #include #include #include +#include #include =20 #include @@ -57,6 +58,46 @@ static char *dmabuffs_dname(struct dentry *dentry, char = *buffer, int buflen) dentry->d_name.name, ret > 0 ? name : ""); } =20 +int dma_buf_register_release_notifier(struct dma_buf *dmabuf, + ext_release_notifier_cb ext_release_cb, void *priv) +{ + int ret =3D 0; + + spin_lock(&dmabuf->ext_release_lock); + + if (dmabuf->ext_release_cb) { + ret =3D -EEXIST; + goto unlock; + } + + dmabuf->ext_release_cb =3D ext_release_cb; + dmabuf->ext_release_priv =3D priv; + unlock: + spin_unlock(&dmabuf->ext_release_lock); + return ret; +} +EXPORT_SYMBOL_GPL(dma_buf_register_release_notifier); + +void dma_buf_unregister_release_notifier(struct dma_buf *dmabuf) +{ + spin_lock(&dmabuf->ext_release_lock); + dmabuf->ext_release_cb =3D NULL; + spin_unlock(&dmabuf->ext_release_lock); +} +EXPORT_SYMBOL_GPL(dma_buf_unregister_release_notifier); + +static void dma_buf_call_release_notifier(struct dma_buf *dmabuf) +{ + if (!dmabuf->ext_release_cb) + return; + + spin_lock(&dmabuf->ext_release_lock); + dmabuf->ext_release_cb(dmabuf, dmabuf->ext_release_priv); + spin_unlock(&dmabuf->ext_release_lock); + + dma_buf_unregister_release_notifier(dmabuf); +} + static void dma_buf_release(struct dentry *dentry) { struct dma_buf *dmabuf; @@ -75,6 +116,8 @@ static void dma_buf_release(struct dentry *dentry) BUG_ON(dmabuf->cb_in.active || dmabuf->cb_out.active); =20 dma_buf_stats_teardown(dmabuf); + dma_buf_call_release_notifier(dmabuf); + dmabuf->ops->release(dmabuf); =20 if (dmabuf->resv =3D=3D (struct dma_resv *)&dmabuf[1]) @@ -642,6 +685,7 @@ struct dma_buf *dma_buf_export(const struct dma_buf_exp= ort_info *exp_info) init_waitqueue_head(&dmabuf->poll); dmabuf->cb_in.poll =3D dmabuf->cb_out.poll =3D &dmabuf->poll; dmabuf->cb_in.active =3D dmabuf->cb_out.active =3D 0; + spin_lock_init(&dmabuf->ext_release_lock); =20 if (!resv) { resv =3D (struct dma_resv *)&dmabuf[1]; diff --git a/include/linux/dma-buf.h b/include/linux/dma-buf.h index 71731796c8c3..6282d56ac040 100644 --- a/include/linux/dma-buf.h +++ b/include/linux/dma-buf.h @@ -287,6 +287,8 @@ struct dma_buf_ops { void (*vunmap)(struct dma_buf *dmabuf, struct iosys_map *map); }; =20 +typedef void (*ext_release_notifier_cb)(struct dma_buf *dmabuf, void *priv= ); + /** * struct dma_buf - shared buffer object * @@ -432,6 +434,15 @@ struct dma_buf { */ struct dma_resv *resv; =20 + /** @ext_release_cb notififier callback to call on release */ + ext_release_notifier_cb ext_release_cb; + + /** @ext_release_priv private data for callback */ + void *ext_release_priv; + + /** @ext_release_lock spinlock for ext_notifier helper */ + spinlock_t ext_release_lock; + /** @poll: for userspace poll support */ wait_queue_head_t poll; =20 @@ -632,4 +643,8 @@ int dma_buf_mmap(struct dma_buf *, struct vm_area_struc= t *, unsigned long); int dma_buf_vmap(struct dma_buf *dmabuf, struct iosys_map *map); void dma_buf_vunmap(struct dma_buf *dmabuf, struct iosys_map *map); + +int dma_buf_register_release_notifier(struct dma_buf *dmabuf, + ext_release_notifier_cb ext_release_cb, void *priv); +void dma_buf_unregister_release_notifier(struct dma_buf *dmabuf); #endif /* __DMA_BUF_H__ */ --=20 2.25.1 From nobody Sat May 4 02:52:39 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; arc=pass (i=1 dmarc=pass fromdomain=epam.com); dmarc=pass(p=quarantine dis=none) header.from=epam.com ARC-Seal: i=2; a=rsa-sha256; t=1672666961; cv=pass; d=zohomail.com; s=zohoarc; b=G6IhV6QgP9iZUIgqoVk2QcC9qi4mlMjRgx32I/mIwD+6wBM2mF6o/IkaMqCU1lI3SMNYCB0IFz++unxHG8bVfCkUx8zN8HZOtMYp5UR6OibBPwcyygX6cjZWhsAYZRG7rhzaTsWO00eb2KiSQ3g5/l0U9eCJfpqKlkcLbIXKIg0= ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1672666961; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=cpM/GTVcMI8fqBjEBRN4Lty7tXpejC9G2s04LY0mrJs=; b=U0FysxH8N5IWbLl3v2GC4dO7xVUoNK4X2dHWfpS8Fe/FXmtmgx+dj3ZtIytqFpCURpLXjhncZsmL2Wyt5F7/Hq8dZtfHbAVn1uwVKC/Y2wOfsd9RDPnWZsGDMMtaa3VWNdqWYQyRv+Q4vyAT9c8U5rjpF3PYDcmkyYQcgn6OuLA= ARC-Authentication-Results: i=2; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; arc=pass (i=1 dmarc=pass fromdomain=epam.com); dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1672666961917545.6411736861967; Mon, 2 Jan 2023 05:42:41 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.470289.729768 (Exim 4.92) (envelope-from ) id 1pCL4v-0002kc-LZ; Mon, 02 Jan 2023 13:42:13 +0000 Received: by outflank-mailman (output) from mailman id 470289.729768; Mon, 02 Jan 2023 13:42:13 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1pCL4v-0002kS-Hf; Mon, 02 Jan 2023 13:42:13 +0000 Received: by outflank-mailman (input) for mailman id 470289; Mon, 02 Jan 2023 13:42:12 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1pCL4t-00020m-W9 for xen-devel@lists.xenproject.org; Mon, 02 Jan 2023 13:42:12 +0000 Received: from mx0b-0039f301.pphosted.com (mx0b-0039f301.pphosted.com [148.163.137.242]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 3f6982ee-8aa3-11ed-91b6-6bf2151ebd3b; Mon, 02 Jan 2023 14:42:07 +0100 (CET) Received: from pps.filterd (m0174682.ppops.net [127.0.0.1]) by mx0b-0039f301.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 302Bx46F011991; Mon, 2 Jan 2023 13:41:53 GMT Received: from eur04-he1-obe.outbound.protection.outlook.com (mail-he1eur04lp2054.outbound.protection.outlook.com [104.47.13.54]) by mx0b-0039f301.pphosted.com (PPS) with ESMTPS id 3mtd0uv746-4 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 02 Jan 2023 13:41:53 +0000 Received: from PA4PR03MB7136.eurprd03.prod.outlook.com (2603:10a6:102:ea::23) by DB9PR03MB9709.eurprd03.prod.outlook.com (2603:10a6:10:459::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5944.19; Mon, 2 Jan 2023 13:41:49 +0000 Received: from PA4PR03MB7136.eurprd03.prod.outlook.com ([fe80::2da6:6d63:389b:3552]) by PA4PR03MB7136.eurprd03.prod.outlook.com ([fe80::2da6:6d63:389b:3552%9]) with mapi id 15.20.5944.019; Mon, 2 Jan 2023 13:41:49 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 3f6982ee-8aa3-11ed-91b6-6bf2151ebd3b ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=NkZR2Py/TQGVGAtl5cGJv1VL4Uut2A5KCLn+PZA2+PahhoQxhJfFgBzXYobMhmnvwoQDDf1UPAPIAsKYUVz2VtVdIH28gm/63gX/KGa92kpexRt8qfbuTm34A0hcnDAxdR3W6kdBSPBZqNMgPNjRu2WYbg0v7Syvo3liHnR6TW/SE8iib0z9Sw/KFwelpQnen+080mK/wfas635JZy9b9xApywDMGlZkRkTdPYpRlX8Nm/NZaxuqr2yVOV9MmxM5C1mAOKUzmQPZl0TwKMZuWvxauR0Mfuj07U7Cojj0W3nnNCqaogfpxk4gdAMbLrJtjBAekEAGo+k1yWs189e3FA== 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=cpM/GTVcMI8fqBjEBRN4Lty7tXpejC9G2s04LY0mrJs=; b=EEzdH3VRQ7JISgO3Pm+5rcqC6ss0N1f4FcqAxW5dcJmGSxaDB859fVc3LQ4f/Bdr/tnhen8I3FfVCcbqmj4jZcqPajiIzCpxry/oXPoEV0z5xjKCX7l14AxanffYJW4YyyyFc5GVBWXjPPe/okzQ13nsZtVgMFZLkT4Vn99ZKoslkVtzmHX4HT2psk9WzwNbrWZ8bsjeX4dXB6/EMstLiL2hL+7+PZPjMKBKcM4ziGsPKYhrIepHW4hk2BS1RHoRBE/4jhbMW8Sg6NyTh3fw6w5QTxvmQd3PBLTEmWgOKkn5syANpoOidPdWqXNii+7QS0dKLeR3Byexzhx0mamHnw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=epam.com; dmarc=pass action=none header.from=epam.com; dkim=pass header.d=epam.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=epam.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=cpM/GTVcMI8fqBjEBRN4Lty7tXpejC9G2s04LY0mrJs=; b=kyQSl3x6MrFAKpRga7jcWIrq4nfcd2PTWuhf4JjUdeSs3A7u8UN/ApDCsby083Ir7XLZ+4/4TJk9cCjrP/GxTX5sgqaDOnWgFWTeLQlciVCtrrIU9pCMA23IbTFnhZ1Y9G6bm/KJmVZ3iu+UpdaWL9iFvia+lrRYtvBGAO38poY+SDZbV+O3I2s+FFLzBv7h3mLk/JKYnPqFVfsjwJs3OJO+GPDZ5MHfjwP+PgouVa5bO1H+usu3J8P0l2Bb6bff0Xqpc8ACHazLsRrSugtkfpgMaEueGGmNh0ogmvkswRXcxd3Wgnmg1WTU3ZKQ80uBwG1qlMtPzVYPlhE1D5YRMw== From: Oleksii Moisieiev To: "jgross@suse.com" CC: Oleksii Moisieiev , Stefano Stabellini , Oleksandr Tyshchenko , "xen-devel@lists.xenproject.org" , "linux-kernel@vger.kernel.org" , Sumit Semwal , =?iso-8859-1?Q?Christian_K=F6nig?= , "linux-media@vger.kernel.org" , "dri-devel@lists.freedesktop.org" , "linaro-mm-sig@lists.linaro.org" Subject: [PATCH v1 3/3] xen/grant-table: add new ioctls to map dmabuf to existing fd Thread-Topic: [PATCH v1 3/3] xen/grant-table: add new ioctls to map dmabuf to existing fd Thread-Index: AQHZHq/2ph/BnX8oTk+YEfKPDbopCA== Date: Mon, 2 Jan 2023 13:41:49 +0000 Message-ID: <157bd897b4dd50b3c724722090b804440914c3cf.1672666311.git.oleksii_moisieiev@epam.com> References: In-Reply-To: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-publictraffictype: Email x-ms-traffictypediagnostic: PA4PR03MB7136:EE_|DB9PR03MB9709:EE_ x-ms-office365-filtering-correlation-id: 1c96edab-5a77-4b99-3495-08daecc71979 x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: fAYApRAI95RYBFTJ84xgUdQh8nwOqFcHmW6e4jmqZfdGqkMYneO/J61PZb2X53TTM6IsMtxHGOWXEqjpiqfknllLuklpWu8+7hYEAIReTjLzdPBw7upIOsmejItupVabytOWiyZW6TaLvjx1JDH0tZAe0l595aaVxXTGY8oWDpWF+DH94QgzqoscUNVHVEnHUkdz/xhybEd/v71F5nppfdeMgkuRe6djsWI+ApEJeTAymu9ZnmblPV7+JKY1ZdC9v62fsjVBAHoQ9tw04K3Q7JCSvkgA+E412eAh1E6Yo3tIUn6ITiNi5fGjl91fd38YanuTYZodEJ1Kvhvx57fwfs4xKi+zE1BnI6dW8qPPkyRbl5vMWmD/85BH+l7GJYhekfbTvTiGswc6TpVAL8xx8Fo5iLfe0TRK/uSR+a7pDH6cWYrCXvP24qt7DAWQSmC3C5INOs/QB3zuUHWDSIKOvQdq7eSKNzmR9LvWq/HKAZZcVWvJOsEIvb2d+UawzAr8uUrmwfu8XMIANhyFi1wqFwHwBu5FrLbNQ77b7PsiTaulriuKWMHaoX7jsdXu0l8AhhQx65Bb4zacsTMoO3KC9mN9Fb+ibKRsFXsd3AeGw/zDJJivIVsXKdmbti7VDsidzp1Z3st1VJq/VscBwS98eiQkWEC1TGm3Tvps7Fum1qqh9SklgWuLpcCNS31PfOZA5l9XgjNLuRI0xWKQYd1IN+qQEZTvASbhQPgP6eYm4J8Oi3QHsmx37Vp/EGJC+gyD x-forefront-antispam-report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:PA4PR03MB7136.eurprd03.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(4636009)(396003)(346002)(136003)(376002)(39860400002)(366004)(451199015)(38100700002)(122000001)(86362001)(8676002)(38070700005)(66476007)(76116006)(66946007)(64756008)(66446008)(66556008)(6916009)(91956017)(4326008)(41300700001)(54906003)(316002)(2906002)(5660300002)(8936002)(30864003)(2616005)(83380400001)(6512007)(478600001)(71200400001)(6486002)(6506007)(26005)(55236004)(186003)(36756003)(22166006)(21314003)(579004)(559001);DIR:OUT;SFP:1101; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?iso-8859-1?Q?SiGlTnYNPDw2YpnUzSo6FpTU+eS1V6Hg5OD2RkL6vKuPrNo8IBCWnG0z7S?= =?iso-8859-1?Q?WBGx1espg5fyI3QzgfX7olheSlizTxfW5qdDpIqsJimXnwH3rfIvoNXkoy?= =?iso-8859-1?Q?XqJtIIiE/3ToUosbcI0LXN+Q5AvOheCg35ov2CL/As8sv/vvSALHr0TFnd?= =?iso-8859-1?Q?UlmStJ9KuIBsmPqwOcunDnRWvkBgUcUgZMtVKWqXMFexIhhKwQl4XjTxSE?= =?iso-8859-1?Q?r2zwAt7JGRGSx0VhqhFyd+socMt5My6h4oP07SGvxnKj7TSqym8MjXUv/+?= =?iso-8859-1?Q?DI4d69HysRmUpP0uFgfrxfVsAdbOcrDkSgSkOsURxE7SiqrFm+PWG9mNxH?= =?iso-8859-1?Q?SCPmqQOOaRLDx7sOpZ7+K9B+gvahNYRBndIht8m09aSlOPWueDGUU/4uH9?= =?iso-8859-1?Q?+XIP19g8B9ry2b83Zy6bIA3Liun1+RLJLqZ9HrZrq2rOY5E6RSVkimzM+l?= =?iso-8859-1?Q?nzHtvxx/F3OfFMbFcWEk0jDZEQcdwqFUQMFISGmU2S+kU37B98UPtBTAVD?= =?iso-8859-1?Q?4XdR2IEguZN5MBtVIXdUEpb7hNu0aB6o/9jWtA3em9NdPDionCaIw1z1Il?= =?iso-8859-1?Q?oDGQ693RDtgG7v+tsPRC96iYJkaUzqIVxz4hrDgeOcOV+CoV3bOacyrv/y?= =?iso-8859-1?Q?ylKYGU7eyVj0bbDqf46aUj2IVlq4wviiGk3fEYuCcnQbuOro+LWP2jJoYi?= =?iso-8859-1?Q?+F78DqHY8TKbvvlFTPEi4kaLysLqIMg8KRk2KQBNRGx+v94swe5jw0uebs?= =?iso-8859-1?Q?GjNsf3PG0fmBYyjT7rG9+Ya8YCKDUcUB7pm19dcg95Gi4rmiX9Yo8pkdHr?= =?iso-8859-1?Q?0sOY7Szifb2rXh5AbbhKG5bETxeamg0F/vGjgCrALjwY7A2ifW9URopXeu?= =?iso-8859-1?Q?6sBlEfCl2VwIxGmwmWUXryiZgRlklZ/VFmx9Ww0KqF9ukVWKtOnGcm6vca?= =?iso-8859-1?Q?3Xwt8TtNQQxDtWZhiUpJZvFdF4SwkCP48qmGo5DwsDL4EFzGvGvIc9/2iu?= =?iso-8859-1?Q?LrEW+9HObIFTZPLEEwX8KSXSsChpxzrKhADvZws56emjs0GieP45wR4sfu?= =?iso-8859-1?Q?0Mqr5KdqXaDg9mr9wtjpc194o7WNZqGdKADYa1yvwDGdd2mOV6AG6t3FdG?= =?iso-8859-1?Q?OOw2l9KFW4giqjZONbNPCRDdXOlQ+mBI7UyoYc5UkuE+L/1VPBvwLTniNO?= =?iso-8859-1?Q?YzJAs4W6Lf+p4zePK7D4v2AVf0Tv5HO/5Fm0hfy7a9Wm6KJ4QaVyyKa9An?= =?iso-8859-1?Q?8/mLO5BpQrzdo/9zLsTDjnoF7jZVGaPkzLQ8IBDY+nVN6xmzZFRtyq+R0j?= =?iso-8859-1?Q?pcjxgUPYZx/PieIoKhbcg8feNMUG/gk3+VtGsDsH/xXhrlEk/CCPl3+df5?= =?iso-8859-1?Q?KuAPCG4bLtvxrdnotVskz37kiofFcIhmpfbdbBjihdv2REoVwJO5CFMbRc?= =?iso-8859-1?Q?roAx6vDRp6jZ2PTddgCwlU3Lx/8G/2tibtqfLIXq5y7LDLPBeYrXEC4W13?= =?iso-8859-1?Q?qqjZ9aQhOQdlU/vGbjsjuW4PsPHIPng7Syi4Hpl92p/1P4a/RxQKSuY56M?= =?iso-8859-1?Q?UZmIBd2cDqkhqUQWFXWqcFLR2n32uaQvyG7q60eb5CEdymGDbPlYbPw3FA?= =?iso-8859-1?Q?2+SDqBbY+WHRph7Lg7/ZyTLn04nfk2kZ0IGbKXItgLvqXWr9osn9ygoA?= =?iso-8859-1?Q?=3D=3D?= Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: epam.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: PA4PR03MB7136.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 1c96edab-5a77-4b99-3495-08daecc71979 X-MS-Exchange-CrossTenant-originalarrivaltime: 02 Jan 2023 13:41:49.5742 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: b41b72d0-4e9f-4c26-8a69-f949f367c91d X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: kgZ/vtfEM6LlGzuqzZbCumY3cAdnlx9y0l19k7z+nFTx/NM3mYE1FHEWj5HVsRXv4EsKbXXLQ6aU7sRFK9pQuJq363AyXdHlrfgtaAbpv0w= X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB9PR03MB9709 X-Proofpoint-GUID: zqExVzAwob7QUgCeIbSNQg5wp2GNIpKB X-Proofpoint-ORIG-GUID: zqExVzAwob7QUgCeIbSNQg5wp2GNIpKB X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.923,Hydra:6.0.545,FMLib:17.11.122.1 definitions=2023-01-02_08,2022-12-30_01,2022-06-22_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxlogscore=999 phishscore=0 lowpriorityscore=0 bulkscore=0 impostorscore=0 spamscore=0 priorityscore=1501 adultscore=0 suspectscore=0 mlxscore=0 malwarescore=0 clxscore=1015 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2212070000 definitions=main-2301020124 X-ZohoMail-DKIM: pass (identity @epam.com) X-ZM-MESSAGEID: 1672666963488100004 Content-Type: text/plain; charset="utf-8" Add new ioctls to allow gntdev to map scatter-gather table on top of the existing dmabuf, referenced by file descriptor. When using dma-buf exporter to create dma-buf with backing storage and map it to the grant refs, provided from the domain, we've met a problem, that several HW (i.MX8 gpu in our case) do not support external buffer and requires backing storage to be created using it's native tools. That's why new ioctls were added to be able to pass existing dma-buffer fd as input parameter and use it as backing storage to export to refs. Following calls were added: IOCTL_GNTDEV_DMABUF_MAP_REFS_TO_BUF - map existing buffer as the backing storage and export it to the provided grant refs; IOCTL_GNTDEV_DMABUF_MAP_RELEASE - detach buffer from the grant table and set notification to unmap grant refs before releasing the external buffer. After this call the external buffer should be detroyed. IOCTL_GNTDEV_DMABUF_MAP_WAIT_RELEASED - wait for timeout until buffer is completely destroyed and gnt refs unmapped so domain could free grant pages. Signed-off-by: Oleksii Moisieiev --- drivers/xen/gntdev-common.h | 8 +- drivers/xen/gntdev-dmabuf.c | 416 +++++++++++++++++++++++++++++++++++- drivers/xen/gntdev-dmabuf.h | 7 + drivers/xen/gntdev.c | 101 ++++++++- drivers/xen/grant-table.c | 57 +++-- include/uapi/xen/gntdev.h | 62 ++++++ include/xen/grant_table.h | 5 + 7 files changed, 626 insertions(+), 30 deletions(-) diff --git a/drivers/xen/gntdev-common.h b/drivers/xen/gntdev-common.h index 9c286b2a1900..3b6980df3f9d 100644 --- a/drivers/xen/gntdev-common.h +++ b/drivers/xen/gntdev-common.h @@ -61,6 +61,10 @@ struct gntdev_grant_map { bool *being_removed; struct page **pages; unsigned long pages_vm_start; + unsigned int preserve_pages; + + /* Needed to avoid allocation in gnttab_dma_free_pages(). */ + xen_pfn_t *frames; =20 #ifdef CONFIG_XEN_GRANT_DMA_ALLOC /* @@ -73,8 +77,6 @@ struct gntdev_grant_map { int dma_flags; void *dma_vaddr; dma_addr_t dma_bus_addr; - /* Needed to avoid allocation in gnttab_dma_free_pages(). */ - xen_pfn_t *frames; #endif =20 /* Number of live grants */ @@ -85,6 +87,8 @@ struct gntdev_grant_map { =20 struct gntdev_grant_map *gntdev_alloc_map(struct gntdev_priv *priv, int co= unt, int dma_flags); +struct gntdev_grant_map *gntdev_get_alloc_from_fd(struct gntdev_priv *priv, + struct sg_table *sgt, int count, int dma_flags); =20 void gntdev_add_map(struct gntdev_priv *priv, struct gntdev_grant_map *add= ); =20 diff --git a/drivers/xen/gntdev-dmabuf.c b/drivers/xen/gntdev-dmabuf.c index 940e5e9e8a54..71d3bfee72aa 100644 --- a/drivers/xen/gntdev-dmabuf.c +++ b/drivers/xen/gntdev-dmabuf.c @@ -10,14 +10,18 @@ =20 #include #include +#include #include +#include #include #include #include #include +#include =20 #include #include +#include =20 #include "gntdev-common.h" #include "gntdev-dmabuf.h" @@ -46,6 +50,18 @@ struct gntdev_dmabuf { /* dma-buf attachment of the imported buffer. */ struct dma_buf_attachment *attach; } imp; + struct { + /* Scatter-gather table of the mapped buffer. */ + struct sg_table *sgt; + /* dma-buf attachment of the mapped buffer. */ + struct dma_buf_attachment *attach; + /* map table */ + struct gntdev_grant_map *map; + /* frames table for memory reservation */ + xen_pfn_t *frames; + + struct gntdev_priv *priv; + } map; } u; =20 /* Number of pages this buffer has. */ @@ -57,6 +73,7 @@ struct gntdev_dmabuf { struct gntdev_dmabuf_wait_obj { struct list_head next; struct gntdev_dmabuf *gntdev_dmabuf; + int fd; struct completion completion; }; =20 @@ -72,6 +89,10 @@ struct gntdev_dmabuf_priv { struct list_head exp_wait_list; /* List of imported DMA buffers. */ struct list_head imp_list; + /* List of mapped DMA buffers. */ + struct list_head map_list; + /* List of wait objects. */ + struct list_head map_wait_list; /* This is the lock which protects dma_buf_xxx lists. */ struct mutex lock; /* @@ -88,6 +109,64 @@ struct gntdev_dmabuf_priv { =20 static void dmabuf_exp_release(struct kref *kref); =20 +static struct gntdev_dmabuf_wait_obj * +dmabuf_map_wait_obj_find(struct gntdev_dmabuf_priv *priv, int fd) +{ + struct gntdev_dmabuf_wait_obj *obj, *ret =3D ERR_PTR(-ENOENT); + + mutex_lock(&priv->lock); + list_for_each_entry(obj, &priv->map_wait_list, next) + if (obj->fd =3D=3D fd) { + pr_debug("Found gntdev_dmabuf in the wait list\n"); + ret =3D obj; + break; + } + mutex_unlock(&priv->lock); + + return ret; +} + +static void dmabuf_exp_wait_obj_free(struct gntdev_dmabuf_priv *priv, + struct gntdev_dmabuf_wait_obj *obj); + +static int +dmabuf_map_wait_obj_set(struct gntdev_dmabuf_priv *priv, + struct gntdev_dmabuf *gntdev_dmabuf, int fd) +{ + struct gntdev_dmabuf_wait_obj *obj; + + obj =3D dmabuf_map_wait_obj_find(gntdev_dmabuf->priv, fd); + if ((!obj) || (PTR_ERR(obj) =3D=3D -ENOENT)) { + obj =3D kzalloc(sizeof(*obj), GFP_KERNEL); + if (!obj) + return -ENOMEM; + } + + init_completion(&obj->completion); + obj->gntdev_dmabuf =3D gntdev_dmabuf; + obj->fd =3D fd; + mutex_lock(&priv->lock); + list_add(&obj->next, &priv->map_wait_list); + mutex_unlock(&priv->lock); + return 0; +} + +static void dmabuf_map_wait_obj_signal(struct gntdev_dmabuf_priv *priv, + struct gntdev_dmabuf *gntdev_dmabuf) +{ + struct gntdev_dmabuf_wait_obj *obj; + + mutex_lock(&priv->lock); + list_for_each_entry(obj, &priv->map_wait_list, next) + if (obj->gntdev_dmabuf =3D=3D gntdev_dmabuf) { + pr_debug("Found gntdev_dmabuf in the wait list, wake\n"); + complete_all(&obj->completion); + break; + } + + mutex_unlock(&priv->lock); +} + static struct gntdev_dmabuf_wait_obj * dmabuf_exp_wait_obj_new(struct gntdev_dmabuf_priv *priv, struct gntdev_dmabuf *gntdev_dmabuf) @@ -410,6 +489,18 @@ static int dmabuf_exp_from_pages(struct gntdev_dmabuf_= export_args *args) return ret; } =20 +static void dmabuf_map_free_gntdev_dmabuf(struct gntdev_dmabuf *gntdev_dma= buf) +{ + if (!gntdev_dmabuf) + return; + + kfree(gntdev_dmabuf->pages); + + kvfree(gntdev_dmabuf->u.map.frames); + kfree(gntdev_dmabuf); + gntdev_dmabuf =3D NULL; +} + static struct gntdev_grant_map * dmabuf_exp_alloc_backing_storage(struct gntdev_priv *priv, int dmabuf_flag= s, int count) @@ -432,6 +523,113 @@ dmabuf_exp_alloc_backing_storage(struct gntdev_priv *= priv, int dmabuf_flags, return map; } =20 +static void dmabuf_map_remove(struct gntdev_priv *priv, + struct gntdev_dmabuf *gntdev_dmabuf) +{ + dmabuf_exp_remove_map(priv, gntdev_dmabuf->u.map.map); + dmabuf_map_free_gntdev_dmabuf(gntdev_dmabuf); +} + +static struct gntdev_dmabuf * +dmabuf_alloc_gntdev_from_buf(struct gntdev_priv *priv, int fd, int dmabuf_= flags, + int count, unsigned int data_ofs) +{ + struct gntdev_dmabuf *gntdev_dmabuf; + struct dma_buf_attachment *attach; + struct dma_buf *dma_buf; + struct sg_table *sgt; + int ret =3D 0; + + gntdev_dmabuf =3D kzalloc(sizeof(*gntdev_dmabuf), GFP_KERNEL); + if (!gntdev_dmabuf) + return ERR_PTR(-ENOMEM); + + gntdev_dmabuf->pages =3D kcalloc(count, + sizeof(gntdev_dmabuf->pages[0]), GFP_KERNEL); + + if (!gntdev_dmabuf->pages) { + ret =3D -ENOMEM; + goto free; + } + + gntdev_dmabuf->u.map.frames =3D kvcalloc(count, + sizeof(gntdev_dmabuf->u.map.frames[0]), GFP_KERNEL); + if (!gntdev_dmabuf->u.map.frames) { + ret =3D -ENOMEM; + goto free; + } + + if (gntdev_test_page_count(count)) { + ret =3D -EINVAL; + goto free; + } + + dma_buf =3D dma_buf_get(fd); + if (IS_ERR_OR_NULL(dma_buf)) { + pr_debug("Unable to get dmabuf from fd\n"); + ret =3D PTR_ERR(dma_buf); + goto free; + } + + attach =3D dma_buf_attach(dma_buf, priv->dma_dev); + if (IS_ERR_OR_NULL(attach)) { + ret =3D PTR_ERR(attach); + goto fail_put; + } + + sgt =3D dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL); + if (IS_ERR_OR_NULL(sgt)) { + ret =3D PTR_ERR(sgt); + goto fail_detach; + } + + if (sgt->sgl->offset !=3D data_ofs) { + pr_debug("DMA buffer offset %d, user-space expects %d\n", + sgt->sgl->offset, data_ofs); + ret =3D -EINVAL; + goto fail_unmap; + } + + /* Check number of pages that imported buffer has. */ + if (attach->dmabuf->size < count << PAGE_SHIFT) { + pr_debug("DMA buffer has %zu pages, user-space expects %d\n", + attach->dmabuf->size, count << PAGE_SHIFT); + ret =3D -EINVAL; + goto fail_unmap; + } + + gntdev_dmabuf->u.map.map =3D gntdev_get_alloc_from_fd(priv, sgt, count, + dmabuf_flags); + if (IS_ERR_OR_NULL(gntdev_dmabuf->u.map.map)) { + ret =3D -ENOMEM; + goto fail_unmap; + }; + + gntdev_dmabuf->priv =3D priv->dmabuf_priv; + gntdev_dmabuf->fd =3D fd; + gntdev_dmabuf->u.map.attach =3D attach; + gntdev_dmabuf->u.map.sgt =3D sgt; + gntdev_dmabuf->dmabuf =3D dma_buf; + gntdev_dmabuf->nr_pages =3D count; + gntdev_dmabuf->u.map.priv =3D priv; + + memcpy(gntdev_dmabuf->pages, gntdev_dmabuf->u.map.map->pages, count * + sizeof(gntdev_dmabuf->u.map.map->pages[0])); + memcpy(gntdev_dmabuf->u.map.frames, gntdev_dmabuf->u.map.map->frames, cou= nt * + sizeof(gntdev_dmabuf->u.map.map->frames[0])); + + return gntdev_dmabuf; +fail_unmap: + dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL); +fail_detach: + dma_buf_detach(dma_buf, attach); +fail_put: + dma_buf_put(dma_buf); +free: + dmabuf_map_free_gntdev_dmabuf(gntdev_dmabuf); + return ERR_PTR(ret); +} + static int dmabuf_exp_from_refs(struct gntdev_priv *priv, int flags, int count, u32 domid, u32 *refs, u32 *fd) { @@ -481,6 +679,117 @@ static int dmabuf_exp_from_refs(struct gntdev_priv *p= riv, int flags, return ret; } =20 +static void dmabuf_release_notifier_cb(struct dma_buf *dmabuf, void *priv) +{ + struct gntdev_dmabuf *gntdev_dmabuf =3D priv; + + if (!gntdev_dmabuf) + return; + + dmabuf_map_remove(gntdev_dmabuf->u.map.priv, gntdev_dmabuf); + dmabuf_map_wait_obj_signal(gntdev_dmabuf->priv, gntdev_dmabuf); +} + +static int dmabuf_detach_map(struct gntdev_dmabuf *gntdev_dmabuf) +{ + struct dma_buf *dma_buf =3D gntdev_dmabuf->dmabuf; + long lret; + + /* Wait on any implicit fences */ + lret =3D dma_resv_wait_timeout(dma_buf->resv, + dma_resv_usage_rw(true), true, + MAX_SCHEDULE_TIMEOUT); + if (lret =3D=3D 0) + return -ETIME; + else if (lret < 0) + return lret; + + if (gntdev_dmabuf->u.map.sgt) { + dma_buf_unmap_attachment(gntdev_dmabuf->u.map.attach, + gntdev_dmabuf->u.map.sgt, DMA_BIDIRECTIONAL); + } + + dma_buf_detach(dma_buf, gntdev_dmabuf->u.map.attach); + dma_buf_put(dma_buf); + + return 0; +} + +static int dmabuf_map_release(struct gntdev_dmabuf *gntdev_dmabuf, bool sy= nc) +{ + int ret; + + if (!sync) { + ret =3D dmabuf_map_wait_obj_set(gntdev_dmabuf->priv, gntdev_dmabuf, + gntdev_dmabuf->fd); + if (ret) + return ret; + } + + ret =3D dmabuf_detach_map(gntdev_dmabuf); + if (ret) + return ret; + + if (!sync) { + ret =3D dma_buf_register_release_notifier(gntdev_dmabuf->dmabuf, + &dmabuf_release_notifier_cb, gntdev_dmabuf); + if (ret) + return ret; + } else { + dmabuf_map_remove(gntdev_dmabuf->u.map.priv, gntdev_dmabuf); + } + + return 0; +} + +static int dmabuf_map_refs_to_fd(struct gntdev_priv *priv, int flags, + int count, u32 domid, u32 *refs, u32 fd, + unsigned int data_ofs) +{ + struct gntdev_dmabuf *gntdev_dmabuf; + int i, ret; + + gntdev_dmabuf =3D dmabuf_alloc_gntdev_from_buf(priv, fd, flags, count, + data_ofs); + + if (IS_ERR_OR_NULL(gntdev_dmabuf)) { + ret =3D PTR_ERR(gntdev_dmabuf); + goto fail_gntdev; + } + + for (i =3D 0; i < count; i++) { + gntdev_dmabuf->u.map.map->grants[i].domid =3D domid; + gntdev_dmabuf->u.map.map->grants[i].ref =3D refs[i]; + } + + mutex_lock(&priv->lock); + gntdev_add_map(priv, gntdev_dmabuf->u.map.map); + mutex_unlock(&priv->lock); + + gntdev_dmabuf->u.map.map->flags |=3D GNTMAP_host_map; +#if defined(CONFIG_X86) + gntdev_dmabuf->u.map.map->flags |=3D GNTMAP_device_map; +#endif + + ret =3D gntdev_map_grant_pages(gntdev_dmabuf->u.map.map); + if (ret < 0) + goto fail; + + mutex_lock(&priv->lock); + list_add(&gntdev_dmabuf->next, &priv->dmabuf_priv->map_list); + mutex_unlock(&priv->lock); + + return 0; +fail: + mutex_lock(&priv->lock); + list_del(&gntdev_dmabuf->u.map.map->next); + mutex_unlock(&priv->lock); + dmabuf_detach_map(gntdev_dmabuf); + dmabuf_map_free_gntdev_dmabuf(gntdev_dmabuf); +fail_gntdev: + return ret; +} + /* DMA buffer import support. */ =20 static int @@ -673,14 +982,15 @@ dmabuf_imp_to_refs(struct gntdev_dmabuf_priv *priv, s= truct device *dev, * it from the buffer's list. */ static struct gntdev_dmabuf * -dmabuf_imp_find_unlink(struct gntdev_dmabuf_priv *priv, int fd) +dmabuf_list_find_unlink(struct gntdev_dmabuf_priv *priv, struct list_head = *list, + int fd) { struct gntdev_dmabuf *q, *gntdev_dmabuf, *ret =3D ERR_PTR(-ENOENT); =20 mutex_lock(&priv->lock); - list_for_each_entry_safe(gntdev_dmabuf, q, &priv->imp_list, next) { + list_for_each_entry_safe(gntdev_dmabuf, q, list, next) { if (gntdev_dmabuf->fd =3D=3D fd) { - pr_debug("Found gntdev_dmabuf in the import list\n"); + pr_debug("Found gntdev_dmabuf in the list\n"); ret =3D gntdev_dmabuf; list_del(&gntdev_dmabuf->next); break; @@ -696,7 +1006,7 @@ static int dmabuf_imp_release(struct gntdev_dmabuf_pri= v *priv, u32 fd) struct dma_buf_attachment *attach; struct dma_buf *dma_buf; =20 - gntdev_dmabuf =3D dmabuf_imp_find_unlink(priv, fd); + gntdev_dmabuf =3D dmabuf_list_find_unlink(priv, &priv->imp_list, fd); if (IS_ERR(gntdev_dmabuf)) return PTR_ERR(gntdev_dmabuf); =20 @@ -726,6 +1036,21 @@ static void dmabuf_imp_release_all(struct gntdev_dmab= uf_priv *priv) dmabuf_imp_release(priv, gntdev_dmabuf->fd); } =20 +static void dmabuf_map_release_all(struct gntdev_dmabuf_priv *priv) +{ + struct gntdev_dmabuf *q, *gntdev_dmabuf; + struct gntdev_dmabuf_wait_obj *o, *obj; + + list_for_each_entry_safe(obj, o, &priv->map_wait_list, next) { + dmabuf_exp_wait_obj_free(priv, obj); + } + + list_for_each_entry_safe(gntdev_dmabuf, q, &priv->map_list, next) { + dmabuf_map_release(gntdev_dmabuf, true); + } + +} + /* DMA buffer IOCTL support. */ =20 long gntdev_ioctl_dmabuf_exp_from_refs(struct gntdev_priv *priv, int use_p= temod, @@ -769,6 +1094,47 @@ long gntdev_ioctl_dmabuf_exp_from_refs(struct gntdev_= priv *priv, int use_ptemod, return ret; } =20 +long gntdev_ioctl_dmabuf_map_refs_to_buf(struct gntdev_priv *priv, int use= _ptemod, + struct ioctl_gntdev_dmabuf_map_refs_to_buf __user *u) +{ + struct ioctl_gntdev_dmabuf_map_refs_to_buf op; + u32 *refs; + long ret; + + if (use_ptemod) { + pr_debug("Cannot provide dma-buf: use_ptemode %d\n", + use_ptemod); + return -EINVAL; + } + + if (copy_from_user(&op, u, sizeof(op)) !=3D 0) + return -EFAULT; + + if (op.count <=3D 0) + return -EINVAL; + + refs =3D kcalloc(op.count, sizeof(*refs), GFP_KERNEL); + if (!refs) + return -ENOMEM; + + if (copy_from_user(refs, u->refs, sizeof(*refs) * op.count) !=3D 0) { + ret =3D -EFAULT; + goto out; + } + + ret =3D dmabuf_map_refs_to_fd(priv, op.flags, op.count, + op.domid, refs, op.fd, op.data_ofs); + if (ret) + goto out; + + if (copy_to_user(u, &op, sizeof(op)) !=3D 0) + ret =3D -EFAULT; + +out: + kfree(refs); + return ret; +} + long gntdev_ioctl_dmabuf_exp_wait_released(struct gntdev_priv *priv, struct ioctl_gntdev_dmabuf_exp_wait_released __user *u) { @@ -823,6 +1189,45 @@ long gntdev_ioctl_dmabuf_imp_release(struct gntdev_pr= iv *priv, return dmabuf_imp_release(priv->dmabuf_priv, op.fd); } =20 +long gntdev_ioctl_dmabuf_map_release(struct gntdev_priv *priv, + struct ioctl_gntdev_dmabuf_map_release __user *u) +{ + struct ioctl_gntdev_dmabuf_map_release op; + struct gntdev_dmabuf *gntdev_dmabuf; + + if (copy_from_user(&op, u, sizeof(op)) !=3D 0) + return -EFAULT; + + gntdev_dmabuf =3D dmabuf_list_find_unlink(priv->dmabuf_priv, + &priv->dmabuf_priv->map_list, op.fd); + if (IS_ERR(gntdev_dmabuf)) + return PTR_ERR(gntdev_dmabuf); + + return dmabuf_map_release(gntdev_dmabuf, false); +} + +long gntdev_ioctl_dmabuf_map_wait_released(struct gntdev_priv *priv, + struct ioctl_gntdev_dmabuf_map_wait_released __user *u) +{ + struct ioctl_gntdev_dmabuf_map_wait_released op; + struct gntdev_dmabuf_wait_obj *obj; + int ret =3D 0; + + if (copy_from_user(&op, u, sizeof(op)) !=3D 0) + return -EFAULT; + + obj =3D dmabuf_map_wait_obj_find(priv->dmabuf_priv, op.fd); + if (IS_ERR_OR_NULL(obj)) + return (PTR_ERR(obj) =3D=3D -ENOENT) ? 0 : PTR_ERR(obj); + + if (!completion_done(&obj->completion)) + ret =3D dmabuf_exp_wait_obj_wait(obj, op.wait_to_ms); + + if (!ret && ret !=3D -ETIMEDOUT) + dmabuf_exp_wait_obj_free(priv->dmabuf_priv, obj); + return ret; +} + struct gntdev_dmabuf_priv *gntdev_dmabuf_init(struct file *filp) { struct gntdev_dmabuf_priv *priv; @@ -835,6 +1240,8 @@ struct gntdev_dmabuf_priv *gntdev_dmabuf_init(struct f= ile *filp) INIT_LIST_HEAD(&priv->exp_list); INIT_LIST_HEAD(&priv->exp_wait_list); INIT_LIST_HEAD(&priv->imp_list); + INIT_LIST_HEAD(&priv->map_list); + INIT_LIST_HEAD(&priv->map_wait_list); =20 priv->filp =3D filp; =20 @@ -844,5 +1251,6 @@ struct gntdev_dmabuf_priv *gntdev_dmabuf_init(struct f= ile *filp) void gntdev_dmabuf_fini(struct gntdev_dmabuf_priv *priv) { dmabuf_imp_release_all(priv); + dmabuf_map_release_all(priv); kfree(priv); } diff --git a/drivers/xen/gntdev-dmabuf.h b/drivers/xen/gntdev-dmabuf.h index 3d9b9cf9d5a1..07301f12ac52 100644 --- a/drivers/xen/gntdev-dmabuf.h +++ b/drivers/xen/gntdev-dmabuf.h @@ -21,6 +21,9 @@ void gntdev_dmabuf_fini(struct gntdev_dmabuf_priv *priv); long gntdev_ioctl_dmabuf_exp_from_refs(struct gntdev_priv *priv, int use_p= temod, struct ioctl_gntdev_dmabuf_exp_from_refs __user *u); =20 +long gntdev_ioctl_dmabuf_map_refs_to_buf(struct gntdev_priv *priv, int use= _ptemod, + struct ioctl_gntdev_dmabuf_map_refs_to_buf __user *u); + long gntdev_ioctl_dmabuf_exp_wait_released(struct gntdev_priv *priv, struct ioctl_gntdev_dmabuf_exp_wait_released __user *u); =20 @@ -30,4 +33,8 @@ long gntdev_ioctl_dmabuf_imp_to_refs(struct gntdev_priv *= priv, long gntdev_ioctl_dmabuf_imp_release(struct gntdev_priv *priv, struct ioctl_gntdev_dmabuf_imp_release __user *u); =20 +long gntdev_ioctl_dmabuf_map_release(struct gntdev_priv *priv, + struct ioctl_gntdev_dmabuf_map_release __user *u); +long gntdev_ioctl_dmabuf_map_wait_released(struct gntdev_priv *priv, + struct ioctl_gntdev_dmabuf_map_wait_released __user *u); #endif diff --git a/drivers/xen/gntdev.c b/drivers/xen/gntdev.c index 4d9a3050de6a..677a51244bb2 100644 --- a/drivers/xen/gntdev.c +++ b/drivers/xen/gntdev.c @@ -22,6 +22,7 @@ =20 #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt =20 +#include #include #include #include @@ -43,6 +44,7 @@ #include #include #include +#include #include #include =20 @@ -96,7 +98,11 @@ static void gntdev_free_map(struct gntdev_grant_map *map) return; =20 #ifdef CONFIG_XEN_GRANT_DMA_ALLOC - if (map->dma_vaddr) { + if (map->pages && map->preserve_pages) { + gnttab_dma_clean_page_reservation(map->count, map->pages, + map->frames); + + } else if (map->dma_vaddr) { struct gnttab_dma_alloc_args args; =20 args.dev =3D map->dma_dev; @@ -216,6 +222,82 @@ struct gntdev_grant_map *gntdev_alloc_map(struct gntde= v_priv *priv, int count, return NULL; } =20 +struct gntdev_grant_map *gntdev_get_alloc_from_fd(struct gntdev_priv *priv, + struct sg_table *sgt, int count, int dma_flags) +{ + struct gntdev_grant_map *add; + int i =3D 0; + struct sg_page_iter sg_iter; + + add =3D kzalloc(sizeof(*add), GFP_KERNEL); + if (!add) + return NULL; + + add->grants =3D kvcalloc(count, sizeof(add->grants[0]), GFP_KERNEL); + add->map_ops =3D kvcalloc(count, sizeof(add->map_ops[0]), GFP_KERNEL); + add->unmap_ops =3D kvcalloc(count, sizeof(add->unmap_ops[0]), GFP_KERNEL); + add->pages =3D kvcalloc(count, sizeof(add->pages[0]), GFP_KERNEL); + add->frames =3D kvcalloc(count, sizeof(add->frames[0]), + GFP_KERNEL); + add->being_removed =3D + kvcalloc(count, sizeof(add->being_removed[0]), GFP_KERNEL); + add->preserve_pages =3D 1; + + if (add->grants =3D=3D NULL || + add->map_ops =3D=3D NULL || + add->unmap_ops =3D=3D NULL || + add->pages =3D=3D NULL || + add->frames =3D=3D NULL || + add->being_removed =3D=3D NULL) + goto err; + + if (use_ptemod) { + add->kmap_ops =3D kvmalloc_array(count, sizeof(add->kmap_ops[0]), + GFP_KERNEL); + add->kunmap_ops =3D kvmalloc_array(count, sizeof(add->kunmap_ops[0]), + GFP_KERNEL); + if (NULL =3D=3D add->kmap_ops || NULL =3D=3D add->kunmap_ops) + goto err; + } + + for_each_sgtable_page(sgt, &sg_iter, 0) { + struct page *page =3D sg_page_iter_page(&sg_iter); + + add->pages[i] =3D page; + add->frames[i] =3D xen_page_to_gfn(page); + i++; + if (i >=3D count) + break; + } + + if (i < count) { + pr_debug("Provided buffer is too small"); + goto err; + } + + if (gnttab_dma_reserve_pages(count, add->pages, add->frames)) + goto err; + + for (i =3D 0; i < count; i++) { + add->map_ops[i].handle =3D -1; + add->unmap_ops[i].handle =3D -1; + if (use_ptemod) { + add->kmap_ops[i].handle =3D -1; + add->kunmap_ops[i].handle =3D -1; + } + } + + add->index =3D 0; + add->count =3D count; + refcount_set(&add->users, 1); + + return add; + +err: + gntdev_free_map(add); + return NULL; +} + void gntdev_add_map(struct gntdev_priv *priv, struct gntdev_grant_map *add) { struct gntdev_grant_map *map; @@ -610,6 +692,9 @@ static int gntdev_release(struct inode *inode, struct f= ile *flip) struct gntdev_grant_map *map; =20 pr_debug("priv %p\n", priv); +#ifdef CONFIG_XEN_GNTDEV_DMABUF + gntdev_dmabuf_fini(priv->dmabuf_priv); +#endif =20 mutex_lock(&priv->lock); while (!list_empty(&priv->maps)) { @@ -620,10 +705,6 @@ static int gntdev_release(struct inode *inode, struct = file *flip) } mutex_unlock(&priv->lock); =20 -#ifdef CONFIG_XEN_GNTDEV_DMABUF - gntdev_dmabuf_fini(priv->dmabuf_priv); -#endif - kfree(priv); return 0; } @@ -1020,6 +1101,16 @@ static long gntdev_ioctl(struct file *flip, =20 case IOCTL_GNTDEV_DMABUF_IMP_RELEASE: return gntdev_ioctl_dmabuf_imp_release(priv, ptr); + + case IOCTL_GNTDEV_DMABUF_MAP_REFS_TO_BUF: + return gntdev_ioctl_dmabuf_map_refs_to_buf(priv, use_ptemod, ptr); + + case IOCTL_GNTDEV_DMABUF_MAP_RELEASE: + return gntdev_ioctl_dmabuf_map_release(priv, ptr); + + case IOCTL_GNTDEV_DMABUF_MAP_WAIT_RELEASED: + return gntdev_ioctl_dmabuf_map_wait_released(priv, ptr); + #endif =20 default: diff --git a/drivers/xen/grant-table.c b/drivers/xen/grant-table.c index d6576c8b6f0f..257e335bc65b 100644 --- a/drivers/xen/grant-table.c +++ b/drivers/xen/grant-table.c @@ -1036,6 +1036,40 @@ void gnttab_free_pages(int nr_pages, struct page **p= ages) } EXPORT_SYMBOL_GPL(gnttab_free_pages); =20 +int gnttab_dma_reserve_pages(int nr_pages, struct page **pages, + xen_pfn_t *frames) +{ + int ret, i; + + for (i =3D 0; i < nr_pages; i++) + xenmem_reservation_scrub_page(pages[i]); + + xenmem_reservation_va_mapping_reset(nr_pages, pages); + + ret =3D xenmem_reservation_decrease(nr_pages, frames); + if (ret !=3D nr_pages) + return ret; + + return 0; +} +EXPORT_SYMBOL_GPL(gnttab_dma_reserve_pages); + +int gnttab_dma_clean_page_reservation(int nr_pages, struct page **pages, + xen_pfn_t *frames) +{ + int ret; + + ret =3D xenmem_reservation_increase(nr_pages, frames); + if (ret !=3D nr_pages) { + pr_debug("Failed to increase reservation for DMA buffer\n"); + return -EFAULT; + } + + xenmem_reservation_va_mapping_update(nr_pages, pages, frames); + return 0; +} +EXPORT_SYMBOL_GPL(gnttab_dma_clean_page_reservation); + #ifdef CONFIG_XEN_GRANT_DMA_ALLOC /** * gnttab_dma_alloc_pages - alloc DMAable pages suitable for grant mapping= into @@ -1071,17 +1105,11 @@ int gnttab_dma_alloc_pages(struct gnttab_dma_alloc_= args *args) =20 args->pages[i] =3D page; args->frames[i] =3D xen_page_to_gfn(page); - xenmem_reservation_scrub_page(page); } =20 - xenmem_reservation_va_mapping_reset(args->nr_pages, args->pages); - - ret =3D xenmem_reservation_decrease(args->nr_pages, args->frames); - if (ret !=3D args->nr_pages) { - pr_debug("Failed to decrease reservation for DMA buffer\n"); - ret =3D -EFAULT; + ret =3D gnttab_dma_reserve_pages(args->nr_pages, args->pages, args->frame= s); + if (ret) goto fail; - } =20 ret =3D gnttab_pages_set_private(args->nr_pages, args->pages); if (ret < 0) @@ -1109,17 +1137,8 @@ int gnttab_dma_free_pages(struct gnttab_dma_alloc_ar= gs *args) for (i =3D 0; i < args->nr_pages; i++) args->frames[i] =3D page_to_xen_pfn(args->pages[i]); =20 - ret =3D xenmem_reservation_increase(args->nr_pages, args->frames); - if (ret !=3D args->nr_pages) { - pr_debug("Failed to increase reservation for DMA buffer\n"); - ret =3D -EFAULT; - } else { - ret =3D 0; - } - - xenmem_reservation_va_mapping_update(args->nr_pages, args->pages, - args->frames); - + ret =3D gnttab_dma_clean_page_reservation(args->nr_pages, args->pages, + args->frames); size =3D args->nr_pages << PAGE_SHIFT; if (args->coherent) dma_free_coherent(args->dev, size, diff --git a/include/uapi/xen/gntdev.h b/include/uapi/xen/gntdev.h index 7a7145395c09..cadc7fd9bc9c 100644 --- a/include/uapi/xen/gntdev.h +++ b/include/uapi/xen/gntdev.h @@ -312,4 +312,66 @@ struct ioctl_gntdev_dmabuf_imp_release { __u32 reserved; }; =20 +/* + * Fd mapping ioctls allows to map @fd to @refs. + * + * Allows gntdev to map scatter-gather table to the existing dma-buf + * file destriptor. It provides the same functionality as + * DMABUF_EXP_FROM_REFS_V2 ioctls, + * but maps sc table on top of the existing buffer memory, instead of + * allocting memory. This is useful when exporter should work with external + * buffer. + */ + +#define IOCTL_GNTDEV_DMABUF_MAP_REFS_TO_BUF \ + _IOC(_IOC_NONE, 'G', 15, \ + sizeof(struct ioctl_gntdev_dmabuf_map_refs_to_buf)) +struct ioctl_gntdev_dmabuf_map_refs_to_buf { + /* IN parameters. */ + /* Specific options for this dma-buf: see GNTDEV_DMA_FLAG_XXX. */ + __u32 flags; + /* Number of grant references in @refs array. */ + __u32 count; + /* Offset of the data in the dma-buf. */ + __u32 data_ofs; + /* File descriptor of the dma-buf. */ + __u32 fd; + /* The domain ID of the grant references to be mapped. */ + __u32 domid; + /* Variable IN parameter. */ + /* Array of grant references of size @count. */ + __u32 refs[1]; +}; + +/* + * This will release gntdev attachment to the provided buffer with file + * descriptor @fd, so it can be released by the owner. This is only valid + * for buffers created with IOCTL_GNTDEV_DMABUF_EXP_REFS_TO_BUF. + * Returns 0 on success, -ETIME when waiting dma_buffer fences to clean + * reached timeout. In this case release call should be repeated after + * releasing dma_buffer fences. + */ +#define IOCTL_GNTDEV_DMABUF_MAP_RELEASE \ + _IOC(_IOC_NONE, 'G', 16, \ + sizeof(struct ioctl_gntdev_dmabuf_map_release)) +struct ioctl_gntdev_dmabuf_map_release { + /* IN parameters */ + __u32 fd; + __u32 reserved; +}; + +/* + * This will wait until gntdev release procedure is finished and buffer was + * released completely. This is only valid for buffers created with + * IOCTL_GNTDEV_DMABUF_EXP_REFS_TO_BUF. + */ +#define IOCTL_GNTDEV_DMABUF_MAP_WAIT_RELEASED \ + _IOC(_IOC_NONE, 'G', 17, \ + sizeof(struct ioctl_gntdev_dmabuf_map_wait_released)) +struct ioctl_gntdev_dmabuf_map_wait_released { + /* IN parameters */ + __u32 fd; + __u32 wait_to_ms; +}; + #endif /* __LINUX_PUBLIC_GNTDEV_H__ */ diff --git a/include/xen/grant_table.h b/include/xen/grant_table.h index 8e220edf44ab..73b473474ac9 100644 --- a/include/xen/grant_table.h +++ b/include/xen/grant_table.h @@ -250,6 +250,11 @@ int gnttab_dma_alloc_pages(struct gnttab_dma_alloc_arg= s *args); int gnttab_dma_free_pages(struct gnttab_dma_alloc_args *args); #endif =20 +int gnttab_dma_reserve_pages(int nr_pages, struct page **pages, + xen_pfn_t *frames); +int gnttab_dma_clean_page_reservation(int nr_pages, struct page **pages, + xen_pfn_t *frames); + int gnttab_pages_set_private(int nr_pages, struct page **pages); void gnttab_pages_clear_private(int nr_pages, struct page **pages); =20 --=20 2.25.1