Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp6939331imu; Thu, 31 Jan 2019 02:09:55 -0800 (PST) X-Google-Smtp-Source: ALg8bN5LmgsXaDUyGE9PEkmKY2QDu+35ySi2V1JMKTPbDAVd/medThO6Z0oaUEOEAKMiSbLqVtv3 X-Received: by 2002:a63:d047:: with SMTP id s7mr30683143pgi.311.1548929395474; Thu, 31 Jan 2019 02:09:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548929395; cv=none; d=google.com; s=arc-20160816; b=ajRJwiZ+x0cEVGnmZI5/qnwHwFPZkeEGbofocJmSuQOZ5nt4YjZEHoipcHWpSuIr4c lXUxfOcorGW69s76mey8+td4I8AvWotWwcqCqhPb7sdU8D2U/aU7eVRXP5b1jSx4R9Im J7H5gv95y4NgFimUqR1gpwHcoHt5pueH+zqBymJuOhaYeL61cKFA+M6N5DbO2bW7+WKb eO3/6ma4VsBchkNMDdDbtRmwirl5t99AVouxXCG+1G2MWpl+jdicgT2tadJbusM2XQF5 +jO9ckY7d9WWEN4M+lua+8cnRhZbS20BKC0zo5IHJSJ90kwQFA1SfzJu6YY1yMBkbpdX euIw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=OUEk/+aMbMeJPfGOUuxjCemjdxe2PAnTslWJE9F17e4=; b=jhyODZnP29PlEf/vQ95HZlGt1D1XLEMy/NvId6XMxReBNpuL2GjDm4xfsQtvXi620Q CMPcRbqDLI35Z9/ikk+P9zVquFJn35Na2UFSBOA367OE0aENN0uOmVk9g5x1N9vRIl02 RC0tZoor7Knr+QW5vR78nTU7/YeRf3KKoGQwg7VuHU6xdSqqSFrN+85UZRJlobqL8bwt ukVUI0N27y2ZcMpYdmDMx4F2eTBZaygQaVNDMLsFLgQ+9melWdVJvsJJZHAHIwZWfC20 T5b76HRzCjmB5iPX70iT4GaBSFMmZDaiLM5F/7ehzeFzPgBW2APIen3F6FFyfHAZ3Qln 2mdw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g15si3834915pgl.141.2019.01.31.02.09.40; Thu, 31 Jan 2019 02:09:55 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732173AbfAaKJO (ORCPT + 99 others); Thu, 31 Jan 2019 05:09:14 -0500 Received: from mx2.suse.de ([195.135.220.15]:49300 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1732132AbfAaKJM (ORCPT ); Thu, 31 Jan 2019 05:09:12 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id C5CDDADE1; Thu, 31 Jan 2019 10:09:09 +0000 (UTC) Date: Thu, 31 Jan 2019 11:09:07 +0100 From: Michal Hocko To: Vlastimil Babka Cc: Andrew Morton , Linus Torvalds , linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-api@vger.kernel.org, Peter Zijlstra , Greg KH , Jann Horn , Jiri Kosina , Dominique Martinet , Andy Lutomirski , Dave Chinner , Kevin Easton , Matthew Wilcox , Cyril Hrubis , Tejun Heo , "Kirill A . Shutemov" , Daniel Gruss , Jiri Kosina Subject: Re: [PATCH 3/3] mm/mincore: provide mapped status when cached status is not allowed Message-ID: <20190131100907.GS18811@dhcp22.suse.cz> References: <20190130124420.1834-1-vbabka@suse.cz> <20190130124420.1834-4-vbabka@suse.cz> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190130124420.1834-4-vbabka@suse.cz> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed 30-01-19 13:44:20, Vlastimil Babka wrote: > After "mm/mincore: make mincore() more conservative" we sometimes restrict the > information about page cache residency, which we have to do without breaking > existing userspace, if possible. We thus fake the resulting values as 1, which > should be safer than faking them as 0, as there might theoretically exist code > that would try to fault in the page(s) until mincore() returns 1. > > Faking 1 however means that such code would not fault in a page even if it was > not in page cache, with unwanted performance implications. We can improve the > situation by revisting the approach of 574823bfab82 ("Change mincore() to count > "mapped" pages rather than "cached" pages") but only applying it to cases where > page cache residency check is restricted. Thus mincore() will return 0 for an > unmapped page (which may or may not be resident in a pagecache), and 1 after > the process faults it in. > > One potential downside is that mincore() will be again able to recognize when a > previously mapped page was reclaimed. While that might be useful for some > attack scenarios, it's not as crucial as recognizing that somebody else faulted > the page in, and there are also other ways to recognize reclaimed pages anyway. Is this really worth it? Do we know about any specific usecase that would benefit from this change? TBH I would rather wait for the report than add a hard to evaluate side channel. > Cc: Jiri Kosina > Cc: Dominique Martinet > Cc: Andy Lutomirski > Cc: Dave Chinner > Cc: Kevin Easton > Cc: Matthew Wilcox > Cc: Cyril Hrubis > Cc: Tejun Heo > Cc: Kirill A. Shutemov > Cc: Daniel Gruss > Signed-off-by: Vlastimil Babka > --- > mm/mincore.c | 49 +++++++++++++++++++++++++++++++++---------------- > 1 file changed, 33 insertions(+), 16 deletions(-) > > diff --git a/mm/mincore.c b/mm/mincore.c > index 747a4907a3ac..d6784a803ae7 100644 > --- a/mm/mincore.c > +++ b/mm/mincore.c > @@ -21,12 +21,18 @@ > #include > #include > > +struct mincore_walk_private { > + unsigned char *vec; > + bool can_check_pagecache; > +}; > + > static int mincore_hugetlb(pte_t *pte, unsigned long hmask, unsigned long addr, > unsigned long end, struct mm_walk *walk) > { > #ifdef CONFIG_HUGETLB_PAGE > unsigned char present; > - unsigned char *vec = walk->private; > + struct mincore_walk_private *walk_private = walk->private; > + unsigned char *vec = walk_private->vec; > > /* > * Hugepages under user process are always in RAM and never > @@ -35,7 +41,7 @@ static int mincore_hugetlb(pte_t *pte, unsigned long hmask, unsigned long addr, > present = pte && !huge_pte_none(huge_ptep_get(pte)); > for (; addr != end; vec++, addr += PAGE_SIZE) > *vec = present; > - walk->private = vec; > + walk_private->vec = vec; > #else > BUG(); > #endif > @@ -85,7 +91,8 @@ static unsigned char mincore_page(struct address_space *mapping, pgoff_t pgoff) > } > > static int __mincore_unmapped_range(unsigned long addr, unsigned long end, > - struct vm_area_struct *vma, unsigned char *vec) > + struct vm_area_struct *vma, unsigned char *vec, > + bool can_check_pagecache) > { > unsigned long nr = (end - addr) >> PAGE_SHIFT; > int i; > @@ -95,7 +102,9 @@ static int __mincore_unmapped_range(unsigned long addr, unsigned long end, > > pgoff = linear_page_index(vma, addr); > for (i = 0; i < nr; i++, pgoff++) > - vec[i] = mincore_page(vma->vm_file->f_mapping, pgoff); > + vec[i] = can_check_pagecache ? > + mincore_page(vma->vm_file->f_mapping, pgoff) > + : 0; > } else { > for (i = 0; i < nr; i++) > vec[i] = 0; > @@ -106,8 +115,11 @@ static int __mincore_unmapped_range(unsigned long addr, unsigned long end, > static int mincore_unmapped_range(unsigned long addr, unsigned long end, > struct mm_walk *walk) > { > - walk->private += __mincore_unmapped_range(addr, end, > - walk->vma, walk->private); > + struct mincore_walk_private *walk_private = walk->private; > + unsigned char *vec = walk_private->vec; > + > + walk_private->vec += __mincore_unmapped_range(addr, end, walk->vma, > + vec, walk_private->can_check_pagecache); > return 0; > } > > @@ -117,7 +129,8 @@ static int mincore_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end, > spinlock_t *ptl; > struct vm_area_struct *vma = walk->vma; > pte_t *ptep; > - unsigned char *vec = walk->private; > + struct mincore_walk_private *walk_private = walk->private; > + unsigned char *vec = walk_private->vec; > int nr = (end - addr) >> PAGE_SHIFT; > > ptl = pmd_trans_huge_lock(pmd, vma); > @@ -128,7 +141,8 @@ static int mincore_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end, > } > > if (pmd_trans_unstable(pmd)) { > - __mincore_unmapped_range(addr, end, vma, vec); > + __mincore_unmapped_range(addr, end, vma, vec, > + walk_private->can_check_pagecache); > goto out; > } > > @@ -138,7 +152,7 @@ static int mincore_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end, > > if (pte_none(pte)) > __mincore_unmapped_range(addr, addr + PAGE_SIZE, > - vma, vec); > + vma, vec, walk_private->can_check_pagecache); > else if (pte_present(pte)) > *vec = 1; > else { /* pte is a swap entry */ > @@ -152,8 +166,12 @@ static int mincore_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end, > *vec = 1; > } else { > #ifdef CONFIG_SWAP > - *vec = mincore_page(swap_address_space(entry), > + if (walk_private->can_check_pagecache) > + *vec = mincore_page( > + swap_address_space(entry), > swp_offset(entry)); > + else > + *vec = 0; > #else > WARN_ON(1); > *vec = 1; > @@ -187,22 +205,21 @@ static long do_mincore(unsigned long addr, unsigned long pages, unsigned char *v > struct vm_area_struct *vma; > unsigned long end; > int err; > + struct mincore_walk_private walk_private = { > + .vec = vec > + }; > struct mm_walk mincore_walk = { > .pmd_entry = mincore_pte_range, > .pte_hole = mincore_unmapped_range, > .hugetlb_entry = mincore_hugetlb, > - .private = vec, > + .private = &walk_private > }; > > vma = find_vma(current->mm, addr); > if (!vma || addr < vma->vm_start) > return -ENOMEM; > end = min(vma->vm_end, addr + (pages << PAGE_SHIFT)); > - if (!can_do_mincore(vma)) { > - unsigned long pages = (end - addr) >> PAGE_SHIFT; > - memset(vec, 1, pages); > - return pages; > - } > + walk_private.can_check_pagecache = can_do_mincore(vma); > mincore_walk.mm = vma->vm_mm; > err = walk_page_range(addr, end, &mincore_walk); > if (err < 0) > -- > 2.20.1 -- Michal Hocko SUSE Labs