From patchwork Fri Mar 27 12:59:04 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Li Xinhai X-Patchwork-Id: 11462381 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id BDF1B13A4 for ; Fri, 27 Mar 2020 13:01:42 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 8148020857 for ; Fri, 27 Mar 2020 13:01:42 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="VZq7Mv7c" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 8148020857 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 9396E6B0010; Fri, 27 Mar 2020 09:01:41 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 8E9B56B0032; Fri, 27 Mar 2020 09:01:41 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 800526B0036; Fri, 27 Mar 2020 09:01:41 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0136.hostedemail.com [216.40.44.136]) by kanga.kvack.org (Postfix) with ESMTP id 6953A6B0010 for ; Fri, 27 Mar 2020 09:01:41 -0400 (EDT) Received: from smtpin12.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 57554824805A for ; Fri, 27 Mar 2020 13:01:41 +0000 (UTC) X-FDA: 76641153960.12.chess19_eadbfb08cd06 X-Spam-Summary: 2,0,0,2cd0e86cf0e702fe,d41d8cd98f00b204,lixinhai.lxh@gmail.com,,RULES_HIT:41:355:379:421:541:800:960:973:988:989:1260:1345:1431:1437:1535:1542:1711:1730:1747:1777:1792:1801:1981:2194:2199:2393:2559:2562:2693:2899:3138:3139:3140:3141:3142:3354:3865:3866:3867:3870:3871:3872:4321:4605:5007:6261:6653:7514:7903:8603:8660:9413:10004:11026:11473:11658:11914:12043:12114:12296:12297:12438:12517:12519:12555:12895:13148:13161:13229:13230:14181:14394:14687:14721:21080:21444:21451:21627:21666:21795:21990:30012:30051:30054:30056:30064:30070,0,RBL:209.85.208.196:@gmail.com:.lbl8.mailshell.net-62.18.0.100 66.100.201.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:24,LUA_SUMMARY:none X-HE-Tag: chess19_eadbfb08cd06 X-Filterd-Recvd-Size: 5483 Received: from mail-lj1-f196.google.com (mail-lj1-f196.google.com [209.85.208.196]) by imf06.hostedemail.com (Postfix) with ESMTP for ; Fri, 27 Mar 2020 13:01:40 +0000 (UTC) Received: by mail-lj1-f196.google.com with SMTP id r24so10112302ljd.4 for ; Fri, 27 Mar 2020 06:01:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=9HkmqOuDiPz1wozA2EiC2QaH51SmIJJ5WxX4THVR4FA=; b=VZq7Mv7cFK8L5LjSk2BNfe56DN2owgBSHiC7mA3wWZ/8oF2dPJ/XBBO8ErpV+lZbzD hspguVJVjkW972Dxxl++Ie/IlTdZmD3F/n9jRmzaDt54QViqfRBXNGe9u7o1t7UqaAVd 7GxH+Y+mjjAkEUU7Hu090hrwvW9gPd8rZguZcxm+cXatF5Hg8lRryB5vL54dWBHQ5nYG OHNjteHpDdukzpwnGbbxGaOqBbuf2aitwFSWZ97HOySywiQ+dvP0oDqK0AMmOG97oymu PjSTbDfRPt78f7QSgyY+V+l13UPbmGQF6Q1H14ghE1/BYAXDmVucghEsR9sLf/K+6xGA P6pg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=9HkmqOuDiPz1wozA2EiC2QaH51SmIJJ5WxX4THVR4FA=; b=sF+WpyJ4BhlkS3a4tIjYrYn6ckR+LXrCCmhtO9qmdcZjXu1moURoyWGkDcGG6jn7TK TDVYO2RGlUbmz1JUN4nsQEqaYdGdffOAF+cYHcSYWDJSg8mqae/AoaMAvjJ1oIYV0q8k 1g8M97p0NrpiYZNMbOPYg9OHTRCCHE5a7tzJppfuj9/YYD3DCoylAsD7lWsyh7draaIR C482L+Vblk1nBSQb+0SnOADs0s5vzr3Pdm/bUM1Qm2vwfI+D3AJG3wzK7i4nNOsNkMXn f6uk1FggMBBYaELnvmgsWhSZUXxcPE0XRfGS1wS3buO+I0FXes9oIwoXUuDDpoA3thxo ll0w== X-Gm-Message-State: AGi0Puapl9xc3oFFolRmw8OkmsGdhHFkotEvLPqerIr2GKKRvJFPlzwD t5gQUUzOIWVgsXhzvzw7/B67Q3vL X-Google-Smtp-Source: ADFU+vvzGSoZzOW0QPUvNqSGvONqzN6UrzsegjfzS7K2Gw0GKPIlCkNMuv+32D75+guUZfn7D1U3Yw== X-Received: by 2002:a2e:5ce:: with SMTP id 197mr8542362ljf.234.1585314098676; Fri, 27 Mar 2020 06:01:38 -0700 (PDT) Received: from localhost.localdomain.localdomain ([131.228.2.20]) by smtp.gmail.com with ESMTPSA id l7sm2890329lfp.65.2020.03.27.06.01.36 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 27 Mar 2020 06:01:37 -0700 (PDT) From: Li Xinhai To: linux-mm@kvack.org Cc: Mike Kravetz , Andrew Morton Subject: [PATCH] mm: introduce MAP_FIXED_HUGETLB_LEN to mmap() Date: Fri, 27 Mar 2020 12:59:04 +0000 Message-Id: <1585313944-8627-1-git-send-email-lixinhai.lxh@gmail.com> X-Mailer: git-send-email 1.8.3.1 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: The purpose of MAP_FIXED_HUGETLB_LEN is to check whether the parameter length is valid or not according to the target file's huge page size. When it is used, if length is not aligned to underlying huge page size, mmap() is failed with errno set to EINVAL. When it is not used, the current semantic is maintained, i.e., length is round up to underlying huge page size. In current code, the vma related call, except mmap, are all consider not correctly aligned length as invalid parameter, including mprotect, munmap, mlock, etc., by checking through hugetlb_vm_op_split. So, user will see failure, after successfully call mmap, although using same length parameter to other mapping syscall. With MAP_FIXED_HUGETLB_LEN, user can choose to check if length is correctly aligned at first place when call mmap, instead of failure after mapping has been created. Signed-off-by: Li Xinhai Cc: Mike Kravetz Cc: Andrew Morton --- include/uapi/asm-generic/mman-common.h | 1 + mm/mmap.c | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/include/uapi/asm-generic/mman-common.h b/include/uapi/asm-generic/mman-common.h index f94f65d..1c9ba97 100644 --- a/include/uapi/asm-generic/mman-common.h +++ b/include/uapi/asm-generic/mman-common.h @@ -21,6 +21,7 @@ #define MAP_TYPE 0x0f /* Mask for type of mapping */ #define MAP_FIXED 0x10 /* Interpret addr exactly */ #define MAP_ANONYMOUS 0x20 /* don't use a file */ +#define MAP_FIXED_HUGETLB_LEN 0x40 /* check alignment of addr, length, offset */ /* 0x0100 - 0x4000 flags are defined in asm-generic/mman.h */ #define MAP_POPULATE 0x008000 /* populate (prefault) pagetables */ diff --git a/mm/mmap.c b/mm/mmap.c index d681a20..50a12e0 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1560,9 +1560,18 @@ unsigned long ksys_mmap_pgoff(unsigned long addr, unsigned long len, file = fget(fd); if (!file) return -EBADF; - if (is_file_hugepages(file)) - len = ALIGN(len, huge_page_size(hstate_file(file))); + retval = -EINVAL; + if (is_file_hugepages(file)) { + struct hstate *hs = hstate_file(file); + + if (flags & MAP_FIXED_HUGETLB_LEN && + len & ~(huge_page_mask(hs))) + goto out_fput; + + len = ALIGN(len, huge_page_size(hs)); + } + if (unlikely(flags & MAP_HUGETLB && !is_file_hugepages(file))) goto out_fput; } else if (flags & MAP_HUGETLB) { @@ -1573,6 +1582,10 @@ unsigned long ksys_mmap_pgoff(unsigned long addr, unsigned long len, if (!hs) return -EINVAL; + if (flags & MAP_FIXED_HUGETLB_LEN && + len & ~(huge_page_mask(hs))) + return -EINVAL; + len = ALIGN(len, huge_page_size(hs)); /* * VM_NORESERVE is used because the reservations will be