From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.5 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 56840C4727F for ; Wed, 7 Oct 2020 17:27:52 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 9FB682173E for ; Wed, 7 Oct 2020 17:27:51 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=ziepe.ca header.i=@ziepe.ca header.b="mSF3jlLK" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 9FB682173E Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=ziepe.ca Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id AAC1F6B006E; Wed, 7 Oct 2020 13:27:50 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A5D976B0070; Wed, 7 Oct 2020 13:27:50 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 924306B0071; Wed, 7 Oct 2020 13:27:50 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0077.hostedemail.com [216.40.44.77]) by kanga.kvack.org (Postfix) with ESMTP id 640566B006E for ; Wed, 7 Oct 2020 13:27:50 -0400 (EDT) Received: from smtpin17.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id E253D180AD804 for ; Wed, 7 Oct 2020 17:27:49 +0000 (UTC) X-FDA: 77345811858.17.unit24_11099d8271d1 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin17.hostedemail.com (Postfix) with ESMTP id C3504180D0180 for ; Wed, 7 Oct 2020 17:27:49 +0000 (UTC) X-HE-Tag: unit24_11099d8271d1 X-Filterd-Recvd-Size: 7243 Received: from mail-qt1-f195.google.com (mail-qt1-f195.google.com [209.85.160.195]) by imf09.hostedemail.com (Postfix) with ESMTP for ; Wed, 7 Oct 2020 17:27:49 +0000 (UTC) Received: by mail-qt1-f195.google.com with SMTP id t9so1778691qtp.9 for ; Wed, 07 Oct 2020 10:27:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ziepe.ca; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:content-transfer-encoding:in-reply-to; bh=XbSoU+/at8nS4vwGrWdy12hS6b+RYvRwUOTYRSNB06Q=; b=mSF3jlLKHl2GcWOtjisnFuOxmsafdLuRV7HNQszKRHTbX7yrWh+1uuimlnTVBDf88S 7CDIDMQGVG9iRv64l0dGVTOGsF3HhoVCmCeCFmDoV09KS3pUl6DC8vC3fTvvt2cweotS YfhKySivQyKZUy5Qq80V3s1ZqG50WhxQuoVnwHYZe7OK1Xk2CZbcO9rDgpzCxYLE695p iM3zaabJqTXzIZDyKFOBFSJJu8gE182ca6m7suRvcG0u6/EkdQ4bhJV4U+DEo5V7zW7u S96CIynxOtOJjWv1pdwcNaqEUxAlUPVqbissjAJxdOugEaGgQF1dKaRPG7Vtfa/bgluo JjMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:content-transfer-encoding :in-reply-to; bh=XbSoU+/at8nS4vwGrWdy12hS6b+RYvRwUOTYRSNB06Q=; b=RQVCAu1PTwGxnrVz3PJjB4DalkvIglzqPowbSU9pjZyLSYiAMbwHQhPel17Jr3Et6A YPvpEdhjnu5w9PrPJLBmkIJSMXVQ3G6/XLBOAZhc3Ar0ia0Wpm1VUojbRLL7wJylFKNn 9T2KLcgu9wnCjuJ9vzATqnc1cw25Q7VzS0xDmjOB8fXgshHUGEIG4eDBXzIuXcqOQwoc mcVcun8T8s5a498qWJw5JcSpIDhD6yRlt2Vhho8Kq6fsIF8Vmu+2KNAC1W0mUbCGaLTq 6oJKORg1rXYDblXgMkzgyE3PQgtBepG67S0cpyWW0R7VyvTOQAu/1CxgV2fE7knaeFn/ S/Kg== X-Gm-Message-State: AOAM5313apDZBMNObRUPFZ41K3LjXjQl+vlRg8jwPA6LIL4C0uaoM99h 42askmi0jjOTvaaDdwpYCaNclA== X-Google-Smtp-Source: ABdhPJzBzIEuFCTfuv3ULvENiYmE1q5S6ZKvaoPSvLERT9h4HN4R+RXmtJoeCi0hQLY2ayjG0AcA6w== X-Received: by 2002:ac8:37ef:: with SMTP id e44mr4247866qtc.342.1602091668463; Wed, 07 Oct 2020 10:27:48 -0700 (PDT) Received: from ziepe.ca (hlfxns017vw-156-34-48-30.dhcp-dynamic.fibreop.ns.bellaliant.net. [156.34.48.30]) by smtp.gmail.com with ESMTPSA id t10sm1904322qkt.55.2020.10.07.10.27.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Oct 2020 10:27:47 -0700 (PDT) Received: from jgg by mlx with local (Exim 4.94) (envelope-from ) id 1kQDEA-0010sP-Uz; Wed, 07 Oct 2020 14:27:46 -0300 Date: Wed, 7 Oct 2020 14:27:46 -0300 From: Jason Gunthorpe To: Daniel Vetter Cc: DRI Development , LKML , kvm@vger.kernel.org, linux-mm@kvack.org, linux-arm-kernel@lists.infradead.org, linux-samsung-soc@vger.kernel.org, linux-media@vger.kernel.org, linux-s390@vger.kernel.org, Dan Williams , Kees Cook , Rik van Riel , Benjamin Herrensmidt , Dave Airlie , Hugh Dickins , Andrew Morton , John Hubbard , =?utf-8?B?SsOpcsO0bWU=?= Glisse , Jan Kara , Daniel Vetter Subject: Re: [PATCH 07/13] mm: close race in generic_access_phys Message-ID: <20201007172746.GU5177@ziepe.ca> References: <20201007164426.1812530-1-daniel.vetter@ffwll.ch> <20201007164426.1812530-8-daniel.vetter@ffwll.ch> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20201007164426.1812530-8-daniel.vetter@ffwll.ch> Content-Transfer-Encoding: quoted-printable 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: On Wed, Oct 07, 2020 at 06:44:20PM +0200, Daniel Vetter wrote: > Way back it was a reasonable assumptions that iomem mappings never > change the pfn range they point at. But this has changed: >=20 > - gpu drivers dynamically manage their memory nowadays, invalidating > ptes with unmap_mapping_range when buffers get moved >=20 > - contiguous dma allocations have moved from dedicated carvetouts to > cma regions. This means if we miss the unmap the pfn might contain > pagecache or anon memory (well anything allocated with GFP_MOVEABLE) >=20 > - even /dev/mem now invalidates mappings when the kernel requests that > iomem region when CONFIG_IO_STRICT_DEVMEM is set, see 3234ac664a87 > ("/dev/mem: Revoke mappings when a driver claims the region") >=20 > Accessing pfns obtained from ptes without holding all the locks is > therefore no longer a good idea. Fix this. >=20 > Since ioremap might need to manipulate pagetables too we need to drop > the pt lock and have a retry loop if we raced. >=20 > While at it, also add kerneldoc and improve the comment for the > vma_ops->access function. It's for accessing, not for moving the > memory from iomem to system memory, as the old comment seemed to > suggest. >=20 > References: 28b2ee20c7cb ("access_process_vm device memory infrastructu= re") > Cc: Jason Gunthorpe > Cc: Dan Williams > Cc: Kees Cook > Cc: Rik van Riel > Cc: Benjamin Herrensmidt > Cc: Dave Airlie > Cc: Hugh Dickins > Cc: Andrew Morton > Cc: John Hubbard > Cc: J=C3=A9r=C3=B4me Glisse > Cc: Jan Kara > Cc: Dan Williams > Cc: linux-mm@kvack.org > Cc: linux-arm-kernel@lists.infradead.org > Cc: linux-samsung-soc@vger.kernel.org > Cc: linux-media@vger.kernel.org > Signed-off-by: Daniel Vetter > --- > include/linux/mm.h | 3 ++- > mm/memory.c | 44 ++++++++++++++++++++++++++++++++++++++++++-- > 2 files changed, 44 insertions(+), 3 deletions(-) This does seem to solve the race with revoke_devmem(), but it is really u= gly. It would be much nicer to wrap a rwsem around this access and the unmap. Any place using it has a nice linear translation from vm_off to pfn, so I don't think there is a such a good reason to use follow_pte in the first place. ie why not the helper be this: int generic_access_phys(unsigned long pfn, unsigned long pgprot, void *buf, size_t len, bool write) Then something like dev/mem would compute pfn and obtain the lock: dev_access(struct vm_area_struct *vma, unsigned long addr, void *buf, int= len, int write) { cpu_addr =3D vma->vm_pgoff*PAGE_SIZE + (addr - vma->vm_start)); /* FIXME: Has to be over each page of len */ if (!devmem_is_allowed_access(PHYS_PFN(cpu_addr/4096))) return -EPERM; down_read(&mem_sem); generic_access_phys(cpu_addr/4096, pgprot_val(vma->vm_page_prot), buf, len, write); up_read(&mem_sem); } The other cases looked simpler because they don't revoke, here the mmap_sem alone should be enough protection, they would just need to provide the linear translation to pfn. What do you think? Jason