Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp295606pxb; Wed, 20 Jan 2021 07:15:40 -0800 (PST) X-Google-Smtp-Source: ABdhPJwBhqEJzS1k30Cuft/pX/kBI75LAJBIPsXG3CX5vvvGMygKOq/M64oLsrffAnYeARN13Qal X-Received: by 2002:a17:906:11d6:: with SMTP id o22mr6590746eja.106.1611155740187; Wed, 20 Jan 2021 07:15:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1611155740; cv=none; d=google.com; s=arc-20160816; b=ou/SGE7l2ElGBnxu3pDRdo9YKx6uuEt64mMBSmvQ+9sLonsU1uDuLa5f8m3esXcI7h s06UOht+XuAob+05chjVKJGcyNoYZl84GGp3GJ3L+XlE9F6dERglsn5tGcVqJfQic+xF CA7EsIGzhyAlwVk6lQQUHzK0CSiB/ZoX2qxUyQP0ALhlaepW3hUqseiPeIGc5MNRNGBY uVTMunCAhEkOT7Wxk0vVpmo4ec8umta7tqF1aqMHC7GSfhGQrrhel/CNkZttCy1JUREZ D4NS6T9JxZM8sdf5a4//pXo/mBdi6rN7LsMHPEEXWLEenooEQQ4XtzO4U1Hr/KRvPcPZ s63g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=OzApJnrhekrB/MzUUMwqNHEC6DR+lVUt2fXETxrYd74=; b=amWuk/c8pMfG/bpr8Wi9zS6g+QpIsZIdUAUmbjl01+fGm1OS9G7GchOutrUBHMs1rH J+UbpQnKTDJSRGcFIYttpDwntgUnlwZNnFK88viYSL8+tpaXD5mFTeAhGM87gJa3XCRO ZvpnIFy6cZS2A+MFqTYclLb+ySUrkzgC+hoc7srVtCfQLU5dOMywWn1PE9CxAlVaAmh+ BMHmo/WRhWsD0vNReu+IqPSIX43VDibUvkvNGy639k0t0ytJNEyiUKhBuCcM+0HiMh8F XLscjaxA2OhXSPXqbIA1/DFf9ubQyVoGFt5OtN5+7+2gS67odFleV7nDiI5Mv1pF1YTl shUA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=uVer5+wz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id f7si261090edd.595.2021.01.20.07.15.15; Wed, 20 Jan 2021 07:15:40 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=uVer5+wz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728292AbhATPM0 (ORCPT + 99 others); Wed, 20 Jan 2021 10:12:26 -0500 Received: from mail.kernel.org ([198.145.29.99]:48730 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2390990AbhATPGO (ORCPT ); Wed, 20 Jan 2021 10:06:14 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 7A01A23356; Wed, 20 Jan 2021 15:05:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1611155125; bh=9874V4EQF8pGhQeRtiLlHuHTZWGypK1q+CZx02TXhZM=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=uVer5+wzd/VviGPx62NgeNadLX736EMkLOQOVHuy+k8sx0BcR+ivCEkKsnZIoxtnT GpWkXKSHUVrX3tMi+iQiZNVYUchhp0zB4TrjPy0CuHAyWgurs4kWeeuPwxfawgBGve xWg9JGEgJG54bZgaB141RZ4cuaX9zxlmp5uYxxVFC9vITcsuqfvwAakLkytsiWOGfL pNtcU5EE62T/qNQ15LxVmcJccjD7XRVg6hOvj3C8536te+4LBKab+YM8J44PRYhLZX NgPhXfCDCBlcUFMljfwDktrV1a/D2LNFJaqimj3Xi9rVUzUTR1HHM4iQVtrs0WE0Q2 /+v3XgxNylKfQ== Date: Wed, 20 Jan 2021 17:05:10 +0200 From: Mike Rapoport To: Matthew Wilcox Cc: Andrew Morton , Alexander Viro , Andy Lutomirski , Arnd Bergmann , Borislav Petkov , Catalin Marinas , Christopher Lameter , Dan Williams , Dave Hansen , David Hildenbrand , Elena Reshetova , "H. Peter Anvin" , Ingo Molnar , James Bottomley , "Kirill A. Shutemov" , Mark Rutland , Mike Rapoport , Michael Kerrisk , Palmer Dabbelt , Paul Walmsley , Peter Zijlstra , Rick Edgecombe , Roman Gushchin , Shakeel Butt , Shuah Khan , Thomas Gleixner , Tycho Andersen , Will Deacon , linux-api@vger.kernel.org, linux-arch@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-nvdimm@lists.01.org, linux-riscv@lists.infradead.org, x86@kernel.org, Hagen Paul Pfeifer Subject: Re: [PATCH v14 05/10] mm: introduce memfd_secret system call to create "secret" memory areas Message-ID: <20210120150510.GO1106298@kernel.org> References: <20201203062949.5484-1-rppt@kernel.org> <20201203062949.5484-6-rppt@kernel.org> <20210119202213.GI2260413@casper.infradead.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210119202213.GI2260413@casper.infradead.org> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Jan 19, 2021 at 08:22:13PM +0000, Matthew Wilcox wrote: > On Thu, Dec 03, 2020 at 08:29:44AM +0200, Mike Rapoport wrote: > > +static vm_fault_t secretmem_fault(struct vm_fault *vmf) > > +{ > > + struct address_space *mapping = vmf->vma->vm_file->f_mapping; > > + struct inode *inode = file_inode(vmf->vma->vm_file); > > + pgoff_t offset = vmf->pgoff; > > + vm_fault_t ret = 0; > > + unsigned long addr; > > + struct page *page; > > + int err; > > + > > + if (((loff_t)vmf->pgoff << PAGE_SHIFT) >= i_size_read(inode)) > > + return vmf_error(-EINVAL); > > + > > + page = find_get_page(mapping, offset); > > + if (!page) { > > + > > + page = secretmem_alloc_page(vmf->gfp_mask); > > + if (!page) > > + return vmf_error(-ENOMEM); > > Just use VM_FAULT_OOM directly. Ok. > > + err = add_to_page_cache(page, mapping, offset, vmf->gfp_mask); > > + if (unlikely(err)) > > + goto err_put_page; > > What if the error is EEXIST because somebody else raced with you to add > a new page to the page cache? Right, for -EEXIST I need a retry here, thanks. > > + err = set_direct_map_invalid_noflush(page, 1); > > + if (err) > > + goto err_del_page_cache; > > Does this work correctly if somebody else has a reference to the page > in the meantime? Yes, it does. If somebody else won the race that page was dropped from the direct map and this call would be essentially a nop. And anyway, the very next patch changes the way pages are removed from the direct map ;-) > > + addr = (unsigned long)page_address(page); > > + flush_tlb_kernel_range(addr, addr + PAGE_SIZE); > > + > > + __SetPageUptodate(page); > > Once you've added it to the cache, somebody else can come along and try > to lock it. They will set PageWaiter. Now you call __SetPageUptodate > and wipe out their PageWaiter bit. So you won't wake them up when you > unlock. > > You can call __SetPageUptodate before adding it to the page cache, > but once it's visible to another thread, you can't do that. Will fix. > > + ret = VM_FAULT_LOCKED; > > + } > > + > > + vmf->page = page; > > You're supposed to return the page locked, so use find_lock_page() instead > of find_get_page(). Ok/ > > + return ret; > > + > > +err_del_page_cache: > > + delete_from_page_cache(page); > > +err_put_page: > > + put_page(page); > > + return vmf_error(err); > > +} -- Sincerely yours, Mike.