From patchwork Mon Jan 2 13:41:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oleksii Moisieiev X-Patchwork-Id: 38121 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp4163520wrt; Mon, 2 Jan 2023 05:42:59 -0800 (PST) X-Google-Smtp-Source: AMrXdXu+aT9z+kILPIoRY5OTQYusHhY2bDtRaJUqFY/qwEj+U+fRJDNvOgUtNV5gdwA1fNnomSJ0 X-Received: by 2002:a05:6a21:3386:b0:a3:1b92:5b1e with SMTP id yy6-20020a056a21338600b000a31b925b1emr15626828pzb.3.1672666978712; Mon, 02 Jan 2023 05:42:58 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1672666978; cv=pass; d=google.com; s=arc-20160816; b=nTG6MCC3owDYnaYBIZRYyfc9hdZdeXN7z+XQDA8WOZeKubjAtzPo6VqcOdPcuE1Kzy s/6ZzvUpxjv4FJ5/vrbt034zr4YeWZ6JlAt8uRhrIHI0ptC71JujU4owowECPleZDuIM Eb5RUqtkzNb6cXasMbwJo5pwiyv9xGRURa0UQbenIioKIRs1Tj0bW7Kb96yH0Gjv4JqR woZpB2HYRgkSWwFoPjmniRbJyzlo5A1UlB27EjChZFQx+jcan0R/retjI+LKegNSvDEk k6TYPamP/PkOKHELXgLdCqOvxc2+DsoMy8J8wHUhR6tvcywzJpEKDW2hTytoMlMNv5QZ ev1A== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:content-transfer-encoding :content-language:accept-language:in-reply-to:references:message-id :date:thread-index:thread-topic:subject:cc:to:from:dkim-signature; bh=upLENppnbaOJqNZ7wAgRPaS772ApiRNNpeCnQADukok=; b=EbCwwsueQOYi7gdW3TX2ql+CvviMayOT41p68mWiEdq1EwI7MbKSazmLg5crqBvioa tQeu09VCFxUmqjPY3JMvCwpoXEvuVBDdgQZK1S3OeHpUgfP1T/he0GuVeJkAR7VPOXSH /d8b1u7gcFHXOKR6dEDK5X5RQyAtQv9tLAFIiS+FZA1aIFXue2ovU8z6vy8x0oW8NEUe n85PyKIjiCP+yBFENciMLwCmv6dHX9pUdeFc6DYd8LK1EbOkAUJgt3l0a523Q7tpLXGd ZW4JSv8b2pmmDuGMMVKNf/1+GGoLvQHEZQnBpN1R/v/MYkHgdRRpR+bTzIj9Ak4YKfva nHCw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@epam.com header.s=selector2 header.b=T3oL8E80; arc=pass (i=1 spf=pass spfdomain=epam.com dkim=pass dkdomain=epam.com dmarc=pass fromdomain=epam.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=epam.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c18-20020a170902d49200b00186b6a32284si31446670plg.548.2023.01.02.05.42.46; Mon, 02 Jan 2023 05:42:58 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@epam.com header.s=selector2 header.b=T3oL8E80; arc=pass (i=1 spf=pass spfdomain=epam.com dkim=pass dkdomain=epam.com dmarc=pass fromdomain=epam.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=epam.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232947AbjABNmS (ORCPT + 99 others); Mon, 2 Jan 2023 08:42:18 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40744 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232050AbjABNmN (ORCPT ); Mon, 2 Jan 2023 08:42:13 -0500 Received: from mx0b-0039f301.pphosted.com (mx0b-0039f301.pphosted.com [148.163.137.242]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C0F2963CE; Mon, 2 Jan 2023 05:42:11 -0800 (PST) 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 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== 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 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/i?= =?iso-8859-1?q?ueQyffU7Aefc222DGZ8engqUXxDYFqDhhWsklXBmUqO8vdTsG9eOYChTyQ3+?= =?iso-8859-1?q?PL/+hhuseX0TkGSJKfjWL5yL1FxZI3DLLLNWx8q2ucibwxBygufAWID7zKm+?= =?iso-8859-1?q?cikKSS2rG1pejj+wR1TIuu+K4jplG65gFh/+hDBrmYEOHMFr+uTX/Lh8Qwwa?= =?iso-8859-1?q?B/6qHCUjgRwIdlHc3a3UYM96ErdWUQOvxwx+ofVp73MmjqxruCfkoSXCYr6T?= =?iso-8859-1?q?3sXHXGJRNK6L5zKsa8qmeIqt7ZbZwrLGQMJPVB0+P6bg4yghqhyj69aQHwy0?= =?iso-8859-1?q?x6O94ezPzNMmQYcIMdO+ekY3uTKEWrbhJMy+Smzzu52UFjuCq5NTQdEoX1mh?= =?iso-8859-1?q?iEvnomzVaryP9ZqHooMRPTT2p9XEorxFULahLRpfxbZUE28+dnNoLxy3Dwrz?= =?iso-8859-1?q?LfP2YecjeBmwxewRXbxMlQ3g+1N68v6lKIRYINDvORC2v7aSOodhMMxcd1Ft?= =?iso-8859-1?q?o72kkDvuQgDrnY27u558OmEd4flK0bQ2jx2sFB5LzGQFN6jUcJIKVIoUyM0l?= =?iso-8859-1?q?SuHQY+/6PKUKfp/IThCgXV/xWkEELFj5S8nu+/znOyxSwEWeXHshjI5VUt45?= =?iso-8859-1?q?VhiCvHaHNFzkoQMVCcKqsJpv+w8qy4aC+O/zHSn88lQWNbMgd8YYWfRgM75k?= =?iso-8859-1?q?eJMuZbWA1lHQrDOnSVB7uyVzulo/R3rWzsTR6oXTE7k3smf49EChQUJRzono?= =?iso-8859-1?q?5yFwXGDKCfZSw4/+83oz2cTfxyPYnd/AFfj+M5himSq/t282B5M00TU4Xz3b?= =?iso-8859-1?q?8YgeDs3QgJ3ZBI/2oaAh5nWD9Y+7758VlM/ANYR9Aj46qDx+FOZzJN0cE/xw?= =?iso-8859-1?q?ggKnVDT0lX616JttIsRpZ0MYz0cDn+Z388IrOdy9tqgiGe3wvEZFujLz3XRx?= =?iso-8859-1?q?nyVlzxRHIhUGyj6ii4r+jvD2KA8nvMV1mcxtbHkcKlzG9O5dn7+w1/pvVF8f?= =?iso-8859-1?q?8tInCJ/1SG18OlFiPoUQkKNXYi0gcjEdnxlIQ9Jp+Bt+NKwn9zr1VtjovfjP?= =?iso-8859-1?q?hRzun/CuzGk/fh2uuaVEadwIcntMwSGXrroTPLOiB17mDlUqd86F+lC/615w?= =?iso-8859-1?q?EzM4vFGJo1gN5F/PmMfolS1kGUcXjVYdYC0wnTFtFlfW1BEZE9EW8MWmTdQ/?= =?iso-8859-1?q?4u6JIBk/Kn8b9cXEOqmg2mcP6yGcIfARxp9CgDU9v9Zuwqe4Ut9v0iFQ8QR4?= =?iso-8859-1?q?DIedxMgyTZPc6wfygq0+Prcu3CzYyAahoY7BAAROmXxKXvIuHxlx+tWyMROl?= =?iso-8859-1?q?Wn5GapUZEH34JKeVSD0H9TdnrT7yvsTncF5KCNdKVCtpOuoM+L1PgL62+5zH?= =?iso-8859-1?q?NaB7PqzKmXwB0nwr9yOPIgblcWs1df9/tVt1syoCdf0YNcayZ6706N9LqTL7?= =?iso-8859-1?q?QejzPhha14Lqym3usffPv8FSd80TdfyRJ5wv0ICxWXp8D/YgIqGHet/mIPyv?= =?iso-8859-1?q?NR10HTpJVUT1rXskt0g3Dc5EPWVM637dWaMp0SfhwOfT0zyufy/nezBU2LaS?= =?iso-8859-1?q?sW2RaKNfl2Gp4tzoSSO3uoJzsHiIDy0pwY1tGcqeB1Hf/0OgvjdnGNOcLr6n?= =?iso-8859-1?q?/E1H018gdiz50V/9uefCUfa6ecGjGARXGxbNAOw=3D=3D?= 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-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW,SPF_HELO_NONE, SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1753918449825662999?= X-GMAIL-MSGID: =?utf-8?q?1753918449825662999?= 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 = 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 *map_ops, case GNTST_okay: { struct xen_page_foreign *foreign; + int page_cnt; SetPageForeign(pages[i]); foreign = xen_page_foreign(pages[i]); foreign->domid = map_ops[i].dom; foreign->gref = map_ops[i].ref; + page_cnt = 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 = 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 = 1; + } else + foreign->private = page_cnt; break; } @@ -1308,9 +1320,11 @@ static void __gnttab_unmap_refs_async(struct gntab_unmap_queue_data* item) { int ret; int pc; + struct xen_page_foreign *foreign; for (pc = 0; pc < item->count; pc++) { - if (page_count(item->pages[pc]) > 1) { + foreign = xen_page_foreign(item->pages[pc]); + if (page_count(item->pages[pc]) > foreign->private) { unsigned long delay = 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 /* * Technically there's no reliably invalid grant reference or grant handle, @@ -274,9 +275,11 @@ int gnttab_unmap_refs_sync(struct gntab_unmap_queue_data *item); void gnttab_batch_map(struct gnttab_map_grant_ref *batch, unsigned count); void gnttab_batch_copy(struct gnttab_copy *batch, unsigned count); +#define FOREIGN_MAX_PAGE_COUNT U16_MAX struct xen_page_foreign { domid_t domid; + uint16_t private; grant_ref_t gref; }; From patchwork Mon Jan 2 13:41:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oleksii Moisieiev X-Patchwork-Id: 38120 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp4163498wrt; Mon, 2 Jan 2023 05:42:55 -0800 (PST) X-Google-Smtp-Source: AMrXdXt3e2ElN8p80QxSwK8c1zb+fICsQrPmzouZi7f3tXWnBexGB+UL/8aWMFpRSmraWC9wOhU8 X-Received: by 2002:a62:1dcc:0:b0:581:bca1:b7a4 with SMTP id d195-20020a621dcc000000b00581bca1b7a4mr3568794pfd.1.1672666974844; Mon, 02 Jan 2023 05:42:54 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1672666974; cv=pass; d=google.com; s=arc-20160816; b=DDThNnDMLi2ncytfBKXWH7nd1y4L/f1E6UuGSF3bMbfBtgo7cOt8DsJt1tLfJomFvp H1F44HGMsBuQBfZKVXmxYOWPeKbJQ88F/uotLk+7JNOeIJpzcB+sWtJAKUlTyNxtRuGz Jr5TVqdAFH8oE61yzxEVSjkslxAmiJQC9tfTcOo0ieZq+3mnmk1/EjuqF8mcIdeHCfXD 4Ofxq/zrwEm0EpjjOTkOmNgtW+WdQmDt2NEhl1LPoy4QFTxffyplo0yWXE1bpzOCeVBr xiwXz/plvCUB9BEwTYLPJe/o3S8hzVlVnAbZy/w/gtdnJKgY4iuvIeF2qxBbWWzmg8v4 g1iA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:content-transfer-encoding :content-language:accept-language:in-reply-to:references:message-id :date:thread-index:thread-topic:subject:cc:to:from:dkim-signature; bh=cIV9qnXRfvVgBfrc0HVcsMdFAbZxe0DZ7oyvFfhKedY=; b=hTNAhV4PNM5vmyp6/O9AlePMK+GSDRFqt6masO/moNRU94e2Ji7O3gyIgYkT5CaEHa ZH+fYz8Gfyscq2rgkJjMLgT3anH4mMLMr5T9xe86xVc1vZ/XRcjlhXkSxhF9oBq+Ug2/ r/L6h9dZgcdi8GTxOCjurh6P0dBPbZcljN1xOOw9DfpEAxvgLAfhvuUdcyJiWR3a7uM+ BeNJ0S/3akQYhk4aTtEzXf6Z2QqyRyL9Ryp8jCY8lInmMp65ydjVEcuYo1V03ngqBFru zJdITbaKv0CvsWKexMD/ZZGIg/8hA/XspIxq3hHxrhV0gn3Ap32qcri+EXgtxyKW/M5R 7NKA== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@epam.com header.s=selector2 header.b=lm6SXYlS; arc=pass (i=1 spf=pass spfdomain=epam.com dkim=pass dkdomain=epam.com dmarc=pass fromdomain=epam.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=epam.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bv4-20020a056a00414400b0056ea2b142b6si6540300pfb.373.2023.01.02.05.42.40; Mon, 02 Jan 2023 05:42:54 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@epam.com header.s=selector2 header.b=lm6SXYlS; arc=pass (i=1 spf=pass spfdomain=epam.com dkim=pass dkdomain=epam.com dmarc=pass fromdomain=epam.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=epam.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235659AbjABNmW (ORCPT + 99 others); Mon, 2 Jan 2023 08:42:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40746 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232531AbjABNmN (ORCPT ); Mon, 2 Jan 2023 08:42:13 -0500 Received: from mx0b-0039f301.pphosted.com (mx0b-0039f301.pphosted.com [148.163.137.242]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F0ADE63D7; Mon, 2 Jan 2023 05:42:11 -0800 (PST) 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 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== 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 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?= =?iso-8859-1?q?//8+zLiN43kF69YgxoeR8frlg8vwJu7x+fXxSVVGWVO+q9rOqDQ9N4W9nEqQ?= =?iso-8859-1?q?Ni2I9DBQGEKkGFY8lAvbuko1m+1Gx5nOz3crIq0sE206KoIUQn0Gcbiw227J?= =?iso-8859-1?q?w/43zylM2WeSxpdk6zwcRcJnD0STJavd/srqcdvx+np25XIsevDxDjp870UX?= =?iso-8859-1?q?FPTrf3IRdhamWSbZwn9pgYQ0oT+1MLET6oOcFfHtashYQ5Ng8LTGXAvTg/w/?= =?iso-8859-1?q?wIg7iYFJ1valtosgTq75RFPu+92IIszRPLSm4D91jgr7DjKgKv/9HG6Lg9yb?= =?iso-8859-1?q?kjaHxqhb3gLBMpdkw0dd0XqHTNTQKTXDjW3FlDAtpd5+4XcJZN5wduuVwzxy?= =?iso-8859-1?q?oGekNeWG2OO9/l4kZGi98JDvhr3wvyfwqQzsRM9uZaMd/uxb6J5zNpUoL1xB?= =?iso-8859-1?q?+ZhaoZ3k1NElO7VKihYXyfWDVlte/YoQvR8qFxGBDpemv1eDSLI4Re20GqnM?= =?iso-8859-1?q?bZdxSlLWmi74SuUnRKev5xBmPRUYMWPoCuV90hoUTsUI8mcxYsbkXd4DD9sl?= =?iso-8859-1?q?IZCXeeSqHCjl15nZVGB3bo4PcmzpxcilJ+XCBDaxaRXge5YFVfEPqaYRq1bj?= =?iso-8859-1?q?DAZn+r6cJNuORoebjHEWBPyYuKKjqtiuGQIggyAJFzcImJgGATOGwTGAk8WE?= =?iso-8859-1?q?1z2CACo1yzYm0TyC9xvxMFRFUApNvf8KzQEb9h++X1yaF2QgM+KNdzTq2UtW?= =?iso-8859-1?q?0WeiuOx8b9giW26H4jd29TMsxLttDkCPzvAkLfth484McHNtMx/+KDocsuFC?= =?iso-8859-1?q?zGMM6osmWrUnNVTgAww+E+CLXCezs+SXKFtpGPib4fV9kOIQBVN0QKmspbCo?= =?iso-8859-1?q?3+wiy1pCuYE/FmvVd65LYXWy46+nHpljHzWQ1cc6T09tMUM1+OT0W/dyuvbU?= =?iso-8859-1?q?kveRRSsFMhvX1Od9GbwHyYkoH7f53dOQNk9xlNsa9rXM8OxCRsuB/fAGnJUc?= =?iso-8859-1?q?vkysU2DkExSpeJ8pYZl1lJtpZT2Qd4jdHxy2aSgjxME/UEMH9krvlW10q7a0?= =?iso-8859-1?q?q5x+y3o0nZQUysNTjISbrddrugIr8UWRiPI+ftxgTG/zc5g3ch69oq43157k?= =?iso-8859-1?q?vQGca/mbT46Lc1zuM8vLC/B5HwRv0Hwx6l6a707EGl6chP7GbF0wHULp/wbw?= =?iso-8859-1?q?K+THDjqzspYRZWtguxMKTZGLCvj7uP0vJ2rJQl6Oq1quyMsT4Baf0ikPtZku?= =?iso-8859-1?q?bNMOn7EXCY8t+v3DzH/Y3bBoBOQsQQwpsx3Wt/hxUx2u9oj2aOoSnnUoVeI+?= =?iso-8859-1?q?uEXZ6ifqg73aVy7Fs4lkz1VtJ2h3TqukUoS3zJnOGGcKX2Q/FYERy0RQA83p?= =?iso-8859-1?q?ZWnjqGrODWrhSPtD15MVFqEmiaFC0pQnWQ0710f5gxDNITEQkGK71JA9yeag?= =?iso-8859-1?q?TmfH2B47Anu1Pigjzi0q9+Nxc+thr52VW2ZuFlFNDUNT4DW70JOX5LB9vjGF?= =?iso-8859-1?q?euefKeoxBKBMe8okVWvG/S58DURQwul6XywvvH7nIPbEed3ynZ5XCGF/MuTD?= =?iso-8859-1?q?MJxaE4OSoFgQ9CxfJhEe3jHmzo8JGW/y+L/EGeEj77WDzrgnbHaz9y2LKMm6?= =?iso-8859-1?q?1n4Q3bHmF5Pe19SL0jC32gwVMOlRULlCDYWk3vA=3D=3D?= 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-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW,SPF_HELO_NONE, SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1753918445734300285?= X-GMAIL-MSGID: =?utf-8?q?1753918445734300285?= 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 #include @@ -57,6 +58,46 @@ static char *dmabuffs_dname(struct dentry *dentry, char *buffer, int buflen) dentry->d_name.name, ret > 0 ? name : ""); } +int dma_buf_register_release_notifier(struct dma_buf *dmabuf, + ext_release_notifier_cb ext_release_cb, void *priv) +{ + int ret = 0; + + spin_lock(&dmabuf->ext_release_lock); + + if (dmabuf->ext_release_cb) { + ret = -EEXIST; + goto unlock; + } + + dmabuf->ext_release_cb = ext_release_cb; + dmabuf->ext_release_priv = 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 = 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); dma_buf_stats_teardown(dmabuf); + dma_buf_call_release_notifier(dmabuf); + dmabuf->ops->release(dmabuf); if (dmabuf->resv == (struct dma_resv *)&dmabuf[1]) @@ -642,6 +685,7 @@ struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info) init_waitqueue_head(&dmabuf->poll); dmabuf->cb_in.poll = dmabuf->cb_out.poll = &dmabuf->poll; dmabuf->cb_in.active = dmabuf->cb_out.active = 0; + spin_lock_init(&dmabuf->ext_release_lock); if (!resv) { resv = (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); }; +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; + /** @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; @@ -632,4 +643,8 @@ int dma_buf_mmap(struct dma_buf *, struct vm_area_struct *, 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__ */ From patchwork Mon Jan 2 13:41:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oleksii Moisieiev X-Patchwork-Id: 38122 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp4163529wrt; Mon, 2 Jan 2023 05:43:00 -0800 (PST) X-Google-Smtp-Source: AMrXdXtxEEBZhTMakpB4rYd69KnRVe4CiHT2Xzpqifcw+SxN1VflY2UdcNFayCpgawK7fkTckjL4 X-Received: by 2002:a05:6a00:44cd:b0:582:6acd:eb5c with SMTP id cv13-20020a056a0044cd00b005826acdeb5cmr997662pfb.1.1672666980603; Mon, 02 Jan 2023 05:43:00 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1672666980; cv=pass; d=google.com; s=arc-20160816; b=xz1b9I9THJZwwzTgYkD91wfv+lm5nyn2B3Z3E3cSoa/8l8AjfVUPA4YEbvpY9Q/xMk 00IDtUXD6LSBOkjbXNFTnGwnpgT2a3MnwMls/Yu2S644CANavXG4/OwmMhfNsNVlt1hM tPtP8eGMxCNKqGlKMzGY/FjfXTDYv+W5B5+DychzFYYADPohFoAaM46RCNE3pM5MfvA0 SxERc5DRHh03j4Z/jNn4IT7vRRRD6R6T94b5A2w8s/iemklcJ49XwBDJSYNQcE6R7GrK wILwQPnlnj2sIC2pGS0J+VXfpEHB9CWUwH421Mb44w+aoJL4JmT/VEYZRAH8sbm7IW/V 309A== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:content-transfer-encoding :content-language:accept-language:in-reply-to:references:message-id :date:thread-index:thread-topic:subject:cc:to:from:dkim-signature; bh=cpM/GTVcMI8fqBjEBRN4Lty7tXpejC9G2s04LY0mrJs=; b=jbLFhVsuTdO9hqHTj7iwY95POeJiPCFXDK++tYuv7h0mw9PFSKfdEA3xNI0EHfuzhk bhiGsBHY1nmE2tDVjTy62ueZtgHwNzpsp5Bbxptw4a/xU6JyyH6fJ/gAZO5USdlyNsy0 BHU6QoyP/oK/kwqQlSnhxv1rn1aplSs9S8VCsxwuqRYUch+uEGrdmGqC3BfRrHBwcUcC +pWGr+gRwXN40Cha85sCEBbPdHRrxtezyHe6LrRmvBuZbCjp7DmRoEYG4dsBISrxM+DV 1n3/mkdkRoM2eOnyw0/iqprElyZt1S1aeYPyroMuthZ6rnK0crmGYrNNWOucH/bVFRHM d5/w== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@epam.com header.s=selector2 header.b=kyQSl3x6; arc=pass (i=1 spf=pass spfdomain=epam.com dkim=pass dkdomain=epam.com dmarc=pass fromdomain=epam.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=epam.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g2-20020aa78182000000b0057fd681788asi27582416pfi.225.2023.01.02.05.42.48; Mon, 02 Jan 2023 05:43:00 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@epam.com header.s=selector2 header.b=kyQSl3x6; arc=pass (i=1 spf=pass spfdomain=epam.com dkim=pass dkdomain=epam.com dmarc=pass fromdomain=epam.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=epam.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235952AbjABNm0 (ORCPT + 99 others); Mon, 2 Jan 2023 08:42:26 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40748 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232791AbjABNmP (ORCPT ); Mon, 2 Jan 2023 08:42:15 -0500 Received: from mx0b-0039f301.pphosted.com (mx0b-0039f301.pphosted.com [148.163.137.242]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6D0B663DA; Mon, 2 Jan 2023 05:42:12 -0800 (PST) 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 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== 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 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?SiGlTnYNPDw2YpnUzSo6FpT?= =?iso-8859-1?q?U+eS1V6Hg5OD2RkL6vKuPrNo8IBCWnG0z7SWBGx1espg5fyI3QzgfX7olheS?= =?iso-8859-1?q?lizTxfW5qdDpIqsJimXnwH3rfIvoNXkoyXqJtIIiE/3ToUosbcI0LXN+Q5Av?= =?iso-8859-1?q?OheCg35ov2CL/As8sv/vvSALHr0TFndUlmStJ9KuIBsmPqwOcunDnRWvkBgU?= =?iso-8859-1?q?cUgZMtVKWqXMFexIhhKwQl4XjTxSEr2zwAt7JGRGSx0VhqhFyd+socMt5My6?= =?iso-8859-1?q?h4oP07SGvxnKj7TSqym8MjXUv/+DI4d69HysRmUpP0uFgfrxfVsAdbOcrDkS?= =?iso-8859-1?q?gSkOsURxE7SiqrFm+PWG9mNxHSCPmqQOOaRLDx7sOpZ7+K9B+gvahNYRBndI?= =?iso-8859-1?q?ht8m09aSlOPWueDGUU/4uH9+XIP19g8B9ry2b83Zy6bIA3Liun1+RLJLqZ9H?= =?iso-8859-1?q?rZrq2rOY5E6RSVkimzM+lnzHtvxx/F3OfFMbFcWEk0jDZEQcdwqFUQMFISGm?= =?iso-8859-1?q?U2S+kU37B98UPtBTAVD4XdR2IEguZN5MBtVIXdUEpb7hNu0aB6o/9jWtA3em?= =?iso-8859-1?q?9NdPDionCaIw1z1IloDGQ693RDtgG7v+tsPRC96iYJkaUzqIVxz4hrDgeOcO?= =?iso-8859-1?q?V+CoV3bOacyrv/yylKYGU7eyVj0bbDqf46aUj2IVlq4wviiGk3fEYuCcnQbu?= =?iso-8859-1?q?Oro+LWP2jJoYi+F78DqHY8TKbvvlFTPEi4kaLysLqIMg8KRk2KQBNRGx+v94?= =?iso-8859-1?q?swe5jw0uebsGjNsf3PG0fmBYyjT7rG9+Ya8YCKDUcUB7pm19dcg95Gi4rmiX?= =?iso-8859-1?q?9Yo8pkdHr0sOY7Szifb2rXh5AbbhKG5bETxeamg0F/vGjgCrALjwY7A2ifW9?= =?iso-8859-1?q?URopXeu6sBlEfCl2VwIxGmwmWUXryiZgRlklZ/VFmx9Ww0KqF9ukVWKtOnGc?= =?iso-8859-1?q?m6vca3Xwt8TtNQQxDtWZhiUpJZvFdF4SwkCP48qmGo5DwsDL4EFzGvGvIc9/?= =?iso-8859-1?q?2iuLrEW+9HObIFTZPLEEwX8KSXSsChpxzrKhADvZws56emjs0GieP45wR4sf?= =?iso-8859-1?q?u0Mqr5KdqXaDg9mr9wtjpc194o7WNZqGdKADYa1yvwDGdd2mOV6AG6t3FdGO?= =?iso-8859-1?q?Ow2l9KFW4giqjZONbNPCRDdXOlQ+mBI7UyoYc5UkuE+L/1VPBvwLTniNOYzJ?= =?iso-8859-1?q?As4W6Lf+p4zePK7D4v2AVf0Tv5HO/5Fm0hfy7a9Wm6KJ4QaVyyKa9An8/mLO?= =?iso-8859-1?q?5BpQrzdo/9zLsTDjnoF7jZVGaPkzLQ8IBDY+nVN6xmzZFRtyq+R0jpcjxgUP?= =?iso-8859-1?q?YZx/PieIoKhbcg8feNMUG/gk3+VtGsDsH/xXhrlEk/CCPl3+df5KuAPCG4bL?= =?iso-8859-1?q?tvxrdnotVskz37kiofFcIhmpfbdbBjihdv2REoVwJO5CFMbRcroAx6vDRp6j?= =?iso-8859-1?q?Z2PTddgCwlU3Lx/8G/2tibtqfLIXq5y7LDLPBeYrXEC4W13qqjZ9aQhOQdlU?= =?iso-8859-1?q?/vGbjsjuW4PsPHIPng7Syi4Hpl92p/1P4a/RxQKSuY56MUZmIBd2cDqkhqUQ?= =?iso-8859-1?q?WFXWqcFLR2n32uaQvyG7q60eb5CEdymGDbPlYbPw3FA2+SDqBbY+WHRph7Lg?= =?iso-8859-1?q?7/ZyTLn04nfk2kZ0IGbKXItgLvqXWr9osn9ygoA=3D=3D?= 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-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW,SPF_HELO_NONE, SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1753918451604736125?= X-GMAIL-MSGID: =?utf-8?q?1753918451604736125?= 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; #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 /* Number of live grants */ @@ -85,6 +87,8 @@ struct gntdev_grant_map { struct gntdev_grant_map *gntdev_alloc_map(struct gntdev_priv *priv, int count, 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); void gntdev_add_map(struct gntdev_priv *priv, struct gntdev_grant_map *add); 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 @@ #include #include +#include #include +#include #include #include #include #include +#include #include #include +#include #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; /* 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; }; @@ -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 { static void dmabuf_exp_release(struct kref *kref); +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 = ERR_PTR(-ENOENT); + + mutex_lock(&priv->lock); + list_for_each_entry(obj, &priv->map_wait_list, next) + if (obj->fd == fd) { + pr_debug("Found gntdev_dmabuf in the wait list\n"); + ret = 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 = dmabuf_map_wait_obj_find(gntdev_dmabuf->priv, fd); + if ((!obj) || (PTR_ERR(obj) == -ENOENT)) { + obj = kzalloc(sizeof(*obj), GFP_KERNEL); + if (!obj) + return -ENOMEM; + } + + init_completion(&obj->completion); + obj->gntdev_dmabuf = gntdev_dmabuf; + obj->fd = 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 == 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; } +static void dmabuf_map_free_gntdev_dmabuf(struct gntdev_dmabuf *gntdev_dmabuf) +{ + if (!gntdev_dmabuf) + return; + + kfree(gntdev_dmabuf->pages); + + kvfree(gntdev_dmabuf->u.map.frames); + kfree(gntdev_dmabuf); + gntdev_dmabuf = NULL; +} + static struct gntdev_grant_map * dmabuf_exp_alloc_backing_storage(struct gntdev_priv *priv, int dmabuf_flags, int count) @@ -432,6 +523,113 @@ dmabuf_exp_alloc_backing_storage(struct gntdev_priv *priv, int dmabuf_flags, return map; } +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 = 0; + + gntdev_dmabuf = kzalloc(sizeof(*gntdev_dmabuf), GFP_KERNEL); + if (!gntdev_dmabuf) + return ERR_PTR(-ENOMEM); + + gntdev_dmabuf->pages = kcalloc(count, + sizeof(gntdev_dmabuf->pages[0]), GFP_KERNEL); + + if (!gntdev_dmabuf->pages) { + ret = -ENOMEM; + goto free; + } + + gntdev_dmabuf->u.map.frames = kvcalloc(count, + sizeof(gntdev_dmabuf->u.map.frames[0]), GFP_KERNEL); + if (!gntdev_dmabuf->u.map.frames) { + ret = -ENOMEM; + goto free; + } + + if (gntdev_test_page_count(count)) { + ret = -EINVAL; + goto free; + } + + dma_buf = dma_buf_get(fd); + if (IS_ERR_OR_NULL(dma_buf)) { + pr_debug("Unable to get dmabuf from fd\n"); + ret = PTR_ERR(dma_buf); + goto free; + } + + attach = dma_buf_attach(dma_buf, priv->dma_dev); + if (IS_ERR_OR_NULL(attach)) { + ret = PTR_ERR(attach); + goto fail_put; + } + + sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL); + if (IS_ERR_OR_NULL(sgt)) { + ret = PTR_ERR(sgt); + goto fail_detach; + } + + if (sgt->sgl->offset != data_ofs) { + pr_debug("DMA buffer offset %d, user-space expects %d\n", + sgt->sgl->offset, data_ofs); + ret = -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 = -EINVAL; + goto fail_unmap; + } + + gntdev_dmabuf->u.map.map = gntdev_get_alloc_from_fd(priv, sgt, count, + dmabuf_flags); + if (IS_ERR_OR_NULL(gntdev_dmabuf->u.map.map)) { + ret = -ENOMEM; + goto fail_unmap; + }; + + gntdev_dmabuf->priv = priv->dmabuf_priv; + gntdev_dmabuf->fd = fd; + gntdev_dmabuf->u.map.attach = attach; + gntdev_dmabuf->u.map.sgt = sgt; + gntdev_dmabuf->dmabuf = dma_buf; + gntdev_dmabuf->nr_pages = count; + gntdev_dmabuf->u.map.priv = 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, count * + 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 *priv, int flags, return ret; } +static void dmabuf_release_notifier_cb(struct dma_buf *dmabuf, void *priv) +{ + struct gntdev_dmabuf *gntdev_dmabuf = 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 = gntdev_dmabuf->dmabuf; + long lret; + + /* Wait on any implicit fences */ + lret = dma_resv_wait_timeout(dma_buf->resv, + dma_resv_usage_rw(true), true, + MAX_SCHEDULE_TIMEOUT); + if (lret == 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 sync) +{ + int ret; + + if (!sync) { + ret = dmabuf_map_wait_obj_set(gntdev_dmabuf->priv, gntdev_dmabuf, + gntdev_dmabuf->fd); + if (ret) + return ret; + } + + ret = dmabuf_detach_map(gntdev_dmabuf); + if (ret) + return ret; + + if (!sync) { + ret = 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 = dmabuf_alloc_gntdev_from_buf(priv, fd, flags, count, + data_ofs); + + if (IS_ERR_OR_NULL(gntdev_dmabuf)) { + ret = PTR_ERR(gntdev_dmabuf); + goto fail_gntdev; + } + + for (i = 0; i < count; i++) { + gntdev_dmabuf->u.map.map->grants[i].domid = domid; + gntdev_dmabuf->u.map.map->grants[i].ref = 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 |= GNTMAP_host_map; +#if defined(CONFIG_X86) + gntdev_dmabuf->u.map.map->flags |= GNTMAP_device_map; +#endif + + ret = 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. */ static int @@ -673,14 +982,15 @@ dmabuf_imp_to_refs(struct gntdev_dmabuf_priv *priv, struct 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 = ERR_PTR(-ENOENT); 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 == fd) { - pr_debug("Found gntdev_dmabuf in the import list\n"); + pr_debug("Found gntdev_dmabuf in the list\n"); ret = gntdev_dmabuf; list_del(&gntdev_dmabuf->next); break; @@ -696,7 +1006,7 @@ static int dmabuf_imp_release(struct gntdev_dmabuf_priv *priv, u32 fd) struct dma_buf_attachment *attach; struct dma_buf *dma_buf; - gntdev_dmabuf = dmabuf_imp_find_unlink(priv, fd); + gntdev_dmabuf = dmabuf_list_find_unlink(priv, &priv->imp_list, fd); if (IS_ERR(gntdev_dmabuf)) return PTR_ERR(gntdev_dmabuf); @@ -726,6 +1036,21 @@ static void dmabuf_imp_release_all(struct gntdev_dmabuf_priv *priv) dmabuf_imp_release(priv, gntdev_dmabuf->fd); } +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. */ long gntdev_ioctl_dmabuf_exp_from_refs(struct gntdev_priv *priv, int use_ptemod, @@ -769,6 +1094,47 @@ long gntdev_ioctl_dmabuf_exp_from_refs(struct gntdev_priv *priv, int use_ptemod, return ret; } +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)) != 0) + return -EFAULT; + + if (op.count <= 0) + return -EINVAL; + + refs = kcalloc(op.count, sizeof(*refs), GFP_KERNEL); + if (!refs) + return -ENOMEM; + + if (copy_from_user(refs, u->refs, sizeof(*refs) * op.count) != 0) { + ret = -EFAULT; + goto out; + } + + ret = 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)) != 0) + ret = -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_priv *priv, return dmabuf_imp_release(priv->dmabuf_priv, op.fd); } +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)) != 0) + return -EFAULT; + + gntdev_dmabuf = 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 = 0; + + if (copy_from_user(&op, u, sizeof(op)) != 0) + return -EFAULT; + + obj = dmabuf_map_wait_obj_find(priv->dmabuf_priv, op.fd); + if (IS_ERR_OR_NULL(obj)) + return (PTR_ERR(obj) == -ENOENT) ? 0 : PTR_ERR(obj); + + if (!completion_done(&obj->completion)) + ret = dmabuf_exp_wait_obj_wait(obj, op.wait_to_ms); + + if (!ret && ret != -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 file *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); priv->filp = filp; @@ -844,5 +1251,6 @@ struct gntdev_dmabuf_priv *gntdev_dmabuf_init(struct file *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_ptemod, struct ioctl_gntdev_dmabuf_exp_from_refs __user *u); +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); @@ -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); +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 @@ #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt +#include #include #include #include @@ -43,6 +44,7 @@ #include #include #include +#include #include #include @@ -96,7 +98,11 @@ static void gntdev_free_map(struct gntdev_grant_map *map) return; #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; args.dev = map->dma_dev; @@ -216,6 +222,82 @@ struct gntdev_grant_map *gntdev_alloc_map(struct gntdev_priv *priv, int count, return NULL; } +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 = 0; + struct sg_page_iter sg_iter; + + add = kzalloc(sizeof(*add), GFP_KERNEL); + if (!add) + return NULL; + + add->grants = kvcalloc(count, sizeof(add->grants[0]), GFP_KERNEL); + add->map_ops = kvcalloc(count, sizeof(add->map_ops[0]), GFP_KERNEL); + add->unmap_ops = kvcalloc(count, sizeof(add->unmap_ops[0]), GFP_KERNEL); + add->pages = kvcalloc(count, sizeof(add->pages[0]), GFP_KERNEL); + add->frames = kvcalloc(count, sizeof(add->frames[0]), + GFP_KERNEL); + add->being_removed = + kvcalloc(count, sizeof(add->being_removed[0]), GFP_KERNEL); + add->preserve_pages = 1; + + if (add->grants == NULL || + add->map_ops == NULL || + add->unmap_ops == NULL || + add->pages == NULL || + add->frames == NULL || + add->being_removed == NULL) + goto err; + + if (use_ptemod) { + add->kmap_ops = kvmalloc_array(count, sizeof(add->kmap_ops[0]), + GFP_KERNEL); + add->kunmap_ops = kvmalloc_array(count, sizeof(add->kunmap_ops[0]), + GFP_KERNEL); + if (NULL == add->kmap_ops || NULL == add->kunmap_ops) + goto err; + } + + for_each_sgtable_page(sgt, &sg_iter, 0) { + struct page *page = sg_page_iter_page(&sg_iter); + + add->pages[i] = page; + add->frames[i] = xen_page_to_gfn(page); + i++; + if (i >= 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 = 0; i < count; i++) { + add->map_ops[i].handle = -1; + add->unmap_ops[i].handle = -1; + if (use_ptemod) { + add->kmap_ops[i].handle = -1; + add->kunmap_ops[i].handle = -1; + } + } + + add->index = 0; + add->count = 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 file *flip) struct gntdev_grant_map *map; pr_debug("priv %p\n", priv); +#ifdef CONFIG_XEN_GNTDEV_DMABUF + gntdev_dmabuf_fini(priv->dmabuf_priv); +#endif 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); -#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, 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 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 **pages) } EXPORT_SYMBOL_GPL(gnttab_free_pages); +int gnttab_dma_reserve_pages(int nr_pages, struct page **pages, + xen_pfn_t *frames) +{ + int ret, i; + + for (i = 0; i < nr_pages; i++) + xenmem_reservation_scrub_page(pages[i]); + + xenmem_reservation_va_mapping_reset(nr_pages, pages); + + ret = xenmem_reservation_decrease(nr_pages, frames); + if (ret != 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 = xenmem_reservation_increase(nr_pages, frames); + if (ret != 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) args->pages[i] = page; args->frames[i] = xen_page_to_gfn(page); - xenmem_reservation_scrub_page(page); } - xenmem_reservation_va_mapping_reset(args->nr_pages, args->pages); - - ret = xenmem_reservation_decrease(args->nr_pages, args->frames); - if (ret != args->nr_pages) { - pr_debug("Failed to decrease reservation for DMA buffer\n"); - ret = -EFAULT; + ret = gnttab_dma_reserve_pages(args->nr_pages, args->pages, args->frames); + if (ret) goto fail; - } ret = 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_args *args) for (i = 0; i < args->nr_pages; i++) args->frames[i] = page_to_xen_pfn(args->pages[i]); - ret = xenmem_reservation_increase(args->nr_pages, args->frames); - if (ret != args->nr_pages) { - pr_debug("Failed to increase reservation for DMA buffer\n"); - ret = -EFAULT; - } else { - ret = 0; - } - - xenmem_reservation_va_mapping_update(args->nr_pages, args->pages, - args->frames); - + ret = gnttab_dma_clean_page_reservation(args->nr_pages, args->pages, + args->frames); size = 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; }; +/* + * 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_args *args); int gnttab_dma_free_pages(struct gnttab_dma_alloc_args *args); #endif +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);