From patchwork Sat Jan 11 00:28:47 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arjun Roy X-Patchwork-Id: 11328623 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D608E138C for ; Sat, 11 Jan 2020 00:29:13 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id A26A02082E for ; Sat, 11 Jan 2020 00:29:13 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="gk7kGspB" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org A26A02082E Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id B04D38E0006; Fri, 10 Jan 2020 19:29:12 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id A8D898E0001; Fri, 10 Jan 2020 19:29:12 -0500 (EST) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 97D028E0006; Fri, 10 Jan 2020 19:29:12 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0022.hostedemail.com [216.40.44.22]) by kanga.kvack.org (Postfix) with ESMTP id 7F37F8E0001 for ; Fri, 10 Jan 2020 19:29:12 -0500 (EST) Received: from smtpin29.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with SMTP id 33F458248041 for ; Sat, 11 Jan 2020 00:29:12 +0000 (UTC) X-FDA: 76363468944.29.drain10_5048fbd5e831e X-Spam-Summary: 2,0,0,83096be27134a47b,d41d8cd98f00b204,arjunroy.kdev@gmail.com,:davem@davemloft.net:netdev@vger.kernel.org:akpm@linux-foundation.org::arjunroy@google.com:arjunroy.kdev@gmail.com:edumazet@google.com:soheil@google.com,RULES_HIT:41:69:355:379:541:800:960:973:988:989:1260:1311:1314:1345:1431:1437:1515:1535:1542:1711:1730:1747:1777:1792:2393:2559:2562:3138:3139:3140:3141:3142:3353:3865:3866:3867:3871:4321:5007:6119:6261:6653:7514:8957:9413:9592:10004:11026:11658:11914:12043:12048:12291:12296:12297:12438:12517:12519:12555:12679:12683:12895:12986:13161:13229:13255:13894:14096:14181:14394:14687:14721:21080:21444:21451:21627:21666:21795:21990:30051:30054,0,RBL:209.85.210.194:@gmail.com:.lbl8.mailshell.net-62.50.0.100 66.100.201.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:24,LUA_SUMMARY:none X-HE-Tag: drain10_5048fbd5e831e X-Filterd-Recvd-Size: 5410 Received: from mail-pf1-f194.google.com (mail-pf1-f194.google.com [209.85.210.194]) by imf17.hostedemail.com (Postfix) with ESMTP for ; Sat, 11 Jan 2020 00:29:11 +0000 (UTC) Received: by mail-pf1-f194.google.com with SMTP id x6so1906073pfo.10 for ; Fri, 10 Jan 2020 16:29:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=Z1oljhl2ycEHZWbLZ+RzzjjF8KtGlR4IWad6y7qdb94=; b=gk7kGspB2snM/wkshYVIwZq62scQn7AiKzZqmvDmE1eeD9zllq+nCsUnqYZrxYgXw0 rcSwqB0u7BZjt+oW0LeFqWCb8bZ1P7y2M2RR7zH4hRm2v9KnNZkL9G9ghJ5koK9wIYHF lWEkV3AFMrjLVWJ4rOYZBwAMZDI9rcsi8GwOYN3cw+CTjrN5MQntIrhKTYFKOz+4iyTg eXpk3HeNyZsorkEaykObyFQD2UgT7ETnr7gi3yI/K3MLTn6684Mg1av9TEglTLz9v8Na uRM7f/nctTSBp05yhpJ0rRcwZ9uibtojWeIio/RuGFQ9aSfViKebG8yJR0uCMEd8sT0f eNhA== 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:mime-version :content-transfer-encoding; bh=Z1oljhl2ycEHZWbLZ+RzzjjF8KtGlR4IWad6y7qdb94=; b=pboGQN9nmANA5vaV/idaiqKE8yuWKLMtSWW8GnASM+cLRbXE2UMSR+lnRlL22/X15Q JwRu5/VeUQj18dgeq63b/D/ISBtvxFttQhskqxaITunzl3RExhgQwFkiqgMElzaTCtkT 2V7ulxDO5HwBzhLCXBIxNOF7lW/kONZ4Mr2sumQ8hfPJV+UK6hpo7vB1t+MkL37f0ZYb OWa0xTUEi1cvcgtQHODhtC9h9BYcysUvYqBxZdVfI5llGobucK8iiTuU25TY2iz901oN /39G8GDzy4J1NyjNkwItCdnkiU6exfNhe0pfi7B4u/sHkPId/rr3r5EKI9YQNovnTs2S HEng== X-Gm-Message-State: APjAAAVDfpXRc/xtAy5Efch7xfoz0pmx6i0rZdsVFCcPfcfYmm0lehBt xeuJH/Z2Szb09+2bV7lS/VU= X-Google-Smtp-Source: APXvYqxlDo/HhhLT6RpUNEhLbMmrYm1u2/xqNvHPplpw3e/JAZOWpZA1TSDoKgYjE197YNwwCTRNpQ== X-Received: by 2002:a62:62c4:: with SMTP id w187mr7300329pfb.216.1578702550723; Fri, 10 Jan 2020 16:29:10 -0800 (PST) Received: from phantasmagoria.svl.corp.google.com ([2620:15c:2c4:201:2b0a:8c1:6a84:1aa0]) by smtp.gmail.com with ESMTPSA id h126sm4567066pfe.19.2020.01.10.16.29.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Jan 2020 16:29:10 -0800 (PST) From: Arjun Roy To: davem@davemloft.net, netdev@vger.kernel.org, akpm@linux-foundation.org, linux-mm@kvack.org Cc: arjunroy@google.com, Arjun Roy , Eric Dumazet , Soheil Hassas Yeganeh Subject: [PATCH mm,net-next 1/3] mm: Refactor insert_page to prepare for batched-lock insert. Date: Fri, 10 Jan 2020 16:28:47 -0800 Message-Id: <20200111002849.252704-1-arjunroy.kdev@gmail.com> X-Mailer: git-send-email 2.25.0.rc1.283.g88dfdc4193-goog MIME-Version: 1.0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Add helper methods for vm_insert_page()/insert_page() to prepare for vm_insert_pages(), which batch-inserts pages to reduce spinlock operations when inserting multiple consecutive pages into the user page table. The intention of this patch-set is to reduce atomic ops for tcp zerocopy receives, which normally hits the same spinlock multiple times consecutively. Signed-off-by: Arjun Roy Signed-off-by: Eric Dumazet Signed-off-by: Soheil Hassas Yeganeh --- mm/memory.c | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index 7c6e19bdc428..7e23a9275386 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -1693,6 +1693,27 @@ pte_t *__get_locked_pte(struct mm_struct *mm, unsigned long addr, return pte_alloc_map_lock(mm, pmd, addr, ptl); } +static int validate_page_before_insert(struct page *page) +{ + if (PageAnon(page) || PageSlab(page)) + return -EINVAL; + flush_dcache_page(page); + return 0; +} + +static int insert_page_into_pte_locked(struct mm_struct *mm, pte_t *pte, + unsigned long addr, struct page *page, pgprot_t prot) +{ + if (!pte_none(*pte)) + return -EBUSY; + /* Ok, finally just insert the thing.. */ + get_page(page); + inc_mm_counter_fast(mm, mm_counter_file(page)); + page_add_file_rmap(page, false); + set_pte_at(mm, addr, pte, mk_pte(page, prot)); + return 0; +} + /* * This is the old fallback for page remapping. * @@ -1708,28 +1729,14 @@ static int insert_page(struct vm_area_struct *vma, unsigned long addr, pte_t *pte; spinlock_t *ptl; - retval = -EINVAL; - if (PageAnon(page) || PageSlab(page)) + retval = validate_page_before_insert(page); + if (retval) goto out; retval = -ENOMEM; - flush_dcache_page(page); pte = get_locked_pte(mm, addr, &ptl); if (!pte) goto out; - retval = -EBUSY; - if (!pte_none(*pte)) - goto out_unlock; - - /* Ok, finally just insert the thing.. */ - get_page(page); - inc_mm_counter_fast(mm, mm_counter_file(page)); - page_add_file_rmap(page, false); - set_pte_at(mm, addr, pte, mk_pte(page, prot)); - - retval = 0; - pte_unmap_unlock(pte, ptl); - return retval; -out_unlock: + retval = insert_page_into_pte_locked(mm, pte, addr, page, prot); pte_unmap_unlock(pte, ptl); out: return retval; From patchwork Sat Jan 11 00:28:48 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arjun Roy X-Patchwork-Id: 11328625 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 8A3331398 for ; Sat, 11 Jan 2020 00:29:22 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 4D1B820838 for ; Sat, 11 Jan 2020 00:29:22 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="SqtEzNNs" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 4D1B820838 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 8BAF38E0007; Fri, 10 Jan 2020 19:29:21 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 8437F8E0001; Fri, 10 Jan 2020 19:29:21 -0500 (EST) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 732588E0007; Fri, 10 Jan 2020 19:29:21 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0046.hostedemail.com [216.40.44.46]) by kanga.kvack.org (Postfix) with ESMTP id 5CEB58E0001 for ; Fri, 10 Jan 2020 19:29:21 -0500 (EST) Received: from smtpin04.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with SMTP id 1291C2C81 for ; Sat, 11 Jan 2020 00:29:21 +0000 (UTC) X-FDA: 76363469322.04.wound81_51926237b0b3f X-Spam-Summary: 20,1.5,0,286bbbd72fb472ce,d41d8cd98f00b204,arjunroy.kdev@gmail.com,:davem@davemloft.net:netdev@vger.kernel.org:akpm@linux-foundation.org::arjunroy@google.com:arjunroy.kdev@gmail.com:edumazet@google.com:soheil@google.com,RULES_HIT:2:41:355:379:541:800:960:973:988:989:1260:1311:1314:1345:1359:1431:1437:1515:1535:1605:1606:1730:1747:1777:1792:2393:2553:2559:2562:2740:2890:3138:3139:3140:3141:3142:3865:3866:3867:3870:3871:3872:3874:4042:4119:4250:4321:4605:5007:6119:6261:6653:7514:7875:7903:8603:9413:10009:11026:11473:11658:11914:12043:12048:12291:12296:12297:12438:12517:12519:12555:12683:12895:12986:13161:13229:13894:14096:14394:14687:21080:21324:21444:21450:21451:21627:21666:21795:21990:30001:30003:30034:30051:30054:30070:30090,0,RBL:209.85.215.194:@gmail.com:.lbl8.mailshell.net-62.18.0.100 66.100.201.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:neutral,Custom_rules:0:1:0,LFtime:25,LUA_SUMMARY:none X-HE-Tag: wound81_51926237b0b3f X-Filterd-Recvd-Size: 8777 Received: from mail-pg1-f194.google.com (mail-pg1-f194.google.com [209.85.215.194]) by imf35.hostedemail.com (Postfix) with ESMTP for ; Sat, 11 Jan 2020 00:29:20 +0000 (UTC) Received: by mail-pg1-f194.google.com with SMTP id l24so1763959pgk.2 for ; Fri, 10 Jan 2020 16:29:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Gi5VusVvvj1EAGHsvSusLtofyqlxnWkFGPP+dJke9XY=; b=SqtEzNNsDIZw16bVAUxP1SUzCkG88m9GcNZEN8KQfhxn80iribMRCwxrHYco8IEP5c Mc3ABV1u5n/mWyo3PRxwlO/TfH45BQnPKoD0MWVUKLbXsNpWAIlg85o4DK3/XFoLHbME 8Ae4HfB8EenYqpxAzOQxOb4OHn4aPSUAm+Qy4uv/AUIULvtgcmV+I+Y4pU6je1HzQ7ax ZYHk9BFwFLr3W+6XN8Ud+nNdwsfsdcXYLCU+W717xirVTp9TOohDjR0YXDMUlpbPGLPs UYM1cgo3NYCC+XK8gtWk3KRfN3VKmQvW4oBeIwr/+NJDbDEaTSIpvmLk/2/+csppyLHN Fq7Q== 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:mime-version:content-transfer-encoding; bh=Gi5VusVvvj1EAGHsvSusLtofyqlxnWkFGPP+dJke9XY=; b=IeTY5gHxwG8hTGHa1maCIWw9kBrU7TKHJgVmIWfHByyCYuUxZZe228Uve0pWp7rzdW 125V8Dwel1jeLP31vrsOwZTc0BQCZe5rmEsiLUpfP5cOgzCqBYcrUjUkTaLhdq3N/4Fi uTu4QcNu317raa0xJy7oKxudW0ZzjINXnDAi8UtlNu0zRQUaGT2RypbC4l62rowawyOq o28r8p4pF5ZWOm0McxH8M7Gvq1dspwESWQUKozbEd5mx6KY2kKifrNm2QbxQqQOie/6k 5SJ9J5aJ2m7vcBOYmIEqlZpJa1S1KNv8vPF/J5OdoN1I0bMetwWxEpDt/8x48KWh84DX 5qaA== X-Gm-Message-State: APjAAAVqrL7VpHsNkcZjj2b05S+9WgismDXuxYkRGlDLnbVLwmKzBn66 /MOn5Dw0XyQtsddIBjvdNK4= X-Google-Smtp-Source: APXvYqzWZFgzucfT1bTpxXTNs41dByCko8XkdEtOTPH7RA2v77uF13CiFCi1KRDmkLOv4OSLHWHEDw== X-Received: by 2002:a63:dc0d:: with SMTP id s13mr7396134pgg.129.1578702559505; Fri, 10 Jan 2020 16:29:19 -0800 (PST) Received: from phantasmagoria.svl.corp.google.com ([2620:15c:2c4:201:2b0a:8c1:6a84:1aa0]) by smtp.gmail.com with ESMTPSA id h126sm4567066pfe.19.2020.01.10.16.29.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Jan 2020 16:29:19 -0800 (PST) From: Arjun Roy To: davem@davemloft.net, netdev@vger.kernel.org, akpm@linux-foundation.org, linux-mm@kvack.org Cc: arjunroy@google.com, Arjun Roy , Eric Dumazet , Soheil Hassas Yeganeh Subject: [PATCH mm,net-next 2/3] mm: Add vm_insert_pages(). Date: Fri, 10 Jan 2020 16:28:48 -0800 Message-Id: <20200111002849.252704-2-arjunroy.kdev@gmail.com> X-Mailer: git-send-email 2.25.0.rc1.283.g88dfdc4193-goog In-Reply-To: <20200111002849.252704-1-arjunroy.kdev@gmail.com> References: <20200111002849.252704-1-arjunroy.kdev@gmail.com> MIME-Version: 1.0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Add the ability to insert multiple pages at once to a user VM with lower PTE spinlock operations. The intention of this patch-set is to reduce atomic ops for tcp zerocopy receives, which normally hits the same spinlock multiple times consecutively. Signed-off-by: Arjun Roy Signed-off-by: Eric Dumazet Signed-off-by: Soheil Hassas Yeganeh --- include/linux/mm.h | 2 + mm/memory.c | 111 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 111 insertions(+), 2 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 29c928ba6b42..a3ac40fbe8fd 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2563,6 +2563,8 @@ struct vm_area_struct *find_extend_vma(struct mm_struct *, unsigned long addr); int remap_pfn_range(struct vm_area_struct *, unsigned long addr, unsigned long pfn, unsigned long size, pgprot_t); int vm_insert_page(struct vm_area_struct *, unsigned long addr, struct page *); +int vm_insert_pages(struct vm_area_struct *vma, unsigned long addr, + struct page **pages, unsigned long *num); int vm_insert_pfn(struct vm_area_struct *vma, unsigned long addr, unsigned long pfn); int vm_insert_pfn_prot(struct vm_area_struct *vma, unsigned long addr, diff --git a/mm/memory.c b/mm/memory.c index 7e23a9275386..1655c5feaf32 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -1670,8 +1670,7 @@ int zap_vma_ptes(struct vm_area_struct *vma, unsigned long address, } EXPORT_SYMBOL_GPL(zap_vma_ptes); -pte_t *__get_locked_pte(struct mm_struct *mm, unsigned long addr, - spinlock_t **ptl) +static pmd_t *walk_to_pmd(struct mm_struct *mm, unsigned long addr) { pgd_t *pgd; p4d_t *p4d; @@ -1690,6 +1689,16 @@ pte_t *__get_locked_pte(struct mm_struct *mm, unsigned long addr, return NULL; VM_BUG_ON(pmd_trans_huge(*pmd)); + return pmd; +} + +pte_t *__get_locked_pte(struct mm_struct *mm, unsigned long addr, + spinlock_t **ptl) +{ + pmd_t *pmd = walk_to_pmd(mm, addr); + + if (!pmd) + return NULL; return pte_alloc_map_lock(mm, pmd, addr, ptl); } @@ -1714,6 +1723,15 @@ static int insert_page_into_pte_locked(struct mm_struct *mm, pte_t *pte, return 0; } +static int insert_page_in_batch_locked(struct mm_struct *mm, pmd_t *pmd, + unsigned long addr, struct page *page, pgprot_t prot) +{ + const int err = validate_page_before_insert(page); + + return err ? err : insert_page_into_pte_locked( + mm, pte_offset_map(pmd, addr), addr, page, prot); +} + /* * This is the old fallback for page remapping. * @@ -1742,6 +1760,95 @@ static int insert_page(struct vm_area_struct *vma, unsigned long addr, return retval; } +/* insert_pages() amortizes the cost of spinlock operations + * when inserting pages in a loop. + */ +static int insert_pages(struct vm_area_struct *vma, unsigned long addr, + struct page **pages, unsigned long *num, pgprot_t prot) +{ + pmd_t *pmd = NULL; + spinlock_t *pte_lock = NULL; + struct mm_struct *const mm = vma->vm_mm; + unsigned long curr_page_idx = 0; + unsigned long remaining_pages_total = *num; + unsigned long pages_to_write_in_pmd; + int ret; +more: + ret = -EFAULT; + pmd = walk_to_pmd(mm, addr); + if (!pmd) + goto out; + + pages_to_write_in_pmd = min_t(unsigned long, + remaining_pages_total, PTRS_PER_PTE - pte_index(addr)); + + /* Allocate the PTE if necessary; takes PMD lock once only. */ + ret = -ENOMEM; + if (pte_alloc(mm, pmd, addr)) + goto out; + pte_lock = pte_lockptr(mm, pmd); + + while (pages_to_write_in_pmd) { + int pte_idx = 0; + const int batch_size = min_t(int, pages_to_write_in_pmd, 8); + + spin_lock(pte_lock); + for (; pte_idx < batch_size; ++pte_idx) { + int err = insert_page_in_batch_locked(mm, pmd, + addr, pages[curr_page_idx], prot); + if (unlikely(err)) { + spin_unlock(pte_lock); + ret = err; + remaining_pages_total -= pte_idx; + goto out; + } + addr += PAGE_SIZE; + ++curr_page_idx; + } + spin_unlock(pte_lock); + pages_to_write_in_pmd -= batch_size; + remaining_pages_total -= batch_size; + } + if (remaining_pages_total) + goto more; + ret = 0; +out: + *num = remaining_pages_total; + return ret; +} + +/** + * vm_insert_pages - insert multiple pages into user vma, batching the pmd lock. + * @vma: user vma to map to + * @addr: target start user address of these pages + * @pages: source kernel pages + * @num: in: number of pages to map. out: number of pages that were *not* + * mapped. (0 means all pages were successfully mapped). + * + * Preferred over vm_insert_page() when inserting multiple pages. + * + * In case of error, we may have mapped a subset of the provided + * pages. It is the caller's responsibility to account for this case. + * + * The same restrictions apply as in vm_insert_page(). + */ +int vm_insert_pages(struct vm_area_struct *vma, unsigned long addr, + struct page **pages, unsigned long *num) +{ + const unsigned long end_addr = addr + (*num * PAGE_SIZE) - 1; + + if (addr < vma->vm_start || end_addr >= vma->vm_end) + return -EFAULT; + if (!(vma->vm_flags & VM_MIXEDMAP)) { + BUG_ON(down_read_trylock(&vma->vm_mm->mmap_sem)); + BUG_ON(vma->vm_flags & VM_PFNMAP); + vma->vm_flags |= VM_MIXEDMAP; + } + /* Defer page refcount checking till we're about to map that page. */ + return insert_pages(vma, addr, pages, num, vma->vm_page_prot); +} +EXPORT_SYMBOL(vm_insert_pages); + /** * vm_insert_page - insert single page into user vma * @vma: user vma to map to From patchwork Sat Jan 11 00:28:49 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arjun Roy X-Patchwork-Id: 11328627 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 272461398 for ; Sat, 11 Jan 2020 00:29:26 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id DE3152082E for ; Sat, 11 Jan 2020 00:29:25 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="oitzAvxb" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org DE3152082E Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 0CBA38E0008; Fri, 10 Jan 2020 19:29:25 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 054528E0001; Fri, 10 Jan 2020 19:29:25 -0500 (EST) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E5F5D8E0008; Fri, 10 Jan 2020 19:29:24 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0076.hostedemail.com [216.40.44.76]) by kanga.kvack.org (Postfix) with ESMTP id CEFD68E0001 for ; Fri, 10 Jan 2020 19:29:24 -0500 (EST) Received: from smtpin19.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with SMTP id 72DEE181AEF00 for ; Sat, 11 Jan 2020 00:29:24 +0000 (UTC) X-FDA: 76363469448.19.beam72_521239a127231 X-Spam-Summary: 2,0,0,b5e2b44d9703519e,d41d8cd98f00b204,arjunroy.kdev@gmail.com,:davem@davemloft.net:netdev@vger.kernel.org:akpm@linux-foundation.org::arjunroy@google.com:arjunroy.kdev@gmail.com:edumazet@google.com:soheil@google.com,RULES_HIT:41:355:379:541:800:960:973:988:989:1260:1311:1314:1345:1359:1431:1437:1515:1535:1544:1711:1730:1747:1777:1792:2393:2553:2559:2562:3138:3139:3140:3141:3142:3355:3369:3865:3866:3867:3870:3871:3872:4118:4250:4321:5007:6119:6120:6261:6653:7514:7875:7901:7903:9413:9592:10004:11026:11473:11658:11914:12043:12048:12109:12291:12296:12297:12438:12517:12519:12555:12679:12683:12895:12986:13146:13161:13229:13230:13255:13894:14181:14394:14687:14721:21080:21444:21451:21627:21666:21990:30003:30045:30054:30090,0,RBL:209.85.215.193:@gmail.com:.lbl8.mailshell.net-62.50.0.100 66.100.201.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:24,LUA_SUMMARY:none X-HE-Tag: beam72_521239a127231 X-Filterd-Recvd-Size: 7144 Received: from mail-pg1-f193.google.com (mail-pg1-f193.google.com [209.85.215.193]) by imf31.hostedemail.com (Postfix) with ESMTP for ; Sat, 11 Jan 2020 00:29:23 +0000 (UTC) Received: by mail-pg1-f193.google.com with SMTP id x7so1744677pgl.11 for ; Fri, 10 Jan 2020 16:29:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=1LDI4J9RjcFLtSkqnZ1wNc4xIuHBN5WiIppT7x73FiY=; b=oitzAvxblJcT6HGYXN37tqOcTuVllVo4oE3LuXvven6wfJ8p78mTXgRCOKE6vhpIrI 5gpd2HOJZTNUmqtS8pKj9qXOsbypMvTLm/krIkJTC6XR7NRJhSBiApNG5QMUtofSvGDC 0+U8Bpm5nyTcHp9nckqbASiV/Wpyw1oNXghPmIoGVh9ylTdiilfHVQSeqSVFzorNop26 TaGUXEa4OyIU1ERTjy8aEcnBGJGhp21SWtKWbqhVZGnjYkw3l2U1htDRjopxKLrkwfCU fsL5KkFLXNYoQn06r3Ph4OOFZFQvKG7I9SYNX3v8E+H/kQnkW5j9OgjW3Iq4ouHE82EX 1OiA== 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:mime-version:content-transfer-encoding; bh=1LDI4J9RjcFLtSkqnZ1wNc4xIuHBN5WiIppT7x73FiY=; b=tjBGDTfwsvsGTqhEHLgSDKgqhfQBHfAEfCsQThEoKSwAZVdm51yCxaM9aT1oU2Dwr1 d0nepNV1jrkLjn0OYgbY4PuLkuLTgfLfogApScpe+4pXPM7zXg9KIDOHMCjVb9e+Om+2 4lpfrmzTMOBLb4CM1QC/5ueaXOxieeN5W10MZ8laDAS5c3yA6TfUUbVm5qCNUDjLpoa0 2MjZFr8SuwS7MLm5y/Vs4Cd3XzJfs+ktmrvPUUXMgapCEFGhjrda+55rTF80lG/jlvPu iDtgOyCni8Qfxm9cE2q8wQBlR8Xc9j78qrIkw7jwkYI2arsZimNcHHwiG0rF/ZfmzuTS 6V6w== X-Gm-Message-State: APjAAAXAw9Bl7xtpLlUxtrtH6gZWMxval57eA+RqtfwHjAcu4FBjIuoU qnfYsBOMAlqp+KczRGSu4+Y= X-Google-Smtp-Source: APXvYqynZPWxev5HGGPaDMT4SeA3bIUYAeFJGYDi8krQcaLK/IaE5Dvc8g5CvUIMBFJWYPXZuzz2rQ== X-Received: by 2002:a63:fe50:: with SMTP id x16mr7557165pgj.31.1578702563076; Fri, 10 Jan 2020 16:29:23 -0800 (PST) Received: from phantasmagoria.svl.corp.google.com ([2620:15c:2c4:201:2b0a:8c1:6a84:1aa0]) by smtp.gmail.com with ESMTPSA id h126sm4567066pfe.19.2020.01.10.16.29.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Jan 2020 16:29:22 -0800 (PST) From: Arjun Roy To: davem@davemloft.net, netdev@vger.kernel.org, akpm@linux-foundation.org, linux-mm@kvack.org Cc: arjunroy@google.com, Arjun Roy , Eric Dumazet , Soheil Hassas Yeganeh Subject: [PATCH mm,net-next 3/3] net-zerocopy: Use vm_insert_pages() for tcp rcv zerocopy. Date: Fri, 10 Jan 2020 16:28:49 -0800 Message-Id: <20200111002849.252704-3-arjunroy.kdev@gmail.com> X-Mailer: git-send-email 2.25.0.rc1.283.g88dfdc4193-goog In-Reply-To: <20200111002849.252704-1-arjunroy.kdev@gmail.com> References: <20200111002849.252704-1-arjunroy.kdev@gmail.com> MIME-Version: 1.0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Use vm_insert_pages() for tcp receive zerocopy. Spin lock cycles (as reported by perf) drop from a couple of percentage points to a fraction of a percent. This results in a roughly 6% increase in efficiency, measured roughly as zerocopy receive count divided by CPU utilization. The intention of this patch-set is to reduce atomic ops for tcp zerocopy receives, which normally hits the same spinlock multiple times consecutively. Signed-off-by: Arjun Roy Signed-off-by: Eric Dumazet Signed-off-by: Soheil Hassas Yeganeh --- net/ipv4/tcp.c | 67 +++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 61 insertions(+), 6 deletions(-) diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c index 34490d972758..52f96c3ceab3 100644 --- a/net/ipv4/tcp.c +++ b/net/ipv4/tcp.c @@ -1861,14 +1861,48 @@ int tcp_mmap(struct file *file, struct socket *sock, } EXPORT_SYMBOL(tcp_mmap); +static int tcp_zerocopy_vm_insert_batch(struct vm_area_struct *vma, + struct page **pages, + unsigned long pages_to_map, + unsigned long *insert_addr, + u32 *length_with_pending, + u32 *seq, + struct tcp_zerocopy_receive *zc) +{ + unsigned long pages_remaining = pages_to_map; + int bytes_mapped; + int ret; + + ret = vm_insert_pages(vma, *insert_addr, pages, &pages_remaining); + bytes_mapped = PAGE_SIZE * (pages_to_map - pages_remaining); + /* Even if vm_insert_pages fails, it may have partially succeeded in + * mapping (some but not all of the pages). + */ + *seq += bytes_mapped; + *insert_addr += bytes_mapped; + if (ret) { + /* But if vm_insert_pages did fail, we have to unroll some state + * we speculatively touched before. + */ + const int bytes_not_mapped = PAGE_SIZE * pages_remaining; + *length_with_pending -= bytes_not_mapped; + zc->recv_skip_hint += bytes_not_mapped; + } + return ret; +} + static int tcp_zerocopy_receive(struct sock *sk, struct tcp_zerocopy_receive *zc) { unsigned long address = (unsigned long)zc->address; u32 length = 0, seq, offset, zap_len; + #define PAGE_BATCH_SIZE 8 + struct page *pages[PAGE_BATCH_SIZE]; const skb_frag_t *frags = NULL; struct vm_area_struct *vma; struct sk_buff *skb = NULL; + unsigned long pg_idx = 0; + unsigned long curr_addr; struct tcp_sock *tp; int inq; int ret; @@ -1901,15 +1935,26 @@ static int tcp_zerocopy_receive(struct sock *sk, zc->recv_skip_hint = zc->length; } ret = 0; + curr_addr = address; while (length + PAGE_SIZE <= zc->length) { if (zc->recv_skip_hint < PAGE_SIZE) { + /* If we're here, finish the current batch. */ + if (pg_idx) { + ret = tcp_zerocopy_vm_insert_batch(vma, pages, + pg_idx, + &curr_addr, + &length, + &seq, zc); + if (ret) + goto out; + pg_idx = 0; + } if (skb) { skb = skb->next; offset = seq - TCP_SKB_CB(skb)->seq; } else { skb = tcp_recv_skb(sk, seq, &offset); } - zc->recv_skip_hint = skb->len - offset; offset -= skb_headlen(skb); if ((int)offset < 0 || skb_has_frag_list(skb)) @@ -1933,14 +1978,24 @@ static int tcp_zerocopy_receive(struct sock *sk, zc->recv_skip_hint -= remaining; break; } - ret = vm_insert_page(vma, address + length, - skb_frag_page(frags)); - if (ret) - break; + pages[pg_idx] = skb_frag_page(frags); + pg_idx++; length += PAGE_SIZE; - seq += PAGE_SIZE; zc->recv_skip_hint -= PAGE_SIZE; frags++; + if (pg_idx == PAGE_BATCH_SIZE) { + ret = tcp_zerocopy_vm_insert_batch(vma, pages, pg_idx, + &curr_addr, &length, + &seq, zc); + if (ret) + goto out; + pg_idx = 0; + } + } + if (pg_idx) { + ret = tcp_zerocopy_vm_insert_batch(vma, pages, pg_idx, + &curr_addr, &length, &seq, + zc); } out: up_read(¤t->mm->mmap_sem);