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.0 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, T_DKIMWL_WL_HIGH,URIBL_BLOCKED,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 AA7EAC282E1 for ; Thu, 23 May 2019 14:15:11 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 77B3220863 for ; Thu, 23 May 2019 14:15:11 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="JRD2eCXb"; dkim=fail reason="signature verification failed" (2048-bit key) header.d=brauner.io header.i=@brauner.io header.b="agfu05mc" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 77B3220863 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=brauner.io Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+infradead-linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References: Message-ID:Subject:To:From:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=EpxSH/plML1XTY3ssm8gsY/m68r6ZbcwlYpx3ve00wY=; b=JRD2eCXbAWJ4CL UJfLesCAWqY0baa3pz/4fz588NKm0msj233fSb2cjx26pBo4ZPxAQHvR2PtI57zjJYqzzA4F13v76 krQ71p8dlITA42bOpYi7nRtBHbBS3M4aibyO5XfUtqncEedZnUgKd2/C2qb6ncLATng/Kh3jwpMXW E01vPmbWp9/XbmI0j9SOP3UiMoy+cKjPp8hnr6gaXeMKrnDWmqFSfWVMSsTOVq1gHd3G1Segfgt9A iMrXZjIPuJpfGO8Cc+c/c1cI8jYjpujS++a0iPkFTZdGgOnCsEjta4Ty1uJX6UgosciEDhvzc0fSO u+PVT6yZlitsbHFLKaxQ==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1hToUr-0003md-AP; Thu, 23 May 2019 14:15:05 +0000 Received: from mail-it1-x142.google.com ([2607:f8b0:4864:20::142]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1hToUn-0003LX-IZ for linux-arm-kernel@lists.infradead.org; Thu, 23 May 2019 14:15:03 +0000 Received: by mail-it1-x142.google.com with SMTP id m140so8739615itg.2 for ; Thu, 23 May 2019 07:15:00 -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=rgSvHJdgJSnF7/xiB9jp3IYiH1RwVXYwyOL2WgM6elM=; b=agfu05mcW6DVn/laHbq7o5peWWMubiiXLa3+mtVVF+i5l0QEt93/bvKWl3JbPoVwXl 0k64ioj3Ph2BkMXRRbAHKAFfkxjOHDSRt5NCBWr2asYnRnNKZL4zjdQz3kH7Jqukj0z6 2og5izGp8YNUen+yStzQvEMwIJdLGCCPJJnwa/rcJXm71w0kxDdqtXnMzoicLjGDEAWi ZhH0AtrgRENAHSCoFkVjHZZZaCx513l14vIozc4GzDXmosIFXifqXJPpo1kJBs9fbYoH SAsp9lAUDI9bdzdzBP2B3VcM+Tv5WeTYZNB/it3XGBVA35In6f74kJj0SxBCXK8Oqary LS0w== 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=rgSvHJdgJSnF7/xiB9jp3IYiH1RwVXYwyOL2WgM6elM=; b=maMZOkoSV0Wmz5xQ6Hixh6N87GTXH0KpkJlkUp8T8o0RSePI2EARZdJZk/aWCJr3wV ikbuca53LLTwmqjmW2FlNdIFb2i3yhYN4p8nDDpSxQ87TXMHN+B+n8bkHPiysZ58vNxi +bldfvPa1y9+jJ/P7lWDGKDWrV7puWcGVgzURO73QuGjWHSIDIHvrXE1j0RuezZ7NsR8 IbiOZeNqHZUN933Kdl98XbLyRPzGbFRXucWaMpvZe5/BvlzZnHyxjLFCwJrAjb7p57AC Hty+TSARevWuj9j5uAarBWB3kkSO5EsmbqKFBjcNv1uSJQ99xcOLet5Ar2dnt0fZEXbf TURQ== X-Gm-Message-State: APjAAAWOEwZogz3WDAGB5a2CDso3FRoMCR6YoO8V6W7DpO+bQAd1Fs1p BOvnVE+lfz1dCTwCTX9n2h89BA== X-Google-Smtp-Source: APXvYqy9XAMrNMkQarBeYE8e7HlYFHTwYh86KxFNh1Z1HbzreF/S6QYjLd8wwHWfkp2KJKFiXZBbVg== X-Received: by 2002:a24:590f:: with SMTP id p15mr12892482itb.12.1558620899772; Thu, 23 May 2019 07:14:59 -0700 (PDT) Received: from brauner.io ([172.56.12.187]) by smtp.gmail.com with ESMTPSA id q142sm4141521itb.17.2019.05.23.07.14.51 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Thu, 23 May 2019 07:14:58 -0700 (PDT) Date: Thu, 23 May 2019 16:14:48 +0200 From: Christian Brauner To: Oleg Nesterov Subject: Re: [PATCH v1 1/2] open: add close_range() Message-ID: <20190523141447.34s3kc3fuwmoeq7n@brauner.io> References: <20190522155259.11174-1-christian@brauner.io> <20190522165737.GC4915@redhat.com> <20190523115118.pmscbd6kaqy37dym@brauner.io> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20190523115118.pmscbd6kaqy37dym@brauner.io> User-Agent: NeoMutt/20180716 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20190523_071501_964601_1610C6B6 X-CRM114-Status: GOOD ( 14.93 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-ia64@vger.kernel.org, linux-sh@vger.kernel.org, ldv@altlinux.org, dhowells@redhat.com, linux-kselftest@vger.kernel.org, sparclinux@vger.kernel.org, shuah@kernel.org, linux-arch@vger.kernel.org, linux-s390@vger.kernel.org, miklos@szeredi.hu, x86@kernel.org, torvalds@linux-foundation.org, linux-mips@vger.kernel.org, linux-xtensa@linux-xtensa.org, tkjos@android.com, arnd@arndb.de, jannh@google.com, linux-m68k@lists.linux-m68k.org, viro@zeniv.linux.org.uk, tglx@linutronix.de, linux-arm-kernel@lists.infradead.org, fweimer@redhat.com, linux-parisc@vger.kernel.org, linux-api@vger.kernel.org, linux-kernel@vger.kernel.org, linux-alpha@vger.kernel.org, linux-fsdevel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+infradead-linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Thu, May 23, 2019 at 01:51:18PM +0200, Christian Brauner wrote: > On Wed, May 22, 2019 at 06:57:37PM +0200, Oleg Nesterov wrote: > > On 05/22, Christian Brauner wrote: > > > > > > +static struct file *pick_file(struct files_struct *files, unsigned fd) > > > { > > > - struct file *file; > > > + struct file *file = NULL; > > > struct fdtable *fdt; > > > > > > spin_lock(&files->file_lock); > > > @@ -632,15 +629,65 @@ int __close_fd(struct files_struct *files, unsigned fd) > > > goto out_unlock; > > > rcu_assign_pointer(fdt->fd[fd], NULL); > > > __put_unused_fd(files, fd); > > > - spin_unlock(&files->file_lock); > > > - return filp_close(file, files); > > > > > > out_unlock: > > > spin_unlock(&files->file_lock); > > > - return -EBADF; > > > + return file; > > > > ... > > > > > +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) { > > > + struct file *file; > > > + > > > + file = pick_file(files, fd++); > > > > Well, how about something like > > > > static unsigned int find_next_opened_fd(struct fdtable *fdt, unsigned start) > > { > > unsigned int maxfd = fdt->max_fds; > > unsigned int maxbit = maxfd / BITS_PER_LONG; > > unsigned int bitbit = start / BITS_PER_LONG; > > > > bitbit = find_next_bit(fdt->full_fds_bits, maxbit, bitbit) * BITS_PER_LONG; > > if (bitbit > maxfd) > > return maxfd; > > if (bitbit > start) > > start = bitbit; > > return find_next_bit(fdt->open_fds, maxfd, start); > > } > > > > > unsigned close_next_fd(struct files_struct *files, unsigned start, unsigned maxfd) > > { > > unsigned fd; > > struct file *file; > > struct fdtable *fdt; > > > > spin_lock(&files->file_lock); > > fdt = files_fdtable(files); > > fd = find_next_opened_fd(fdt, start); > > if (fd >= fdt->max_fds || fd > maxfd) { > > fd = -1; > > goto out; > > } > > > > file = fdt->fd[fd]; > > rcu_assign_pointer(fdt->fd[fd], NULL); > > __put_unused_fd(files, fd); > > out: > > spin_unlock(&files->file_lock); > > > > if (fd == -1u) > > return fd; > > > > filp_close(file, files); > > return fd + 1; > > } > > Thanks, Oleg! > > 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?: That's obviously not correct atm but I'll send out a tweaked version in a bit. Christian _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel