From patchwork Fri Jan 29 03:05:14 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 373277 Delivered-To: patches@linaro.org Received: by 2002:a02:a60d:0:0:0:0:0 with SMTP id c13csp1807989jam; Thu, 28 Jan 2021 19:05:24 -0800 (PST) X-Received: by 2002:a17:902:744b:b029:e1:2169:6272 with SMTP id e11-20020a170902744bb02900e121696272mr1029668plt.35.1611889523980; Thu, 28 Jan 2021 19:05:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1611889523; cv=none; d=google.com; s=arc-20160816; b=GE59xihQyo3vh2r3DJX4MxqNstD0nlk2Wwdhz9SynuEzFkneWQ5wZfWcU4A5Wq17Hp 9DaLUkIz0my3Y6Dq9rhmP6iV5eYjQj8K9Z6J/P6R+19YIkwQRKYhb7vgNTztf+rCvQoe O5fkpqio6K4YiYdfPiSbQpiteWloDqLvIw5taFIUxh5zk0mI3njX9twCqgv5CbWjXhMt emLzcY4rqDjK1hGxVe+Gqv8wWfnPjpavbvVS6019GLiB92x6Hn7O82BzCf1bu2bVwo/7 cOMUpIgCvRdQv3iwuJdbGDjjfOa3zgOpfdBspbVRm2i+bj1yklhyetsG/z6oKeEOGepa TqHw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=wpJLS1/K6quxDCtF6ZEY+ZhZmpb559yflYmJJ80BQbw=; b=t8BsgJXeT5wdllAv0SQwzNUpF0/J09BLsrvMJJsugke3U8CCQEhDADKLF+tjfNyB/u YinWSUs95YnOOuBHe6nxxzCn9Oojo7N8qTWQIV1tj0VnPyjDdu+NLop46mKvBeFkbV+r FGAAR8nt6mBJuizRDrDNKPRUISVrTZUMVPymknLhrFh3j9J5+KFwUoPdD2LegOj0x8f+ wTrpGawc3rRUFSVZFxUMuKf6z2Ht1DKOAE1W6nFyMt6L7n2d8EUuO4K4V9tFbLrgGGCe RqFrpYmL7Wc5cGbhCnnMiQdLGwVzdUQ+RH7BcPU4SbN0BrCQFlIdPkOVj+VHdc+E+HKf IScA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=cZnqhzUW; spf=pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.41 as permitted sender) smtp.mailfrom=john.stultz@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from mail-sor-f41.google.com (mail-sor-f41.google.com. [209.85.220.41]) by mx.google.com with SMTPS id lr18sor4310658pjb.7.2021.01.28.19.05.23 for (Google Transport Security); Thu, 28 Jan 2021 19:05:23 -0800 (PST) Received-SPF: pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.41 as permitted sender) client-ip=209.85.220.41; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=cZnqhzUW; spf=pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.41 as permitted sender) smtp.mailfrom=john.stultz@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=wpJLS1/K6quxDCtF6ZEY+ZhZmpb559yflYmJJ80BQbw=; b=cZnqhzUWTkKR+GwYZalkxCsk9v9+2TUHpqGdSzHbuzov/fz1EkYstH68r/3oEpBW4B j9HutROkJfo28jDndi/gV5EZvIxT/IvLYHbyAhy042zJll0UXIyFaTtdkdfm1pWRZToD qJNtH2kDgXuipk6oJauJVzId5wPVjtS8ROisYKCY1doPKQ9nTPjmC/4IvcnlCRc1KC1n DRsJbcQ++faIJpPy/nlSw9IvR5qTxi4nXUkmJxV83pOPbfv1Elfk36n3a5eGRn6K7uKN hS8uxtEET+0pZuiSShr41z6L8+dxqy/ZVQzp55rFqyS8L8K8IXZDJV1eEsX2o3LQGuP7 ENuA== 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=wpJLS1/K6quxDCtF6ZEY+ZhZmpb559yflYmJJ80BQbw=; b=BPmB1aIpnz6XlBF8CCQz13IoxyP1jcZoacq+y1FoERBYLAuWKGZAPtxZXIUTKyhutv WozbYkkrJnbEfAUfe7OM1HDS5n0xrVgdCtej4GJj2slJbPMDS1AAYfm52ZeSe/FZ6BFG KMuvkk7wAAabuDpaIuWxSY/fDfs6QdENhQ7heSsBGPdwHvd4vfGmNo+p0mqib53t1Ksl Spho8pI607lMyq8eXOv83C4608kg28EeBXisWDtcDQoMwZbNYO0jYbk1KzP314s/2LJP z+ahmCSpZZCRIVMw0UfBXWb7gUIi8o1Vas8txrAs8Rbwz81a9Iqpk+cORdmJ984lpiR7 wxCg== X-Gm-Message-State: AOAM532YueCjGnFKa2WS+MqWuxAF1PqjuAoFUFez1d0K8I50XWmsTEmv 1HAuiyN+Rb3TS4CD8nv3SC7GpoVw X-Google-Smtp-Source: ABdhPJy2aymxhZ7yZWwAmLLNyftYCwd61PtfrChhA2K/S/ZWzs+EOTzqXZihvePJ3BjBNSZPTK19KA== X-Received: by 2002:a17:90a:8e82:: with SMTP id f2mr2405873pjo.234.1611889523668; Thu, 28 Jan 2021 19:05:23 -0800 (PST) Return-Path: Received: from localhost.localdomain ([2601:1c2:680:1319:692:26ff:feda:3a81]) by smtp.gmail.com with ESMTPSA id y75sm6854496pfg.119.2021.01.28.19.05.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Jan 2021 19:05:22 -0800 (PST) From: John Stultz To: lkml Cc: John Stultz , Shuah Khan , Brian Starkey , Sumit Semwal , Laura Abbott , Hridya Valsaraju , Suren Baghdasaryan , Sandeep Patil , Daniel Mentz , linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-kselftest@vger.kernel.org Subject: [PATCH 5/5] kselftests: dmabuf-heaps: Add extra checking that allocated buffers are zeroed Date: Fri, 29 Jan 2021 03:05:14 +0000 Message-Id: <20210129030514.1231773-5-john.stultz@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210129030514.1231773-1-john.stultz@linaro.org> References: <20210129030514.1231773-1-john.stultz@linaro.org> MIME-Version: 1.0 Add a check to validate that buffers allocated from the heaps are properly zeroed before being given to userland. It is done by allocating a number of buffers, and filling them with a nonzero pattern, then closing and reallocating more buffers and checking that they are all properly zeroed. This is helpful to validate any cached buffers are zeroed before being given back out. Cc: Shuah Khan Cc: Brian Starkey Cc: Sumit Semwal Cc: Laura Abbott Cc: Hridya Valsaraju Cc: Suren Baghdasaryan Cc: Sandeep Patil Cc: Daniel Mentz Cc: linux-media@vger.kernel.org Cc: dri-devel@lists.freedesktop.org Cc: linux-kselftest@vger.kernel.org Signed-off-by: John Stultz --- .../selftests/dmabuf-heaps/dmabuf-heap.c | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) -- 2.25.1 diff --git a/tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c b/tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c index d179d81e2355..29af27acd40e 100644 --- a/tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c +++ b/tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c @@ -218,6 +218,84 @@ static int test_alloc_and_import(char *heap_name) return ret; } +static int test_alloc_zeroed(char *heap_name, size_t size) +{ + int heap_fd = -1, dmabuf_fd[32]; + int i, j, ret; + void *p = NULL; + char *c; + + printf(" Testing alloced %ldk buffers are zeroed: ", size / 1024); + heap_fd = dmabuf_heap_open(heap_name); + if (heap_fd < 0) + return -1; + + /* Allocate and fill a bunch of buffers */ + for (i = 0; i < 32; i++) { + ret = dmabuf_heap_alloc(heap_fd, size, 0, &dmabuf_fd[i]); + if (ret < 0) { + printf("FAIL (Allocation (%i) failed)\n", i); + goto out; + } + /* mmap and fill with simple pattern */ + p = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, dmabuf_fd[i], 0); + if (p == MAP_FAILED) { + printf("FAIL (mmap() failed!)\n"); + ret = -1; + goto out; + } + dmabuf_sync(dmabuf_fd[i], DMA_BUF_SYNC_START); + memset(p, 0xff, size); + dmabuf_sync(dmabuf_fd[i], DMA_BUF_SYNC_END); + munmap(p, size); + } + /* close them all */ + for (i = 0; i < 32; i++) + close(dmabuf_fd[i]); + + /* Allocate and validate all buffers are zeroed */ + for (i = 0; i < 32; i++) { + ret = dmabuf_heap_alloc(heap_fd, size, 0, &dmabuf_fd[i]); + if (ret < 0) { + printf("FAIL (Allocation (%i) failed)\n", i); + goto out; + } + + /* mmap and validate everything is zero */ + p = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, dmabuf_fd[i], 0); + if (p == MAP_FAILED) { + printf("FAIL (mmap() failed!)\n"); + ret = -1; + goto out; + } + dmabuf_sync(dmabuf_fd[i], DMA_BUF_SYNC_START); + c = (char *)p; + for (j = 0; j < size; j++) { + if (c[j] != 0) { + printf("FAIL (Allocated buffer not zeroed @ %i)\n", j); + break; + } + } + dmabuf_sync(dmabuf_fd[i], DMA_BUF_SYNC_END); + munmap(p, size); + } + /* close them all */ + for (i = 0; i < 32; i++) + close(dmabuf_fd[i]); + + close(heap_fd); + printf("OK\n"); + return 0; + +out: + while (i > 0) { + close(dmabuf_fd[i]); + i--; + } + close(heap_fd); + return ret; +} + /* Test the ioctl version compatibility w/ a smaller structure then expected */ static int dmabuf_heap_alloc_older(int fd, size_t len, unsigned int flags, int *dmabuf_fd) @@ -386,6 +464,14 @@ int main(void) if (ret) break; + ret = test_alloc_zeroed(dir->d_name, 4 * 1024); + if (ret) + break; + + ret = test_alloc_zeroed(dir->d_name, ONE_MEG); + if (ret) + break; + ret = test_alloc_compat(dir->d_name); if (ret) break;