Received: by 10.192.165.148 with SMTP id m20csp424821imm; Fri, 20 Apr 2018 08:57:52 -0700 (PDT) X-Google-Smtp-Source: AIpwx496jk6vSqp5qUXYwJyVdMDuxGj5lScr6uPhSzcLdbfhMRD6AGO4UCfrHyHvAwFYACtaVX5/ X-Received: by 10.99.106.7 with SMTP id f7mr9068407pgc.363.1524239871981; Fri, 20 Apr 2018 08:57:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1524239871; cv=none; d=google.com; s=arc-20160816; b=RV3x7Xi/fKeUDFNRUtssarUwh2Hs03dyiG/j9ZDHOgidXt09ktGDLAT4v/uJ2StN4I c3gfD6QLTaQj4M88ubdLarxsEO8uF/j3/u4xVkpyNv/EEJXPAk8hwX4lr+Ixp5mtfjyI 7deuI6DCtraNZ0BTiHkv4eHpK36pF3W/Q4kN+KAdg7Snpgl0HU6/bAVEZLnrw5IpwHbV t6HMywtiQDIVd/JAIbhDZenvpyaVci1f3tj+97R2bUC0CrFx80CYt71MszHMtD1xX1Xv 19f2R/qiB0A8qObtFHUd8qR7e5H6i51ZyEvLDJ2Kqi0lbHoa5jLSBHYOW4QGrQmJzV7c EE5g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=4p9GENac9bMC39tvlGVVbyEpYdtaM0ykcuE5gbgRgK0=; b=c6+6vlpF4Ubp19CtZSiz6UUzfEWuAmtAyBJMQp44X19RjhYobBr0hMm17vNSqSyKRT 6KwOpPTfDc/apdVuLapdtIBvVeEfnEdTPCgUOQpe490OKB4VY3382f4nod6HO9S0WrXS g7EvEKNVvp5gjAalSjBVFv17ZQA5JxxKm4QoaDqwkHq1cg7QVZLkt9jlyVt48n9QI1f1 IuR5EQljzR4MBt+r0I4utDB7zUpH3EfLISMNb0MccsroktQSJIScI7hYNIjH+5eA11p/ 530iC5oLH1gN32mjvJECe5E50N/TKbrXjM9osAs/qHpoIa+KJkItIbOXGShxLycNQhyv xi5g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=RLFPqRCB; 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=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y14si4050544pgr.218.2018.04.20.08.57.37; Fri, 20 Apr 2018 08:57:51 -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; dkim=pass header.i=@google.com header.s=20161025 header.b=RLFPqRCB; 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=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755888AbeDTPzx (ORCPT + 99 others); Fri, 20 Apr 2018 11:55:53 -0400 Received: from mail-pf0-f170.google.com ([209.85.192.170]:39081 "EHLO mail-pf0-f170.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755850AbeDTPzt (ORCPT ); Fri, 20 Apr 2018 11:55:49 -0400 Received: by mail-pf0-f170.google.com with SMTP id z9so4485345pfe.6 for ; Fri, 20 Apr 2018 08:55:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=4p9GENac9bMC39tvlGVVbyEpYdtaM0ykcuE5gbgRgK0=; b=RLFPqRCBPd9+qGQ1Wa4yQdVE0WZ339xWxdkZczW9n3c/ohiPxBlhNJGLENJTieuJry 2tBVZs6dLAfB0F9IfdGSaI7eFMIWaC+EcPuRRSgSLqDeYSdUpj0LOppuXFbqcvHQNgUK 543XTH1e5bhfdTVyN55HkH/cqKyNflRL+AhsW3MhGo4TfV8SzaMJmTnZSFc3pq45eoSK XbXS3Ws2hiNBwGPuCrRBNRaRAv9bODv2BOZsHsDdhhLlRnZX7IQyLtOWkodX1MQIhsle yhl0YDVHKRqBKFH1EpjqnfEvDoDmvCJ7K9fvBpDbLf7t7Wn650o5s/JVuEugVKQRDeCJ Haww== 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:in-reply-to :references; bh=4p9GENac9bMC39tvlGVVbyEpYdtaM0ykcuE5gbgRgK0=; b=md8SiRCA+ir48N9MJobkcIe28g0eW3AlzySPWP/L/YtBWuTbNt7W9LyqCxBvjtGCM/ 79y5kiIxLgh57jzgw0EA+3Gu3ocfmhzs1/eF9K02iH2ZnKtS99cpfeLPvVIFLthpRjE/ WGeAyW0JQRvCl35UaDEG2DCol2iXJiXut6r2C2E0oQMeb4FFfHkQ8ZES60srs/lMBSch 0TTZl9fMFBIOd7gNcCAC/0Q4XkooNdIAIJN2zNT+MLkSHtvgw/DIPzXf+/Vy60bh6sBK 7jnX6Dk/zX/fGw6uWyRixuWwZABnDbm90JMb7tFIG7EQceGtNSQlCvJTOML1lQ3VIOhn t3Jw== X-Gm-Message-State: ALQs6tBB/ITiuXx4Qvk2ddg13I6miKZzlkn8ZY73057POIBDVWff8gvY kwVybzamESn7+WFl9K+VdPnPpw== X-Received: by 10.98.162.2 with SMTP id m2mr10322827pff.251.1524239748252; Fri, 20 Apr 2018 08:55:48 -0700 (PDT) Received: from localhost ([2620:15c:2c4:1:7e6f:1e60:1805:893c]) by smtp.gmail.com with ESMTPSA id u68sm11396164pfu.167.2018.04.20.08.55.46 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 20 Apr 2018 08:55:47 -0700 (PDT) From: Eric Dumazet To: "David S . Miller" Cc: netdev , linux-kernel , Soheil Hassas Yeganeh , Eric Dumazet , Eric Dumazet Subject: [PATCH net-next 1/4] mm: provide a mmap_hook infrastructure Date: Fri, 20 Apr 2018 08:55:39 -0700 Message-Id: <20180420155542.122183-2-edumazet@google.com> X-Mailer: git-send-email 2.17.0.484.g0c8726318c-goog In-Reply-To: <20180420155542.122183-1-edumazet@google.com> References: <20180420155542.122183-1-edumazet@google.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When adding tcp mmap() implementation, I forgot that socket lock had to be taken before current->mm->mmap_sem. syzbot eventually caught the bug. This patch provides a new mmap_hook() method in struct file_operations that might be provided by fs to implement a finer control of whats to be done before and after do_mmap_pgoff() and/or the mm->mmap_sem acquire/release. This is used in following patches by networking and TCP stacks to solve the lockdep issue, and also allows some preparation and cleanup work being done before/after mmap_sem is held, allowing better scalability in multi-threading programs. Fixes: 93ab6cc69162 ("tcp: implement mmap() for zero copy receive") Signed-off-by: Eric Dumazet Reported-by: syzbot --- include/linux/fs.h | 6 ++++++ mm/util.c | 19 ++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/include/linux/fs.h b/include/linux/fs.h index 92efaf1f89775f7b017477617dd983c10e0dc4d2..ef3526f84686585678861fc585efea974a69ca55 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -1698,6 +1698,11 @@ struct block_device_operations; #define NOMMU_VMFLAGS \ (NOMMU_MAP_READ | NOMMU_MAP_WRITE | NOMMU_MAP_EXEC) +enum mmap_hook { + MMAP_HOOK_PREPARE, + MMAP_HOOK_ROLLBACK, + MMAP_HOOK_COMMIT, +}; struct iov_iter; @@ -1714,6 +1719,7 @@ struct file_operations { long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long); long (*compat_ioctl) (struct file *, unsigned int, unsigned long); int (*mmap) (struct file *, struct vm_area_struct *); + int (*mmap_hook) (struct file *, enum mmap_hook); unsigned long mmap_supported_flags; int (*open) (struct inode *, struct file *); int (*flush) (struct file *, fl_owner_t id); diff --git a/mm/util.c b/mm/util.c index 1fc4fa7576f762bbbf341f056ca6d0be803a423f..3ddb18ab367f069d5884083e992e999546ccd995 100644 --- a/mm/util.c +++ b/mm/util.c @@ -350,11 +350,28 @@ unsigned long vm_mmap_pgoff(struct file *file, unsigned long addr, ret = security_mmap_file(file, prot, flag); if (!ret) { - if (down_write_killable(&mm->mmap_sem)) + int (*mmap_hook)(struct file *, enum mmap_hook) = NULL; + + if (file) { + mmap_hook = file->f_op->mmap_hook; + + if (mmap_hook) { + ret = mmap_hook(file, MMAP_HOOK_PREPARE); + if (ret) + return ret; + } + } + if (down_write_killable(&mm->mmap_sem)) { + if (mmap_hook) + mmap_hook(file, MMAP_HOOK_ROLLBACK); return -EINTR; + } ret = do_mmap_pgoff(file, addr, len, prot, flag, pgoff, &populate, &uf); up_write(&mm->mmap_sem); + if (mmap_hook) + mmap_hook(file, IS_ERR(ret) ? MMAP_HOOK_ROLLBACK : + MMAP_HOOK_COMMIT); userfaultfd_unmap_complete(mm, &uf); if (populate) mm_populate(ret, populate); -- 2.17.0.484.g0c8726318c-goog