From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751720AbdJFHAk (ORCPT ); Fri, 6 Oct 2017 03:00:40 -0400 Received: from bombadil.infradead.org ([65.50.211.133]:33512 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750883AbdJFHAj (ORCPT ); Fri, 6 Oct 2017 03:00:39 -0400 Date: Fri, 6 Oct 2017 00:00:38 -0700 From: Christoph Hellwig To: Nicolas Pitre Cc: Alexander Viro , Christoph Hellwig , linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-embedded@vger.kernel.org, linux-kernel@vger.kernel.org, Chris Brandt Subject: Re: [PATCH v5 4/5] cramfs: add mmap support Message-ID: <20171006070038.GA29142@infradead.org> References: <20171006024531.8885-1-nicolas.pitre@linaro.org> <20171006024531.8885-5-nicolas.pitre@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20171006024531.8885-5-nicolas.pitre@linaro.org> User-Agent: Mutt/1.8.3 (2017-05-23) X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org > + /* Don't map the last page if it contains some other data */ > + if (unlikely(pgoff + pages == max_pages)) { > + unsigned int partial = offset_in_page(inode->i_size); > + if (partial) { > + char *data = sbi->linear_virt_addr + offset; > + data += (max_pages - 1) * PAGE_SIZE + partial; > + if (memchr_inv(data, 0, PAGE_SIZE - partial) != NULL) { > + pr_debug("mmap: %s: last page is shared\n", > + file_dentry(file)->d_name.name); > + pages--; > + } > + } > + } Why is pgoff + pages == max_pages marked unlikely? Mapping the whole file seems like a perfectly normal and likely case to me.. Also if this was my code I'd really prefer to move this into a helper: static bool cramfs_mmap_last_page_is_shared(struct inode *inode, int offset) { unsigned int partial = offset_in_page(inode->i_size); char *data = CRAMFS_SB(inode->i_sb)->linear_virt_addr + offset + (inode->i_size & PAGE_MASK); return memchr_inv(data + partial, 0, PAGE_SIZE - partial); } if (pgoff + pages == max_pages && offset_in_page(inode->i_size) && cramfs_mmap_last_page_is_shared(inode, offset)) pages--; as that's much more readable and the function name provides a good documentation of what is going on. > + if (pages != vma_pages(vma)) { here is how I would turn this around: if (!pages) goto done; if (pages == vma_pages(vma)) { remap_pfn_range(); goto done; } ... for (i = 0; i < pages; i++) { ... vm_insert_mixed(); nr_mapped++; } done: pr_debug("mapped %d out ouf %d\n", ..); if (pages != vma_pages(vma)) vma->vm_ops = &generic_file_vm_ops; return 0; } In fact we probably could just set the vm_ops unconditionally, they just wouldn't be called, but that might be more confusing then helpful. From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Date: Fri, 6 Oct 2017 00:00:38 -0700 From: Christoph Hellwig To: Nicolas Pitre Cc: Alexander Viro , Christoph Hellwig , linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-embedded@vger.kernel.org, linux-kernel@vger.kernel.org, Chris Brandt Subject: Re: [PATCH v5 4/5] cramfs: add mmap support Message-ID: <20171006070038.GA29142@infradead.org> References: <20171006024531.8885-1-nicolas.pitre@linaro.org> <20171006024531.8885-5-nicolas.pitre@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20171006024531.8885-5-nicolas.pitre@linaro.org> Sender: owner-linux-mm@kvack.org List-ID: > + /* Don't map the last page if it contains some other data */ > + if (unlikely(pgoff + pages == max_pages)) { > + unsigned int partial = offset_in_page(inode->i_size); > + if (partial) { > + char *data = sbi->linear_virt_addr + offset; > + data += (max_pages - 1) * PAGE_SIZE + partial; > + if (memchr_inv(data, 0, PAGE_SIZE - partial) != NULL) { > + pr_debug("mmap: %s: last page is shared\n", > + file_dentry(file)->d_name.name); > + pages--; > + } > + } > + } Why is pgoff + pages == max_pages marked unlikely? Mapping the whole file seems like a perfectly normal and likely case to me.. Also if this was my code I'd really prefer to move this into a helper: static bool cramfs_mmap_last_page_is_shared(struct inode *inode, int offset) { unsigned int partial = offset_in_page(inode->i_size); char *data = CRAMFS_SB(inode->i_sb)->linear_virt_addr + offset + (inode->i_size & PAGE_MASK); return memchr_inv(data + partial, 0, PAGE_SIZE - partial); } if (pgoff + pages == max_pages && offset_in_page(inode->i_size) && cramfs_mmap_last_page_is_shared(inode, offset)) pages--; as that's much more readable and the function name provides a good documentation of what is going on. > + if (pages != vma_pages(vma)) { here is how I would turn this around: if (!pages) goto done; if (pages == vma_pages(vma)) { remap_pfn_range(); goto done; } ... for (i = 0; i < pages; i++) { ... vm_insert_mixed(); nr_mapped++; } done: pr_debug("mapped %d out ouf %d\n", ..); if (pages != vma_pages(vma)) vma->vm_ops = &generic_file_vm_ops; return 0; } In fact we probably could just set the vm_ops unconditionally, they just wouldn't be called, but that might be more confusing then helpful. -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to majordomo@kvack.org. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: email@kvack.org