unsigned int num_buffers, unsigned int num_planes,
                             const unsigned plane_sizes[VB2_MAX_PLANES])
 {
+       unsigned int q_num_buffers = vb2_get_num_buffers(q);
        unsigned int buffer, plane;
        struct vb2_buffer *vb;
        int ret;
 
        /* Ensure that q->num_buffers+num_buffers is below VB2_MAX_FRAME */
        num_buffers = min_t(unsigned int, num_buffers,
-                           VB2_MAX_FRAME - q->num_buffers);
+                           VB2_MAX_FRAME - q_num_buffers);
 
        for (buffer = 0; buffer < num_buffers; ++buffer) {
                /* Allocate vb2 buffer structures */
                        vb->planes[plane].min_length = plane_sizes[plane];
                }
 
-               vb2_queue_add_buffer(q, vb, q->num_buffers + buffer);
+               vb2_queue_add_buffer(q, vb, q_num_buffers + buffer);
                call_void_bufop(q, init_buffer, vb);
 
                /* Allocate video buffer memory for the MMAP type */
 {
        unsigned int buffer;
        struct vb2_buffer *vb;
+       unsigned int q_num_buffers = vb2_get_num_buffers(q);
 
-       for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
+       for (buffer = q_num_buffers - buffers; buffer < q_num_buffers;
             ++buffer) {
                vb = vb2_get_buffer(q, buffer);
                if (!vb)
 static void __vb2_queue_free(struct vb2_queue *q, unsigned int buffers)
 {
        unsigned int buffer;
+       unsigned int q_num_buffers = vb2_get_num_buffers(q);
 
        lockdep_assert_held(&q->mmap_lock);
 
        /* Call driver-provided cleanup function for each buffer, if provided */
-       for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
+       for (buffer = q_num_buffers - buffers; buffer < q_num_buffers;
             ++buffer) {
                struct vb2_buffer *vb = vb2_get_buffer(q, buffer);
 
         * Check that all the calls were balanced during the life-time of this
         * queue. If not then dump the counters to the kernel log.
         */
-       if (q->num_buffers) {
+       if (q_num_buffers) {
                bool unbalanced = q->cnt_start_streaming != q->cnt_stop_streaming ||
                                  q->cnt_prepare_streaming != q->cnt_unprepare_streaming ||
                                  q->cnt_wait_prepare != q->cnt_wait_finish;
                q->cnt_stop_streaming = 0;
                q->cnt_unprepare_streaming = 0;
        }
-       for (buffer = 0; buffer < q->num_buffers; ++buffer) {
+       for (buffer = 0; buffer < vb2_get_num_buffers(q); buffer++) {
                struct vb2_buffer *vb = vb2_get_buffer(q, buffer);
                bool unbalanced;
 
 #endif
 
        /* Free vb2 buffers */
-       for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
+       for (buffer = q_num_buffers - buffers; buffer < q_num_buffers;
             ++buffer) {
                struct vb2_buffer *vb = vb2_get_buffer(q, buffer);
 
        }
 
        q->num_buffers -= buffers;
-       if (!q->num_buffers) {
+       if (!vb2_get_num_buffers(q)) {
                q->memory = VB2_MEMORY_UNKNOWN;
                INIT_LIST_HEAD(&q->queued_list);
        }
 static bool __buffers_in_use(struct vb2_queue *q)
 {
        unsigned int buffer;
-       for (buffer = 0; buffer < q->num_buffers; ++buffer) {
+       for (buffer = 0; buffer < vb2_get_num_buffers(q); ++buffer) {
                struct vb2_buffer *vb = vb2_get_buffer(q, buffer);
 
                if (!vb)
                     unsigned int flags, unsigned int *count)
 {
        unsigned int num_buffers, allocated_buffers, num_planes = 0;
+       unsigned int q_num_bufs = vb2_get_num_buffers(q);
        unsigned plane_sizes[VB2_MAX_PLANES] = { };
        bool non_coherent_mem = flags & V4L2_MEMORY_FLAG_NON_COHERENT;
        unsigned int i;
                return -EBUSY;
        }
 
-       if (*count == 0 || q->num_buffers != 0 ||
+       if (*count == 0 || q_num_bufs != 0 ||
            (q->memory != VB2_MEMORY_UNKNOWN && q->memory != memory) ||
            !verify_coherency_flags(q, non_coherent_mem)) {
                /*
                 * queued without ever calling STREAMON.
                 */
                __vb2_queue_cancel(q);
-               __vb2_queue_free(q, q->num_buffers);
+               __vb2_queue_free(q, q_num_bufs);
                mutex_unlock(&q->mmap_lock);
 
                /*
        if (ret < 0) {
                /*
                 * Note: __vb2_queue_free() will subtract 'allocated_buffers'
-                * from q->num_buffers and it will reset q->memory to
+                * from already queued buffers and it will reset q->memory to
                 * VB2_MEMORY_UNKNOWN.
                 */
                __vb2_queue_free(q, allocated_buffers);
        unsigned int num_planes = 0, num_buffers, allocated_buffers;
        unsigned plane_sizes[VB2_MAX_PLANES] = { };
        bool non_coherent_mem = flags & V4L2_MEMORY_FLAG_NON_COHERENT;
-       bool no_previous_buffers = !q->num_buffers;
-       int ret;
+       unsigned int q_num_bufs = vb2_get_num_buffers(q);
+       bool no_previous_buffers = !q_num_bufs;
+       int ret = 0;
 
-       if (q->num_buffers == VB2_MAX_FRAME) {
+       if (q_num_bufs == VB2_MAX_FRAME) {
                dprintk(q, 1, "maximum number of buffers already allocated\n");
                return -ENOBUFS;
        }
                        return -EINVAL;
        }
 
-       num_buffers = min(*count, VB2_MAX_FRAME - q->num_buffers);
+       num_buffers = min(*count, VB2_MAX_FRAME - q_num_bufs);
 
        if (requested_planes && requested_sizes) {
                num_planes = requested_planes;
                num_buffers = allocated_buffers;
 
                /*
-                * q->num_buffers contains the total number of buffers, that the
+                * num_buffers contains the total number of buffers, that the
                 * queue driver has set up
                 */
                ret = call_qop(q, queue_setup, q, &num_buffers,
        if (ret < 0) {
                /*
                 * Note: __vb2_queue_free() will subtract 'allocated_buffers'
-                * from q->num_buffers and it will reset q->memory to
+                * from already queued buffers and it will reset q->memory to
                 * VB2_MEMORY_UNKNOWN.
                 */
                __vb2_queue_free(q, allocated_buffers);
                 * Forcefully reclaim buffers if the driver did not
                 * correctly return them to vb2.
                 */
-               for (i = 0; i < q->num_buffers; ++i) {
+               for (i = 0; i < vb2_get_num_buffers(q); ++i) {
                        vb = vb2_get_buffer(q, i);
 
                        if (!vb)
         * to vb2 in stop_streaming().
         */
        if (WARN_ON(atomic_read(&q->owned_by_drv_count))) {
-               for (i = 0; i < q->num_buffers; ++i) {
+               for (i = 0; i < vb2_get_num_buffers(q); i++) {
                        struct vb2_buffer *vb = vb2_get_buffer(q, i);
 
                        if (!vb)
         * call to __fill_user_buffer() after buf_finish(). That order can't
         * be changed, so we can't move the buf_finish() to __vb2_dqbuf().
         */
-       for (i = 0; i < q->num_buffers; ++i) {
+       for (i = 0; i < vb2_get_num_buffers(q); i++) {
                struct vb2_buffer *vb;
                struct media_request *req;
 
 
 int vb2_core_streamon(struct vb2_queue *q, unsigned int type)
 {
+       unsigned int q_num_bufs = vb2_get_num_buffers(q);
        int ret;
 
        if (type != q->type) {
                return 0;
        }
 
-       if (!q->num_buffers) {
+       if (!q_num_bufs) {
                dprintk(q, 1, "no buffers have been allocated\n");
                return -EINVAL;
        }
 
-       if (q->num_buffers < q->min_buffers_needed) {
+       if (q_num_bufs < q->min_buffers_needed) {
                dprintk(q, 1, "need at least %u allocated buffers\n",
                                q->min_buffers_needed);
                return -EINVAL;
        __vb2_cleanup_fileio(q);
        __vb2_queue_cancel(q);
        mutex_lock(&q->mmap_lock);
-       __vb2_queue_free(q, q->num_buffers);
+       __vb2_queue_free(q, vb2_get_num_buffers(q));
        mutex_unlock(&q->mmap_lock);
 }
 EXPORT_SYMBOL_GPL(vb2_core_queue_release);
        /*
         * Start file I/O emulator only if streaming API has not been used yet.
         */
-       if (q->num_buffers == 0 && !vb2_fileio_is_active(q)) {
+       if (vb2_get_num_buffers(q) == 0 && !vb2_fileio_is_active(q)) {
                if (!q->is_output && (q->io_modes & VB2_READ) &&
                                (req_events & (EPOLLIN | EPOLLRDNORM))) {
                        if (__vb2_init_fileio(q, 1))
         * For output streams you can call write() as long as there are fewer
         * buffers queued than there are buffers available.
         */
-       if (q->is_output && q->fileio && q->queued_count < q->num_buffers)
+       if (q->is_output && q->fileio && q->queued_count < vb2_get_num_buffers(q))
                return EPOLLOUT | EPOLLWRNORM;
 
        if (list_empty(&q->done_list)) {
  * struct vb2_fileio_data - queue context used by file io emulator
  *
  * @cur_index: the index of the buffer currently being read from or
- *             written to. If equal to q->num_buffers then a new buffer
- *             must be dequeued.
+ *             written to. If equal to number of buffers in the vb2_queue
+ *             then a new buffer must be dequeued.
  * @initial_index: in the read() case all buffers are queued up immediately
  *             in __vb2_init_fileio() and __vb2_perform_fileio() just cycles
  *             buffers. However, in the write() case no buffers are initially
  *             buffers. This means that initially __vb2_perform_fileio()
  *             needs to know what buffer index to use when it is queuing up
  *             the buffers for the first time. That initial index is stored
- *             in this field. Once it is equal to q->num_buffers all
- *             available buffers have been queued and __vb2_perform_fileio()
- *             should start the normal dequeue/queue cycle.
+ *             in this field. Once it is equal to number of buffers in the
+ *             vb2_queue all available buffers have been queued and
+ *             __vb2_perform_fileio() should start the normal dequeue/queue cycle.
  *
  * vb2 provides a compatibility layer and emulator of file io (read and
  * write) calls on top of streaming API. For proper operation it required
        /*
         * Check if streaming api has not been already activated.
         */
-       if (q->streaming || q->num_buffers > 0)
+       if (q->streaming || vb2_get_num_buffers(q) > 0)
                return -EBUSY;
 
        /*
        /*
         * Get kernel address of each buffer.
         */
-       for (i = 0; i < q->num_buffers; i++) {
+       for (i = 0; i < vb2_get_num_buffers(q); i++) {
                /* vb can never be NULL when using fileio. */
                vb = vb2_get_buffer(q, i);
 
                /*
                 * Queue all buffers.
                 */
-               for (i = 0; i < q->num_buffers; i++) {
+               for (i = 0; i < vb2_get_num_buffers(q); i++) {
                        struct vb2_buffer *vb2 = vb2_get_buffer(q, i);
 
                        if (!vb2)
                }
                /*
                 * All buffers have been queued, so mark that by setting
-                * initial_index to q->num_buffers
+                * initial_index to the number of buffers in the vb2_queue
                 */
-               fileio->initial_index = q->num_buffers;
-               fileio->cur_index = q->num_buffers;
+               fileio->initial_index = vb2_get_num_buffers(q);
+               fileio->cur_index = fileio->initial_index;
        }
 
        /*
         * Check if we need to dequeue the buffer.
         */
        index = fileio->cur_index;
-       if (index >= q->num_buffers) {
+       if (index >= vb2_get_num_buffers(q)) {
                struct vb2_buffer *b;
 
                /*
                 * If we are queuing up buffers for the first time, then
                 * increase initial_index by one.
                 */
-               if (fileio->initial_index < q->num_buffers)
+               if (fileio->initial_index < vb2_get_num_buffers(q))
                        fileio->initial_index++;
                /*
                 * The next buffer to use is either a buffer that's going to be
-                * queued for the first time (initial_index < q->num_buffers)
-                * or it is equal to q->num_buffers, meaning that the next
-                * time we need to dequeue a buffer since we've now queued up
-                * all the 'first time' buffers.
+                * queued for the first time (initial_index < number of buffers in the vb2_queue)
+                * or it is equal to the number of buffers in the vb2_queue,
+                * meaning that the next time we need to dequeue a buffer since
+                * we've now queued up all the 'first time' buffers.
                 */
                fileio->cur_index = fileio->initial_index;
        }
        int ret = 0;
 
        if (q->is_output) {
-               prequeue = q->num_buffers;
+               prequeue = vb2_get_num_buffers(q);
                copy_timestamp = q->copy_timestamp;
        }