From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.1 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,USER_AGENT_NEOMUTT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 19E8DC282DD for ; Thu, 23 May 2019 14:37:43 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id D787B20879 for ; Thu, 23 May 2019 14:37:42 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=brauner.io header.i=@brauner.io header.b="YbCZESmb" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730900AbfEWOhi (ORCPT ); Thu, 23 May 2019 10:37:38 -0400 Received: from mail-it1-f194.google.com ([209.85.166.194]:36825 "EHLO mail-it1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730888AbfEWOhh (ORCPT ); Thu, 23 May 2019 10:37:37 -0400 Received: by mail-it1-f194.google.com with SMTP id e184so8880693ite.1 for ; Thu, 23 May 2019 07:37:37 -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:in-reply-to:user-agent; bh=DahHECUK2AF1J12XBLs2bKF9FH9rjTuzkU7KMfzCZKg=; b=YbCZESmbvCwAQCtgJ5vNBW4fzZ0rmez/RQRMkvQ2gBf8m2vQZ7pZbh6Bnf/yhxyKVI 53LigfqSgxhhyV3cJwQAxeBYmchcYj4+L6sb6hM6ANhJ3YzqD5nVaV8VUWyXmRfwM0Js s0qbXNd2fPwKrwA6FMsqrbW+O3oe4jc4dfMVogKbtgea8qKxT3IcnEz6YQoGlNR0pY3m WBeNhDCmn5OB53HQT4TWeQ+Gay0cWaoJ8RQtor16rRwite01Brwb3BNAlCJ/+wg1v28J HJ9HsyrSvI8gu0Uy/czVhZ9L/NkkHH+hj2hN0LXZm7YA7qWQlzwpnrEcctNdcL7rOP5V pe4A== 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:in-reply-to:user-agent; bh=DahHECUK2AF1J12XBLs2bKF9FH9rjTuzkU7KMfzCZKg=; b=BONVLP6bYvHgtpKvof+1Dt98WtpjTWbXMEy2PJo1Eedgc9tbBevslf9rIjo/59ZU/Y 4YtGjuycZD6iv+fP+liG4p/XFR3dBAJeWSo8GvSc7xwOrp+q+KnCagPKXko0TOGy2zuK ZEmyR740pDcVKW/tK520DcSi+1UjVdz2dwxAlYUkFEcCRC4VBcfGD5NEZKwqxpOcl0mf k5QAWJi5sM8onnSZLTYfhxjCNsRsew9dukvkshLU4RzBy/H6GJatlqjMdeWemYYUqU/a fdWdrHpIvopEv3L/gbxfutQLlFkZiOVF+vQeVkMZfeYePQABYEjL246t2GtO2ly1CD5H EapQ== X-Gm-Message-State: APjAAAWqxPfc7Va2EFI3rd521rwT3r+tgSt8ZU5yDZc3vANmK+GmPj6z pAJw2IbZ282WNfYaxLilYmnWKw== X-Google-Smtp-Source: APXvYqxCsu1nKhbS62harFEpfvgZRg7+s7a4b8LII1alkH5yuzE7xzETotobVwglIakCJJls3jgHwg== X-Received: by 2002:a02:9381:: with SMTP id z1mr24471487jah.130.1558622256966; Thu, 23 May 2019 07:37:36 -0700 (PDT) Received: from brauner.io ([172.56.12.187]) by smtp.gmail.com with ESMTPSA id l186sm4603784itb.5.2019.05.23.07.37.29 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Thu, 23 May 2019 07:37:36 -0700 (PDT) Date: Thu, 23 May 2019 16:37:26 +0200 From: Christian Brauner To: Jann Horn Cc: Oleg Nesterov , Al Viro , kernel list , linux-fsdevel , Linux API , Linus Torvalds , Florian Weimer , Thomas Gleixner , Arnd Bergmann , Shuah Khan , David Howells , Todd Kjos , "Dmitry V. Levin" , Miklos Szeredi , 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 , linux-sh@vger.kernel.org, sparclinux@vger.kernel.org, linux-xtensa@linux-xtensa.org, linux-arch , "open list:KERNEL SELFTEST FRAMEWORK" , the arch/x86 maintainers Subject: Re: [PATCH v1 1/2] open: add close_range() Message-ID: <20190523143725.y67czx4jxsy6yqrj@brauner.io> References: <20190522155259.11174-1-christian@brauner.io> <20190522165737.GC4915@redhat.com> <20190523115118.pmscbd6kaqy37dym@brauner.io> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: User-Agent: NeoMutt/20180716 Sender: linux-mips-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-mips@vger.kernel.org On Thu, May 23, 2019 at 04:32:14PM +0200, Jann Horn wrote: > On Thu, May 23, 2019 at 1:51 PM Christian Brauner wrote: > [...] > > I kept it dumb and was about to reply that your solution introduces more > > code when it seemed we wanted to keep this very simple for now. > > But then I saw that find_next_opened_fd() already exists as > > find_next_fd(). So it's actually not bad compared to what I sent in v1. > > So - with some small tweaks (need to test it and all now) - how do we > > feel about?: > [...] > > static int __close_next_open_fd(struct files_struct *files, unsigned *curfd, unsigned maxfd) > > { > > struct file *file = NULL; > > unsigned fd; > > struct fdtable *fdt; > > > > spin_lock(&files->file_lock); > > fdt = files_fdtable(files); > > fd = find_next_fd(fdt, *curfd); > > find_next_fd() finds free fds, not used ones. > > > if (fd >= fdt->max_fds || fd > maxfd) > > goto out_unlock; > > > > file = fdt->fd[fd]; > > rcu_assign_pointer(fdt->fd[fd], NULL); > > __put_unused_fd(files, fd); > > You can't do __put_unused_fd() if the old pointer in fdt->fd[fd] was > NULL - because that means that the fd has been reserved by another > thread that is about to put a file pointer in there, and if you > release the fd here, that messes up the refcounting (or hits the > BUG_ON() in __fd_install()). > > > out_unlock: > > spin_unlock(&files->file_lock); > > > > if (!file) > > return -EBADF; > > > > *curfd = fd; > > filp_close(file, files); > > return 0; > > } > > > > int __close_range(struct files_struct *files, unsigned fd, unsigned max_fd) > > { > > if (fd > max_fd) > > return -EINVAL; > > > > while (fd <= max_fd) { > > Note that with a pattern like this, you have to be careful about what > happens if someone gives you max_fd==0xffffffff - then this condition > is always true and the loop can not terminate this way. > > > if (__close_next_fd(files, &fd, maxfd)) > > break; > > (obviously it can still terminate this way) Yup, this was only a quick draft. I think the dumb simple thing that I did before was the best way to do it for now. I first thought that the find_next_open_fd() function already exists but when I went to write a POC for testing realized it doesn't anyway.