From patchwork Sat Nov 18 06:32:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13459886 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id C50CFC54FB9 for ; Sat, 18 Nov 2023 06:56:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 32CEF6B03DB; Sat, 18 Nov 2023 01:56:12 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 24F966B0538; Sat, 18 Nov 2023 01:56:12 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id ECB3B6B0535; Sat, 18 Nov 2023 01:56:11 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id DE7876B03DB for ; Sat, 18 Nov 2023 01:56:11 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id BFB1F160E3C for ; Sat, 18 Nov 2023 06:56:11 +0000 (UTC) X-FDA: 81470165742.19.0A65B9B Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.100]) by imf14.hostedemail.com (Postfix) with ESMTP id C4C2E100008 for ; Sat, 18 Nov 2023 06:56:09 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=QuIabWgD; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf14.hostedemail.com: domain of vivek.kasireddy@intel.com designates 134.134.136.100 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1700290570; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=OFWxI1i+p4efvmikRdB46knSq10ZwkVaViiQ0SsvQMA=; b=Jdqu7aieKJzdxYxsG8Dhq+sOo66viPNnd2FtCpbUOVTOMSuTtCOHZMjiQM1dx3txfZDZTR uPMLI2pijlTMxzhEFjjVP0Os8elHbTe2g7zzpGcT2DU3IKoTIndCuyIRTggPS+jtLbTAyq 9czW54b2eWt2RQsqQwrTaWT4JKbIRiY= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=QuIabWgD; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf14.hostedemail.com: domain of vivek.kasireddy@intel.com designates 134.134.136.100 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700290570; a=rsa-sha256; cv=none; b=UN7l8d0mhCKA4qGzOcMBFkItcXhmwtbYoRDuHINYrOoLj1sxcyFmDYDZsobhKqb7GQnBm6 jKsK33HiIaFkSzEO8BTSPXPbSmhk/H/CCpOgk4cfVPRq9IucOQX+G5PjNuLs6pMBAgJhG2 CEI22uo1QHrSJRhqMlFu9XerLxMMfK8= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1700290569; x=1731826569; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=U5Op4CjhDDAHfKtiDzpe6+9a1b7R8CllBxVTE/TesJE=; b=QuIabWgDBjr48MKUEe6AGH/StxP3oN17obpZTHKRBaCFHI/JPlEif0PO ojIgSFuAsd6xjYnhGmzOfg6/BLPfHY/b7nx5SOMd0E4npUB5b+yncZyK8 iVDdl3m0VC9kaM7mP03rin9zg08Szy1cm6PAAtW3zSgkZgXh3FitSD4oP 1+VEniQuxQU028A6iihQp6XAe1/LAMpi7faLFIqeuJ7gCP3aEnb5ObGA4 k709FlbCamr0VzooRNm3bIHQFaoONk4FgM9+qmDwYSHe5qbyFWWrLhH05 2vuhEuFFPkAqaDKopF9nzXkDSl0w3+a28EmlVFGfoe+wvYqsSLs4iIgmG w==; X-IronPort-AV: E=McAfee;i="6600,9927,10897"; a="457912353" X-IronPort-AV: E=Sophos;i="6.04,207,1695711600"; d="scan'208";a="457912353" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Nov 2023 22:56:07 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10897"; a="800686575" X-IronPort-AV: E=Sophos;i="6.04,207,1695711600"; d="scan'208";a="800686575" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Nov 2023 22:56:06 -0800 From: Vivek Kasireddy To: dri-devel@lists.freedesktop.org, linux-mm@kvack.org Cc: Vivek Kasireddy , David Hildenbrand , Daniel Vetter , Mike Kravetz , Hugh Dickins , Peter Xu , Jason Gunthorpe , Gerd Hoffmann , Dongwon Kim , Junxiao Chang Subject: [PATCH v4 1/5] udmabuf: Use vmf_insert_pfn and VM_PFNMAP for handling mmap Date: Fri, 17 Nov 2023 22:32:29 -0800 Message-Id: <20231118063233.733523-2-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231118063233.733523-1-vivek.kasireddy@intel.com> References: <20231118063233.733523-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: C4C2E100008 X-Stat-Signature: jaxw9o8ducr6b4axwno173nf99y8bikt X-HE-Tag: 1700290569-732944 X-HE-Meta: U2FsdGVkX18hwD5AtupikzLGQkoziTDqD4wglZhUmnmlXZihshQGUr/6IeVAkkhCVvP6Ac9vcyaB4KBoxpAmKT/H1dWGWh8EdpCklmso7X1F8Xq6rKF2GL3UwNzDXm41OIlJfsHrhkSVkCdVbN/j0Q35aWaoNN33Mai/29QSrpzlvLGHRehZ05Hv0iHbwy2p/foY4BxbihYf0D6Bv0H5B5WdVxjXAEJUom4bw8ZqjoeA4gCinL6b7QnsJ8I9YCSyqeVAwp/t9DJAZw7PZXMj70gtEqwAJUEFjeNBGfbw0qyiu5Vb9Yemp01UZojt032eMJ/mWlhEMAmygVARypaQjX6bsxkzMWnBQn9H3wgbGWsQrgv19Qk3V//N1ft1JNjwEngY8b7E7GfX/BfHYYaIpUvjtgbLqi2OtWkZXgf7ZC4rzgkJVfluzYUAEYmQ21bnPpFi1lqpX8DwvdmFnZ2kd52sLl1xsjiLbptJqGBog7sAO+MgkPoZhW4QApFZ2Mk4OXiUownX+fnQ7/nEO89Z0DhEGR5QH/B+sDUXICmHuMS3zWSvVKLeg7sNIUgjLEkFOB4xRYvYkfiM8AY8RMVrVNAN+XbS8mgVL2ivsM25TLAiiSe/wT/fGZLmIo7khNj3Is4Havyg/kpWAiERB//dG6NMBP4riJYrq7P4cZ74Pe3HgEwqiKXBNxKGzAUlrk3lIMu73Tv7C3cZ+WEFUbnNWBnpSX8cLMFoImDr2zWQCzoqEbB9lVA1b7KmfDy6qQTg06qgl1PxxYX8cByGDG2vnKPQs8P3pernk52VkXcfQg3PfQ+omED0ced1iaP1dtcP5AMWpFQX3dWvlec7/r101AYwKcrIR3nFqLQknHfmdjMNDeuthdqQx5Ma4BjtPTubcd/jPUsKZcEjqleOX7DxVJrrAUfdOKKBnV+On2ZhOhfvVfK+DxwkQGOKxiNN2nfnYj35OdkHTygs6POkNem qrF7SCMm e1iTNP82/5ATzjifd6Ob546uLlNZonBHIlZM9xY1jQSKdUQOQH0AeBam4SwpWvG+4GdCvfshsLnVC8qIQxWpKgccXevSgZPQv2roD492qTkAn+2TTyPJkB05YOWYMxXnPvnU44ajhyKlgtWN2i0szvpOvspewIUquXHRNTvMlZQh3maW/LNFVnEr+1GR2BX5dn9nH1EqogVyREuOALMl0OTOMSmB9ivWmXm4NZT8ItzNN3AbZB2KHpNhUwQTAk6pm4H/V6bYfUSniBZ4EqWR+zgHhPcqJi/wCs0ZPkMwdhBMAh84= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Add VM_PFNMAP to vm_flags in the mmap handler to ensure that the mappings would be managed without using struct page. And, in the vm_fault handler, use vmf_insert_pfn to share the page's pfn to userspace instead of directly sharing the page (via struct page *). Cc: David Hildenbrand Cc: Daniel Vetter Cc: Mike Kravetz Cc: Hugh Dickins Cc: Peter Xu Cc: Jason Gunthorpe Cc: Gerd Hoffmann Cc: Dongwon Kim Cc: Junxiao Chang Suggested-by: David Hildenbrand Acked-by: David Hildenbrand Signed-off-by: Vivek Kasireddy --- drivers/dma-buf/udmabuf.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c index c40645999648..820c993c8659 100644 --- a/drivers/dma-buf/udmabuf.c +++ b/drivers/dma-buf/udmabuf.c @@ -35,12 +35,13 @@ static vm_fault_t udmabuf_vm_fault(struct vm_fault *vmf) struct vm_area_struct *vma = vmf->vma; struct udmabuf *ubuf = vma->vm_private_data; pgoff_t pgoff = vmf->pgoff; + unsigned long pfn; if (pgoff >= ubuf->pagecount) return VM_FAULT_SIGBUS; - vmf->page = ubuf->pages[pgoff]; - get_page(vmf->page); - return 0; + + pfn = page_to_pfn(ubuf->pages[pgoff]); + return vmf_insert_pfn(vma, vmf->address, pfn); } static const struct vm_operations_struct udmabuf_vm_ops = { @@ -56,6 +57,7 @@ static int mmap_udmabuf(struct dma_buf *buf, struct vm_area_struct *vma) vma->vm_ops = &udmabuf_vm_ops; vma->vm_private_data = ubuf; + vm_flags_set(vma, VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP); return 0; } From patchwork Sat Nov 18 06:32:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13459887 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 05BE3C2BB3F for ; Sat, 18 Nov 2023 06:56:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 341FE6B053C; Sat, 18 Nov 2023 01:56:14 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 2978D6B053E; Sat, 18 Nov 2023 01:56:14 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 075646B053C; Sat, 18 Nov 2023 01:56:14 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id EB6296B0536 for ; Sat, 18 Nov 2023 01:56:13 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id C243C140ECE for ; Sat, 18 Nov 2023 06:56:13 +0000 (UTC) X-FDA: 81470165826.14.4CDD2AB Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.100]) by imf19.hostedemail.com (Postfix) with ESMTP id C99251A000C for ; Sat, 18 Nov 2023 06:56:11 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=UIa8KHUq; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf19.hostedemail.com: domain of vivek.kasireddy@intel.com designates 134.134.136.100 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700290572; a=rsa-sha256; cv=none; b=f4NjXMmNbvc/qxqcezTzcIgUCe2XBC7g/Cs8oolTpDsykQltq89v/Szq9IDKreAAhvjDwQ HWGrzC4iivBAMCTq+5Q86OOnHyM0a0HWrXQ2n8QmLNIC+lcRrKkVZ8us0B+SRgY1L0SgbC e5w6oaitWOyvL3WWNoUE4jydsXAz4FE= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=UIa8KHUq; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf19.hostedemail.com: domain of vivek.kasireddy@intel.com designates 134.134.136.100 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1700290572; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=wSX8DVYyL+2CfxA74KiR5uRA4uAXoO1SDkpTnwsvGc8=; b=e4J7z978b+/xtXcfcl9ftjAchwq87KZ9kxiJ1/QckjbjAFVSJygj/D1VbJ4ebdOzolgQW9 Z2UnnB1cp3ighnYb2nWTGMH5KcX02FIES5uclC1zRMRId3LUQDFwJQV27IEnLVbrF6C9mq wJI6+GvmQJwU51IbAbiEklRsEbe0bKs= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1700290571; x=1731826571; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=4gHoMhYSuE9PY2sLaMjLckWZ81rKPYGfJDvuxgZ60RU=; b=UIa8KHUqzXDh06Re/hfsvd9G4Y/MPTDlJthQbj+tpjduGTXCwfnt6qP6 oBYyq6kTyfQclRH801q+C5ziLnQv6/845GBnRAmhwQXu5WRCmwfSMoO9U pioS1MzZunJVnqYMPD86V0oXZkstPyL3HvwcSKFToB4fyZxh6RInhPKJo 4l8a16azBkxaenmV0qTI6wFFlOzi4RFP3BkpczfRqcWRhQi02k0Ik63VC fZwo5ggxzfK46ndt/g4CKc3PxWg+sSon1XdLCCsmZXAU8sv/38/0ZwThr DIR/CzJM5KExjccBvhF2xMg9cgreirZh1N+WCHDykelzhwYNKZbvS92Uc g==; X-IronPort-AV: E=McAfee;i="6600,9927,10897"; a="457912360" X-IronPort-AV: E=Sophos;i="6.04,207,1695711600"; d="scan'208";a="457912360" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Nov 2023 22:56:07 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10897"; a="800686578" X-IronPort-AV: E=Sophos;i="6.04,207,1695711600"; d="scan'208";a="800686578" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Nov 2023 22:56:07 -0800 From: Vivek Kasireddy To: dri-devel@lists.freedesktop.org, linux-mm@kvack.org Cc: Vivek Kasireddy , David Hildenbrand , Daniel Vetter , Mike Kravetz , Hugh Dickins , Peter Xu , Jason Gunthorpe , Gerd Hoffmann , Dongwon Kim , Junxiao Chang Subject: [PATCH v4 2/5] udmabuf: Add back support for mapping hugetlb pages (v3) Date: Fri, 17 Nov 2023 22:32:30 -0800 Message-Id: <20231118063233.733523-3-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231118063233.733523-1-vivek.kasireddy@intel.com> References: <20231118063233.733523-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: C99251A000C X-Stat-Signature: 1i9gpmu3cts3uay7a1nhxndfg7ynrjmr X-HE-Tag: 1700290571-314725 X-HE-Meta: U2FsdGVkX181sAp8q4oIn6FyrUuXVvJyPNaJiwCnl7NeWPq5L5hSDDZdeusJuaboNQ4hMcQxoWz8xhdvUgaxhUxlpjc1Ez6six+JCAgBnRjldhFqAZgDepXf4EsleOn+jreRoYxJIUbYnz+aulZLsT8Mxdgf77xr9F+hbalb12mrnLuhKsRTLAQLULsKMcg9AqluneUZ1UUnqSEENFQHLe9AovhYz/UzkJ8yUsDTDZsVVQUQpaTu22Ciip7MNcekwZmKgkU6qu8cAk3wLurgnBNwwEMrpG3usRpPUKAO8TgBFUH+LV0pv2JGFEsQF57q2aXYsGUaF8ivewPQAfvL8ZKhz/AJBWYcAe7QfCC+sXuJYRU7RUw0kmCvngWmB0n623qQQz+LD5tx2/LWFsX9qjKNrUzq1j6D9K81htkXh0N34fvwQQwO4BefJ6L8u3oZgz6d5EvjqUmX0SVRfsvUnHMebbk98H0CX+ZXLApfq4Sf77pG4PLQn1wib4xOE524V6IL3wXj/8WccCDNbWqblDgt2LLQcw9cZiTlGvJL+Y18AdQpRhhx/lkD+Z83hbdno/lPQmZrR7WMCmIiz6HLUzxi1EblSulCIr3AVD5kVKDqPGJr+Mqrrrufr2ypS4fx+9ltPAcoxJ0rebMW2uoRtAAxdiWtN1ylU7xzwYfuJisZY58QaPoiRAuxXPTId+KiQqVeXU6X1xlY8diccmptEDicUZPXhf+D0fMAPJLRP+Ag/2uSkm3CNJLYqL8XP3tIj4bHyQip+yiOdYpfNkJPj3YvxRBXnSCICMhVWV+ThE4YV5T3KBfk2ctDvtp63Es8lziXgdpgXQFR2kzVDwnVnLIW4+JsoJFLF1nRJ5WqQI5kpIb6gpK0szmaqojIUV4EmtFr/hKAl7qAxXzIr1kEiE+fvuxPBKvGfq9MJAjXveU2l8+jhcEK5B4lVF0aor+XFI2c9ozEqrzKDsob9dn Pb5E31mL +hT8bPFOOR2PBnhvFq9Q+pEA9tE+FQGBd4QDfVp9G+NQQiYlPJCHBmb7/1x6lSqdkdy0TVVZNZuJfVRc1vZeSdJnUY2t1eNBLQ4c8vc9Y1vvxWJDguaP7l5+7EZGmORtOFFOHUwzPBNPsKmELrXIcHFpQHF8BJMNP4/3JbCa0D/tTHW4/laVQeKRDIyZWW/7ST1iZ5gSWerAwl14qlp+YArr3WaRulcO7jp05+R7Swta0Hj/Ydv9C4moTjnze0EZUHTDa X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: A user or admin can configure a VMM (Qemu) Guest's memory to be backed by hugetlb pages for various reasons. However, a Guest OS would still allocate (and pin) buffers that are backed by regular 4k sized pages. In order to map these buffers and create dma-bufs for them on the Host, we first need to find the hugetlb pages where the buffer allocations are located and then determine the offsets of individual chunks (within those pages) and use this information to eventually populate a scatterlist. Testcase: default_hugepagesz=2M hugepagesz=2M hugepages=2500 options were passed to the Host kernel and Qemu was launched with these relevant options: qemu-system-x86_64 -m 4096m.... -device virtio-gpu-pci,max_outputs=1,blob=true,xres=1920,yres=1080 -display gtk,gl=on -object memory-backend-memfd,hugetlb=on,id=mem1,size=4096M -machine memory-backend=mem1 Replacing -display gtk,gl=on with -display gtk,gl=off above would exercise the mmap handler. v2: Updated get_sg_table() to manually populate the scatterlist for both huge page and non-huge-page cases. v3: s/offsets/subpgoff/g s/hpoff/mapidx/g Cc: David Hildenbrand Cc: Daniel Vetter Cc: Mike Kravetz Cc: Hugh Dickins Cc: Peter Xu Cc: Jason Gunthorpe Cc: Gerd Hoffmann Cc: Dongwon Kim Cc: Junxiao Chang Acked-by: Mike Kravetz (v2) Signed-off-by: Vivek Kasireddy --- drivers/dma-buf/udmabuf.c | 85 +++++++++++++++++++++++++++++++++------ 1 file changed, 72 insertions(+), 13 deletions(-) diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c index 820c993c8659..1a41c4a069ea 100644 --- a/drivers/dma-buf/udmabuf.c +++ b/drivers/dma-buf/udmabuf.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -28,6 +29,7 @@ struct udmabuf { struct page **pages; struct sg_table *sg; struct miscdevice *device; + pgoff_t *subpgoff; }; static vm_fault_t udmabuf_vm_fault(struct vm_fault *vmf) @@ -41,6 +43,10 @@ static vm_fault_t udmabuf_vm_fault(struct vm_fault *vmf) return VM_FAULT_SIGBUS; pfn = page_to_pfn(ubuf->pages[pgoff]); + if (ubuf->subpgoff) { + pfn += ubuf->subpgoff[pgoff] >> PAGE_SHIFT; + } + return vmf_insert_pfn(vma, vmf->address, pfn); } @@ -90,23 +96,31 @@ static struct sg_table *get_sg_table(struct device *dev, struct dma_buf *buf, { struct udmabuf *ubuf = buf->priv; struct sg_table *sg; + struct scatterlist *sgl; + pgoff_t offset; + unsigned long i = 0; int ret; sg = kzalloc(sizeof(*sg), GFP_KERNEL); if (!sg) return ERR_PTR(-ENOMEM); - ret = sg_alloc_table_from_pages(sg, ubuf->pages, ubuf->pagecount, - 0, ubuf->pagecount << PAGE_SHIFT, - GFP_KERNEL); + + ret = sg_alloc_table(sg, ubuf->pagecount, GFP_KERNEL); if (ret < 0) - goto err; + goto err_alloc; + + for_each_sg(sg->sgl, sgl, ubuf->pagecount, i) { + offset = ubuf->subpgoff ? ubuf->subpgoff[i] : 0; + sg_set_page(sgl, ubuf->pages[i], PAGE_SIZE, offset); + } ret = dma_map_sgtable(dev, sg, direction, 0); if (ret < 0) - goto err; + goto err_map; return sg; -err: +err_map: sg_free_table(sg); +err_alloc: kfree(sg); return ERR_PTR(ret); } @@ -143,6 +157,7 @@ static void release_udmabuf(struct dma_buf *buf) for (pg = 0; pg < ubuf->pagecount; pg++) put_page(ubuf->pages[pg]); + kfree(ubuf->subpgoff); kfree(ubuf->pages); kfree(ubuf); } @@ -206,7 +221,9 @@ static long udmabuf_create(struct miscdevice *device, struct udmabuf *ubuf; struct dma_buf *buf; pgoff_t pgoff, pgcnt, pgidx, pgbuf = 0, pglimit; - struct page *page; + struct page *page, *hpage = NULL; + pgoff_t mapidx, chunkoff, maxchunks; + struct hstate *hpstate; int seals, ret = -EINVAL; u32 i, flags; @@ -242,7 +259,7 @@ static long udmabuf_create(struct miscdevice *device, if (!memfd) goto err; mapping = memfd->f_mapping; - if (!shmem_mapping(mapping)) + if (!shmem_mapping(mapping) && !is_file_hugepages(memfd)) goto err; seals = memfd_fcntl(memfd, F_GET_SEALS, 0); if (seals == -EINVAL) @@ -253,16 +270,57 @@ static long udmabuf_create(struct miscdevice *device, goto err; pgoff = list[i].offset >> PAGE_SHIFT; pgcnt = list[i].size >> PAGE_SHIFT; + if (is_file_hugepages(memfd)) { + if (!ubuf->subpgoff) { + ubuf->subpgoff = kmalloc_array(ubuf->pagecount, + sizeof(*ubuf->subpgoff), + GFP_KERNEL); + if (!ubuf->subpgoff) { + ret = -ENOMEM; + goto err; + } + } + hpstate = hstate_file(memfd); + mapidx = list[i].offset >> huge_page_shift(hpstate); + chunkoff = (list[i].offset & + ~huge_page_mask(hpstate)) >> PAGE_SHIFT; + maxchunks = huge_page_size(hpstate) >> PAGE_SHIFT; + } for (pgidx = 0; pgidx < pgcnt; pgidx++) { - page = shmem_read_mapping_page(mapping, pgoff + pgidx); - if (IS_ERR(page)) { - ret = PTR_ERR(page); - goto err; + if (is_file_hugepages(memfd)) { + if (!hpage) { + hpage = find_get_page_flags(mapping, mapidx, + FGP_ACCESSED); + if (!hpage) { + ret = -EINVAL; + goto err; + } + } + get_page(hpage); + ubuf->pages[pgbuf] = hpage; + ubuf->subpgoff[pgbuf++] = chunkoff << PAGE_SHIFT; + if (++chunkoff == maxchunks) { + put_page(hpage); + hpage = NULL; + chunkoff = 0; + mapidx++; + } + } else { + mapidx = pgoff + pgidx; + page = shmem_read_mapping_page(mapping, mapidx); + if (IS_ERR(page)) { + ret = PTR_ERR(page); + goto err; + } + ubuf->pages[pgbuf++] = page; } - ubuf->pages[pgbuf++] = page; } fput(memfd); memfd = NULL; + if (hpage) { + put_page(hpage); + hpage = NULL; + } } exp_info.ops = &udmabuf_ops; @@ -287,6 +345,7 @@ static long udmabuf_create(struct miscdevice *device, put_page(ubuf->pages[--pgbuf]); if (memfd) fput(memfd); + kfree(ubuf->subpgoff); kfree(ubuf->pages); kfree(ubuf); return ret; From patchwork Sat Nov 18 06:32:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13459888 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7F70DC5AE5B for ; Sat, 18 Nov 2023 06:56:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 629406B0536; Sat, 18 Nov 2023 01:56:14 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 55C0D6B053D; Sat, 18 Nov 2023 01:56:14 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 333E36B0536; Sat, 18 Nov 2023 01:56:14 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 15CB56B0536 for ; Sat, 18 Nov 2023 01:56:14 -0500 (EST) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id DB00380E91 for ; Sat, 18 Nov 2023 06:56:13 +0000 (UTC) X-FDA: 81470165826.16.D6F881D Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.100]) by imf14.hostedemail.com (Postfix) with ESMTP id EC6F7100008 for ; Sat, 18 Nov 2023 06:56:11 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=PkPXny+S; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf14.hostedemail.com: domain of vivek.kasireddy@intel.com designates 134.134.136.100 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1700290572; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=6NDJaKPhgfcekl+XPIf2VyMKoPFilktObkPHplWG3po=; b=2L06UK/vg/o+zCvOp20VCjPnLAAA7JgA6OoLDoyP3V4QEL6X5dDS8cVvQ7xFdeLFD1tpD+ gUGyz3LJfOlT2L8i0Exa8ByOrgAaRz/7TdaCi40IrnU0NgvmDzz3zl+Njepbl1t4QEKbuU ien9ked3t1tLkvV2+8t1dZHe5+Aau9M= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=PkPXny+S; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf14.hostedemail.com: domain of vivek.kasireddy@intel.com designates 134.134.136.100 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700290572; a=rsa-sha256; cv=none; b=npaJvR3PUub0Pj6PDiH+RSBT24dlkp/yhypqVxivZcRQmCAQ12IREh+mDLh0RdLWJcn1mm r7OG6SxZi8uSt0H44XTQoB/vxLIiCygn3SbAPhx6wpqDxvTSqCA4f3XDD3jNkfFGEJyCgs rHsDztznubTj0lYj9vsgUZipMp8NdWU= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1700290572; x=1731826572; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=YY87Er6qN1jewK0RzQjInjnxM1qupP4NPHpDKf8Qf+Q=; b=PkPXny+SU0bLx6Ns5CLoQL5fnySoXrD7iJQEUkCAyh4Ng20KI6KRIqYj v7U7g3K4X+ub1LN0+7UbYI+7TB+BPI+VE4GMnt7FYD7fWJjv18LQKHF1S ElIQik7fpem+uJEXsl7DwwOmY2jDNV27IXyNP1TAoRSo+FaI9CG6dr5Q4 G6Hbg1Spue3K8EV8C8XeV3JP/FoppTtx6MiFwVvXnxyeQ94sAX4FuMOpP VK9DwesiajTRMP4KR8safUflEwlJxcrPTk0oZbeT9qaZ3F0Q6PYx54Jma ++RtS8a5XbGXIF/GW4ZCLOGEnZcnopmOm0WgGCOuRwry5By7jkxkpq27m g==; X-IronPort-AV: E=McAfee;i="6600,9927,10897"; a="457912368" X-IronPort-AV: E=Sophos;i="6.04,207,1695711600"; d="scan'208";a="457912368" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Nov 2023 22:56:08 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10897"; a="800686581" X-IronPort-AV: E=Sophos;i="6.04,207,1695711600"; d="scan'208";a="800686581" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Nov 2023 22:56:07 -0800 From: Vivek Kasireddy To: dri-devel@lists.freedesktop.org, linux-mm@kvack.org Cc: Vivek Kasireddy , David Hildenbrand , Daniel Vetter , Mike Kravetz , Hugh Dickins , Peter Xu , Gerd Hoffmann , Dongwon Kim , Junxiao Chang , Jason Gunthorpe Subject: [PATCH v4 3/5] mm/gup: Introduce pin_user_pages_fd() for pinning shmem/hugetlbfs file pages (v4) Date: Fri, 17 Nov 2023 22:32:31 -0800 Message-Id: <20231118063233.733523-4-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231118063233.733523-1-vivek.kasireddy@intel.com> References: <20231118063233.733523-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: EC6F7100008 X-Stat-Signature: arwz5xx1ysu94odgpgb5oxn5nar9cf4c X-HE-Tag: 1700290571-34030 X-HE-Meta: U2FsdGVkX1/zY1km2nHzglFoP59Vrx4CRNlkq4tKNS7KgCLnWS53hZ/thgLsZx2c8KjFn1dqeYTBOmlWR1euKVjwnaTYDaid4wdFL95EYwOBBgPliuYrDWd3GGRdOeW38QaGoRsaCIlU75H+gHjlPSDuagpLYS0JoryAlQ96OOJxRsOCXNohyfU5H3oN96nVyWG+oUHUBuhNpj0yjM9T5Uh7i694pfQer4STEdoE14subylJWxC+AjMIZOcF+v5ijf6ptNPVQrFCmC5gTzvk1iszaEcGY/qx/rI3xA7LFKvULcd8NXFxtNcZA3W2COwPhrhsNW4LW2aVR9jGOhgGzIzB6ZzDNKtKfNwgsTbxPa/wERAIXlgIK/24YaIw89WlRIvW3X9q0kC/qH3IkJFjJH/Sikd6maPq/XvxA3sZRe0MuvCu1Qj0BSL0NEylJI+zeVHZHi8r39JVGOL3beSdEFzITaAgfhnfQVGDhOXnp7OD+qcuSpDbEB0wAkKzbGzJ4fQ4IoTfzIIjI6ciuvn98m6dSni53pioT9iRp1eda2EMbdtv2yXk4nmYvqzQtLHlH7ZyACELOmqQKJPf61HIEMqPnpYgNYsL0eknTHHuLAmzsamdez9eh0GcIgZOZApH9rh7riqKs/ygucZv0Z+5SUCzQ0qCz/Q9CqdyZLY8yI6v4aLu+YgdyouUodD54YNpr5bnPW8VUyW3VYVRj8x/W+UmdUUWkAPb0Awy9xt/RZ+M2vHyWN50DxJZdkO8h8WmrfkI5YwBfrPiYTTKD0zYiJgJL/36p9rBfVD5wpy3Jl+aYvGebEeP7fhmYKJ3tDU5K3cdTjKgurLm0UjyUNwEskrcRlReeuGfJqNtgKNjWu6riziDOGVfWYD+sDUuoTyJzAWHCaGe8aPeo3wD1M+lKcfQetxQfAIUf6PNgT7APjewNNyPxiNZ4IJUkFhi6pvvfV9Bwfjt0kz5Yay5C/Q 2Ici6xav xgzoW6r7T4wcemsu0rqM0bRcTisPLDRNLhkdtBmKLr0mc8LLKbMAr6a7rXqeG49Eg2GCxCvVj0GQY+uUhXRFF2SnFC1o4PZQFv7p5dpT0MBlftsKJNiuEa3beS8d7YV3ow7PYpU1lxvXqagl3KiwMuNs7AZk2+If/OxgN7LIX8vN9Sdcoy2HLyK0WN2rLYXAOGVHaV8/6cCxSaBXFOMXZjeHlB3IQrEjPYH5ZWJ9fkpbm8hjtqWvwnyTCQ41OShmzwgy4bxtBXgxKKpLILq0HZd5FN6nilq2ac23kAz7EXxKImtQ= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: For drivers that would like to longterm-pin the pages associated with a file, the pin_user_pages_fd() API provides an option to not only pin the pages via FOLL_PIN but also to check and migrate them if they reside in movable zone or CMA block. This API currently works with files that belong to either shmem or hugetlbfs. Files belonging to other filesystems are rejected for now. The pages need to be located first before pinning them via FOLL_PIN. If they are found in the page cache, they can be immediately pinned. Otherwise, they need to be allocated using the filesystem specific APIs and then pinned. v2: - Drop gup_flags and improve comments and commit message (David) - Allocate a page if we cannot find in page cache for the hugetlbfs case as well (David) - Don't unpin pages if there is a migration related failure (David) - Drop the unnecessary nr_pages <= 0 check (Jason) - Have the caller of the API pass in file * instead of fd (Jason) v3: (David) - Enclose the huge page allocation code with #ifdef CONFIG_HUGETLB_PAGE (Build error reported by kernel test robot ) - Don't forget memalloc_pin_restore() on non-migration related errors - Improve the readability of the cleanup code associated with non-migration related errors - Augment the comments by describing FOLL_LONGTERM like behavior - Include the R-b tag from Jason v4: - Remove the local variable "page" and instead use 3 return statements in alloc_file_page() (David) - Add the R-b tag from David Cc: David Hildenbrand Cc: Daniel Vetter Cc: Mike Kravetz Cc: Hugh Dickins Cc: Peter Xu Cc: Gerd Hoffmann Cc: Dongwon Kim Cc: Junxiao Chang Suggested-by: Jason Gunthorpe Reviewed-by: Jason Gunthorpe (v2) Reviewed-by: David Hildenbrand (v3) Signed-off-by: Vivek Kasireddy --- include/linux/mm.h | 2 + mm/gup.c | 108 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+) diff --git a/include/linux/mm.h b/include/linux/mm.h index 418d26608ece..1b675fa35059 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2472,6 +2472,8 @@ long get_user_pages_unlocked(unsigned long start, unsigned long nr_pages, struct page **pages, unsigned int gup_flags); long pin_user_pages_unlocked(unsigned long start, unsigned long nr_pages, struct page **pages, unsigned int gup_flags); +long pin_user_pages_fd(struct file *file, pgoff_t start, + unsigned long nr_pages, struct page **pages); int get_user_pages_fast(unsigned long start, int nr_pages, unsigned int gup_flags, struct page **pages); diff --git a/mm/gup.c b/mm/gup.c index 231711efa390..875c51d13ee5 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -3410,3 +3410,111 @@ long pin_user_pages_unlocked(unsigned long start, unsigned long nr_pages, &locked, gup_flags); } EXPORT_SYMBOL(pin_user_pages_unlocked); + +static struct page *alloc_file_page(struct file *file, pgoff_t idx) +{ +#ifdef CONFIG_HUGETLB_PAGE + struct folio *folio; + int err; + + if (is_file_hugepages(file)) { + folio = alloc_hugetlb_folio_nodemask(hstate_file(file), + NUMA_NO_NODE, + NULL, + GFP_USER); + if (folio && folio_try_get(folio)) { + err = hugetlb_add_to_page_cache(folio, + file->f_mapping, + idx); + if (err) { + folio_put(folio); + free_huge_folio(folio); + return ERR_PTR(err); + } + return &folio->page; + } + return ERR_PTR(-ENOMEM); + } +#endif + return shmem_read_mapping_page(file->f_mapping, idx); +} + +/** + * pin_user_pages_fd() - pin user pages associated with a file + * @file: the file whose pages are to be pinned + * @start: starting file offset + * @nr_pages: number of pages from start to pin + * @pages: array that receives pointers to the pages pinned. + * Should be at-least nr_pages long. + * + * Attempt to pin pages associated with a file that belongs to either shmem + * or hugetlb. The pages are either found in the page cache or allocated if + * necessary. Once the pages are located, they are all pinned via FOLL_PIN. + * And, these pinned pages need to be released either using unpin_user_pages() + * or unpin_user_page(). + * + * It must be noted that the pages may be pinned for an indefinite amount + * of time. And, in most cases, the duration of time they may stay pinned + * would be controlled by the userspace. This behavior is effectively the + * same as using FOLL_LONGTERM with other GUP APIs. + * + * Returns number of pages pinned. This would be equal to the number of + * pages requested. If no pages were pinned, it returns -errno. + */ +long pin_user_pages_fd(struct file *file, pgoff_t start, + unsigned long nr_pages, struct page **pages) +{ + struct page *page; + unsigned int flags, i; + long ret; + + if (start < 0) + return -EINVAL; + + if (!file) + return -EINVAL; + + if (!shmem_file(file) && !is_file_hugepages(file)) + return -EINVAL; + + flags = memalloc_pin_save(); + do { + for (i = 0; i < nr_pages; i++) { + /* + * In most cases, we should be able to find the page + * in the page cache. If we cannot find it, we try to + * allocate one and add it to the page cache. + */ + page = find_get_page_flags(file->f_mapping, + start + i, + FGP_ACCESSED); + if (!page) { + page = alloc_file_page(file, start + i); + if (IS_ERR(page)) { + ret = PTR_ERR(page); + goto err; + } + } + ret = try_grab_page(page, FOLL_PIN); + if (unlikely(ret)) + goto err; + + pages[i] = page; + put_page(pages[i]); + } + + ret = check_and_migrate_movable_pages(nr_pages, pages); + } while (ret == -EAGAIN); + + memalloc_pin_restore(flags); + return ret ? ret : nr_pages; +err: + memalloc_pin_restore(flags); + while (i-- > 0) + if (pages[i]) + unpin_user_page(pages[i]); + + return ret; +} +EXPORT_SYMBOL_GPL(pin_user_pages_fd); + From patchwork Sat Nov 18 06:32:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13459889 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id BAF0EC54E76 for ; Sat, 18 Nov 2023 06:56:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DE4006B053D; Sat, 18 Nov 2023 01:56:15 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D953E6B053E; Sat, 18 Nov 2023 01:56:15 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C34706B0540; Sat, 18 Nov 2023 01:56:15 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id ACA356B053D for ; Sat, 18 Nov 2023 01:56:15 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 73C16C0E84 for ; Sat, 18 Nov 2023 06:56:15 +0000 (UTC) X-FDA: 81470165910.15.4DA6889 Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.100]) by imf25.hostedemail.com (Postfix) with ESMTP id 6D8B2A0011 for ; Sat, 18 Nov 2023 06:56:12 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=T3gG15QG; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf25.hostedemail.com: domain of vivek.kasireddy@intel.com designates 134.134.136.100 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1700290572; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=ZW/8DcyIeh+0dLw6Bn8PpTwNfeCkDsY2FY8vV5XnrXw=; b=HmDEajHMay+/YEgAlH3orQDg15SB6f2C0Ra7Rv0NXz56mKLmfQw9n3zLzigDZPzhdLIwNo iRsrylqFAsp7UD35+SBVlGm2KoofoNybP8w2BE9vkpGs4SGKkWcZJnu3hQwiEb3300oJJI VF6zcK5jWfZpkbyx9iTNeFW5Qqima2A= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=T3gG15QG; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf25.hostedemail.com: domain of vivek.kasireddy@intel.com designates 134.134.136.100 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700290572; a=rsa-sha256; cv=none; b=5GQ3MhBNbMIeaFAev0xZ6ubdz/NYYFjGghAMUj7cD3in1nk6LjdjJfbX9wgVHsV694jgse 6wHviouyKGUhPZfhMMdGEUBssd0j0wsB4V5CLti9DI7b4gFQCRgouAQqu8ODtB0ky2uALT YlR70ZPf+BkH2xn6m+8V1+7ClHolm7Q= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1700290572; x=1731826572; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Ue+VeqRqoLXQmcxX5yfx2PkL0r79LSA81sdXut6SbQU=; b=T3gG15QGTvLtW40FCcnoEkzNoOKRfLxLm0cQVqNVwl+uCIkijfYAi8ll q4cNFeXhE3s1whhJZsTIDEamkT4Cp7dyEXxogOSEhDrfekPdONvxPZjFg oN4Rzzdy94++k55/eP4nM9FWFJARrM63QYcnnxDPVG5xs1ChJKnxpM+2g qETzpwOCoYcAOZFWGSPx/KoYVlP5TTaAkxFHhIZl9dBKaIMxe5sVMnSCO C8qcSbNf/af+IFsXDZtcq1ttsybb9VOevARGfDEnbwvuQtXJjHNtiJrva QTeTnZJ58QbB52th3KvIMAk9lk1/Bhc6TC4SXlStFq6iafEITDEFppAfN A==; X-IronPort-AV: E=McAfee;i="6600,9927,10897"; a="457912377" X-IronPort-AV: E=Sophos;i="6.04,207,1695711600"; d="scan'208";a="457912377" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Nov 2023 22:56:08 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10897"; a="800686584" X-IronPort-AV: E=Sophos;i="6.04,207,1695711600"; d="scan'208";a="800686584" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Nov 2023 22:56:08 -0800 From: Vivek Kasireddy To: dri-devel@lists.freedesktop.org, linux-mm@kvack.org Cc: Vivek Kasireddy , David Hildenbrand , Daniel Vetter , Mike Kravetz , Hugh Dickins , Peter Xu , Jason Gunthorpe , Gerd Hoffmann , Dongwon Kim , Junxiao Chang Subject: [PATCH v4 4/5] udmabuf: Pin the pages using pin_user_pages_fd() API (v3) Date: Fri, 17 Nov 2023 22:32:32 -0800 Message-Id: <20231118063233.733523-5-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231118063233.733523-1-vivek.kasireddy@intel.com> References: <20231118063233.733523-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 6D8B2A0011 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: oxk9w4kjhoiyckzbcien8diqpa6xqhp9 X-HE-Tag: 1700290572-704204 X-HE-Meta: U2FsdGVkX1/zRp9TNnYyKFlI/VoXUlB1yAzscMUkyS1WtQGS3sQ7g1eqP6wwMilEt/Up7bg/ghDcHRmXwC0O9gQV7VUk0DbRw8HvX2+PNpTLm4UIuVO697eXXakg2DfJOBT5a3a2GkU9OkL2SVZtebDe6RvFcFFvwy4iS31KgE5A/20s2+jrlSkRwGZABGmsSf+WKAiIotu4DJU8SBmTq19yf0asMGBmUGwcbrApRbzFbpeae3+lXqvinEdsonYwNHeJlBzdMRKIgZ3hsGyRqKtVYG//EaQVZf2KGv7w3FgUDiZZmfJuQTKzEe8/m6ZHGvHFBYWpqx/oHaC9ogjrc8r1kTfbqf+7DUcWUQBWQOTDGGLo3FnJvvEOwzS82x6DZnYmFB72YQRlpRNy/17WFBDbrVkhgbF62LOVzPZ2r+1T4P5i7pnjr+vc9GjNG84gMCpYjb7dsTQPPI55Ghb+HO63qgonoeTLdYfoC8fmA59f7/TxJrpuNA7WZR8EHooGz9wjXtj24tZJwUIuYVVF74F7H1i4khKtYsgFiCSDifHXQGJ2L0Q8D9C/WGfqxm3t5eHvqif5X/leTDNW9WWSVM9JZ+borc8Rdczo9aEBKb9x0KBzmsmdrpez8Tr+bP4p28KtQEuiDochn/h/qvQ0RVJCpAyjHVZHqoGk77QSn7l0vxPKMorQ5g+kkjfx6D0HmwWAyAKist7fpBYsWaaVMp8FdOwZAlvtxy3INCPp6RTrIivd+B6ytXG7BAX4g9CctNKItCH7bM4vipru1TwDdDS5x09y2xP3Ngg5L/HUd7tKGipV4byI6MpbQo8J5wnpqJJeOTSlwTooE+zHFyt6SqQ+5DGWdpPLQoEWvRXF/drWwK6OMMclhSk3R6Fw74FEj9swPh5HUx45zLX/IGn6O5E8M2oqoS/QBS435UOOfCl3pKIcCgdS1pDVQlQfcbzrUuIoV0yQSNYImCQCKrT VyleQgl1 PLhM5824aCLMpuOdqwbGSj8eIbdFd2k4U8Eg2oAv1qcVpQf11mFBy5PfE2dkiPZIBJJ16JybmP8U3npoU6aWjxSFcrkek9JezORo8qkKLwD9jbEcROQLR7Q94QIEA3FaHbo2c5cwBp/wQ9gzEdQoBRcMXcLQ7jzG5fuj7R5IuiPYz+uSzc4CNjaYY3pAFDjKgqGk36J3h1cXIytY76JBQX1Xf7G5vCMtXc8WRcnB2ebxaFH2CEMkgjX8508mh/GyV4PmSQOlfVv8PPuh6GvIcxG5YI4S/eaTtbA6Y6NZJBhGlAs0= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Using pin_user_pages_fd() will ensure that the pages are pinned correctly using FOLL_PIN. And, this also ensures that we don't accidentally break features such as memory hotunplug as it would not allow pinning pages in the movable zone. Using this new API also simplifies the code as we no longer have to deal with extracting individual pages from their mappings. As a result, we can drop some of the local variables such as page, hpage, mapping, etc. v2: - Adjust to the change in signature of pin_user_pages_fd() by passing in file * instead of fd. v3: - Limit the changes in this patch only to those that are required for using pin_user_pages_fd() - Slightly improve the commit message Cc: David Hildenbrand Cc: Daniel Vetter Cc: Mike Kravetz Cc: Hugh Dickins Cc: Peter Xu Cc: Jason Gunthorpe Cc: Gerd Hoffmann Cc: Dongwon Kim Cc: Junxiao Chang Signed-off-by: Vivek Kasireddy --- drivers/dma-buf/udmabuf.c | 59 +++++++++++++++------------------------ 1 file changed, 22 insertions(+), 37 deletions(-) diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c index 1a41c4a069ea..883bd97e4076 100644 --- a/drivers/dma-buf/udmabuf.c +++ b/drivers/dma-buf/udmabuf.c @@ -156,7 +156,8 @@ static void release_udmabuf(struct dma_buf *buf) put_sg_table(dev, ubuf->sg, DMA_BIDIRECTIONAL); for (pg = 0; pg < ubuf->pagecount; pg++) - put_page(ubuf->pages[pg]); + unpin_user_page(ubuf->pages[pg]); + kfree(ubuf->subpgoff); kfree(ubuf->pages); kfree(ubuf); @@ -217,14 +218,13 @@ static long udmabuf_create(struct miscdevice *device, { DEFINE_DMA_BUF_EXPORT_INFO(exp_info); struct file *memfd = NULL; - struct address_space *mapping = NULL; struct udmabuf *ubuf; struct dma_buf *buf; - pgoff_t pgoff, pgcnt, pgidx, pgbuf = 0, pglimit; - struct page *page, *hpage = NULL; + pgoff_t pgcnt, pgbuf = 0, pglimit, nr_pages; pgoff_t mapidx, chunkoff, maxchunks; struct hstate *hpstate; - int seals, ret = -EINVAL; + long ret = -EINVAL; + int seals; u32 i, flags; ubuf = kzalloc(sizeof(*ubuf), GFP_KERNEL); @@ -258,8 +258,7 @@ static long udmabuf_create(struct miscdevice *device, memfd = fget(list[i].memfd); if (!memfd) goto err; - mapping = memfd->f_mapping; - if (!shmem_mapping(mapping) && !is_file_hugepages(memfd)) + if (!shmem_file(memfd) && !is_file_hugepages(memfd)) goto err; seals = memfd_fcntl(memfd, F_GET_SEALS, 0); if (seals == -EINVAL) @@ -268,7 +267,7 @@ static long udmabuf_create(struct miscdevice *device, if ((seals & SEALS_WANTED) != SEALS_WANTED || (seals & SEALS_DENIED) != 0) goto err; - pgoff = list[i].offset >> PAGE_SHIFT; + mapidx = list[i].offset >> PAGE_SHIFT; pgcnt = list[i].size >> PAGE_SHIFT; if (is_file_hugepages(memfd)) { if (!ubuf->subpgoff) { @@ -286,41 +285,26 @@ static long udmabuf_create(struct miscdevice *device, ~huge_page_mask(hpstate)) >> PAGE_SHIFT; maxchunks = huge_page_size(hpstate) >> PAGE_SHIFT; } - for (pgidx = 0; pgidx < pgcnt; pgidx++) { + + do { + nr_pages = shmem_file(memfd) ? pgcnt : 1; + ret = pin_user_pages_fd(memfd, mapidx, nr_pages, + ubuf->pages + pgbuf); + if (ret < 0) + goto err; + if (is_file_hugepages(memfd)) { - if (!hpage) { - hpage = find_get_page_flags(mapping, mapidx, - FGP_ACCESSED); - if (!hpage) { - ret = -EINVAL; - goto err; - } - } - get_page(hpage); - ubuf->pages[pgbuf] = hpage; - ubuf->subpgoff[pgbuf++] = chunkoff << PAGE_SHIFT; + ubuf->subpgoff[pgbuf] = chunkoff << PAGE_SHIFT; if (++chunkoff == maxchunks) { - put_page(hpage); - hpage = NULL; chunkoff = 0; mapidx++; } - } else { - mapidx = pgoff + pgidx; - page = shmem_read_mapping_page(mapping, mapidx); - if (IS_ERR(page)) { - ret = PTR_ERR(page); - goto err; - } - ubuf->pages[pgbuf++] = page; } - } + pgbuf += nr_pages; + pgcnt -= nr_pages; + } while (pgcnt > 0); fput(memfd); memfd = NULL; - if (hpage) { - put_page(hpage); - hpage = NULL; - } } exp_info.ops = &udmabuf_ops; @@ -341,8 +325,9 @@ static long udmabuf_create(struct miscdevice *device, return dma_buf_fd(buf, flags); err: - while (pgbuf > 0) - put_page(ubuf->pages[--pgbuf]); + while (pgbuf-- > 0) + if (ubuf->pages[pgbuf]) + unpin_user_page(ubuf->pages[pgbuf]); if (memfd) fput(memfd); kfree(ubuf->subpgoff); From patchwork Sat Nov 18 06:32:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13459890 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8E8C9C54FB9 for ; Sat, 18 Nov 2023 06:56:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 61CDA6B053E; Sat, 18 Nov 2023 01:56:16 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5A3F26B0541; Sat, 18 Nov 2023 01:56:16 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4516E6B0542; Sat, 18 Nov 2023 01:56:16 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 26CF56B053E for ; Sat, 18 Nov 2023 01:56:16 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 01B8E80E80 for ; Sat, 18 Nov 2023 06:56:16 +0000 (UTC) X-FDA: 81470165952.13.2DE7BDC Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.100]) by imf19.hostedemail.com (Postfix) with ESMTP id 018AC1A000C for ; Sat, 18 Nov 2023 06:56:13 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=aPQcbzo8; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf19.hostedemail.com: domain of vivek.kasireddy@intel.com designates 134.134.136.100 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700290574; a=rsa-sha256; cv=none; b=fhzJAtpuZlm6an+VNleaVr5wmDtZF/VeuYKvvP2QU176xQJe48MLiOcDPRRgQwgchAfHTs FoDHOdW54CwAKHSj6DmDGn+Ttkpk9R3qxaFrHV0OzwMPoGBHLDk6Rxun5tfIfbBrVwUQe6 vUwY3/lryT9vH4DgrexD+Uo+cXqxjZk= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=aPQcbzo8; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf19.hostedemail.com: domain of vivek.kasireddy@intel.com designates 134.134.136.100 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1700290574; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=h3emmYlObQz4YBWvxea89j9SxvY/uMNOIgA3BV8wvNU=; b=igTmXvJtsI3F9NgPjx77PJt38nNKVQ2QS9dKlfS3ESuDN5qBa9c/dwHQztEiQaWS1ix0xW BPq9dUZFurgUQ0yNA3C8pV1RFEfkiJd3aP7XgwnigwJ3IE2iAKk+go+wRu7MKLqi3GWq7L 4H+Pb1bs/IxWiG6mMrjsZQPJzfepwag= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1700290574; x=1731826574; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=r39C9fURv/tYjjTgfRvVfOiacLkGBXJF65fG2x4l+b0=; b=aPQcbzo8qAKEBMuZ1saO6yXisJ2FO4oos5XBiS6IhPfSGiZHSuWwp8Bo srVRYTlUjBxJVcFYndfcFCcbzS+D0PoUhMWUGz1weUjh+ZnWPURU3nAK/ aPRAv4yjfM/zzGLr4zCk0mAaVuzs/YushmNEEoY+9uQgGz97ikRH9tDnI nS5jgE1OZNL33X+8rC1tBjb9CWz+khHKPjM9blMW7YVuOtfFABvaUyMqS yxg6SZPNOb6VaNeg3HfActLdKhRNPMbr9XtTOyLUjVZD6ACc76xsm5738 fmvEU4Ni2cRJw1A4mWAnlA9W7g+MK3kRAxxGMrYN3yNAE115fdAtPOTT7 g==; X-IronPort-AV: E=McAfee;i="6600,9927,10897"; a="457912385" X-IronPort-AV: E=Sophos;i="6.04,207,1695711600"; d="scan'208";a="457912385" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Nov 2023 22:56:09 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10897"; a="800686587" X-IronPort-AV: E=Sophos;i="6.04,207,1695711600"; d="scan'208";a="800686587" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Nov 2023 22:56:08 -0800 From: Vivek Kasireddy To: dri-devel@lists.freedesktop.org, linux-mm@kvack.org Cc: Vivek Kasireddy , Shuah Khan , David Hildenbrand , Daniel Vetter , Mike Kravetz , Hugh Dickins , Peter Xu , Jason Gunthorpe , Gerd Hoffmann , Dongwon Kim , Junxiao Chang Subject: [PATCH v4 5/5] selftests/dma-buf/udmabuf: Add tests to verify data after page migration Date: Fri, 17 Nov 2023 22:32:33 -0800 Message-Id: <20231118063233.733523-6-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231118063233.733523-1-vivek.kasireddy@intel.com> References: <20231118063233.733523-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 018AC1A000C X-Stat-Signature: zayuqkchf9ote5p89mzf5akti4i77kgt X-HE-Tag: 1700290573-671090 X-HE-Meta: U2FsdGVkX1/NLmzfYACP/RDLhMhAO4BckiJeb8eDtaNSdIZvLejzZcCXFefjjErFtgw055i1DaGQtHBLfB8ikcwXbZx8DThIcuteqPxaveYlt3tWDRaLUOhpDxJqddx6E9pbTn/aa6p6GVeM5+1Wfph5m9ic2FNPpruecgtJseHczGewIO5hAK11DBTCSiNbOctKoZWGjZ0ZAhD+sgVQpppdPkd1PHalFytE8+uQFaohZTtFQuHH4EJVP4v/hbCS5fCdAr9oHcHPylaO7Wc5rplVKJYdn8CRB4+9ogqMpwhIAyprjvOW09drdRpEkOgHhLO2mPWLRhwwY1e5CHy/c1Z96pUN7HyWbY26dKAPYnj9pR3PnxdfnXJpIDygJ9cSNIO1Zi9UWvE943j3C6OTkjvuxZqJ1lb3ULy3b8BxcEL73otR+RGNCRhm2ot9tWhSyoBO/cM3QMm7lhANPxJ/HXc6Lh0uMzEzWQD+MoXczY2KI/xFpvTXefuIO9lfqjWRO8DAuwY+y3fmc47WRKyr5ORRrkou4lXoVgEfOQjstTUeBhX7riahCrHcIXrmi3W2HCAsYN1R/Zaf8ETTKf0QeCjebhE29vJ2UKJkZQg4fCv6wPv4fMNwYlfB/zsKvfFzz/vOltUNUmMT0ZUBfcwIyTF0kWzeBVPqvtUE+kBRJHxk0fFnZhjYPnSr2TCtVxkablWOcJ/iV2S289LseBIcAu9pCcdAXoW+lFobVqCTWGGHjBvkgk0nDcr0aACBC4c3o9eZeu8bzZbZ9/AB8kcC7U35OrOiJmHCPZ7sYMzXSwDyZMDxTh4pscvUa67vfrBlffvWqHx9XZ6ijntG7lOLLc8TmHmy6MwZanWqotsgrthSh3YJECf4m8kB57Q2A6Codxnix94mAxY1Uks7aHE+xHfh1KUC4muYPvzZ7JuIg6rKUMtSOlH7SygbvytYBB9I76Ra5KT2AM/bblcoqvb A+jVn758 d2QDI6W30FP50i4D4Z+ThT5Nn+OYkb8/fe0UlV913zE+GgFDZP4/SW7iGK+8vcPT6TiHhzBUdmDv/VlBrMyYum3dMhNWuOM/D3r25RJbRUm3UN6hdUxuyM0IWiGO2V4XbGhHz/wmuwTO/ZFq8wwESSvMVRVJFdD/sQeg1ayFGbSzC78La7uf1VwruJnS3vlFF6pstdT13Wdnu86kxEyYiyHrpTN1ZXWycg152vQdHTOKNjVSf6js6FaKspgx6D76mGKy0K22sgmjnSUViIlaXgypATt1TP+vmYKXkZFo7tmRjzBHreoH70tNa+YpCmeaPUpMC X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Since the memfd pages associated with a udmabuf may be migrated as part of udmabuf create, we need to verify the data coherency after successful migration. The new tests added in this patch try to do just that using 4k sized pages and also 2 MB sized huge pages for the memfd. Successful completion of the tests would mean that there is no disconnect between the memfd pages and the ones associated with a udmabuf. And, these tests can also be augmented in the future to test newer udmabuf features (such as handling memfd hole punch). Cc: Shuah Khan Cc: David Hildenbrand Cc: Daniel Vetter Cc: Mike Kravetz Cc: Hugh Dickins Cc: Peter Xu Cc: Jason Gunthorpe Cc: Gerd Hoffmann Cc: Dongwon Kim Cc: Junxiao Chang Based-on-patch-by: Mike Kravetz Signed-off-by: Vivek Kasireddy --- .../selftests/drivers/dma-buf/udmabuf.c | 151 +++++++++++++++++- 1 file changed, 147 insertions(+), 4 deletions(-) diff --git a/tools/testing/selftests/drivers/dma-buf/udmabuf.c b/tools/testing/selftests/drivers/dma-buf/udmabuf.c index c812080e304e..d76c813fe652 100644 --- a/tools/testing/selftests/drivers/dma-buf/udmabuf.c +++ b/tools/testing/selftests/drivers/dma-buf/udmabuf.c @@ -9,26 +9,132 @@ #include #include #include +#include #include #include +#include #include #include #define TEST_PREFIX "drivers/dma-buf/udmabuf" #define NUM_PAGES 4 +#define NUM_ENTRIES 4 +#define MEMFD_SIZE 1024 /* in pages */ -static int memfd_create(const char *name, unsigned int flags) +static unsigned int page_size; + +static int create_memfd_with_seals(off64_t size, bool hpage) +{ + int memfd, ret; + unsigned int flags = MFD_ALLOW_SEALING; + + if (hpage) + flags |= MFD_HUGETLB; + + memfd = memfd_create("udmabuf-test", flags); + if (memfd < 0) { + printf("%s: [skip,no-memfd]\n", TEST_PREFIX); + exit(77); + } + + ret = fcntl(memfd, F_ADD_SEALS, F_SEAL_SHRINK); + if (ret < 0) { + printf("%s: [skip,fcntl-add-seals]\n", TEST_PREFIX); + exit(77); + } + + ret = ftruncate(memfd, size); + if (ret == -1) { + printf("%s: [FAIL,memfd-truncate]\n", TEST_PREFIX); + exit(1); + } + + return memfd; +} + +static int create_udmabuf_list(int devfd, int memfd, off64_t memfd_size) +{ + struct udmabuf_create_list *list; + int ubuf_fd, i; + + list = malloc(sizeof(struct udmabuf_create_list) + + sizeof(struct udmabuf_create_item) * NUM_ENTRIES); + if (!list) { + printf("%s: [FAIL, udmabuf-malloc]\n", TEST_PREFIX); + exit(1); + } + + for (i = 0; i < NUM_ENTRIES; i++) { + list->list[i].memfd = memfd; + list->list[i].offset = i * (memfd_size / NUM_ENTRIES); + list->list[i].size = getpagesize() * NUM_PAGES; + } + + list->count = NUM_ENTRIES; + list->flags = UDMABUF_FLAGS_CLOEXEC; + ubuf_fd = ioctl(devfd, UDMABUF_CREATE_LIST, list); + free(list); + if (ubuf_fd < 0) { + printf("%s: [FAIL, udmabuf-create]\n", TEST_PREFIX); + exit(1); + } + + return ubuf_fd; +} + +static void write_to_memfd(void *addr, off64_t size, char chr) +{ + int i; + + for (i = 0; i < size / page_size; i++) { + *((char *)addr + (i * page_size)) = chr; + } +} + +static void *mmap_fd(int fd, off64_t size) { - return syscall(__NR_memfd_create, name, flags); + void *addr; + + addr = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); + if (addr == MAP_FAILED) { + printf("%s: ubuf_fd mmap fail\n", TEST_PREFIX); + exit(1); + } + + return addr; +} + +static int compare_chunks(void *addr1, void *addr2, off64_t memfd_size) +{ + off64_t off; + int i = 0, j, k = 0, ret = 0; + char char1, char2; + + while (i < NUM_ENTRIES) { + off = i * (memfd_size / NUM_ENTRIES); + for (j = 0; j < NUM_PAGES; j++, k++) { + char1 = *((char *)addr1 + off + (j * getpagesize())); + char2 = *((char *)addr2 + (k * getpagesize())); + if (char1 != char2) { + ret = -1; + goto err; + } + } + i++; + } +err: + munmap(addr1, memfd_size); + munmap(addr2, NUM_ENTRIES * NUM_PAGES * getpagesize()); + return ret; } int main(int argc, char *argv[]) { struct udmabuf_create create; int devfd, memfd, buf, ret; - off_t size; - void *mem; + off64_t size; + void *addr1, *addr2; devfd = open("/dev/udmabuf", O_RDWR); if (devfd < 0) { @@ -90,6 +196,9 @@ int main(int argc, char *argv[]) } /* should work */ + page_size = getpagesize(); + addr1 = mmap_fd(memfd, size); + write_to_memfd(addr1, size, 'a'); create.memfd = memfd; create.offset = 0; create.size = size; @@ -98,6 +207,40 @@ int main(int argc, char *argv[]) printf("%s: [FAIL,test-4]\n", TEST_PREFIX); exit(1); } + munmap(addr1, size); + close(buf); + close(memfd); + + /* should work (migration of 4k size pages)*/ + size = MEMFD_SIZE * page_size; + memfd = create_memfd_with_seals(size, false); + addr1 = mmap_fd(memfd, size); + write_to_memfd(addr1, size, 'a'); + buf = create_udmabuf_list(devfd, memfd, size); + addr2 = mmap_fd(buf, NUM_PAGES * NUM_ENTRIES * getpagesize()); + write_to_memfd(addr1, size, 'b'); + ret = compare_chunks(addr1, addr2, size); + if (ret < 0) { + printf("%s: [FAIL,test-5]\n", TEST_PREFIX); + exit(1); + } + close(buf); + close(memfd); + + /* should work (migration of 2MB size huge pages)*/ + page_size = getpagesize() * 512; /* 2 MB */ + size = MEMFD_SIZE * page_size; + memfd = create_memfd_with_seals(size, true); + addr1 = mmap_fd(memfd, size); + write_to_memfd(addr1, size, 'a'); + buf = create_udmabuf_list(devfd, memfd, size); + addr2 = mmap_fd(buf, NUM_PAGES * NUM_ENTRIES * getpagesize()); + write_to_memfd(addr1, size, 'b'); + ret = compare_chunks(addr1, addr2, size); + if (ret < 0) { + printf("%s: [FAIL,test-6]\n", TEST_PREFIX); + exit(1); + } fprintf(stderr, "%s: ok\n", TEST_PREFIX); close(buf);