Received: by 2002:a25:86ce:0:0:0:0:0 with SMTP id y14csp928138ybm; Tue, 21 May 2019 06:06:54 -0700 (PDT) X-Google-Smtp-Source: APXvYqyt5spPXIJuChipFASqyTijv+yqsEi7sJUcT9xCHH6M9nPtaCvCq+UQwSHykzF1QtdqQc0o X-Received: by 2002:a17:902:714e:: with SMTP id u14mr76332489plm.218.1558444013945; Tue, 21 May 2019 06:06:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558444013; cv=none; d=google.com; s=arc-20160816; b=cPVPoZDxjvXx0c3V0gJFndzbDhsLqSZU8OE2WeoinUjHD7NAX7+8hYEUUVruaxpmvO sR6DQP+5ufNdWBJMSH9X3X32M+agcms3JHSW93JAyANxb+3gYwg+Df2Q4/AkErRScjjw ZvSkmgTqlO7Drf8EUTF0s2BDQ437J56FCjDWkYY+5r5++BvCtt0mBu2o30+eP06eBPXw 2r4Li++Wufesl68kPD5gy+ItlxTKbQM/GJwrMYS8O7pFTYy0ypvAoBNSkuHRPH+mz9yb iX9ArofnpL0rynYPnle3RJJd/Od/EYA3ouZlQCwEHhbabtoTj0l2VfVgSCm2D4F8eDKX mJ9Q== 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-transfer-encoding:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=UoeHu3yoNUcPPl9PKQVlxSlKB7SL3/RQXELHWxMzhXU=; b=DiR5EX1a6M1z48MOojSjjOs+s9bkuLN9AktjPztocRCLweM2+hfVReqn6ZLsspO/YK JuzEhFiobmiBhu3MsnBOLexrqad5L6RZVY+MfV13u1ec0EbJb79RQ+i4O4RFskAT4+gJ XiMwjXh7Qbif9X1wuF5w+zOtytYgDoiFV2l7fE6cwbenPjVo502iibiZOFKenxpcemTo 9RnGvt+ujdpQ31EDn8/G9mU8sDmm1Nbb/o2K3uYJ2KC9HUzaiNvNEqBtn9QZUD7MyrS3 O3hwkuPRefcrzID8DmzA3OZaCWnHiZu1m8Eckjq3ptJct0pXP3twEeDRd/iLzFfKYLnw e5Gg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@brauner.io header.s=google header.b=XJSG1EyP; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id cn10si22784387plb.270.2019.05.21.06.06.36; Tue, 21 May 2019 06:06:53 -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=@brauner.io header.s=google header.b=XJSG1EyP; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728285AbfEUNEp (ORCPT + 99 others); Tue, 21 May 2019 09:04:45 -0400 Received: from mail-wr1-f67.google.com ([209.85.221.67]:32981 "EHLO mail-wr1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728271AbfEUNEo (ORCPT ); Tue, 21 May 2019 09:04:44 -0400 Received: by mail-wr1-f67.google.com with SMTP id d9so5571692wrx.0 for ; Tue, 21 May 2019 06:04:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=brauner.io; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:content-transfer-encoding:in-reply-to :user-agent; bh=UoeHu3yoNUcPPl9PKQVlxSlKB7SL3/RQXELHWxMzhXU=; b=XJSG1EyPYqVFv06qjxl82rQK6DnXz+zzZ5SuDKstpUJiXRNS8tQ5JU13/0R23pzYo8 CBflOikG7l96+4gPyOlywhufFiPRmoYNDl41896XQ/0MJatuwnRejMHB6TDC98rsUSsk 2AmSYFgAwYwyiIa3Uw0OG4PN7UgonE+wdbkLG/dhBH8kdQoOGpBbAw+wyKcjrVvNwBmM VMKMZI30ibz9pqFsG/Cn36t1s2Pm2QH0dtNNkeKYHL7aO5r+71RV9j9Ohu/+y3mncQUm NMgjzNYEt1ik5sf8wphXNVOj5VnbG86Jclu6wpcMm3hVcqUXQ13o2yCTql/kibMeVk3R 6GZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:content-transfer-encoding :in-reply-to:user-agent; bh=UoeHu3yoNUcPPl9PKQVlxSlKB7SL3/RQXELHWxMzhXU=; b=IAg6FzfHqGrHmEoutBv9Q8cfOgQK1LMsyliQfkkrztVyHmcOEIMUiqMOts40PDtmee jAtM7GFXNOtGLGvS5Gl1sTr3iVywQWcjCoRi11oQyLy5Aax5UFagpr8A/sRWw64cJmE5 GwSfvt1EatQ5cFF0jp0yeoJCRz/2PuRJdSbSCk/8qdUljivDSt/6N9NgOJjdWMYjZqQp b4xEVFgT/lYleM8TfLsUMufK5pZklZA81I8HWtK8D96sMmhu3sMi6g48R/TJ5DZRBCJO qJ/HA2rCDsdO+bBAGdXavmtOxUwJswrNe7/UIO4LZPzljJWD1y+6/Ly9roHUB/17FFXb P1oA== X-Gm-Message-State: APjAAAXjzk+1XfaDcpiTvtvrggKp+8mHjEYRdthaiCmhkePe94K5BysY 4GcA7TbKdjGDg8uPn8vNGC08ug== X-Received: by 2002:a5d:6b12:: with SMTP id v18mr34420146wrw.306.1558443882738; Tue, 21 May 2019 06:04:42 -0700 (PDT) Received: from brauner.io (p548C9938.dip0.t-ipconnect.de. [84.140.153.56]) by smtp.gmail.com with ESMTPSA id x64sm5789182wmg.17.2019.05.21.06.04.41 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Tue, 21 May 2019 06:04:42 -0700 (PDT) Date: Tue, 21 May 2019 15:04:39 +0200 From: Christian Brauner To: Florian Weimer Cc: viro@zeniv.linux.org.uk, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-api@vger.kernel.org, jannh@google.com, oleg@redhat.com, tglx@linutronix.de, torvalds@linux-foundation.org, arnd@arndb.de, shuah@kernel.org, dhowells@redhat.com, tkjos@android.com, ldv@altlinux.org, miklos@szeredi.hu, linux-alpha@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-ia64@vger.kernel.org, linux-m68k@lists.linux-m68k.org, linux-mips@vger.kernel.org, linux-parisc@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-s390@vger.kernel.org, linux-sh@vger.kernel.org, sparclinux@vger.kernel.org, linux-xtensa@linux-xtensa.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org, x86@kernel.org Subject: Re: [PATCH 1/2] open: add close_range() Message-ID: <20190521130438.q3u4wvve7p6md6cm@brauner.io> References: <20190521113448.20654-1-christian@brauner.io> <87tvdoau12.fsf@oldenburg2.str.redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <87tvdoau12.fsf@oldenburg2.str.redhat.com> User-Agent: NeoMutt/20180716 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, May 21, 2019 at 02:09:29PM +0200, Florian Weimer wrote: > * Christian Brauner: > > > +/** > > + * __close_range() - Close all file descriptors in a given range. > > + * > > + * @fd: starting file descriptor to close > > + * @max_fd: last file descriptor to close > > + * > > + * This closes a range of file descriptors. All file descriptors > > + * from @fd up to and including @max_fd are closed. > > + */ > > +int __close_range(struct files_struct *files, unsigned fd, unsigned max_fd) > > +{ > > + unsigned int cur_max; > > + > > + if (fd > max_fd) > > + return -EINVAL; > > + > > + rcu_read_lock(); > > + cur_max = files_fdtable(files)->max_fds; > > + rcu_read_unlock(); > > + > > + /* cap to last valid index into fdtable */ > > + if (max_fd >= cur_max) > > + max_fd = cur_max - 1; > > + > > + while (fd <= max_fd) > > + __close_fd(files, fd++); > > + > > + return 0; > > +} > > This seems rather drastic. How long does this block in kernel mode? > Maybe it's okay as long as the maximum possible value for cur_max stays > around 4 million or so. That's probably valid concern when you reach very high numbers though I wonder how relevant this is in practice. Also, you would only be blocking yourself I imagine, i.e. you can't DOS another task with this unless your multi-threaded. > > Solaris has an fdwalk function: > > > > So a different way to implement this would expose a nextfd system call Meh. If nextfd() then I would like it to be able to: - get the nextfd(fd) >= fd - get highest open fd e.g. nextfd(-1) But then I wonder if nextfd() needs to be a syscall and isn't just either: fcntl(fd, F_GET_NEXT)? or prctl(PR_GET_NEXT)? Technically, one could also do: fd_range(unsigned fd, unsigend end_fd, unsigned flags); fd_range(3, 50, FD_RANGE_CLOSE); /* return highest fd within the range [3, 50] */ fd_range(3, 50, FD_RANGE_NEXT); /* return highest fd */ fd_range(3, UINT_MAX, FD_RANGE_NEXT); This syscall could also reasonably be extended. > to userspace, so that we can use that to implement both fdwalk and > closefrom. But maybe fdwalk is just too obscure, given the existence of > /proc. Yeah we probably don't need fdwalk. > > I'll happily implement closefrom on top of close_range in glibc (plus > fallback for older kernels based on /proc—with an abort in case that > doesn't work because the RLIMIT_NOFILE hack is unreliable > unfortunately). > > Thanks, > Florian