Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp1195736pxb; Fri, 21 Jan 2022 12:01:07 -0800 (PST) X-Google-Smtp-Source: ABdhPJxFEEZxBOB6tx0g/Abog97B4dd6dTNukdbbg/asYDzt/FPNG/Pz3LDMzZ95E+dhtWIQ6ePV X-Received: by 2002:a17:90b:1103:: with SMTP id gi3mr2265891pjb.177.1642795267282; Fri, 21 Jan 2022 12:01:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1642795267; cv=none; d=google.com; s=arc-20160816; b=oOUKaj5+K7KbThIhd11Ve9s2GYHyJmSk0wGoHZ9s2x4s+GFXElAFfmvpfoSJwVkRGf Q21Osc95+vLFFGaiaJULbd044JqHAcPoXJhfpL6boWRtCyUCFs7UqHnNzs64U/sj0FDF OEPHNdr5NAkUXFVKy1sdGCvNkdF76bsxm+Mmn7McAEtK9YUTS3FuldcRAoK5BS0abjdD jzWUb/nyUKjF/ajPkUDQ6Sf5yck7mNfy2/P9nmslM5lNAcsokYq9SAT9bI1zppLbqj2w 0VRH+4HKgk1kYeRgmDzKz6N39jQ3NWxshHxJ56mWskVmHQoAuTXWQCVDP6V3FIrT9mL+ yNLA== 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=BTctFDlugghbPb+mT0fgLBcwlvNBpzSgllYA3xjcWrs=; b=aQj3ebzF1ZvZ/XQULDNvYcjUexGzcIeQYsCcbd5krVx4rDGFwZ037owVgnilI8Ik0B eZlIunLekr5mQr5xJZVouLsrTRZvgIKP+nOuyKY58lH9xh//Y1pHd6COj6SNf5a7s0qw pCik97v4wNJDmVN4LIaB7Yh50nl5xOr5mcP2/W9R8b0lyUdF/VE0PFaZ809n1sbnNYnT LNxHq8zHKrS8f6oTqdY5bUXadMhtK7jpRD1FVngGWnPDOkF1cnQqMnVWq1EXWbyjFEIv dj+qJwJDNT5QpPu0VmlDDxzGnJsA+SEhzul7suuhOE4Erx6ofrXi8bwD9+H78fqcEKuZ UsVw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=jcnZQSl5; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id j3si6580548pfr.288.2022.01.21.12.00.55; Fri, 21 Jan 2022 12:01:07 -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=@infradead.org header.s=casper.20170209 header.b=jcnZQSl5; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1357038AbiASTgK (ORCPT + 99 others); Wed, 19 Jan 2022 14:36:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59546 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1357034AbiASTft (ORCPT ); Wed, 19 Jan 2022 14:35:49 -0500 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C3BA1C06173E for ; Wed, 19 Jan 2022 11:35:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=BTctFDlugghbPb+mT0fgLBcwlvNBpzSgllYA3xjcWrs=; b=jcnZQSl5oKu5z9MZdKcUVh/HDN QIRUbKxvEgbiqaoJ8yVLCy8UxESf7Ve2AtfN42EmuXgcCMBS8LQl4VNpa8nZEK/FzdZaXnlOh5neE X+ALUAl+ZF8C9z0qIpLd4axHfWML+pcSJ3TYZ75vfXLre1Esd+GX0bdzwElrlbbJWOesI9MG/NYbj qi3cmhafL8em48vvoN4EAoL8fbjZCHbkd4hpmArMAbbpDhS84XPpQAYEI1RW6kcdfbTH/E6q+s9ap +c2vLEmj+5VKpXS4MZkOKUT6FAQL7RVaGnvQGCox80+T0EwnwL0foIvDx5jef9sazBRLGiW4dZ4Gr 2wts/3lw==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1nAGk1-00BvrJ-F0; Wed, 19 Jan 2022 19:35:33 +0000 Date: Wed, 19 Jan 2022 19:35:33 +0000 From: Matthew Wilcox To: "Russell King (Oracle)" Cc: Robin Murphy , Yury Norov , Catalin Marinas , Will Deacon , Andrew Morton , Nicholas Piggin , Ding Tianhong , Anshuman Khandual , Alexey Klimov , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: Re: [PATCH] vmap(): don't allow invalid pages Message-ID: References: <20220118235244.540103-1-yury.norov@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Jan 19, 2022 at 06:57:34PM +0000, Russell King (Oracle) wrote: > On Wed, Jan 19, 2022 at 06:01:24PM +0000, Matthew Wilcox wrote: > > On Wed, Jan 19, 2022 at 05:54:15PM +0000, Russell King (Oracle) wrote: > > > On Wed, Jan 19, 2022 at 04:27:32PM +0000, Matthew Wilcox wrote: > > > > On Wed, Jan 19, 2022 at 01:28:14PM +0000, Robin Murphy wrote: > > > > > > + if (WARN_ON(!pfn_valid(page_to_pfn(page)))) > > > > > > > > > > Is it page_to_pfn() guaranteed to work without blowing up if page is invalid > > > > > in the first place? Looking at the CONFIG_SPARSEMEM case I'm not sure that's > > > > > true... > > > > > > > > Even if it does blow up, at least it's blowing up here where someone > > > > can start to debug it, rather than blowing up on first access, where > > > > we no longer have the invlid struct page pointer. > > > > > > > > I don't think we have a 'page_valid' function which will tell us whether > > > > a random pointer is actually a struct page or not. > > > > > > Isn't it supposed to be: > > > > > > if (!pfn_valid(pfn)) { > > > handle invalid pfn; > > > } > > > > > > page = pfn_to_page(pfn); > > > > > > Anything else - even trying to convert an invalid page back to a pfn, > > > could well be unreliable (sparsemem or discontigmem). > > > > This function is passed an array of pages. We have no way of doing > > what you propose. > > You can't go from a struct page to "this is valid", it's too late by the > time you call vmap() - that's my fundamental point. Yes, and we have debugging code in __virt_to_phys() that would have caught this, had Yury enabled CONFIG_DEBUG_VIRTUAL. My point is that in this instance, page_to_pfn() doesn't crash, which lets vmap() set up a mapping to a completely bogus physical address. We're better off checking pfn_valid() here than not. > If the translation from a PFN to a struct page can return pointers to > something that isn't a valid struct page, then it can also (with > sparsemem) return a pointer to _another_ struct page that could well > be valid depending on how the struct page arrays are laid out in > memory. Sure, it's not going to catch everything. But I don't think that letting perfect be the enemy of the good here is the right approach.