Received: by 2002:a5b:505:0:0:0:0:0 with SMTP id o5csp5907374ybp; Tue, 8 Oct 2019 10:00:27 -0700 (PDT) X-Google-Smtp-Source: APXvYqw5JV8hglXqDbweV7SKVJRgvCUiaDkE5epdmoRxBloO2q2QwODw33Ky22auDJJchtGzSkvl X-Received: by 2002:a17:906:6c8:: with SMTP id v8mr30213694ejb.40.1570554027053; Tue, 08 Oct 2019 10:00:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1570554027; cv=none; d=google.com; s=arc-20160816; b=vCssyzGJ8ryiQUfeP4tHf++8LPr6EqAr9pYmPbpc8CNDDDs2NWHr2sic/k2VoEqXbL KZnFwZc5XoBXZQWNI2ru9t0lycxYvw3vZ9ACXLtwgXpbQLoxUL711ZyxBFk83uOMCRmA Lva3v5WjIVHAt4/sPVNQXLtojYdtg6LD7UjqqBw1PPeb279do/7+OPFbYry9RksukBts mPuWJ+DmdnaL9l4USkHmi0L+l2qFkuMuzi+5Vautyju4PLowF3ICJ+Lfb4e95oUVbtbl +K0crJ0nYTl2hdkATZW2nGjBYRbYmqS8xwIWA3485i9db7gp2dIa+aFfHbGgHqULNf1Q HCeg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from; bh=F+p/XLzgGK35dc/+vVCczri8W+LLCyIKn+GJ6nstQ1U=; b=pQC3IAJ3JA9auhhNLI4rvGWY9HfFAwgtow9jK69F0VdxvOKF9Cx3sTzo+t95sSox81 YTBLkIDmk6UpzHSiN3rb+gh3ixYyICdcQFNoskKZFbrrZMIQ5R2VllmW68jZx4kNjq09 K9qFUDxykDuoo/qDRAXRD8CdfgaG7+nFPJqB/zcajQ9rq1bNDuIVOqI1ZoVDawn33es9 /w7Ho303Ec2Rzuujbos8AHmCIiCrTKbf/GKhcNSeIfJ/xk7lOv5A8Vkj4+RrZXQHilYY YH4ungTQNuEg8r8537uyePLkPGVjFRqA5y6T07l5Qf7kKYAeQgBy4sVM/MIrtYc65sa9 cMWA== 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=QUARANTINE sp=NONE dis=NONE) header.from=vmware.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id be13si10046553edb.362.2019.10.08.10.00.02; Tue, 08 Oct 2019 10:00:27 -0700 (PDT) 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=QUARANTINE sp=NONE dis=NONE) header.from=vmware.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729285AbfJHQ7c (ORCPT + 99 others); Tue, 8 Oct 2019 12:59:32 -0400 Received: from ex13-edg-ou-002.vmware.com ([208.91.0.190]:32045 "EHLO EX13-EDG-OU-002.vmware.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726822AbfJHQ7c (ORCPT ); Tue, 8 Oct 2019 12:59:32 -0400 Received: from sc9-mailhost3.vmware.com (10.113.161.73) by EX13-EDG-OU-002.vmware.com (10.113.208.156) with Microsoft SMTP Server id 15.0.1156.6; Tue, 8 Oct 2019 09:44:22 -0700 Received: from akaher-lnx-dev.eng.vmware.com (unknown [10.110.19.203]) by sc9-mailhost3.vmware.com (Postfix) with ESMTP id F139340AF8; Tue, 8 Oct 2019 09:44:17 -0700 (PDT) From: Ajay Kaher To: CC: , , , , , , , , , , , , , , , , , , , Michal Hocko , "Aneesh Kumar K . V" , Catalin Marinas , Naoya Horiguchi , Mark Rutland , Hillf Danton , Mike Kravetz Subject: [PATCH v2 5/8] mm, gup: ensure real head page is ref-counted when using hugepages Date: Wed, 9 Oct 2019 06:14:20 +0530 Message-ID: <1570581863-12090-6-git-send-email-akaher@vmware.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1570581863-12090-1-git-send-email-akaher@vmware.com> References: <1570581863-12090-1-git-send-email-akaher@vmware.com> MIME-Version: 1.0 Content-Type: text/plain Received-SPF: None (EX13-EDG-OU-002.vmware.com: akaher@vmware.com does not designate permitted sender hosts) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Punit Agrawal commit d63206ee32b6e64b0e12d46e5d6004afd9913713 upstream. When speculatively taking references to a hugepage using page_cache_add_speculative() in gup_huge_pmd(), it is assumed that the page returned by pmd_page() is the head page. Although normally true, this assumption doesn't hold when the hugepage comprises of successive page table entries such as when using contiguous bit on arm64 at PTE or PMD levels. This can be addressed by ensuring that the page passed to page_cache_add_speculative() is the real head or by de-referencing the head page within the function. We take the first approach to keep the usage pattern aligned with page_cache_get_speculative() where users already pass the appropriate page, i.e., the de-referenced head. Apply the same logic to fix gup_huge_[pud|pgd]() as well. [punit.agrawal@arm.com: fix arm64 ltp failure] Link: http://lkml.kernel.org/r/20170619170145.25577-5-punit.agrawal@arm.com Link: http://lkml.kernel.org/r/20170522133604.11392-3-punit.agrawal@arm.com Signed-off-by: Punit Agrawal Acked-by: Steve Capper Cc: Michal Hocko Cc: "Kirill A. Shutemov" Cc: Aneesh Kumar K.V Cc: Catalin Marinas Cc: Will Deacon Cc: Naoya Horiguchi Cc: Mark Rutland Cc: Hillf Danton Cc: Mike Kravetz Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Ajay Kaher Reviewed-by: Srivatsa S. Bhat (VMware) --- mm/gup.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/mm/gup.c b/mm/gup.c index 6e7cfaa..fae4d1e 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -1132,8 +1132,7 @@ static int gup_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr, return 0; refs = 0; - head = pmd_page(orig); - page = head + ((addr & ~PMD_MASK) >> PAGE_SHIFT); + page = pmd_page(orig) + ((addr & ~PMD_MASK) >> PAGE_SHIFT); tail = page; do { pages[*nr] = page; @@ -1142,6 +1141,7 @@ static int gup_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr, refs++; } while (addr += PAGE_SIZE, addr != end); + head = compound_head(pmd_page(orig)); if (!page_cache_add_speculative(head, refs)) { *nr -= refs; return 0; @@ -1178,8 +1178,7 @@ static int gup_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr, return 0; refs = 0; - head = pud_page(orig); - page = head + ((addr & ~PUD_MASK) >> PAGE_SHIFT); + page = pud_page(orig) + ((addr & ~PUD_MASK) >> PAGE_SHIFT); tail = page; do { pages[*nr] = page; @@ -1188,6 +1187,7 @@ static int gup_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr, refs++; } while (addr += PAGE_SIZE, addr != end); + head = compound_head(pud_page(orig)); if (!page_cache_add_speculative(head, refs)) { *nr -= refs; return 0; @@ -1220,8 +1220,7 @@ static int gup_huge_pgd(pgd_t orig, pgd_t *pgdp, unsigned long addr, return 0; refs = 0; - head = pgd_page(orig); - page = head + ((addr & ~PGDIR_MASK) >> PAGE_SHIFT); + page = pgd_page(orig) + ((addr & ~PGDIR_MASK) >> PAGE_SHIFT); tail = page; do { pages[*nr] = page; @@ -1230,6 +1229,7 @@ static int gup_huge_pgd(pgd_t orig, pgd_t *pgdp, unsigned long addr, refs++; } while (addr += PAGE_SIZE, addr != end); + head = compound_head(pgd_page(orig)); if (!page_cache_add_speculative(head, refs)) { *nr -= refs; return 0; -- 2.7.4